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:
Steven Job 2016-10-17 19:27:20 +01:00
parent 31f48f0651
commit 1f04cab99f
7 changed files with 776 additions and 702 deletions

View File

@ -160,7 +160,7 @@ var astTransformVisitor = {
} }
if (path.isRestProperty() || path.isSpreadProperty()) { if (path.isRestProperty() || path.isSpreadProperty()) {
node.type = "Experimental" + node.type; node.type = `Experimental${node.type}`;
} }
if (path.isTypeParameter && path.isTypeParameter()) { if (path.isTypeParameter && path.isTypeParameter()) {

View File

@ -53,7 +53,7 @@ module.exports = function (token, tt, source) {
pattern: value.pattern, pattern: value.pattern,
flags: value.flags flags: value.flags
}; };
token.value = "/" + value.pattern + "/" + value.flags; token.value = `/${value.pattern}/${value.flags}`;
} }
return token; return token;

View File

@ -396,7 +396,7 @@ exports.parseNoPatch = function (code, options) {
err.column = err.loc.column + 1; err.column = err.loc.column + 1;
// remove trailing "(LINE:COLUMN)" acorn message and add in esprima syntax error message start // 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; throw err;

View File

@ -2,6 +2,7 @@ var assert = require("assert");
var babelEslint = require(".."); var babelEslint = require("..");
var espree = require("espree"); var espree = require("espree");
var util = require("util"); var util = require("util");
var unpad = require("../utils/unpad");
// Checks if the source ast implements the target ast. Ignores extra keys on source ast // Checks if the source ast implements the target ast. Ignores extra keys on source ast
function assertImplementsAST(target, source, path) { function assertImplementsAST(target, source, path) {
@ -10,7 +11,7 @@ function assertImplementsAST(target, source, path) {
} }
function error(text) { function error(text) {
var err = new Error("At " + path.join(".") + ": " + text + ":"); var err = new Error(`At ${path.join(".")}: ${text}:`);
err.depth = path.length + 1; err.depth = path.length + 1;
throw err; throw err;
} }
@ -18,7 +19,7 @@ function assertImplementsAST(target, source, path) {
var typeA = target === null ? "null" : typeof target; var typeA = target === null ? "null" : typeof target;
var typeB = source === null ? "null" : typeof source; var typeB = source === null ? "null" : typeof source;
if (typeA !== typeB) { if (typeA !== typeB) {
error("have different types (" + typeA + " !== " + typeB + ") " + "(" + target + " !== " + source + ")"); error(`have different types (${typeA} !== ${typeB}) (${target} !== ${source})`);
} else if (typeA === "object") { } else if (typeA === "object") {
var keysTarget = Object.keys(target); var keysTarget = Object.keys(target);
for (var i in keysTarget) { for (var i in keysTarget) {
@ -28,7 +29,7 @@ function assertImplementsAST(target, source, path) {
path.pop(); path.pop();
} }
} else if (target !== source) { } 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) { if (babylonAST.tokens) {
delete babylonAST.tokens; delete babylonAST.tokens;
} }
err.message += err.message += unpad(`
"\nespree:\n" + espree:
util.inspect(lookup(esAST, traversal, 2), {depth: err.depth, colors: true}) + ${util.inspect(lookup(esAST, traversal, 2), {depth: err.depth, colors: true})}
"\nbabel-eslint:\n" + babel-eslint:
util.inspect(lookup(babylonAST, traversal, 2), {depth: err.depth, colors: true}); ${util.inspect(lookup(babylonAST, traversal, 2), {depth: err.depth, colors: true})}
`);
throw err; throw err;
} }
// assert.equal(esAST, babylonAST); // assert.equal(esAST, babylonAST);
@ -132,25 +134,29 @@ describe("babylon-to-esprima", function () {
it("template also with braces #96", function () { it("template also with braces #96", function () {
parseAndAssertSame( parseAndAssertSame(
"export default function f1() {" + unpad(`
"function f2(foo) {" + export default function f1() {
"const bar = 3;" + function f2(foo) {
"return `${foo} ${bar}`;" + const bar = 3;
"}" + return \`\${foo} \${bar}\`;
"return f2;" + }
"}" return f2;
}
`)
); );
}); });
it("template with destructuring #31", function () { it("template with destructuring #31", function () {
parseAndAssertSame([ parseAndAssertSame(
"module.exports = {", unpad(`
"render() {", module.exports = {
"var {name} = this.props;", render() {
"return Math.max(null, `Name: ${name}, Name: ${name}`);", var {name} = this.props;
"}", return Math.max(null, \`Name: \${name}, Name: \${name}\`);
"};" }
].join("\n")); };
`)
);
}); });
}); });
@ -251,34 +257,40 @@ describe("babylon-to-esprima", function () {
}); });
it("line comments", function () { it("line comments", function () {
parseAndAssertSame([ parseAndAssertSame(
" // single comment", unpad(`
"var foo = 15; // comment next to statement", // single comment
"// second comment after statement" var foo = 15; // comment next to statement
].join("\n")); // second comment after statement
`)
);
}); });
it("block comments", function () { it("block comments", function () {
parseAndAssertSame([ parseAndAssertSame(
" /* single comment */ ", unpad(`
"var foo = 15; /* comment next to statement */", /* single comment */
"/*", var foo = 15; /* comment next to statement */
" * multiline", /*
" * comment", * multiline
" */" * comment
].join("\n")); */
`)
);
}); });
it("block comments #124", function () { it("block comments #124", function () {
parseAndAssertSame([ parseAndAssertSame(
"React.createClass({", unpad(`
"render() {", React.createClass({
"// return (", render() {
"// <div />", // return (
"// ); // <-- this is the line that is reported", // <div />
"}", // ); // <-- this is the line that is reported
"});" }
].join("\n")); });
`)
);
}); });
it("null", function () { it("null", function () {
@ -306,76 +318,87 @@ describe("babylon-to-esprima", function () {
}); });
it("jsdoc", function () { it("jsdoc", function () {
parseAndAssertSame([ parseAndAssertSame(
"/**", unpad(`
"* @param {object} options", /**
"* @return {number}", * @param {object} options
"*/", * @return {number}
"const test = function({ a, b, c }) {", */
"return a + b + c;", const test = function({ a, b, c }) {
"};", return a + b + c;
"module.exports = test;" };
].join("\n")); module.exports = test;
`)
);
}); });
it("empty block with comment", function () { it("empty block with comment", function () {
parseAndAssertSame([ parseAndAssertSame(
"function a () {", unpad(`
"try {", function a () {
"b();", try {
"} catch (e) {", b();
"// asdf", } catch (e) {
"}", // asdf
"}" }
].join("\n")); }
`)
);
}); });
describe("babel 6 tests", function () { describe("babel 6 tests", function () {
it("MethodDefinition", function () { it("MethodDefinition", function () {
parseAndAssertSame([ parseAndAssertSame(
"export default class A {", unpad(`
"a() {}", export default class A {
"}" a() {}
].join("\n")); }
`)
);
}); });
it("MethodDefinition 2", function () { it("MethodDefinition 2", function () {
parseAndAssertSame([ parseAndAssertSame("export default class Bar { get bar() { return 42; }}");
"export default class Bar { get bar() { return 42; }}"
].join("\n"));
}); });
it("ClassMethod", function () { it("ClassMethod", function () {
parseAndAssertSame([ parseAndAssertSame(
"class A {", unpad(`
"constructor() {", class A {
"}", constructor() {
"}" }
].join("\n")); }
`)
);
}); });
it("ClassMethod multiple params", function () { it("ClassMethod multiple params", function () {
parseAndAssertSame([ parseAndAssertSame(
"class A {", unpad(`
"constructor(a, b, c) {", class A {
"}", constructor(a, b, c) {
"}" }
].join("\n")); }
`)
);
}); });
it("ClassMethod multiline", function () { it("ClassMethod multiline", function () {
parseAndAssertSame([ parseAndAssertSame(
"class A {", unpad(`
" constructor (", class A {
" a,", constructor (
" b,", a,
" c", b,
" )", c
"{", )
"",
" }", {
"}"
].join("\n")); }
}
`)
);
}); });
it("ClassMethod oneline", function () { it("ClassMethod oneline", function () {
@ -383,12 +406,14 @@ describe("babylon-to-esprima", function () {
}); });
it("ObjectMethod", function () { it("ObjectMethod", function () {
parseAndAssertSame([ parseAndAssertSame(
"var a = {", unpad(`
"b(c) {", var a = {
"}", b(c) {
"}" }
].join("\n")); }
`)
);
}); });
it("do not allow import export everywhere", function() { it("do not allow import export everywhere", function() {
@ -413,35 +438,41 @@ describe("babylon-to-esprima", function () {
it("getters and setters", function () { it("getters and setters", function () {
parseAndAssertSame("class A { get x ( ) { ; } }"); parseAndAssertSame("class A { get x ( ) { ; } }");
parseAndAssertSame([ parseAndAssertSame(
"class A {", unpad(`
"get x(", class A {
")", get x(
"{", )
";", {
"}", ;
"}" }
].join("\n")); }
`)
);
parseAndAssertSame("class A { set x (a) { ; } }"); parseAndAssertSame("class A { set x (a) { ; } }");
parseAndAssertSame([ parseAndAssertSame(
"class A {", unpad(`
"set x(a", class A {
")", set x(a
"{", )
";", {
"}", ;
"}" }
].join("\n")); }
parseAndAssertSame([ `)
"var B = {", );
"get x () {", parseAndAssertSame(
"return this.ecks;", unpad(`
"},", var B = {
"set x (ecks) {", get x () {
"this.ecks = ecks;", return this.ecks;
"}", },
"};" set x (ecks) {
].join("\n")); this.ecks = ecks;
}
};
`)
);
}); });
it("RestOperator", function () { it("RestOperator", function () {
@ -458,9 +489,11 @@ describe("babylon-to-esprima", function () {
it("Async/Await", function() { it("Async/Await", function() {
parseAndAssertSame( parseAndAssertSame(
`async function a() { unpad(`
async function a() {
await 1; await 1;
}` }
`)
); );
}); });
}); });

View File

@ -61,8 +61,7 @@ function strictSuite () {
eslintOpts.rules[ruleId] = [errorLevel, "never"]; eslintOpts.rules[ruleId] = [errorLevel, "never"];
["global-with", "function-with"].forEach(function (fixture) { ["global-with", "function-with"].forEach(function (fixture) {
it( it(`should error on ${fixture.match(/^[^-]+/)[0]} directive`,
"should error on " + fixture.match(/^[^-]+/)[0] + " directive",
function (done) { function (done) {
lint({ lint({
fixture: ["strict", fixture], fixture: ["strict", fixture],

File diff suppressed because it is too large Load Diff

View 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;