Now using template strings (babel/babel-eslint#410)
* Use template strings in non-regression tests * Refactor non-regression tests to fix test failures Moved backtick to fix test 'getter/setter babel/babel-eslint#218' as indent matters Fixed line numbers for some tests * Use template strings in babel-eslint tests * Fix tests for babel-eslint Avoids error that shows when using template strings for tests: line 253 line comments line 260 block comments line 306 jsdoc Error: At loc.start.column: are different (6 !== 0) * Other small template literal changes * Add unpad to correctly indent template literals
This commit is contained in:
parent
31f48f0651
commit
1f04cab99f
@ -160,7 +160,7 @@ var astTransformVisitor = {
|
||||
}
|
||||
|
||||
if (path.isRestProperty() || path.isSpreadProperty()) {
|
||||
node.type = "Experimental" + node.type;
|
||||
node.type = `Experimental${node.type}`;
|
||||
}
|
||||
|
||||
if (path.isTypeParameter && path.isTypeParameter()) {
|
||||
|
||||
@ -53,7 +53,7 @@ module.exports = function (token, tt, source) {
|
||||
pattern: value.pattern,
|
||||
flags: value.flags
|
||||
};
|
||||
token.value = "/" + value.pattern + "/" + value.flags;
|
||||
token.value = `/${value.pattern}/${value.flags}`;
|
||||
}
|
||||
|
||||
return token;
|
||||
|
||||
@ -396,7 +396,7 @@ exports.parseNoPatch = function (code, options) {
|
||||
err.column = err.loc.column + 1;
|
||||
|
||||
// remove trailing "(LINE:COLUMN)" acorn message and add in esprima syntax error message start
|
||||
err.message = "Line " + err.lineNumber + ": " + err.message.replace(/ \((\d+):(\d+)\)$/, "");
|
||||
err.message = `Line ${err.lineNumber}: ${err.message.replace(/ \((\d+):(\d+)\)$/, "")}`;
|
||||
}
|
||||
|
||||
throw err;
|
||||
|
||||
@ -2,6 +2,7 @@ var assert = require("assert");
|
||||
var babelEslint = require("..");
|
||||
var espree = require("espree");
|
||||
var util = require("util");
|
||||
var unpad = require("../utils/unpad");
|
||||
|
||||
// Checks if the source ast implements the target ast. Ignores extra keys on source ast
|
||||
function assertImplementsAST(target, source, path) {
|
||||
@ -10,7 +11,7 @@ function assertImplementsAST(target, source, path) {
|
||||
}
|
||||
|
||||
function error(text) {
|
||||
var err = new Error("At " + path.join(".") + ": " + text + ":");
|
||||
var err = new Error(`At ${path.join(".")}: ${text}:`);
|
||||
err.depth = path.length + 1;
|
||||
throw err;
|
||||
}
|
||||
@ -18,7 +19,7 @@ function assertImplementsAST(target, source, path) {
|
||||
var typeA = target === null ? "null" : typeof target;
|
||||
var typeB = source === null ? "null" : typeof source;
|
||||
if (typeA !== typeB) {
|
||||
error("have different types (" + typeA + " !== " + typeB + ") " + "(" + target + " !== " + source + ")");
|
||||
error(`have different types (${typeA} !== ${typeB}) (${target} !== ${source})`);
|
||||
} else if (typeA === "object") {
|
||||
var keysTarget = Object.keys(target);
|
||||
for (var i in keysTarget) {
|
||||
@ -28,7 +29,7 @@ function assertImplementsAST(target, source, path) {
|
||||
path.pop();
|
||||
}
|
||||
} else if (target !== source) {
|
||||
error("are different (" + JSON.stringify(target) + " !== " + JSON.stringify(source) + ")");
|
||||
error(`are different (${JSON.stringify(target)} !== ${JSON.stringify(source)})`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,11 +71,12 @@ function parseAndAssertSame(code) {
|
||||
if (babylonAST.tokens) {
|
||||
delete babylonAST.tokens;
|
||||
}
|
||||
err.message +=
|
||||
"\nespree:\n" +
|
||||
util.inspect(lookup(esAST, traversal, 2), {depth: err.depth, colors: true}) +
|
||||
"\nbabel-eslint:\n" +
|
||||
util.inspect(lookup(babylonAST, traversal, 2), {depth: err.depth, colors: true});
|
||||
err.message += unpad(`
|
||||
espree:
|
||||
${util.inspect(lookup(esAST, traversal, 2), {depth: err.depth, colors: true})}
|
||||
babel-eslint:
|
||||
${util.inspect(lookup(babylonAST, traversal, 2), {depth: err.depth, colors: true})}
|
||||
`);
|
||||
throw err;
|
||||
}
|
||||
// assert.equal(esAST, babylonAST);
|
||||
@ -132,25 +134,29 @@ describe("babylon-to-esprima", function () {
|
||||
|
||||
it("template also with braces #96", function () {
|
||||
parseAndAssertSame(
|
||||
"export default function f1() {" +
|
||||
"function f2(foo) {" +
|
||||
"const bar = 3;" +
|
||||
"return `${foo} ${bar}`;" +
|
||||
"}" +
|
||||
"return f2;" +
|
||||
"}"
|
||||
unpad(`
|
||||
export default function f1() {
|
||||
function f2(foo) {
|
||||
const bar = 3;
|
||||
return \`\${foo} \${bar}\`;
|
||||
}
|
||||
return f2;
|
||||
}
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("template with destructuring #31", function () {
|
||||
parseAndAssertSame([
|
||||
"module.exports = {",
|
||||
"render() {",
|
||||
"var {name} = this.props;",
|
||||
"return Math.max(null, `Name: ${name}, Name: ${name}`);",
|
||||
"}",
|
||||
"};"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
module.exports = {
|
||||
render() {
|
||||
var {name} = this.props;
|
||||
return Math.max(null, \`Name: \${name}, Name: \${name}\`);
|
||||
}
|
||||
};
|
||||
`)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -251,34 +257,40 @@ describe("babylon-to-esprima", function () {
|
||||
});
|
||||
|
||||
it("line comments", function () {
|
||||
parseAndAssertSame([
|
||||
" // single comment",
|
||||
"var foo = 15; // comment next to statement",
|
||||
"// second comment after statement"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
// single comment
|
||||
var foo = 15; // comment next to statement
|
||||
// second comment after statement
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("block comments", function () {
|
||||
parseAndAssertSame([
|
||||
" /* single comment */ ",
|
||||
"var foo = 15; /* comment next to statement */",
|
||||
"/*",
|
||||
" * multiline",
|
||||
" * comment",
|
||||
" */"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
/* single comment */
|
||||
var foo = 15; /* comment next to statement */
|
||||
/*
|
||||
* multiline
|
||||
* comment
|
||||
*/
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("block comments #124", function () {
|
||||
parseAndAssertSame([
|
||||
"React.createClass({",
|
||||
"render() {",
|
||||
"// return (",
|
||||
"// <div />",
|
||||
"// ); // <-- this is the line that is reported",
|
||||
"}",
|
||||
"});"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
React.createClass({
|
||||
render() {
|
||||
// return (
|
||||
// <div />
|
||||
// ); // <-- this is the line that is reported
|
||||
}
|
||||
});
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("null", function () {
|
||||
@ -306,76 +318,87 @@ describe("babylon-to-esprima", function () {
|
||||
});
|
||||
|
||||
it("jsdoc", function () {
|
||||
parseAndAssertSame([
|
||||
"/**",
|
||||
"* @param {object} options",
|
||||
"* @return {number}",
|
||||
"*/",
|
||||
"const test = function({ a, b, c }) {",
|
||||
"return a + b + c;",
|
||||
"};",
|
||||
"module.exports = test;"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
/**
|
||||
* @param {object} options
|
||||
* @return {number}
|
||||
*/
|
||||
const test = function({ a, b, c }) {
|
||||
return a + b + c;
|
||||
};
|
||||
module.exports = test;
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("empty block with comment", function () {
|
||||
parseAndAssertSame([
|
||||
"function a () {",
|
||||
"try {",
|
||||
"b();",
|
||||
"} catch (e) {",
|
||||
"// asdf",
|
||||
"}",
|
||||
"}"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
function a () {
|
||||
try {
|
||||
b();
|
||||
} catch (e) {
|
||||
// asdf
|
||||
}
|
||||
}
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
describe("babel 6 tests", function () {
|
||||
it("MethodDefinition", function () {
|
||||
parseAndAssertSame([
|
||||
"export default class A {",
|
||||
"a() {}",
|
||||
"}"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
export default class A {
|
||||
a() {}
|
||||
}
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("MethodDefinition 2", function () {
|
||||
parseAndAssertSame([
|
||||
"export default class Bar { get bar() { return 42; }}"
|
||||
].join("\n"));
|
||||
parseAndAssertSame("export default class Bar { get bar() { return 42; }}");
|
||||
});
|
||||
|
||||
it("ClassMethod", function () {
|
||||
parseAndAssertSame([
|
||||
"class A {",
|
||||
"constructor() {",
|
||||
"}",
|
||||
"}"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
class A {
|
||||
constructor() {
|
||||
}
|
||||
}
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("ClassMethod multiple params", function () {
|
||||
parseAndAssertSame([
|
||||
"class A {",
|
||||
"constructor(a, b, c) {",
|
||||
"}",
|
||||
"}"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
class A {
|
||||
constructor(a, b, c) {
|
||||
}
|
||||
}
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("ClassMethod multiline", function () {
|
||||
parseAndAssertSame([
|
||||
"class A {",
|
||||
" constructor (",
|
||||
" a,",
|
||||
" b,",
|
||||
" c",
|
||||
" )",
|
||||
"{",
|
||||
"",
|
||||
" }",
|
||||
"}"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
class A {
|
||||
constructor (
|
||||
a,
|
||||
b,
|
||||
c
|
||||
)
|
||||
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("ClassMethod oneline", function () {
|
||||
@ -383,12 +406,14 @@ describe("babylon-to-esprima", function () {
|
||||
});
|
||||
|
||||
it("ObjectMethod", function () {
|
||||
parseAndAssertSame([
|
||||
"var a = {",
|
||||
"b(c) {",
|
||||
"}",
|
||||
"}"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
var a = {
|
||||
b(c) {
|
||||
}
|
||||
}
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("do not allow import export everywhere", function() {
|
||||
@ -413,35 +438,41 @@ describe("babylon-to-esprima", function () {
|
||||
|
||||
it("getters and setters", function () {
|
||||
parseAndAssertSame("class A { get x ( ) { ; } }");
|
||||
parseAndAssertSame([
|
||||
"class A {",
|
||||
"get x(",
|
||||
")",
|
||||
"{",
|
||||
";",
|
||||
"}",
|
||||
"}"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
class A {
|
||||
get x(
|
||||
)
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
||||
`)
|
||||
);
|
||||
parseAndAssertSame("class A { set x (a) { ; } }");
|
||||
parseAndAssertSame([
|
||||
"class A {",
|
||||
"set x(a",
|
||||
")",
|
||||
"{",
|
||||
";",
|
||||
"}",
|
||||
"}"
|
||||
].join("\n"));
|
||||
parseAndAssertSame([
|
||||
"var B = {",
|
||||
"get x () {",
|
||||
"return this.ecks;",
|
||||
"},",
|
||||
"set x (ecks) {",
|
||||
"this.ecks = ecks;",
|
||||
"}",
|
||||
"};"
|
||||
].join("\n"));
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
class A {
|
||||
set x(a
|
||||
)
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
||||
`)
|
||||
);
|
||||
parseAndAssertSame(
|
||||
unpad(`
|
||||
var B = {
|
||||
get x () {
|
||||
return this.ecks;
|
||||
},
|
||||
set x (ecks) {
|
||||
this.ecks = ecks;
|
||||
}
|
||||
};
|
||||
`)
|
||||
);
|
||||
});
|
||||
|
||||
it("RestOperator", function () {
|
||||
@ -458,9 +489,11 @@ describe("babylon-to-esprima", function () {
|
||||
|
||||
it("Async/Await", function() {
|
||||
parseAndAssertSame(
|
||||
`async function a() {
|
||||
await 1;
|
||||
}`
|
||||
unpad(`
|
||||
async function a() {
|
||||
await 1;
|
||||
}
|
||||
`)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -61,8 +61,7 @@ function strictSuite () {
|
||||
eslintOpts.rules[ruleId] = [errorLevel, "never"];
|
||||
|
||||
["global-with", "function-with"].forEach(function (fixture) {
|
||||
it(
|
||||
"should error on " + fixture.match(/^[^-]+/)[0] + " directive",
|
||||
it(`should error on ${fixture.match(/^[^-]+/)[0]} directive`,
|
||||
function (done) {
|
||||
lint({
|
||||
fixture: ["strict", fixture],
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
14
eslint/babel-eslint-parser/utils/unpad.js
Normal file
14
eslint/babel-eslint-parser/utils/unpad.js
Normal file
@ -0,0 +1,14 @@
|
||||
// Remove padding from a string.
|
||||
function unpad(str) {
|
||||
const lines = str.split("\n");
|
||||
const m = lines[1] && lines[1].match(/^\s+/);
|
||||
if (!m) {
|
||||
return str;
|
||||
}
|
||||
const spaces = m[0].length;
|
||||
return lines.map(
|
||||
(line) => line.slice(spaces)
|
||||
).join("\n").trim();
|
||||
}
|
||||
|
||||
module.exports = unpad;
|
||||
Loading…
x
Reference in New Issue
Block a user