diff --git a/eslint/babel-eslint-parser/babylon-to-espree/toAST.js b/eslint/babel-eslint-parser/babylon-to-espree/toAST.js index caa6a4e4cb..77190f3695 100644 --- a/eslint/babel-eslint-parser/babylon-to-espree/toAST.js +++ b/eslint/babel-eslint-parser/babylon-to-espree/toAST.js @@ -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()) { diff --git a/eslint/babel-eslint-parser/babylon-to-espree/toToken.js b/eslint/babel-eslint-parser/babylon-to-espree/toToken.js index 7e13f28e0c..dcfd48f87c 100644 --- a/eslint/babel-eslint-parser/babylon-to-espree/toToken.js +++ b/eslint/babel-eslint-parser/babylon-to-espree/toToken.js @@ -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; diff --git a/eslint/babel-eslint-parser/index.js b/eslint/babel-eslint-parser/index.js index 048cdbaca3..ab2c3e7a09 100644 --- a/eslint/babel-eslint-parser/index.js +++ b/eslint/babel-eslint-parser/index.js @@ -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; diff --git a/eslint/babel-eslint-parser/test/babel-eslint.js b/eslint/babel-eslint-parser/test/babel-eslint.js index 1de61af7a0..65e70b10ae 100644 --- a/eslint/babel-eslint-parser/test/babel-eslint.js +++ b/eslint/babel-eslint-parser/test/babel-eslint.js @@ -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 (", - "//
", - "// ); // <-- this is the line that is reported", - "}", - "});" - ].join("\n")); + parseAndAssertSame( + unpad(` + React.createClass({ + render() { + // return ( + //
+ // ); // <-- 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; + } + `) ); }); }); diff --git a/eslint/babel-eslint-parser/test/integration.js b/eslint/babel-eslint-parser/test/integration.js index b7f735121a..2e62188e90 100644 --- a/eslint/babel-eslint-parser/test/integration.js +++ b/eslint/babel-eslint-parser/test/integration.js @@ -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], diff --git a/eslint/babel-eslint-parser/test/non-regression.js b/eslint/babel-eslint-parser/test/non-regression.js index 4ddead6e20..2d563782d1 100644 --- a/eslint/babel-eslint-parser/test/non-regression.js +++ b/eslint/babel-eslint-parser/test/non-regression.js @@ -1,6 +1,7 @@ /*eslint-env mocha*/ "use strict"; var eslint = require("eslint"); +var unpad = require("../utils/unpad"); function verifyAndAssertMessages(code, rules, expectedMessages, sourceType, overrideConfig) { var config = { @@ -30,13 +31,19 @@ function verifyAndAssertMessages(code, rules, expectedMessages, sourceType, over var messages = eslint.linter.verify(code, config); if (messages.length !== expectedMessages.length) { - throw new Error("Expected " + expectedMessages.length + " message(s), got " + messages.length + " " + JSON.stringify(messages)); + throw new Error(`Expected ${expectedMessages.length} message(s), got ${messages.length} ${JSON.stringify(messages)}`); } messages.forEach(function (message, i) { - var formatedMessage = message.line + ":" + message.column + " " + message.message + (message.ruleId ? " " + message.ruleId : ""); + var formatedMessage = `${message.line}:${message.column} ${message.message}${(message.ruleId ? ` ${message.ruleId}` : "")}`; if (formatedMessage !== expectedMessages[i]) { - throw new Error("Message " + i + " does not match:\nExpected: " + expectedMessages[i] + "\nActual: " + formatedMessage); + throw new Error( + unpad(` + Message ${i} does not match: + Expected: ${expectedMessages[i]} + Actual: ${formatedMessage} + `) + ); } }); } @@ -68,10 +75,12 @@ describe("verify", function () { it("Modules support (issue #5)", function () { verifyAndAssertMessages( - "import Foo from 'foo';\n" + - "export default Foo;\n" + - "export const c = 'c';\n" + - "export class Store {}", + unpad(` + import Foo from 'foo'; + export default Foo; + export const c = 'c'; + export class Store {} + `), {}, [] ); @@ -160,12 +169,13 @@ describe("verify", function () { }); it("comment with padded-blocks (issue #33)", function () { - verifyAndAssertMessages([ - "if (a){", - "// i'm a comment!", - "let b = c", - "}" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + if (a) { + // i'm a comment! + let b = c + } + `), { "padded-blocks": [1, "never"] }, [] ); @@ -173,9 +183,8 @@ describe("verify", function () { describe("flow", function () { it("check regular function", function () { - verifyAndAssertMessages([ + verifyAndAssertMessages( "function a(b, c) { b += 1; c += 1; return b + c; } a;", - ].join("\n"), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -198,209 +207,225 @@ describe("verify", function () { }); it("multiple nullable type annotations and return #108", function () { - verifyAndAssertMessages([ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "import type Foo3 from 'foo';", - "function log(foo: ?Foo, foo2: ?Foo2): ?Foo3 {", - "console.log(foo, foo2);", - "}", - "log(1, 2);" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + import type Foo3 from 'foo'; + function log(foo: ?Foo, foo2: ?Foo2): ?Foo3 { + console.log(foo, foo2); + } + log(1, 2); + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("type parameters", function () { - verifyAndAssertMessages([ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "function log(a: T1, b: T2) { return a + b; }", - "log(1, 2);" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + function log(a: T1, b: T2) { return a + b; } + log(1, 2); + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("nested type annotations", function () { - verifyAndAssertMessages([ - "import type Foo from 'foo';", - "function foo(callback: () => Foo) {", - "return callback();", - "}", - "foo();" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Foo from 'foo'; + function foo(callback: () => Foo) { + return callback(); + } + foo(); + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("type in var declaration", function () { - verifyAndAssertMessages([ - "import type Foo from 'foo';", - "var x: Foo = 1;", - "x;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Foo from 'foo'; + var x: Foo = 1; + x; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("object type annotation", function () { - verifyAndAssertMessages([ - "import type Foo from 'foo';", - "var a: {numVal: Foo};", - "a;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Foo from 'foo'; + var a: {numVal: Foo}; + a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("object property types", function () { - verifyAndAssertMessages([ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "var a = {", - "circle: (null : ?{ setNativeProps(props: Foo): Foo2 })", - "};", - "a;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + var a = { + circle: (null : ?{ setNativeProps(props: Foo): Foo2 }) + }; + a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("namespaced types", function () { - verifyAndAssertMessages([ - "var React = require('react-native');", - "var b = {", - "openExternalExample: (null: ?React.Component)", - "};", - "var c = {", - "render(): React.Component {}", - "};", - "b;", - "c;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + var React = require('react-native'); + var b = { + openExternalExample: (null: ?React.Component) + }; + var c = { + render(): React.Component {} + }; + b; + c; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("ArrayTypeAnnotation", function () { - verifyAndAssertMessages([ - "import type Foo from 'foo';", - "var x: Foo[]; x;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Foo from 'foo'; + var x: Foo[]; x; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("ClassImplements", function () { - verifyAndAssertMessages([ - "import type Bar from 'foo';", - "export default class Foo implements Bar {}" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Bar from 'foo'; + export default class Foo implements Bar {} + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("type alias creates declaration + usage", function () { - verifyAndAssertMessages([ - "type Foo = any;", - "var x : Foo = 1; x;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + type Foo = any; + var x : Foo = 1; x; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("type alias with type parameters", function () { - verifyAndAssertMessages([ - "import type Bar from 'foo';", - "import type Foo3 from 'foo';", - "type Foo = Bar", - "var x : Foo = 1; x;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Bar from 'foo'; + import type Foo3 from 'foo'; + type Foo = Bar + var x : Foo = 1; x; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("export type alias", function () { - verifyAndAssertMessages([ - "import type Foo2 from 'foo';", - "export type Foo = Foo2;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Foo2 from 'foo'; + export type Foo = Foo2; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("polymorphpic types #109", function () { - verifyAndAssertMessages([ - "export default function groupByEveryN(array: Array, n: number): Array> { n; }" - ].join("\n"), + verifyAndAssertMessages( + "export default function groupByEveryN(array: Array, n: number): Array> { n; }", { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("types definition from import", function () { - verifyAndAssertMessages([ - "import type Promise from 'bluebird';", - "type Operation = () => Promise;", - "x: Operation;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Promise from 'bluebird'; + type Operation = () => Promise; + x: Operation; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("polymorphpic/generic types for class #123", function () { - verifyAndAssertMessages([ - "class Box {", - "value: T;", - "}", - "var box = new Box();", - "console.log(box.value);" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + class Box { + value: T; + } + var box = new Box(); + console.log(box.value); + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("polymorphpic/generic types for function #123", function () { - verifyAndAssertMessages([ - "export function identity(value) {", - "var a: T = value; a;", - "}" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + export function identity(value) { + var a: T = value; a; + } + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("polymorphpic/generic types for type alias #123", function () { - verifyAndAssertMessages([ - "import Bar from './Bar';", - "type Foo = Bar; var x: Foo = 1; console.log(x);" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import Bar from './Bar'; + type Foo = Bar; var x: Foo = 1; console.log(x); + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); }); it("polymorphpic/generic types - outside of fn scope #123", function () { - verifyAndAssertMessages([ - "export function foo(value) { value; };", - "var b: T = 1; b;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + export function foo(value) { value; }; + var b: T = 1; b; + `), { "no-unused-vars": 1, "no-undef": 1 }, [ "1:21 'T' is defined but never used. no-unused-vars", "2:8 'T' is not defined. no-undef" ] @@ -408,23 +433,25 @@ describe("verify", function () { }); it("polymorphpic/generic types - extending unknown #123", function () { - verifyAndAssertMessages([ - "import Bar from 'bar';", - "export class Foo extends Bar {}", - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import Bar from 'bar'; + export class Foo extends Bar {} + `), { "no-unused-vars": 1, "no-undef": 1 }, [ "2:30 'T' is not defined. no-undef" ] ); }); it("support declarations #132", function () { - verifyAndAssertMessages([ - "declare class A { static () : number }", - "declare module B { declare var x: number; }", - "declare function foo(): void;", - "declare var bar", - "A; B; foo(); bar;" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + declare class A { static () : number } + declare module B { declare var x: number; } + declare function foo(): void; + declare var bar + A; B; foo(); bar; + `), { "no-undef": 1, "no-unused-vars": 1 }, [] ); @@ -432,11 +459,11 @@ describe("verify", function () { it("1", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "export default function(a: Foo, b: ?Foo2, c){ a; b; c; }" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + export default function(a: Foo, b: ?Foo2, c){ a; b; c; } + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -444,10 +471,10 @@ describe("verify", function () { it("2", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "export default function(a: () => Foo){ a; }" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + export default function(a: () => Foo){ a; } + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -455,11 +482,11 @@ describe("verify", function () { it("3", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "export default function(a: (_:Foo) => Foo2){ a; }" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + export default function(a: (_:Foo) => Foo2){ a; } + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -467,12 +494,12 @@ describe("verify", function () { it("4", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "import type Foo3 from 'foo';", - "export default function(a: (_1:Foo, _2:Foo2) => Foo3){ a; }" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + import type Foo3 from 'foo'; + export default function(a: (_1:Foo, _2:Foo2) => Foo3){ a; } + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -480,11 +507,11 @@ describe("verify", function () { it("5", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "export default function(a: (_1:Foo, ...foo:Array) => number){ a; }" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + export default function(a: (_1:Foo, ...foo:Array) => number){ a; } + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -492,10 +519,10 @@ describe("verify", function () { it("6", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "export default function(): Foo {}" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + export default function(): Foo {} + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -503,10 +530,10 @@ describe("verify", function () { it("7", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "export default function():() => Foo {}" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + export default function():() => Foo {} + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -514,11 +541,11 @@ describe("verify", function () { it("8", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "export default function():(_?:Foo) => Foo2{}" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + export default function():(_?:Foo) => Foo2{} + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -526,9 +553,7 @@ describe("verify", function () { it("9", function () { verifyAndAssertMessages( - [ - "export default function (a: T1, b: T2) { b; }" - ].join("\n"), + "export default function (a: T1, b: T2) { b; }", { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -536,9 +561,7 @@ describe("verify", function () { it("10", function () { verifyAndAssertMessages( - [ - "var a=function(a: T1, b: T2) {return a + b;}; a;" - ].join("\n"), + "var a=function(a: T1, b: T2) {return a + b;}; a;", { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -546,9 +569,7 @@ describe("verify", function () { it("11", function () { verifyAndAssertMessages( - [ - "var a={*id(x: T): T { x; }}; a;" - ].join("\n"), + "var a={*id(x: T): T { x; }}; a;", { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -556,9 +577,7 @@ describe("verify", function () { it("12", function () { verifyAndAssertMessages( - [ - "var a={async id(x: T): T { x; }}; a;" - ].join("\n"), + "var a={async id(x: T): T { x; }}; a;", { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -566,9 +585,7 @@ describe("verify", function () { it("13", function () { verifyAndAssertMessages( - [ - "var a={123(x: T): T { x; }}; a;" - ].join("\n"), + "var a={123(x: T): T { x; }}; a;", { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -576,11 +593,11 @@ describe("verify", function () { it("14", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "export default class Bar {set fooProp(value:Foo):Foo2{ value; }}" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + export default class Bar {set fooProp(value:Foo):Foo2{ value; }} + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -588,10 +605,10 @@ describe("verify", function () { it("15", function () { verifyAndAssertMessages( - [ - "import type Foo2 from 'foo';", - "export default class Foo {get fooProp(): Foo2{}}" - ].join("\n"), + unpad(` + import type Foo2 from 'foo'; + export default class Foo {get fooProp(): Foo2{}} + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -599,10 +616,10 @@ describe("verify", function () { it("16", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var numVal:Foo; numVal;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var numVal:Foo; numVal; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -610,10 +627,10 @@ describe("verify", function () { it("17", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var a: {numVal: Foo;}; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var a: {numVal: Foo;}; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -621,12 +638,12 @@ describe("verify", function () { it("18", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "import type Foo3 from 'foo';", - "var a: ?{numVal: Foo; [indexer: Foo2]: Foo3}; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + import type Foo3 from 'foo'; + var a: ?{numVal: Foo; [indexer: Foo2]: Foo3}; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -634,11 +651,11 @@ describe("verify", function () { it("19", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "var a: {numVal: Foo; subObj?: ?{strVal: Foo2}}; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + var a: {numVal: Foo; subObj?: ?{strVal: Foo2}}; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -646,13 +663,13 @@ describe("verify", function () { it("20", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "import type Foo3 from 'foo';", - "import type Foo4 from 'foo';", - "var a: { [a: Foo]: Foo2; [b: Foo3]: Foo4; }; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + import type Foo3 from 'foo'; + import type Foo4 from 'foo'; + var a: { [a: Foo]: Foo2; [b: Foo3]: Foo4; }; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -660,12 +677,12 @@ describe("verify", function () { it("21", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "import type Foo3 from 'foo';", - "var a: {add(x:Foo, ...y:Array): Foo3}; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + import type Foo3 from 'foo'; + var a: {add(x:Foo, ...y:Array): Foo3}; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -673,12 +690,12 @@ describe("verify", function () { it("22", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "import type Foo3 from 'foo';", - "var a: { id(x: Foo2): Foo3; }; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + import type Foo3 from 'foo'; + var a: { id(x: Foo2): Foo3; }; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -686,10 +703,10 @@ describe("verify", function () { it("23", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var a:Array = [1, 2, 3]; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var a:Array = [1, 2, 3]; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -697,10 +714,10 @@ describe("verify", function () { it("24", function () { verifyAndAssertMessages( - [ - "import type Baz from 'baz';", - "export default class Bar extends Baz { };" - ].join("\n"), + unpad(` + import type Baz from 'baz'; + export default class Bar extends Baz { }; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -708,9 +725,7 @@ describe("verify", function () { it("25", function () { verifyAndAssertMessages( - [ - "export default class Bar { bar(): T { return 42; }}" - ].join("\n"), + "export default class Bar { bar(): T { return 42; }}", { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -718,11 +733,11 @@ describe("verify", function () { it("26", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "export default class Bar { static prop1:Foo; prop2:Foo2; }" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + export default class Bar { static prop1:Foo; prop2:Foo2; } + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -730,11 +745,11 @@ describe("verify", function () { it("27", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "var x : Foo | Foo2 = 4; x;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + var x : Foo | Foo2 = 4; x; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -742,11 +757,11 @@ describe("verify", function () { it("28", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "var x : () => Foo | () => Foo2; x;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + var x : () => Foo | () => Foo2; x; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -754,11 +769,11 @@ describe("verify", function () { it("29", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "var x: typeof Foo | number = Foo2; x;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + var x: typeof Foo | number = Foo2; x; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -766,10 +781,10 @@ describe("verify", function () { it("30", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var {x}: {x: Foo; } = { x: 'hello' }; x;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var {x}: {x: Foo; } = { x: 'hello' }; x; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -777,10 +792,10 @@ describe("verify", function () { it("31", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var [x]: Array = [ 'hello' ]; x;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var [x]: Array = [ 'hello' ]; x; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -788,10 +803,10 @@ describe("verify", function () { it("32", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "export default function({x}: { x: Foo; }) { x; }" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + export default function({x}: { x: Foo; }) { x; } + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -799,10 +814,10 @@ describe("verify", function () { it("33", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "function foo([x]: Array) { x; } foo();" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + function foo([x]: Array) { x; } foo(); + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -810,11 +825,11 @@ describe("verify", function () { it("34", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "var a: Map >; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + var a: Map >; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -822,10 +837,10 @@ describe("verify", function () { it("35", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var a: ?Promise[]; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var a: ?Promise[]; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -833,11 +848,11 @@ describe("verify", function () { it("36", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "var a:(...rest:Array) => Foo2; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + var a:(...rest:Array) => Foo2; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -845,13 +860,13 @@ describe("verify", function () { it("37", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "import type Foo3 from 'foo';", - "import type Foo4 from 'foo';", - "var a: (x: Foo2, ...y:Foo3[]) => Foo4; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + import type Foo3 from 'foo'; + import type Foo4 from 'foo'; + var a: (x: Foo2, ...y:Foo3[]) => Foo4; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -859,10 +874,10 @@ describe("verify", function () { it("38", function () { verifyAndAssertMessages( - [ - "import type {foo, bar} from 'baz';", - "foo; bar;" - ].join("\n"), + unpad(` + import type {foo, bar} from 'baz'; + foo; bar; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -870,10 +885,10 @@ describe("verify", function () { it("39", function () { verifyAndAssertMessages( - [ - "import type {foo as bar} from 'baz';", - "bar;" - ].join("\n"), + unpad(` + import type {foo as bar} from 'baz'; + bar; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -881,10 +896,10 @@ describe("verify", function () { it("40", function () { verifyAndAssertMessages( - [ - "import type from 'foo';", - "type;" - ].join("\n"), + unpad(` + import type from 'foo'; + type; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -892,10 +907,10 @@ describe("verify", function () { it("41", function () { verifyAndAssertMessages( - [ - "import type, {foo} from 'bar';", - "type; foo;" - ].join("\n"), + unpad(` + import type, {foo} from 'bar'; + type; foo; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -903,10 +918,10 @@ describe("verify", function () { it("42", function () { verifyAndAssertMessages( - [ - "import type * as namespace from 'bar';", - "namespace;" - ].join("\n"), + unpad(` + import type * as namespace from 'bar'; + namespace; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -914,10 +929,10 @@ describe("verify", function () { it("43", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var a: Foo[]; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var a: Foo[]; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -925,10 +940,10 @@ describe("verify", function () { it("44", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var a: ?Foo[]; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var a: ?Foo[]; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -936,10 +951,10 @@ describe("verify", function () { it("45", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var a: (?Foo)[]; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var a: (?Foo)[]; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -947,10 +962,10 @@ describe("verify", function () { it("46", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var a: () => Foo[]; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var a: () => Foo[]; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -958,10 +973,10 @@ describe("verify", function () { it("47", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var a: (() => Foo)[]; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var a: (() => Foo)[]; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -969,10 +984,10 @@ describe("verify", function () { it("48", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "var a: typeof Foo[]; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + var a: typeof Foo[]; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -980,12 +995,12 @@ describe("verify", function () { it("49", function () { verifyAndAssertMessages( - [ - "import type Foo from 'foo';", - "import type Foo2 from 'foo';", - "import type Foo3 from 'foo';", - "var a : [Foo, Foo2,] = [123, 'duck',]; a;" - ].join("\n"), + unpad(` + import type Foo from 'foo'; + import type Foo2 from 'foo'; + import type Foo3 from 'foo'; + var a : [Foo, Foo2,] = [123, 'duck',]; a; + `), { "no-unused-vars": 1, "no-undef": 1 }, [] ); @@ -1001,13 +1016,14 @@ describe("verify", function () { }); it("class definition: gaearon/redux#24", function () { - verifyAndAssertMessages([ - "export default function root(stores) {", - "return DecoratedComponent => class ReduxRootDecorator {", - "a() { DecoratedComponent; stores; }", - "};", - "}", - ].join("\n"), + verifyAndAssertMessages( + unpad(` + export default function root(stores) { + return DecoratedComponent => class ReduxRootDecorator { + a() { DecoratedComponent; stores; } + }; + } + `), { "no-undef": 1, "no-unused-vars": 1 }, [] ); @@ -1030,13 +1046,15 @@ describe("verify", function () { }); it("template with destructuring #31", function () { - verifyAndAssertMessages([ - "module.exports = {", - "render() {", - "var {name} = this.props;", - "return Math.max(null, `Name: ${name}, Name: ${name}`);", - "}", - "};"].join("\n"), + verifyAndAssertMessages( + unpad(` + module.exports = { + render() { + var {name} = this.props; + return Math.max(null, \`Name: \${name}, Name: \${name}\`); + } + }; + `), { "comma-spacing": 1 }, [] ); @@ -1045,14 +1063,14 @@ describe("verify", function () { describe("decorators #72", function () { it("class declaration", function () { verifyAndAssertMessages( - [ - "import classDeclaration from 'decorator';", - "import decoratorParameter from 'decorator';", - "@classDeclaration((parameter) => parameter)", - "@classDeclaration(decoratorParameter)", - "@classDeclaration", - "export class TextareaAutosize {}" - ].join("\n"), + unpad(` + import classDeclaration from 'decorator'; + import decoratorParameter from 'decorator'; + @classDeclaration((parameter) => parameter) + @classDeclaration(decoratorParameter) + @classDeclaration + export class TextareaAutosize {} + `), { "no-unused-vars": 1 }, [] ); @@ -1060,18 +1078,18 @@ describe("verify", function () { it("method definition", function () { verifyAndAssertMessages( - [ - "import classMethodDeclarationA from 'decorator';", - "import decoratorParameter from 'decorator';", - "export class TextareaAutosize {", - "@classMethodDeclarationA((parameter) => parameter)", - "@classMethodDeclarationA(decoratorParameter)", - "@classMethodDeclarationA", - "methodDeclaration(e) {", - "e();", - "}", - "}" - ].join("\n"), + unpad(` + import classMethodDeclarationA from 'decorator'; + import decoratorParameter from 'decorator'; + export class TextareaAutosize { + @classMethodDeclarationA((parameter) => parameter) + @classMethodDeclarationA(decoratorParameter) + @classMethodDeclarationA + methodDeclaration(e) { + e(); + } + } + `), { "no-unused-vars": 1 }, [] ); @@ -1079,20 +1097,20 @@ describe("verify", function () { it("method definition get/set", function () { verifyAndAssertMessages( - [ - "import classMethodDeclarationA from 'decorator';", - "import decoratorParameter from 'decorator';", - "export class TextareaAutosize {", - "@classMethodDeclarationA((parameter) => parameter)", - "@classMethodDeclarationA(decoratorParameter)", - "@classMethodDeclarationA", - "get bar() { }", - "@classMethodDeclarationA((parameter) => parameter)", - "@classMethodDeclarationA(decoratorParameter)", - "@classMethodDeclarationA", - "set bar(val) { val; }", - "}" - ].join("\n"), + unpad(` + import classMethodDeclarationA from 'decorator'; + import decoratorParameter from 'decorator'; + export class TextareaAutosize { + @classMethodDeclarationA((parameter) => parameter) + @classMethodDeclarationA(decoratorParameter) + @classMethodDeclarationA + get bar() { } + @classMethodDeclarationA((parameter) => parameter) + @classMethodDeclarationA(decoratorParameter) + @classMethodDeclarationA + set bar(val) { val; } + } + `), { "no-unused-vars": 1 }, [] ); @@ -1100,19 +1118,19 @@ describe("verify", function () { it("object property", function () { verifyAndAssertMessages( - [ - "import classMethodDeclarationA from 'decorator';", - "import decoratorParameter from 'decorator';", - "var obj = {", - "@classMethodDeclarationA((parameter) => parameter)", - "@classMethodDeclarationA(decoratorParameter)", - "@classMethodDeclarationA", - "methodDeclaration(e) {", - "e();", - "}", - "};", - "obj;" - ].join("\n"), + unpad(` + import classMethodDeclarationA from 'decorator'; + import decoratorParameter from 'decorator'; + var obj = { + @classMethodDeclarationA((parameter) => parameter) + @classMethodDeclarationA(decoratorParameter) + @classMethodDeclarationA + methodDeclaration(e) { + e(); + } + }; + obj; + `), { "no-unused-vars": 1 }, [] ); @@ -1120,21 +1138,21 @@ describe("verify", function () { it("object property get/set", function () { verifyAndAssertMessages( - [ - "import classMethodDeclarationA from 'decorator';", - "import decoratorParameter from 'decorator';", - "var obj = {", - "@classMethodDeclarationA((parameter) => parameter)", - "@classMethodDeclarationA(decoratorParameter)", - "@classMethodDeclarationA", - "get bar() { },", - "@classMethodDeclarationA((parameter) => parameter)", - "@classMethodDeclarationA(decoratorParameter)", - "@classMethodDeclarationA", - "set bar(val) { val; }", - "};", - "obj;" - ].join("\n"), + unpad(` + import classMethodDeclarationA from 'decorator'; + import decoratorParameter from 'decorator'; + var obj = { + @classMethodDeclarationA((parameter) => parameter) + @classMethodDeclarationA(decoratorParameter) + @classMethodDeclarationA + get bar() { }, + @classMethodDeclarationA((parameter) => parameter) + @classMethodDeclarationA(decoratorParameter) + @classMethodDeclarationA + set bar(val) { val; } + }; + obj; + `), { "no-unused-vars": 1 }, [] ); @@ -1201,14 +1219,15 @@ describe("verify", function () { }); it("don't warn no-unused-vars with spread #142", function () { - verifyAndAssertMessages([ - "export default function test(data) {", - "return {", - "foo: 'bar',", - "...data", - "};", - "}", - ].join("\n"), + verifyAndAssertMessages( + unpad(` + export default function test(data) { + return { + foo: 'bar', + ...data + }; + } + `), { "no-undef": 1, "no-unused-vars": 1 }, [] ); @@ -1216,33 +1235,32 @@ describe("verify", function () { it("excludes comment tokens #153", function () { verifyAndAssertMessages( - [ - "var a = [", - "1,", - "2, // a trailing comment makes this line fail comma-dangle (always-multiline)", - "];", - ].join("\n"), + unpad(` + var a = [ + 1, + 2, // a trailing comment makes this line fail comma-dangle (always-multiline) + ]; + `), { "comma-dangle": [2, "always-multiline"] }, [] ); verifyAndAssertMessages( - [ - "switch (a) {", - "// A comment here makes the above line fail brace-style", - "case 1:", - "console.log(a);", - "}" - ].join("\n"), + unpad(` + switch (a) { + // A comment here makes the above line fail brace-style + case 1: + console.log(a); + } + `), { "brace-style": 2 }, [] ); }); it("ternary and parens #149", function () { - verifyAndAssertMessages([ - "true ? (true) : false;" - ].join("\n"), + verifyAndAssertMessages( + "true ? (true) : false;", { "space-infix-ops": 1 }, [] ); @@ -1250,15 +1268,15 @@ describe("verify", function () { it("line comment space-in-parens #124", function () { verifyAndAssertMessages( - [ - "React.createClass({", - "render() {", - "// return (", - "//
", - "// ); // <-- this is the line that is reported", - "}", - "});" - ].join("\n"), + unpad(` + React.createClass({ + render() { + // return ( + //
+ // ); // <-- this is the line that is reported + } + }); + `), { "space-in-parens": 1 }, [ ] ); @@ -1266,17 +1284,17 @@ describe("verify", function () { it("block comment space-in-parens #124", function () { verifyAndAssertMessages( - [ - "React.createClass({", - "render() {", - "/*", - "return (", - "
", - "); // <-- this is the line that is reported", - "*/", - "}", - "});" - ].join("\n"), + unpad(` + React.createClass({ + render() { + /* + return ( +
+ ); // <-- this is the line that is reported + */ + } + }); + `), { "space-in-parens": 1 }, [ ] ); @@ -1298,18 +1316,18 @@ describe("verify", function () { it("default param flow type no-unused-vars #184", function () { verifyAndAssertMessages( - [ - "type ResolveOptionType = {", - "depth?: number,", - "identifier?: string", - "};", - "", - "export default function resolve(", - "options: ResolveOptionType = {}", - "): Object {", - "options;", - "}", - ].join("\n"), + unpad(` + type ResolveOptionType = { + depth?: number, + identifier?: string + }; + + export default function resolve( + options: ResolveOptionType = {} + ): Object { + options; + } + `), { "no-unused-vars": 1, "no-undef": 1 }, [ ] ); @@ -1317,10 +1335,10 @@ describe("verify", function () { it("no-use-before-define #192", function () { verifyAndAssertMessages( - [ - "console.log(x);", - "var x = 1;" - ].join("\n"), + unpad(` + console.log(x); + var x = 1; + `), { "no-use-before-define": 1 }, [ "1:13 'x' was used before it was defined. no-use-before-define" ] ); @@ -1335,50 +1353,54 @@ describe("verify", function () { }); it("getter/setter #218", function () { - verifyAndAssertMessages([ - "class Person {", - " set a (v) { }", - "}" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + class Person { + set a (v) { } + } + `), { "space-before-function-paren": 1, "keyword-spacing": [1, {"before": true}], "indent": 1 }, [] ); }); it("getter/setter #220", function () { - verifyAndAssertMessages([ - "var B = {", - "get x () {", - "return this.ecks;", - "},", - "set x (ecks) {", - "this.ecks = ecks;", - "}", - "};" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + var B = { + get x () { + return this.ecks; + }, + set x (ecks) { + this.ecks = ecks; + } + }; + `), { "no-dupe-keys": 1 }, [] ); }); it("fixes issues with flow types and ObjectPattern", function () { - verifyAndAssertMessages([ - "import type Foo from 'bar';", - "export default class Foobar {", - " foo({ bar }: Foo) { bar; }", - " bar({ foo }: Foo) { foo; }", - "}" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import type Foo from 'bar'; + export default class Foobar { + foo({ bar }: Foo) { bar; } + bar({ foo }: Foo) { foo; } + } + `), { "no-unused-vars": 1, "no-shadow": 1 }, [] ); }); it("correctly detects redeclares if in script mode #217", function () { - verifyAndAssertMessages([ - "var a = 321;", - "var a = 123;", - ].join("\n"), + verifyAndAssertMessages( + unpad(` + var a = 321; + var a = 123; + `), { "no-redeclare": 1 }, [ "2:5 'a' is already defined. no-redeclare" ], "script" @@ -1386,10 +1408,11 @@ describe("verify", function () { }); it("correctly detects redeclares if in module mode #217", function () { - verifyAndAssertMessages([ - "var a = 321;", - "var a = 123;", - ].join("\n"), + verifyAndAssertMessages( + unpad(` + var a = 321; + var a = 123; + `), { "no-redeclare": 1 }, [ "2:5 'a' is already defined. no-redeclare" ], "module" @@ -1436,11 +1459,12 @@ describe("verify", function () { }); it("allowImportExportEverywhere option (#327)", function () { - verifyAndAssertMessages([ - "if (true) { import Foo from 'foo'; }", - "function foo() { import Bar from 'bar'; }", - "switch (a) { case 1: import FooBar from 'foobar'; }" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + if (true) { import Foo from 'foo'; } + function foo() { import Bar from 'bar'; } + switch (a) { case 1: import FooBar from 'foobar'; } + `), {}, [], "module", @@ -1477,49 +1501,53 @@ describe("verify", function () { }); it("decorator does not create TypeError #229", function () { - verifyAndAssertMessages([ - "class A {", - " @test", - " f() {}", - "}" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + class A { + @test + f() {} + } + `), { "no-undef": 1 }, [ "2:4 'test' is not defined. no-undef" ] ); }); it("Flow definition does not trigger warnings #223", function () { - verifyAndAssertMessages([ - "import { Map as $Map } from 'immutable';", - "function myFunction($state: $Map, { a, b, c } : { a: ?Object, b: ?Object, c: $Map }) {}" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + import { Map as $Map } from 'immutable'; + function myFunction($state: $Map, { a, b, c } : { a: ?Object, b: ?Object, c: $Map }) {} + `), { "no-dupe-args": 1, "no-redeclare": 1, "no-shadow": 1 }, [] ); }); it("newline-before-return with comments #289", function () { - verifyAndAssertMessages([ - "function a() {", - "if (b) {", - "/* eslint-disable no-console */", - "console.log('test');", - "/* eslint-enable no-console */", - "}", - "", - "return hasGlobal;", - "}" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + function a() { + if (b) { + /* eslint-disable no-console */ + console.log('test'); + /* eslint-enable no-console */ + } + + return hasGlobal; + } + `), { "newline-before-return": 1 }, [] ); }); it("spaced-comment with shebang #163", function () { - verifyAndAssertMessages(["#!/usr/bin/env babel-node", - "", - "import {spawn} from 'foobar';" - ].join("\n"), + verifyAndAssertMessages( + unpad(` + #!/usr/bin/env babel-node + import {spawn} from 'foobar'; + `), { "spaced-comment": 1 }, [] ); @@ -1528,14 +1556,14 @@ describe("verify", function () { describe("Class Property Declarations", function() { it("no-redeclare false positive 1", function() { verifyAndAssertMessages( - [ - "class Group {", - " static propTypes = {};", - "}", - "class TypicalForm {", - " static propTypes = {};", - "}" - ].join("\n"), + unpad(` + class Group { + static propTypes = {}; + } + class TypicalForm { + static propTypes = {}; + } + `), { "no-redeclare": 1 }, [] ); @@ -1543,12 +1571,12 @@ describe("verify", function () { it("no-redeclare false positive 2", function() { verifyAndAssertMessages( - [ - "function validate() {}", - "class MyComponent {", - " static validate = validate;", - "}" - ].join("\n"), + unpad(` + function validate() {} + class MyComponent { + static validate = validate; + } + `), { "no-redeclare": 1 }, [] ); @@ -1556,15 +1584,15 @@ describe("verify", function () { it("check references", function() { verifyAndAssertMessages( - [ - "var a;", - "class A {", - " prop1;", - " prop2 = a;", - " prop3 = b;", - "}", - "new A" - ].join("\n"), + unpad(` + var a; + class A { + prop1; + prop2 = a; + prop3 = b; + } + new A + `), { "no-undef": 1, "no-unused-vars": 1, "no-redeclare": 1 }, [ "5:11 'b' is not defined. no-undef" diff --git a/eslint/babel-eslint-parser/utils/unpad.js b/eslint/babel-eslint-parser/utils/unpad.js new file mode 100644 index 0000000000..63aca71efb --- /dev/null +++ b/eslint/babel-eslint-parser/utils/unpad.js @@ -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;