From 53a3dac011dbc20808cafcd17bab5076dacf3628 Mon Sep 17 00:00:00 2001 From: Kai Cataldo <7041728+kaicataldo@users.noreply.github.com> Date: Sun, 17 Nov 2019 05:01:10 -0500 Subject: [PATCH] Clean up eslint/* directories (#10729) --- .../babel-eslint-config-internal/.npmignore | 4 + eslint/babel-eslint-parser/.gitignore | 2 - eslint/babel-eslint-parser/.npmignore | 4 + eslint/babel-eslint-parser/.npmrc | 1 - eslint/babel-eslint-parser/.travis.yml | 16 - eslint/babel-eslint-parser/Makefile | 7 - eslint/babel-eslint-parser/package.json | 3 - .../.npmignore | 6 +- .../package.json | 2 +- eslint/babel-eslint-plugin/.npmignore | 4 + eslint/babel-eslint-plugin/.travis.yml | 9 - eslint/babel-eslint-plugin/package.json | 2 +- eslint/babel-eslint-plugin/{ => src}/index.js | 0 .../{ => src}/rules/array-bracket-spacing.js | 0 .../{ => src}/rules/arrow-parens.js | 0 .../{ => src}/rules/camelcase.js | 0 .../{ => src}/rules/flow-object-type.js | 0 .../rules/func-params-comma-dangle.js | 0 .../{ => src}/rules/generator-star-spacing.js | 0 .../{ => src}/rules/new-cap.js | 0 .../{ => src}/rules/no-await-in-loop.js | 0 .../{ => src}/rules/no-invalid-this.js | 0 .../{ => src}/rules/no-unused-expressions.js | 0 .../{ => src}/rules/object-curly-spacing.js | 0 .../{ => src}/rules/object-shorthand.js | 0 .../{ => src}/rules/quotes.js | 0 .../{ => src}/rules/semi.js | 0 .../{ => src}/rules/valid-typeof.js | 0 .../test/rules/camelcase.js | 340 +-- .../babel-eslint-plugin/test/rules/new-cap.js | 397 ++-- .../test/rules/no-invalid-this.js | 1115 +++++----- .../test/rules/no-unused-expressions.js | 482 +++-- .../test/rules/object-curly-spacing.js | 1855 ++++++++++------- .../babel-eslint-plugin/test/rules/quotes.js | 761 ++++--- eslint/babel-eslint-plugin/test/rules/semi.js | 843 ++++++-- .../test/rules/valid-typeof.js | 379 ++-- 36 files changed, 3838 insertions(+), 2394 deletions(-) create mode 100644 eslint/babel-eslint-config-internal/.npmignore delete mode 100644 eslint/babel-eslint-parser/.gitignore create mode 100644 eslint/babel-eslint-parser/.npmignore delete mode 100644 eslint/babel-eslint-parser/.npmrc delete mode 100644 eslint/babel-eslint-parser/.travis.yml delete mode 100644 eslint/babel-eslint-parser/Makefile create mode 100644 eslint/babel-eslint-plugin/.npmignore delete mode 100644 eslint/babel-eslint-plugin/.travis.yml rename eslint/babel-eslint-plugin/{ => src}/index.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/array-bracket-spacing.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/arrow-parens.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/camelcase.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/flow-object-type.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/func-params-comma-dangle.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/generator-star-spacing.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/new-cap.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/no-await-in-loop.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/no-invalid-this.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/no-unused-expressions.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/object-curly-spacing.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/object-shorthand.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/quotes.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/semi.js (100%) rename eslint/babel-eslint-plugin/{ => src}/rules/valid-typeof.js (100%) diff --git a/eslint/babel-eslint-config-internal/.npmignore b/eslint/babel-eslint-config-internal/.npmignore new file mode 100644 index 0000000000..ad69326ce8 --- /dev/null +++ b/eslint/babel-eslint-config-internal/.npmignore @@ -0,0 +1,4 @@ +src +test +.* +*.log diff --git a/eslint/babel-eslint-parser/.gitignore b/eslint/babel-eslint-parser/.gitignore deleted file mode 100644 index 93f1361991..0000000000 --- a/eslint/babel-eslint-parser/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules -npm-debug.log diff --git a/eslint/babel-eslint-parser/.npmignore b/eslint/babel-eslint-parser/.npmignore new file mode 100644 index 0000000000..c3aafe7a77 --- /dev/null +++ b/eslint/babel-eslint-parser/.npmignore @@ -0,0 +1,4 @@ +src +test +.* +*.log \ No newline at end of file diff --git a/eslint/babel-eslint-parser/.npmrc b/eslint/babel-eslint-parser/.npmrc deleted file mode 100644 index c1ca392fea..0000000000 --- a/eslint/babel-eslint-parser/.npmrc +++ /dev/null @@ -1 +0,0 @@ -package-lock = false diff --git a/eslint/babel-eslint-parser/.travis.yml b/eslint/babel-eslint-parser/.travis.yml deleted file mode 100644 index 8ece2fc212..0000000000 --- a/eslint/babel-eslint-parser/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -sudo: false -language: node_js -node_js: - - "12" - - "10" - - "8" - -matrix: - fast_finish: true - include: - - node_js: "node" - env: LINT=true - -script: - - 'if [ -n "${LINT-}" ]; then npm run lint ; fi' - - 'if [ -z "${LINT-}" ]; then npm test ; fi' diff --git a/eslint/babel-eslint-parser/Makefile b/eslint/babel-eslint-parser/Makefile deleted file mode 100644 index d35bcfa016..0000000000 --- a/eslint/babel-eslint-parser/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -.PHONY: publish-patch - -publish-patch: - ./node_modules/.bin/mocha - npm version patch - npm publish - git push --follow-tags diff --git a/eslint/babel-eslint-parser/package.json b/eslint/babel-eslint-parser/package.json index 6cb74e76f2..ebbcbdb79d 100644 --- a/eslint/babel-eslint-parser/package.json +++ b/eslint/babel-eslint-parser/package.json @@ -20,9 +20,6 @@ "node": ">=6" }, "main": "lib/index.js", - "files": [ - "lib" - ], "peerDependencies": { "@babel/core": ">=7.2.0", "eslint": ">= 4.12.1" diff --git a/eslint/babel-eslint-plugin-development/.npmignore b/eslint/babel-eslint-plugin-development/.npmignore index 7810671790..c3aafe7a77 100644 --- a/eslint/babel-eslint-plugin-development/.npmignore +++ b/eslint/babel-eslint-plugin-development/.npmignore @@ -1,2 +1,4 @@ -tests/ -.* \ No newline at end of file +src +test +.* +*.log \ No newline at end of file diff --git a/eslint/babel-eslint-plugin-development/package.json b/eslint/babel-eslint-plugin-development/package.json index 1cb2805898..68d877ba3c 100644 --- a/eslint/babel-eslint-plugin-development/package.json +++ b/eslint/babel-eslint-plugin-development/package.json @@ -13,7 +13,7 @@ "email": "nicolo.ribaudo@gmail.com", "url": "https://github.com/nicolo-ribaudo" }, - "main": "src/index.js", + "main": "lib/index.js", "devDependencies": { "eslint": "^5.9.0" }, diff --git a/eslint/babel-eslint-plugin/.npmignore b/eslint/babel-eslint-plugin/.npmignore new file mode 100644 index 0000000000..ad69326ce8 --- /dev/null +++ b/eslint/babel-eslint-plugin/.npmignore @@ -0,0 +1,4 @@ +src +test +.* +*.log diff --git a/eslint/babel-eslint-plugin/.travis.yml b/eslint/babel-eslint-plugin/.travis.yml deleted file mode 100644 index 068a0122c8..0000000000 --- a/eslint/babel-eslint-plugin/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ ---- -git: - depth: 1 -sudo: false -language: node_js -node_js: - - 6 - - 8 - - 10 diff --git a/eslint/babel-eslint-plugin/package.json b/eslint/babel-eslint-plugin/package.json index b4780d73d6..653ebcaf43 100644 --- a/eslint/babel-eslint-plugin/package.json +++ b/eslint/babel-eslint-plugin/package.json @@ -2,7 +2,7 @@ "name": "eslint-plugin-babel", "version": "5.3.0", "description": "an eslint rule plugin companion to babel-eslint", - "main": "index.js", + "main": "lib/index.js", "repository": { "type": "git", "url": "git+https://github.com/babel/eslint-plugin-babel.git" diff --git a/eslint/babel-eslint-plugin/index.js b/eslint/babel-eslint-plugin/src/index.js similarity index 100% rename from eslint/babel-eslint-plugin/index.js rename to eslint/babel-eslint-plugin/src/index.js diff --git a/eslint/babel-eslint-plugin/rules/array-bracket-spacing.js b/eslint/babel-eslint-plugin/src/rules/array-bracket-spacing.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/array-bracket-spacing.js rename to eslint/babel-eslint-plugin/src/rules/array-bracket-spacing.js diff --git a/eslint/babel-eslint-plugin/rules/arrow-parens.js b/eslint/babel-eslint-plugin/src/rules/arrow-parens.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/arrow-parens.js rename to eslint/babel-eslint-plugin/src/rules/arrow-parens.js diff --git a/eslint/babel-eslint-plugin/rules/camelcase.js b/eslint/babel-eslint-plugin/src/rules/camelcase.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/camelcase.js rename to eslint/babel-eslint-plugin/src/rules/camelcase.js diff --git a/eslint/babel-eslint-plugin/rules/flow-object-type.js b/eslint/babel-eslint-plugin/src/rules/flow-object-type.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/flow-object-type.js rename to eslint/babel-eslint-plugin/src/rules/flow-object-type.js diff --git a/eslint/babel-eslint-plugin/rules/func-params-comma-dangle.js b/eslint/babel-eslint-plugin/src/rules/func-params-comma-dangle.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/func-params-comma-dangle.js rename to eslint/babel-eslint-plugin/src/rules/func-params-comma-dangle.js diff --git a/eslint/babel-eslint-plugin/rules/generator-star-spacing.js b/eslint/babel-eslint-plugin/src/rules/generator-star-spacing.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/generator-star-spacing.js rename to eslint/babel-eslint-plugin/src/rules/generator-star-spacing.js diff --git a/eslint/babel-eslint-plugin/rules/new-cap.js b/eslint/babel-eslint-plugin/src/rules/new-cap.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/new-cap.js rename to eslint/babel-eslint-plugin/src/rules/new-cap.js diff --git a/eslint/babel-eslint-plugin/rules/no-await-in-loop.js b/eslint/babel-eslint-plugin/src/rules/no-await-in-loop.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/no-await-in-loop.js rename to eslint/babel-eslint-plugin/src/rules/no-await-in-loop.js diff --git a/eslint/babel-eslint-plugin/rules/no-invalid-this.js b/eslint/babel-eslint-plugin/src/rules/no-invalid-this.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/no-invalid-this.js rename to eslint/babel-eslint-plugin/src/rules/no-invalid-this.js diff --git a/eslint/babel-eslint-plugin/rules/no-unused-expressions.js b/eslint/babel-eslint-plugin/src/rules/no-unused-expressions.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/no-unused-expressions.js rename to eslint/babel-eslint-plugin/src/rules/no-unused-expressions.js diff --git a/eslint/babel-eslint-plugin/rules/object-curly-spacing.js b/eslint/babel-eslint-plugin/src/rules/object-curly-spacing.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/object-curly-spacing.js rename to eslint/babel-eslint-plugin/src/rules/object-curly-spacing.js diff --git a/eslint/babel-eslint-plugin/rules/object-shorthand.js b/eslint/babel-eslint-plugin/src/rules/object-shorthand.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/object-shorthand.js rename to eslint/babel-eslint-plugin/src/rules/object-shorthand.js diff --git a/eslint/babel-eslint-plugin/rules/quotes.js b/eslint/babel-eslint-plugin/src/rules/quotes.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/quotes.js rename to eslint/babel-eslint-plugin/src/rules/quotes.js diff --git a/eslint/babel-eslint-plugin/rules/semi.js b/eslint/babel-eslint-plugin/src/rules/semi.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/semi.js rename to eslint/babel-eslint-plugin/src/rules/semi.js diff --git a/eslint/babel-eslint-plugin/rules/valid-typeof.js b/eslint/babel-eslint-plugin/src/rules/valid-typeof.js similarity index 100% rename from eslint/babel-eslint-plugin/rules/valid-typeof.js rename to eslint/babel-eslint-plugin/src/rules/valid-typeof.js diff --git a/eslint/babel-eslint-plugin/test/rules/camelcase.js b/eslint/babel-eslint-plugin/test/rules/camelcase.js index af399b4133..f6d1d9249b 100644 --- a/eslint/babel-eslint-plugin/test/rules/camelcase.js +++ b/eslint/babel-eslint-plugin/test/rules/camelcase.js @@ -9,7 +9,7 @@ // Requirements //------------------------------------------------------------------------------ -const rule = require("../../rules/camelcase"), +const rule = require("../../src/rules/camelcase"), RuleTester = require("../helpers/RuleTester"); //------------------------------------------------------------------------------ @@ -21,10 +21,10 @@ const ruleTester = new RuleTester(); ruleTester.run("camelcase", rule, { valid: [ // Original test cases. - "firstName = \"Nicholas\"", - "FIRST_NAME = \"Nicholas\"", - "__myPrivateVariable = \"Patrick\"", - "myPrivateVariable_ = \"Patrick\"", + 'firstName = "Nicholas"', + 'FIRST_NAME = "Nicholas"', + '__myPrivateVariable = "Patrick"', + 'myPrivateVariable_ = "Patrick"', "function doSomething(){}", "do_something()", "new do_something", @@ -42,175 +42,175 @@ ruleTester.run("camelcase", rule, { "if (foo.bar_baz === boom.bam_pow) { [foo.baz_boom] }", { code: "var o = {key: 1}", - options: [{ properties: "always" }] + options: [{ properties: "always" }], }, { code: "var o = {_leading: 1}", - options: [{ properties: "always" }] + options: [{ properties: "always" }], }, { code: "var o = {trailing_: 1}", - options: [{ properties: "always" }] + options: [{ properties: "always" }], }, { code: "var o = {bar_baz: 1}", - options: [{ properties: "never" }] + options: [{ properties: "never" }], }, { code: "var o = {_leading: 1}", - options: [{ properties: "never" }] + options: [{ properties: "never" }], }, { code: "var o = {trailing_: 1}", - options: [{ properties: "never" }] + options: [{ properties: "never" }], }, { code: "obj.a_b = 2;", - options: [{ properties: "never" }] + options: [{ properties: "never" }], }, { code: "obj._a = 2;", - options: [{ properties: "always" }] + options: [{ properties: "always" }], }, { code: "obj.a_ = 2;", - options: [{ properties: "always" }] + options: [{ properties: "always" }], }, { code: "obj._a = 2;", - options: [{ properties: "never" }] + options: [{ properties: "never" }], }, { code: "obj.a_ = 2;", - options: [{ properties: "never" }] + options: [{ properties: "never" }], }, { code: "var obj = {\n a_a: 1 \n};\n obj.a_b = 2;", - options: [{ properties: "never" }] + options: [{ properties: "never" }], }, { code: "obj.foo_bar = function(){};", - options: [{ properties: "never" }] + options: [{ properties: "never" }], }, { code: "var { category_id } = query;", options: [{ ignoreDestructuring: true }], - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "var { category_id: category_id } = query;", options: [{ ignoreDestructuring: true }], - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "var { category_id = 1 } = query;", options: [{ ignoreDestructuring: true }], parserOptions: { ecmaVersion: 6 }, - }, { code: "var { category_id: category } = query;", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "var { _leading } = query;", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "var { trailing_ } = query;", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { - code: "import { camelCased } from \"external module\";", - parserOptions: { ecmaVersion: 6, sourceType: "module" } + code: 'import { camelCased } from "external module";', + parserOptions: { ecmaVersion: 6, sourceType: "module" }, }, { - code: "import { _leading } from \"external module\";", - parserOptions: { ecmaVersion: 6, sourceType: "module" } + code: 'import { _leading } from "external module";', + parserOptions: { ecmaVersion: 6, sourceType: "module" }, }, { - code: "import { trailing_ } from \"external module\";", - parserOptions: { ecmaVersion: 6, sourceType: "module" } + code: 'import { trailing_ } from "external module";', + parserOptions: { ecmaVersion: 6, sourceType: "module" }, }, { - code: "import { no_camelcased as camelCased } from \"external-module\";", - parserOptions: { ecmaVersion: 6, sourceType: "module" } + code: 'import { no_camelcased as camelCased } from "external-module";', + parserOptions: { ecmaVersion: 6, sourceType: "module" }, }, { - code: "import { no_camelcased as _leading } from \"external-module\";", - parserOptions: { ecmaVersion: 6, sourceType: "module" } + code: 'import { no_camelcased as _leading } from "external-module";', + parserOptions: { ecmaVersion: 6, sourceType: "module" }, }, { - code: "import { no_camelcased as trailing_ } from \"external-module\";", - parserOptions: { ecmaVersion: 6, sourceType: "module" } + code: 'import { no_camelcased as trailing_ } from "external-module";', + parserOptions: { ecmaVersion: 6, sourceType: "module" }, }, { - code: "import { no_camelcased as camelCased, anoterCamelCased } from \"external-module\";", - parserOptions: { ecmaVersion: 6, sourceType: "module" } + code: + 'import { no_camelcased as camelCased, anoterCamelCased } from "external-module";', + parserOptions: { ecmaVersion: 6, sourceType: "module" }, }, { code: "function foo({ no_camelcased: camelCased }) {};", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "function foo({ no_camelcased: _leading }) {};", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "function foo({ no_camelcased: trailing_ }) {};", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "function foo({ camelCased = 'default value' }) {};", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "function foo({ _leading = 'default value' }) {};", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "function foo({ trailing_ = 'default value' }) {};", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "function foo({ camelCased }) {};", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "function foo({ _leading }) {}", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, { code: "function foo({ trailing_ }) {}", - parserOptions: { ecmaVersion: 6 } + parserOptions: { ecmaVersion: 6 }, }, // Babel-specific test cases { code: "var foo = bar?.a_b;", - options: [{ properties: "never" }] + options: [{ properties: "never" }], }, ], invalid: [ { - code: "first_name = \"Nicholas\"", + code: 'first_name = "Nicholas"', errors: [ { messageId: "notCamelCase", data: { name: "first_name" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "__private_first_name = \"Patrick\"", + code: '__private_first_name = "Patrick"', errors: [ { messageId: "notCamelCase", data: { name: "__private_first_name" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "function foo_bar(){}", @@ -218,9 +218,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "foo_bar" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "obj.foo_bar = function(){};", @@ -228,9 +228,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "foo_bar" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "bar_baz.foo = function(){};", @@ -238,9 +238,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "bar_baz" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "[foo_bar.baz]", @@ -248,9 +248,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "foo_bar" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "if (foo.bar_baz === boom.bam_pow) { [foo_bar.baz] }", @@ -258,9 +258,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "foo_bar" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "foo.bar_baz = boom.bam_pow", @@ -268,9 +268,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "bar_baz" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "var foo = { bar_baz: boom.bam_pow }", @@ -278,9 +278,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "bar_baz" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "foo.qux.boom_pow = { bar: boom.bam_pow }", @@ -288,9 +288,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "boom_pow" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "var o = {bar_baz: 1}", @@ -299,9 +299,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "bar_baz" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "obj.a_b = 2;", @@ -310,9 +310,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "a_b" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "var { category_id: category_alias } = query;", @@ -321,9 +321,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "category_alias" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "var { category_id: category_alias } = query;", @@ -333,9 +333,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "category_alias" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "var { category_id: categoryId, ...other_props } = query;", @@ -345,9 +345,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "other_props" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "var { category_id } = query;", @@ -356,9 +356,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "category_id" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "var { category_id: category_id } = query;", @@ -367,9 +367,9 @@ ruleTester.run("camelcase", rule, { { messageId: "notCamelCase", data: { name: "category_id" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "var { category_id = 1 } = query;", @@ -377,108 +377,111 @@ ruleTester.run("camelcase", rule, { errors: [ { message: "Identifier 'category_id' is not in camel case.", - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "import no_camelcased from \"external-module\";", + code: 'import no_camelcased from "external-module";', parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ { messageId: "notCamelCase", data: { name: "no_camelcased" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "import * as no_camelcased from \"external-module\";", + code: 'import * as no_camelcased from "external-module";', parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ { messageId: "notCamelCase", data: { name: "no_camelcased" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "import { no_camelcased } from \"external-module\";", + code: 'import { no_camelcased } from "external-module";', parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ { messageId: "notCamelCase", data: { name: "no_camelcased" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "import { no_camelcased as no_camel_cased } from \"external module\";", + code: + 'import { no_camelcased as no_camel_cased } from "external module";', parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ { messageId: "notCamelCase", data: { name: "no_camel_cased" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "import { camelCased as no_camel_cased } from \"external module\";", + code: 'import { camelCased as no_camel_cased } from "external module";', parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ { messageId: "notCamelCase", data: { name: "no_camel_cased" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "import { camelCased, no_camelcased } from \"external-module\";", + code: 'import { camelCased, no_camelcased } from "external-module";', parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ { messageId: "notCamelCase", data: { name: "no_camelcased" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "import { no_camelcased as camelCased, another_no_camelcased } from \"external-module\";", + code: + 'import { no_camelcased as camelCased, another_no_camelcased } from "external-module";', parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ { messageId: "notCamelCase", data: { name: "another_no_camelcased" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "import camelCased, { no_camelcased } from \"external-module\";", + code: 'import camelCased, { no_camelcased } from "external-module";', parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ { messageId: "notCamelCase", data: { name: "no_camelcased" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "import no_camelcased, { another_no_camelcased as camelCased } from \"external-module\";", + code: + 'import no_camelcased, { another_no_camelcased as camelCased } from "external-module";', parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ { messageId: "notCamelCase", data: { name: "no_camelcased" }, - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "function foo({ no_camelcased }) {};", @@ -486,9 +489,9 @@ ruleTester.run("camelcase", rule, { errors: [ { message: "Identifier 'no_camelcased' is not in camel case.", - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "function foo({ no_camelcased = 'default value' }) {};", @@ -496,23 +499,24 @@ ruleTester.run("camelcase", rule, { errors: [ { message: "Identifier 'no_camelcased' is not in camel case.", - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { - code: "const no_camelcased = 0; function foo({ camelcased_value = no_camelcased}) {}", + code: + "const no_camelcased = 0; function foo({ camelcased_value = no_camelcased}) {}", parserOptions: { ecmaVersion: 6 }, errors: [ { message: "Identifier 'no_camelcased' is not in camel case.", - type: "Identifier" + type: "Identifier", }, { message: "Identifier 'camelcased_value' is not in camel case.", - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "const { bar: no_camelcased } = foo;", @@ -520,9 +524,9 @@ ruleTester.run("camelcase", rule, { errors: [ { message: "Identifier 'no_camelcased' is not in camel case.", - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "function foo({ value_1: my_default }) {}", @@ -530,9 +534,9 @@ ruleTester.run("camelcase", rule, { errors: [ { message: "Identifier 'my_default' is not in camel case.", - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "function foo({ isCamelcased: no_camelcased }) {};", @@ -540,9 +544,9 @@ ruleTester.run("camelcase", rule, { errors: [ { message: "Identifier 'no_camelcased' is not in camel case.", - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "var { foo: bar_baz = 1 } = quz;", @@ -550,9 +554,9 @@ ruleTester.run("camelcase", rule, { errors: [ { message: "Identifier 'bar_baz' is not in camel case.", - type: "Identifier" - } - ] + type: "Identifier", + }, + ], }, { code: "const { no_camelcased = false } = bar;", @@ -560,9 +564,9 @@ ruleTester.run("camelcase", rule, { errors: [ { message: "Identifier 'no_camelcased' is not in camel case.", - type: "Identifier" - } - ] - } - ] + type: "Identifier", + }, + ], + }, + ], }); diff --git a/eslint/babel-eslint-plugin/test/rules/new-cap.js b/eslint/babel-eslint-plugin/test/rules/new-cap.js index 225867e597..1dc8d3069a 100644 --- a/eslint/babel-eslint-plugin/test/rules/new-cap.js +++ b/eslint/babel-eslint-plugin/test/rules/new-cap.js @@ -3,155 +3,298 @@ * @author Nicholas C. Zakas */ -var rule = require('../../rules/new-cap'), - RuleTester = require("../helpers/RuleTester"); +var rule = require("../../src/rules/new-cap"), + RuleTester = require("../helpers/RuleTester"); var ruleTester = new RuleTester(); -ruleTester.run('babel/new-cap', rule, { - valid: [ - // Original test cases. - "var x = new Constructor();", - "var x = new a.b.Constructor();", - "var x = new a.b['Constructor']();", - "var x = new a.b[Constructor]();", - "var x = new a.b[constructor]();", - "var x = new function(){};", - "var x = new _;", - "var x = new $;", - "var x = new Σ;", - "var x = new _x;", - "var x = new $x;", - "var x = new this;", - "var x = Array(42)", - "var x = Boolean(42)", - "var x = Date(42)", - "var x = Date.UTC(2000, 0)", - "var x = Error('error')", - "var x = Function('return 0')", - "var x = Number(42)", - "var x = Object(null)", - "var x = RegExp(42)", - "var x = String(42)", - "var x = Symbol('symbol')", - "var x = _();", - "var x = $();", - { code: "var x = Foo(42)", options: [{ capIsNew: false }] }, - { code: "var x = bar.Foo(42)", options: [{ capIsNew: false }] }, - { code: "var x = Foo.bar(42)", options: [{ capIsNew: false }] }, - "var x = bar[Foo](42)", - { code: "var x = bar['Foo'](42)", options: [{ capIsNew: false }] }, - "var x = Foo.bar(42)", - { code: "var x = new foo(42)", options: [{ newIsCap: false }] }, - "var o = { 1: function() {} }; o[1]();", - "var o = { 1: function() {} }; new o[1]();", - { code: "var x = Foo(42);", options: [{ capIsNew: true, capIsNewExceptions: ["Foo"] }] }, - { code: "var x = Foo(42);", options: [{ capIsNewExceptionPattern: "^Foo" }] }, - { code: "var x = new foo(42);", options: [{ newIsCap: true, newIsCapExceptions: ["foo"] }] }, - { code: "var x = new foo(42);", options: [{ newIsCapExceptionPattern: "^foo" }] }, - { code: "var x = Object(42);", options: [{ capIsNewExceptions: ["Foo"] }] }, +ruleTester.run("babel/new-cap", rule, { + valid: [ + // Original test cases. + "var x = new Constructor();", + "var x = new a.b.Constructor();", + "var x = new a.b['Constructor']();", + "var x = new a.b[Constructor]();", + "var x = new a.b[constructor]();", + "var x = new function(){};", + "var x = new _;", + "var x = new $;", + "var x = new Σ;", + "var x = new _x;", + "var x = new $x;", + "var x = new this;", + "var x = Array(42)", + "var x = Boolean(42)", + "var x = Date(42)", + "var x = Date.UTC(2000, 0)", + "var x = Error('error')", + "var x = Function('return 0')", + "var x = Number(42)", + "var x = Object(null)", + "var x = RegExp(42)", + "var x = String(42)", + "var x = Symbol('symbol')", + "var x = _();", + "var x = $();", + { code: "var x = Foo(42)", options: [{ capIsNew: false }] }, + { code: "var x = bar.Foo(42)", options: [{ capIsNew: false }] }, + { code: "var x = Foo.bar(42)", options: [{ capIsNew: false }] }, + "var x = bar[Foo](42)", + { code: "var x = bar['Foo'](42)", options: [{ capIsNew: false }] }, + "var x = Foo.bar(42)", + { code: "var x = new foo(42)", options: [{ newIsCap: false }] }, + "var o = { 1: function() {} }; o[1]();", + "var o = { 1: function() {} }; new o[1]();", + { + code: "var x = Foo(42);", + options: [{ capIsNew: true, capIsNewExceptions: ["Foo"] }], + }, + { + code: "var x = Foo(42);", + options: [{ capIsNewExceptionPattern: "^Foo" }], + }, + { + code: "var x = new foo(42);", + options: [{ newIsCap: true, newIsCapExceptions: ["foo"] }], + }, + { + code: "var x = new foo(42);", + options: [{ newIsCapExceptionPattern: "^foo" }], + }, + { code: "var x = Object(42);", options: [{ capIsNewExceptions: ["Foo"] }] }, - { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Bar"] }] }, - { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Foo.Bar"] }] }, + { + code: "var x = Foo.Bar(42);", + options: [{ capIsNewExceptions: ["Bar"] }], + }, + { + code: "var x = Foo.Bar(42);", + options: [{ capIsNewExceptions: ["Foo.Bar"] }], + }, - { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptionPattern: "^Foo\\.." }] }, - { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["bar"] }] }, - { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["foo.bar"] }] }, + { + code: "var x = Foo.Bar(42);", + options: [{ capIsNewExceptionPattern: "^Foo\\.." }], + }, + { + code: "var x = new foo.bar(42);", + options: [{ newIsCapExceptions: ["bar"] }], + }, + { + code: "var x = new foo.bar(42);", + options: [{ newIsCapExceptions: ["foo.bar"] }], + }, - { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptionPattern: "^foo\\.." }] }, - { code: "var x = new foo.bar(42);", options: [{ properties: false }] }, - { code: "var x = Foo.bar(42);", options: [{ properties: false }] }, - { code: "var x = foo.Bar(42);", options: [{ capIsNew: false, properties: false }] }, + { + code: "var x = new foo.bar(42);", + options: [{ newIsCapExceptionPattern: "^foo\\.." }], + }, + { code: "var x = new foo.bar(42);", options: [{ properties: false }] }, + { code: "var x = Foo.bar(42);", options: [{ properties: false }] }, + { + code: "var x = foo.Bar(42);", + options: [{ capIsNew: false, properties: false }], + }, - // Babel-specific test cases. - { code: "@MyDecorator(123) class MyClass{}", parser: "babel-eslint" }, - ], - invalid: [ - { code: "var x = new c();", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] }, - { code: "var x = new φ;", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] }, - { code: "var x = new a.b.c;", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] }, - { code: "var x = new a.b['c'];", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] }, - { code: "var b = Foo();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] }, - { code: "var b = a.Foo();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] }, - { code: "var b = a['Foo']();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] }, - { code: "var b = a.Date.UTC();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] }, - { code: "var b = UTC();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] }, + // Babel-specific test cases. + { code: "@MyDecorator(123) class MyClass{}", parser: "babel-eslint" }, + ], + invalid: [ + { + code: "var x = new c();", + errors: [ { - code: "var a = B.C();", - errors: [ - { - message: "A function with a name starting with an uppercase letter should only be used as a constructor.", - type: "CallExpression", - line: 1, - column: 11 - } - ] + message: + "A constructor name should not start with a lowercase letter.", + type: "NewExpression", }, + ], + }, + { + code: "var x = new φ;", + errors: [ { - code: "var a = B\n.C();", - errors: [ - { - message: "A function with a name starting with an uppercase letter should only be used as a constructor.", - type: "CallExpression", - line: 2, - column: 2 - } - ] + message: + "A constructor name should not start with a lowercase letter.", + type: "NewExpression", }, + ], + }, + { + code: "var x = new a.b.c;", + errors: [ { - code: "var a = new B.c();", - errors: [ - { - message: "A constructor name should not start with a lowercase letter.", - type: "NewExpression", - line: 1, - column: 15 - } - ] + message: + "A constructor name should not start with a lowercase letter.", + type: "NewExpression", }, + ], + }, + { + code: "var x = new a.b['c'];", + errors: [ { - code: "var a = new B.\nc();", - errors: [ - { - message: "A constructor name should not start with a lowercase letter.", - type: "NewExpression", - line: 2, - column: 1 - } - ] + message: + "A constructor name should not start with a lowercase letter.", + type: "NewExpression", }, + ], + }, + { + code: "var b = Foo();", + errors: [ { - code: "var a = new c();", - errors: [ - { - message: "A constructor name should not start with a lowercase letter.", - type: "NewExpression", - line: 1, - column: 13 - } - ] + message: + "A function with a name starting with an uppercase letter should only be used as a constructor.", + type: "CallExpression", }, + ], + }, + { + code: "var b = a.Foo();", + errors: [ + { + message: + "A function with a name starting with an uppercase letter should only be used as a constructor.", + type: "CallExpression", + }, + ], + }, + { + code: "var b = a['Foo']();", + errors: [ + { + message: + "A function with a name starting with an uppercase letter should only be used as a constructor.", + type: "CallExpression", + }, + ], + }, + { + code: "var b = a.Date.UTC();", + errors: [ + { + message: + "A function with a name starting with an uppercase letter should only be used as a constructor.", + type: "CallExpression", + }, + ], + }, + { + code: "var b = UTC();", + errors: [ + { + message: + "A function with a name starting with an uppercase letter should only be used as a constructor.", + type: "CallExpression", + }, + ], + }, + { + code: "var a = B.C();", + errors: [ + { + message: + "A function with a name starting with an uppercase letter should only be used as a constructor.", + type: "CallExpression", + line: 1, + column: 11, + }, + ], + }, + { + code: "var a = B\n.C();", + errors: [ + { + message: + "A function with a name starting with an uppercase letter should only be used as a constructor.", + type: "CallExpression", + line: 2, + column: 2, + }, + ], + }, + { + code: "var a = new B.c();", + errors: [ + { + message: + "A constructor name should not start with a lowercase letter.", + type: "NewExpression", + line: 1, + column: 15, + }, + ], + }, + { + code: "var a = new B.\nc();", + errors: [ + { + message: + "A constructor name should not start with a lowercase letter.", + type: "NewExpression", + line: 2, + column: 1, + }, + ], + }, + { + code: "var a = new c();", + errors: [ + { + message: + "A constructor name should not start with a lowercase letter.", + type: "NewExpression", + line: 1, + column: 13, + }, + ], + }, + { + code: "var x = Foo.Bar(42);", + options: [{ capIsNewExceptions: ["Foo"] }], + errors: [ { - code: "var x = Foo.Bar(42);", - options: [{ capIsNewExceptions: ["Foo"] }], - errors: [{ type: "CallExpression", message: "A function with a name starting with an uppercase letter should only be used as a constructor." }] + type: "CallExpression", + message: + "A function with a name starting with an uppercase letter should only be used as a constructor.", }, - { - code: "var x = Bar.Foo(42);", + ], + }, + { + code: "var x = Bar.Foo(42);", - options: [{ capIsNewExceptionPattern: "^Foo\\.." }], - errors: [{ type: "CallExpression", message: "A function with a name starting with an uppercase letter should only be used as a constructor." }] - }, + options: [{ capIsNewExceptionPattern: "^Foo\\.." }], + errors: [ { - code: "var x = new foo.bar(42);", - options: [{ newIsCapExceptions: ["foo"] }], - errors: [{ type: "NewExpression", message: "A constructor name should not start with a lowercase letter." }] + type: "CallExpression", + message: + "A function with a name starting with an uppercase letter should only be used as a constructor.", }, + ], + }, + { + code: "var x = new foo.bar(42);", + options: [{ newIsCapExceptions: ["foo"] }], + errors: [ { - code: "var x = new bar.foo(42);", + type: "NewExpression", + message: + "A constructor name should not start with a lowercase letter.", + }, + ], + }, + { + code: "var x = new bar.foo(42);", - options: [{ newIsCapExceptionPattern: "^foo\\.." }], - errors: [{ type: "NewExpression", message: "A constructor name should not start with a lowercase letter." }] - } - ] + options: [{ newIsCapExceptionPattern: "^foo\\.." }], + errors: [ + { + type: "NewExpression", + message: + "A constructor name should not start with a lowercase letter.", + }, + ], + }, + ], }); diff --git a/eslint/babel-eslint-plugin/test/rules/no-invalid-this.js b/eslint/babel-eslint-plugin/test/rules/no-invalid-this.js index b4c385b466..7264a030f0 100644 --- a/eslint/babel-eslint-plugin/test/rules/no-invalid-this.js +++ b/eslint/babel-eslint-plugin/test/rules/no-invalid-this.js @@ -10,8 +10,8 @@ //------------------------------------------------------------------------------ const cloneDeep = require("lodash.clonedeep"); -const rule = require("../../rules/no-invalid-this"), - RuleTester = require("../helpers/RuleTester"); +const rule = require("../../src/rules/no-invalid-this"), + RuleTester = require("../helpers/RuleTester"); //------------------------------------------------------------------------------ // Helpers @@ -22,7 +22,7 @@ const rule = require("../../rules/no-invalid-this"), * @returns {void} */ function NORMAL(pattern) { - pattern.parserOptions.sourceType = "script"; + pattern.parserOptions.sourceType = "script"; } /** @@ -32,7 +32,7 @@ function NORMAL(pattern) { * @returns {void} */ function USE_STRICT(pattern) { - pattern.code = "\"use strict\"; " + pattern.code; + pattern.code = '"use strict"; ' + pattern.code; } /** @@ -42,9 +42,9 @@ function USE_STRICT(pattern) { * @returns {void} */ function IMPLIED_STRICT(pattern) { - pattern.code = "/* implied strict mode */ " + pattern.code; - pattern.parserOptions.ecmaFeatures = pattern.parserOptions.ecmaFeatures || {}; - pattern.parserOptions.ecmaFeatures.impliedStrict = true; + pattern.code = "/* implied strict mode */ " + pattern.code; + pattern.parserOptions.ecmaFeatures = pattern.parserOptions.ecmaFeatures || {}; + pattern.parserOptions.ecmaFeatures.impliedStrict = true; } /** @@ -54,7 +54,7 @@ function IMPLIED_STRICT(pattern) { * @returns {void} */ function MODULES(pattern) { - pattern.code = "/* modules */ " + pattern.code; + pattern.code = "/* modules */ " + pattern.code; } /** @@ -64,564 +64,625 @@ function MODULES(pattern) { * @returns {Object[]} Test patterns. */ function extractPatterns(patterns, type) { + // Clone and apply the pattern environment. + const patternsList = patterns.map(function(pattern) { + return pattern[type].map(function(applyCondition) { + const thisPattern = cloneDeep(pattern); - // Clone and apply the pattern environment. - const patternsList = patterns.map(function(pattern) { - return pattern[type].map(function(applyCondition) { - const thisPattern = cloneDeep(pattern); + applyCondition(thisPattern); - applyCondition(thisPattern); + if (type === "valid") { + thisPattern.errors = []; + } else { + thisPattern.code += " /* should error */"; + } - if (type === "valid") { - thisPattern.errors = []; - } else { - thisPattern.code += " /* should error */"; - } + delete thisPattern.invalid; + delete thisPattern.valid; - delete thisPattern.invalid; - delete thisPattern.valid; - - return thisPattern; - }); + return thisPattern; }); + }); - // Flatten. - return Array.prototype.concat.apply([], patternsList); + // Flatten. + return Array.prototype.concat.apply([], patternsList); } - //------------------------------------------------------------------------------ // Tests //------------------------------------------------------------------------------ const errors = [ - {message: "Unexpected 'this'.", type: "ThisExpression"}, - {message: "Unexpected 'this'.", type: "ThisExpression"} + { message: "Unexpected 'this'.", type: "ThisExpression" }, + { message: "Unexpected 'this'.", type: "ThisExpression" }, ]; const patterns = [ + // Global. + { + code: "console.log(this); z(x => console.log(x, this));", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: "console.log(this); z(x => console.log(x, this));", + parserOptions: { + ecmaVersion: 6, + ecmaFeatures: { globalReturn: true }, + }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, - // Global. - { - code: "console.log(this); z(x => console.log(x, this));", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "console.log(this); z(x => console.log(x, this));", - parserOptions: { - ecmaVersion: 6, - ecmaFeatures: {globalReturn: true} - }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, + // IIFE. + { + code: + "(function() { console.log(this); z(x => console.log(x, this)); })();", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, - // IIFE. - { - code: "(function() { console.log(this); z(x => console.log(x, this)); })();", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] + // Just functions. + { + code: "function foo() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + 'function foo() { "use strict"; console.log(this); z(x => console.log(x, this)); }', + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [], + invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "return function() { console.log(this); z(x => console.log(x, this)); };", + parserOptions: { + ecmaVersion: 6, + ecmaFeatures: { globalReturn: true }, }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT], // modules cannot return on global. + }, + { + code: + "var foo = (function() { console.log(this); z(x => console.log(x, this)); }).bar(obj);", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, - // Just functions. - { - code: "function foo() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "function foo() { \"use strict\"; console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [], - invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "return function() { console.log(this); z(x => console.log(x, this)); };", - parserOptions: { - ecmaVersion: 6, - ecmaFeatures: {globalReturn: true} - }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT] // modules cannot return on global. - }, - { - code: "var foo = (function() { console.log(this); z(x => console.log(x, this)); }).bar(obj);", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, + // Functions in methods. + { + code: + "var obj = {foo: function() { function foo() { console.log(this); z(x => console.log(x, this)); } foo(); }};", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "var obj = {foo() { function foo() { console.log(this); z(x => console.log(x, this)); } foo(); }};", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "var obj = {foo: function() { return function() { console.log(this); z(x => console.log(x, this)); }; }};", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + 'var obj = {foo: function() { "use strict"; return function() { console.log(this); z(x => console.log(x, this)); }; }};', + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [], + invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "obj.foo = function() { return function() { console.log(this); z(x => console.log(x, this)); }; };", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + 'obj.foo = function() { "use strict"; return function() { console.log(this); z(x => console.log(x, this)); }; };', + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [], + invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "class A { foo() { return function() { console.log(this); z(x => console.log(x, this)); }; } }", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [], + invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + }, - // Functions in methods. - { - code: "var obj = {foo: function() { function foo() { console.log(this); z(x => console.log(x, this)); } foo(); }};", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "var obj = {foo() { function foo() { console.log(this); z(x => console.log(x, this)); } foo(); }};", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "var obj = {foo: function() { return function() { console.log(this); z(x => console.log(x, this)); }; }};", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "var obj = {foo: function() { \"use strict\"; return function() { console.log(this); z(x => console.log(x, this)); }; }};", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [], - invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "obj.foo = function() { return function() { console.log(this); z(x => console.log(x, this)); }; };", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "obj.foo = function() { \"use strict\"; return function() { console.log(this); z(x => console.log(x, this)); }; };", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [], - invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "class A { foo() { return function() { console.log(this); z(x => console.log(x, this)); }; } }", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [], - invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES] - }, + // Class Static methods. + { + code: + "class A {static foo() { console.log(this); z(x => console.log(x, this)); }};", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // Class Static methods. - { - code: "class A {static foo() { console.log(this); z(x => console.log(x, this)); }};", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + // Constructors. + { + code: "function Foo() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "var Foo = function Foo() { console.log(this); z(x => console.log(x, this)); };", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "class A {constructor() { console.log(this); z(x => console.log(x, this)); }};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // Constructors. - { - code: "function Foo() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "var Foo = function Foo() { console.log(this); z(x => console.log(x, this)); };", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "class A {constructor() { console.log(this); z(x => console.log(x, this)); }};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + // On a property. + { + code: + "var obj = {foo: function() { console.log(this); z(x => console.log(x, this)); }};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "var obj = {foo() { console.log(this); z(x => console.log(x, this)); }};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "var obj = {foo: foo || function() { console.log(this); z(x => console.log(x, this)); }};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "var obj = {foo: hasNative ? foo : function() { console.log(this); z(x => console.log(x, this)); }};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "var obj = {foo: (function() { return function() { console.log(this); z(x => console.log(x, this)); }; })()};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + 'Object.defineProperty(obj, "foo", {value: function() { console.log(this); z(x => console.log(x, this)); }})', + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "Object.defineProperties(obj, {foo: {value: function() { console.log(this); z(x => console.log(x, this)); }}})", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // On a property. - { - code: "var obj = {foo: function() { console.log(this); z(x => console.log(x, this)); }};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "var obj = {foo() { console.log(this); z(x => console.log(x, this)); }};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "var obj = {foo: foo || function() { console.log(this); z(x => console.log(x, this)); }};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "var obj = {foo: hasNative ? foo : function() { console.log(this); z(x => console.log(x, this)); }};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "var obj = {foo: (function() { return function() { console.log(this); z(x => console.log(x, this)); }; })()};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "Object.defineProperty(obj, \"foo\", {value: function() { console.log(this); z(x => console.log(x, this)); }})", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "Object.defineProperties(obj, {foo: {value: function() { console.log(this); z(x => console.log(x, this)); }}})", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + // Assigns to a property. + { + code: + "obj.foo = function() { console.log(this); z(x => console.log(x, this)); };", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "obj.foo = foo || function() { console.log(this); z(x => console.log(x, this)); };", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "obj.foo = foo ? bar : function() { console.log(this); z(x => console.log(x, this)); };", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "obj.foo = (function() { return function() { console.log(this); z(x => console.log(x, this)); }; })();", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // Assigns to a property. - { - code: "obj.foo = function() { console.log(this); z(x => console.log(x, this)); };", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "obj.foo = foo || function() { console.log(this); z(x => console.log(x, this)); };", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "obj.foo = foo ? bar : function() { console.log(this); z(x => console.log(x, this)); };", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "obj.foo = (function() { return function() { console.log(this); z(x => console.log(x, this)); }; })();", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + // Class Instance Methods. + { + code: + "class A {foo() { console.log(this); z(x => console.log(x, this)); }};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // Class Instance Methods. - { - code: "class A {foo() { console.log(this); z(x => console.log(x, this)); }};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + // Bind/Call/Apply + { + code: + "var foo = function() { console.log(this); z(x => console.log(x, this)); }.bind(obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "var foo = function() { console.log(this); z(x => console.log(x, this)); }.bind(null);", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "(function() { console.log(this); z(x => console.log(x, this)); }).call(obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "(function() { console.log(this); z(x => console.log(x, this)); }).call(undefined);", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "(function() { console.log(this); z(x => console.log(x, this)); }).apply(obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "(function() { console.log(this); z(x => console.log(x, this)); }).apply(void 0);", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "Reflect.apply(function() { console.log(this); z(x => console.log(x, this)); }, obj, []);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // Bind/Call/Apply - { - code: "var foo = function() { console.log(this); z(x => console.log(x, this)); }.bind(obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "var foo = function() { console.log(this); z(x => console.log(x, this)); }.bind(null);", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "(function() { console.log(this); z(x => console.log(x, this)); }).call(obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "(function() { console.log(this); z(x => console.log(x, this)); }).call(undefined);", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "(function() { console.log(this); z(x => console.log(x, this)); }).apply(obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "(function() { console.log(this); z(x => console.log(x, this)); }).apply(void 0);", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "Reflect.apply(function() { console.log(this); z(x => console.log(x, this)); }, obj, []);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + // Array methods. + { + code: + "Array.from([], function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "foo.every(function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "foo.filter(function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "foo.find(function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "foo.findIndex(function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "foo.forEach(function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "foo.map(function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "foo.some(function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "Array.from([], function() { console.log(this); z(x => console.log(x, this)); }, obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "foo.every(function() { console.log(this); z(x => console.log(x, this)); }, obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "foo.filter(function() { console.log(this); z(x => console.log(x, this)); }, obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "foo.find(function() { console.log(this); z(x => console.log(x, this)); }, obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "foo.findIndex(function() { console.log(this); z(x => console.log(x, this)); }, obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "foo.forEach(function() { console.log(this); z(x => console.log(x, this)); }, obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "foo.map(function() { console.log(this); z(x => console.log(x, this)); }, obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "foo.some(function() { console.log(this); z(x => console.log(x, this)); }, obj);", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "foo.forEach(function() { console.log(this); z(x => console.log(x, this)); }, null);", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, - // Array methods. - { - code: "Array.from([], function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "foo.every(function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "foo.filter(function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "foo.find(function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "foo.findIndex(function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "foo.forEach(function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "foo.map(function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "foo.some(function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "Array.from([], function() { console.log(this); z(x => console.log(x, this)); }, obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "foo.every(function() { console.log(this); z(x => console.log(x, this)); }, obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "foo.filter(function() { console.log(this); z(x => console.log(x, this)); }, obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "foo.find(function() { console.log(this); z(x => console.log(x, this)); }, obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "foo.findIndex(function() { console.log(this); z(x => console.log(x, this)); }, obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "foo.forEach(function() { console.log(this); z(x => console.log(x, this)); }, obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "foo.map(function() { console.log(this); z(x => console.log(x, this)); }, obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "foo.some(function() { console.log(this); z(x => console.log(x, this)); }, obj);", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "foo.forEach(function() { console.log(this); z(x => console.log(x, this)); }, null);", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, + // @this tag. + { + code: + "/** @this Obj */ function foo() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "/**\n * @returns {void}\n * @this Obj\n */\nfunction foo() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "/** @returns {void} */ function foo() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "/** @this Obj */ foo(function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "foo(/* @this Obj */ function() { console.log(this); z(x => console.log(x, this)); });", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // @this tag. - { - code: "/** @this Obj */ function foo() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "/**\n * @returns {void}\n * @this Obj\n */\nfunction foo() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "/** @returns {void} */ function foo() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "/** @this Obj */ foo(function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "foo(/* @this Obj */ function() { console.log(this); z(x => console.log(x, this)); });", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + // https://github.com/eslint/eslint/issues/3254 + { + code: "function foo() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, - // https://github.com/eslint/eslint/issues/3254 - { - code: "function foo() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, + // https://github.com/eslint/eslint/issues/3287 + { + code: + "function foo() { /** @this Obj*/ return function bar() { console.log(this); z(x => console.log(x, this)); }; }", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // https://github.com/eslint/eslint/issues/3287 - { - code: "function foo() { /** @this Obj*/ return function bar() { console.log(this); z(x => console.log(x, this)); }; }", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + // https://github.com/eslint/eslint/issues/6824 + { + code: + "var Ctor = function() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "var func = function() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "Ctor = function() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "func = function() { console.log(this); z(x => console.log(x, this)); }", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "function foo(Ctor = function() { console.log(this); z(x => console.log(x, this)); }) {}", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "function foo(func = function() { console.log(this); z(x => console.log(x, this)); }) {}", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, + { + code: + "[obj.method = function() { console.log(this); z(x => console.log(x, this)); }] = a", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, + { + code: + "[func = function() { console.log(this); z(x => console.log(x, this)); }] = a", + parserOptions: { ecmaVersion: 6 }, + errors, + valid: [NORMAL], + invalid: [USE_STRICT, IMPLIED_STRICT, MODULES], + }, - // https://github.com/eslint/eslint/issues/6824 - { - code: "var Ctor = function() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "var func = function() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "Ctor = function() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "func = function() { console.log(this); z(x => console.log(x, this)); }", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "function foo(Ctor = function() { console.log(this); z(x => console.log(x, this)); }) {}", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "function foo(func = function() { console.log(this); z(x => console.log(x, this)); }) {}", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, - { - code: "[obj.method = function() { console.log(this); z(x => console.log(x, this)); }] = a", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - { - code: "[func = function() { console.log(this); z(x => console.log(x, this)); }] = a", - parserOptions: { ecmaVersion: 6 }, - errors, - valid: [NORMAL], - invalid: [USE_STRICT, IMPLIED_STRICT, MODULES] - }, + // babel/no-invalid-this - // babel/no-invalid-this + // Class Instance Properties. + { + code: "class A {a = this.b;};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // Class Instance Properties. - { - code: "class A {a = this.b;};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + { + code: "class A {a = () => {return this.b;};};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - { - code: "class A {a = () => {return this.b;};};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + // Class Private Instance Properties. + { + code: "class A {#a = this.b;};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, - // Class Private Instance Properties. - { - code: "class A {#a = this.b;};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, - - { - code: "class A {#a = () => {return this.b;};};", - parserOptions: { ecmaVersion: 6 }, - valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], - invalid: [] - }, + { + code: "class A {#a = () => {return this.b;};};", + parserOptions: { ecmaVersion: 6 }, + valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES], + invalid: [], + }, ]; const ruleTester = new RuleTester(); ruleTester.run("no-invalid-this", rule, { - valid: extractPatterns(patterns, "valid"), - invalid: extractPatterns(patterns, "invalid") + valid: extractPatterns(patterns, "valid"), + invalid: extractPatterns(patterns, "invalid"), }); diff --git a/eslint/babel-eslint-plugin/test/rules/no-unused-expressions.js b/eslint/babel-eslint-plugin/test/rules/no-unused-expressions.js index b7d4ca6e7b..fc07ad73d5 100644 --- a/eslint/babel-eslint-plugin/test/rules/no-unused-expressions.js +++ b/eslint/babel-eslint-plugin/test/rules/no-unused-expressions.js @@ -9,8 +9,8 @@ // Requirements //------------------------------------------------------------------------------ -const rule = require("../../rules/no-unused-expressions"), - RuleTester = require("../helpers/RuleTester"); +const rule = require("../../src/rules/no-unused-expressions"), + RuleTester = require("../helpers/RuleTester"); //------------------------------------------------------------------------------ // Tests @@ -19,126 +19,372 @@ const rule = require("../../rules/no-unused-expressions"), const ruleTester = new RuleTester(); ruleTester.run("no-unused-expressions", rule, { - valid: [ - // Original test cases. - "function f(){}", - "a = b", - "new a", - "{}", - "f(); g()", - "i++", - "a()", - { code: "a && a()", options: [{ allowShortCircuit: true }] }, - { code: "a() || (b = c)", options: [{ allowShortCircuit: true }] }, - { code: "a ? b() : c()", options: [{ allowTernary: true }] }, - { code: "a ? b() || (c = d) : e()", options: [{ allowShortCircuit: true, allowTernary: true }] }, - "delete foo.bar", - "void new C", - "\"use strict\";", - "\"directive one\"; \"directive two\"; f();", - "function foo() {\"use strict\"; return true; }", - { code: "var foo = () => {\"use strict\"; return true; }", parserOptions: { ecmaVersion: 6 } }, - "function foo() {\"directive one\"; \"directive two\"; f(); }", - "function foo() { var foo = \"use strict\"; return true; }", - { - code: "function* foo(){ yield 0; }", - parserOptions: { ecmaVersion: 6 } - }, - { - code: "async function foo() { await 5; }", - parserOptions: { ecmaVersion: 8 } - }, - { - code: "async function foo() { await foo.bar; }", - parserOptions: { ecmaVersion: 8 } - }, - { - code: "async function foo() { bar && await baz; }", - options: [{ allowShortCircuit: true }], - parserOptions: { ecmaVersion: 8 } - }, - { - code: "async function foo() { foo ? await bar : await baz; }", - options: [{ allowTernary: true }], - parserOptions: { ecmaVersion: 8 } - }, - { - code: "tag`tagged template literal`", - options: [{ allowTaggedTemplates: true }], - parserOptions: { ecmaVersion: 6 } - }, - { - code: "shouldNotBeAffectedByAllowTemplateTagsOption()", - options: [{ allowTaggedTemplates: true }], - parserOptions: { ecmaVersion: 6 } - }, + valid: [ + // Original test cases. + "function f(){}", + "a = b", + "new a", + "{}", + "f(); g()", + "i++", + "a()", + { code: "a && a()", options: [{ allowShortCircuit: true }] }, + { code: "a() || (b = c)", options: [{ allowShortCircuit: true }] }, + { code: "a ? b() : c()", options: [{ allowTernary: true }] }, + { + code: "a ? b() || (c = d) : e()", + options: [{ allowShortCircuit: true, allowTernary: true }], + }, + "delete foo.bar", + "void new C", + '"use strict";', + '"directive one"; "directive two"; f();', + 'function foo() {"use strict"; return true; }', + { + code: 'var foo = () => {"use strict"; return true; }', + parserOptions: { ecmaVersion: 6 }, + }, + 'function foo() {"directive one"; "directive two"; f(); }', + 'function foo() { var foo = "use strict"; return true; }', + { + code: "function* foo(){ yield 0; }", + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "async function foo() { await 5; }", + parserOptions: { ecmaVersion: 8 }, + }, + { + code: "async function foo() { await foo.bar; }", + parserOptions: { ecmaVersion: 8 }, + }, + { + code: "async function foo() { bar && await baz; }", + options: [{ allowShortCircuit: true }], + parserOptions: { ecmaVersion: 8 }, + }, + { + code: "async function foo() { foo ? await bar : await baz; }", + options: [{ allowTernary: true }], + parserOptions: { ecmaVersion: 8 }, + }, + { + code: "tag`tagged template literal`", + options: [{ allowTaggedTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "shouldNotBeAffectedByAllowTemplateTagsOption()", + options: [{ allowTaggedTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + }, - // Babel-specific test cases. - "let a = do { if (foo) { foo.bar; } }", - "let a = do { foo; }", - "let a = do { let b = 2; foo; }", - "let a = do { (foo + 1); }", - "let a = do { if (foo) { if (foo.bar) { foo.bar; } } }", - "let a = do { if (foo) { if (foo.bar) { foo.bar; } else if (foo.baz) { foo.baz; } } }", - "foo.bar?.();", + // Babel-specific test cases. + "let a = do { if (foo) { foo.bar; } }", + "let a = do { foo; }", + "let a = do { let b = 2; foo; }", + "let a = do { (foo + 1); }", + "let a = do { if (foo) { if (foo.bar) { foo.bar; } } }", + "let a = do { if (foo) { if (foo.bar) { foo.bar; } else if (foo.baz) { foo.baz; } } }", + "foo.bar?.();", + ], + invalid: [ + { + code: "0", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "f(), 0", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "{0}", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "[]", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a && b();", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a() || false", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a || (b = c)", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a ? b() || (c = d) : e", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "`untagged template literal`", + parserOptions: { ecmaVersion: 6 }, + errors: [ + "Expected an assignment or function call and instead saw an expression.", + ], + }, + { + code: "tag`tagged template literal`", + parserOptions: { ecmaVersion: 6 }, + errors: [ + "Expected an assignment or function call and instead saw an expression.", + ], + }, + { + code: "a && b()", + options: [{ allowTernary: true }], + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a ? b() : c()", + options: [{ allowShortCircuit: true }], + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a || b", + options: [{ allowShortCircuit: true }], + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a() && b", + options: [{ allowShortCircuit: true }], + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a ? b : 0", + options: [{ allowTernary: true }], + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "a ? b : c()", + options: [{ allowTernary: true }], + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "foo.bar;", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "!a", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "+a", + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: '"directive one"; f(); "directive two";', + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: 'function foo() {"directive one"; f(); "directive two"; }', + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: 'if (0) { "not a directive"; f(); }', + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: 'function foo() { var foo = true; "use strict"; }', + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: 'var foo = () => { var foo = true; "use strict"; }', + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", + }, + ], + }, + { + code: "`untagged template literal`", + options: [{ allowTaggedTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + "Expected an assignment or function call and instead saw an expression.", + ], + }, + { + code: "`untagged template literal`", + options: [{ allowTaggedTemplates: false }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + "Expected an assignment or function call and instead saw an expression.", + ], + }, + { + code: "tag`tagged template literal`", + options: [{ allowTaggedTemplates: false }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + "Expected an assignment or function call and instead saw an expression.", + ], + }, - ], - invalid: [ - { code: "0", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "f(), 0", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "{0}", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "[]", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a && b();", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a() || false", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a || (b = c)", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a ? b() || (c = d) : e", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, + // Babel-specific test cases. + { + code: "let a = do { foo; let b = 2; }", + errors: [ { - code: "`untagged template literal`", - parserOptions: { ecmaVersion: 6 }, - errors: ["Expected an assignment or function call and instead saw an expression."] + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", }, + ], + }, + { + code: "let a = do { if (foo) { foo.bar } else { a; bar.foo } }", + errors: [ { - code: "tag`tagged template literal`", - parserOptions: { ecmaVersion: 6 }, - errors: ["Expected an assignment or function call and instead saw an expression."] + message: + "Expected an assignment or function call and instead saw an expression.", + type: "ExpressionStatement", }, - { code: "a && b()", options: [{ allowTernary: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a ? b() : c()", options: [{ allowShortCircuit: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a || b", options: [{ allowShortCircuit: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a() && b", options: [{ allowShortCircuit: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a ? b : 0", options: [{ allowTernary: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "a ? b : c()", options: [{ allowTernary: true }], errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "foo.bar;", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "!a", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "+a", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "\"directive one\"; f(); \"directive two\";", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "function foo() {\"directive one\"; f(); \"directive two\"; }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "if (0) { \"not a directive\"; f(); }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "function foo() { var foo = true; \"use strict\"; }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "var foo = () => { var foo = true; \"use strict\"; }", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { - code: "`untagged template literal`", - options: [{ allowTaggedTemplates: true }], - parserOptions: { ecmaVersion: 6 }, - errors: ["Expected an assignment or function call and instead saw an expression."] - }, - { - code: "`untagged template literal`", - options: [{ allowTaggedTemplates: false }], - parserOptions: { ecmaVersion: 6 }, - errors: ["Expected an assignment or function call and instead saw an expression."] - }, - { - code: "tag`tagged template literal`", - options: [{ allowTaggedTemplates: false }], - parserOptions: { ecmaVersion: 6 }, - errors: ["Expected an assignment or function call and instead saw an expression."] - }, - - // Babel-specific test cases. - { code: "let a = do { foo; let b = 2; }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - { code: "let a = do { if (foo) { foo.bar } else { a; bar.foo } }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, - - ] + ], + }, + ], }); diff --git a/eslint/babel-eslint-plugin/test/rules/object-curly-spacing.js b/eslint/babel-eslint-plugin/test/rules/object-curly-spacing.js index cf5315102d..ff51d248ba 100644 --- a/eslint/babel-eslint-plugin/test/rules/object-curly-spacing.js +++ b/eslint/babel-eslint-plugin/test/rules/object-curly-spacing.js @@ -6,836 +6,1133 @@ * @copyright 2015 Mathieu M-Gosselin. All rights reserved. */ -var rule = require('../../rules/object-curly-spacing'), - RuleTester = require("../helpers/RuleTester"); +var rule = require("../../src/rules/object-curly-spacing"), + RuleTester = require("../helpers/RuleTester"); var ruleTester = new RuleTester(); -ruleTester.run('babel/object-curly-spacing', rule, { +ruleTester.run("babel/object-curly-spacing", rule, { + valid: [ + // always - object literals + { code: "var obj = { foo: bar, baz: qux };", options: ["always"] }, + { + code: "var obj = { foo: { bar: quxx }, baz: qux };", + options: ["always"], + }, + { code: "var obj = {\nfoo: bar,\nbaz: qux\n};", options: ["always"] }, - valid: [ + // always - destructuring + { + code: "var { x } = y", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var { x, y } = y", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var { x,y } = y", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {\nx,y } = y", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {\nx,y\n} = z", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var { x = 10, y } = y", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var { x: { z }, y } = y", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {\ny,\n} = x", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var { y, } = x", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var { y: x } = x", + options: ["always"], + ecmaFeatures: { destructuring: true }, + }, - // always - object literals - { code: "var obj = { foo: bar, baz: qux };", options: ["always"] }, - { code: "var obj = { foo: { bar: quxx }, baz: qux };", options: ["always"] }, - { code: "var obj = {\nfoo: bar,\nbaz: qux\n};", options: ["always"] }, + // always - import / export + { + code: "import door from 'room'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import * as door from 'room'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import { door } from 'room'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {\ndoor } from 'room'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "export { door } from 'room'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import { house, mouse } from 'caravan'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {\nhouse,\nmouse\n} from 'caravan'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {\nhouse,\nmouse,\n} from 'caravan'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import house, { mouse } from 'caravan'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import door, { house, mouse } from 'caravan'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "export { door }", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {\ndoor,\nhouse\n}", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {\ndoor,\nhouse,\n}", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import 'room'", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import { bar as x } from 'foo';", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import { x, } from 'foo';", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {\nx,\n} from 'foo';", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "export { x, } from 'foo';", + options: ["always"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {\nx,\n} from 'foo';", + options: ["always"], + ecmaFeatures: { modules: true }, + }, - // always - destructuring - { code: "var { x } = y", options: ["always"], ecmaFeatures: { destructuring: true } }, - { code: "var { x, y } = y", options: ["always"], ecmaFeatures: { destructuring: true } }, - { code: "var { x,y } = y", options: ["always"], ecmaFeatures: { destructuring: true } }, - { code: "var {\nx,y } = y", options: ["always"], ecmaFeatures: { destructuring: true } }, - { code: "var {\nx,y\n} = z", options: ["always"], ecmaFeatures: { destructuring: true } }, - { code: "var { x = 10, y } = y", options: ["always"], ecmaFeatures: { destructuring: true } }, - { code: "var { x: { z }, y } = y", options: ["always"], ecmaFeatures: { destructuring: true } }, - { code: "var {\ny,\n} = x", options: ["always"], ecmaFeatures: { destructuring: true } }, - { code: "var { y, } = x", options: ["always"], ecmaFeatures: { destructuring: true } }, - { code: "var { y: x } = x", options: ["always"], ecmaFeatures: { destructuring: true } }, + // always - empty object + { code: "var foo = {};", options: ["always"] }, - // always - import / export - { code: "import door from 'room'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import * as door from 'room'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import { door } from 'room'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import {\ndoor } from 'room'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "export { door } from 'room'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import { house, mouse } from 'caravan'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import {\nhouse,\nmouse\n} from 'caravan'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import {\nhouse,\nmouse,\n} from 'caravan'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import house, { mouse } from 'caravan'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import door, { house, mouse } from 'caravan'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "export { door }", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "export {\ndoor,\nhouse\n}", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "export {\ndoor,\nhouse,\n}", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import 'room'", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import { bar as x } from 'foo';", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import { x, } from 'foo';", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "import {\nx,\n} from 'foo';", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "export { x, } from 'foo';", options: ["always"], ecmaFeatures: { modules: true } }, - { code: "export {\nx,\n} from 'foo';", options: ["always"], ecmaFeatures: { modules: true } }, + // always - objectsInObjects + { + code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 }};", + options: ["always", { objectsInObjects: false }], + }, - // always - empty object - { code: "var foo = {};", options: ["always"] }, + // always - arraysInObjects + { + code: "var obj = { 'foo': [ 1, 2 ]};", + options: ["always", { arraysInObjects: false }], + }, - // always - objectsInObjects - { code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 }};", options: ["always", {"objectsInObjects": false}] }, + // always - arraysInObjects, objectsInObjects + { + code: "var obj = { 'qux': [ 1, 2 ], 'foo': { 'bar': 1, 'baz': 2 }};", + options: ["always", { arraysInObjects: false, objectsInObjects: false }], + }, - // always - arraysInObjects - { code: "var obj = { 'foo': [ 1, 2 ]};", options: ["always", {"arraysInObjects": false}] }, + // always - arraysInObjects, objectsInObjects (reverse) + { + code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 }, 'qux': [ 1, 2 ]};", + options: ["always", { arraysInObjects: false, objectsInObjects: false }], + }, - // always - arraysInObjects, objectsInObjects - { code: "var obj = { 'qux': [ 1, 2 ], 'foo': { 'bar': 1, 'baz': 2 }};", options: ["always", {"arraysInObjects": false, "objectsInObjects": false}] }, + // never + { code: "var obj = {foo: bar,\nbaz: qux\n};", options: ["never"] }, + { code: "var obj = {\nfoo: bar,\nbaz: qux};", options: ["never"] }, - // always - arraysInObjects, objectsInObjects (reverse) - { code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 }, 'qux': [ 1, 2 ]};", options: ["always", {"arraysInObjects": false, "objectsInObjects": false}] }, + // never - object literals + { code: "var obj = {foo: bar, baz: qux};", options: ["never"] }, + { code: "var obj = {foo: {bar: quxx}, baz: qux};", options: ["never"] }, + { code: "var obj = {foo: {\nbar: quxx}, baz: qux\n};", options: ["never"] }, + { code: "var obj = {foo: {\nbar: quxx\n}, baz: qux};", options: ["never"] }, + { code: "var obj = {\nfoo: bar,\nbaz: qux\n};", options: ["never"] }, - // never - { code: "var obj = {foo: bar,\nbaz: qux\n};", options: ["never"] }, - { code: "var obj = {\nfoo: bar,\nbaz: qux};", options: ["never"] }, + // never - destructuring + { + code: "var {x} = y", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {x, y} = y", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {x,y} = y", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {\nx,y\n} = y", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {x = 10} = y", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {x = 10, y} = y", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {x: {z}, y} = y", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {\nx: {z\n}, y} = y", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {\ny,\n} = x", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {y,} = x", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {y:x} = x", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, - // never - object literals - { code: "var obj = {foo: bar, baz: qux};", options: ["never"] }, - { code: "var obj = {foo: {bar: quxx}, baz: qux};", options: ["never"] }, - { code: "var obj = {foo: {\nbar: quxx}, baz: qux\n};", options: ["never"] }, - { code: "var obj = {foo: {\nbar: quxx\n}, baz: qux};", options: ["never"] }, - { code: "var obj = {\nfoo: bar,\nbaz: qux\n};", options: ["never"] }, + // never - import / export + { + code: "import door from 'room'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import * as door from 'room'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {door} from 'room'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {door} from 'room'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {\ndoor} from 'room'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {\ndoor\n} from 'room'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {house,mouse} from 'caravan'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {house, mouse} from 'caravan'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {\nhouse,\nmouse} from 'caravan'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {\nhouse,\nmouse,\n} from 'caravan'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {door}", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {\ndoor,\nmouse\n}", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {\ndoor,\nmouse,\n}", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import 'room'", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import x, {bar} from 'foo';", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import x, {bar, baz} from 'foo';", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {bar as y} from 'foo';", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {x,} from 'foo';", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "import {\nx,\n} from 'foo';", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {x,} from 'foo';", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {\nx,\n} from 'foo';", + options: ["never"], + ecmaFeatures: { modules: true }, + }, - // never - destructuring - { code: "var {x} = y", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {x, y} = y", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {x,y} = y", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {\nx,y\n} = y", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {x = 10} = y", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {x = 10, y} = y", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {x: {z}, y} = y", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {\nx: {z\n}, y} = y", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {\ny,\n} = x", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {y,} = x", options: ["never"], ecmaFeatures: { destructuring: true } }, - { code: "var {y:x} = x", options: ["never"], ecmaFeatures: { destructuring: true } }, + // never - empty object + { code: "var foo = {};", options: ["never"] }, - // never - import / export - { code: "import door from 'room'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import * as door from 'room'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import {door} from 'room'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "export {door} from 'room'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import {\ndoor} from 'room'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "export {\ndoor\n} from 'room'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import {house,mouse} from 'caravan'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import {house, mouse} from 'caravan'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import {\nhouse,\nmouse} from 'caravan'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import {\nhouse,\nmouse,\n} from 'caravan'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "export {door}", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "export {\ndoor,\nmouse\n}", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "export {\ndoor,\nmouse,\n}", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import 'room'", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import x, {bar} from 'foo';", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import x, {bar, baz} from 'foo';", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import {bar as y} from 'foo';", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import {x,} from 'foo';", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "import {\nx,\n} from 'foo';", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "export {x,} from 'foo';", options: ["never"], ecmaFeatures: { modules: true } }, - { code: "export {\nx,\n} from 'foo';", options: ["never"], ecmaFeatures: { modules: true } }, + // never - objectsInObjects + { + code: "var obj = {'foo': {'bar': 1, 'baz': 2} };", + options: ["never", { objectsInObjects: true }], + }, + // https://github.com/eslint/eslint/issues/3658 + // Empty cases. + { code: "var {} = foo;", ecmaFeatures: { destructuring: true } }, + { code: "var [] = foo;", ecmaFeatures: { destructuring: true } }, + { code: "var {a: {}} = foo;", ecmaFeatures: { destructuring: true } }, + { code: "var {a: []} = foo;", ecmaFeatures: { destructuring: true } }, + { code: "import {} from 'foo';", ecmaFeatures: { modules: true } }, + { code: "export {} from 'foo';", ecmaFeatures: { modules: true } }, + { code: "export {};", ecmaFeatures: { modules: true } }, + { + code: "var {} = foo;", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var [] = foo;", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {a: {}} = foo;", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "var {a: []} = foo;", + options: ["never"], + ecmaFeatures: { destructuring: true }, + }, + { + code: "import {} from 'foo';", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { + code: "export {} from 'foo';", + options: ["never"], + ecmaFeatures: { modules: true }, + }, + { code: "export {};", options: ["never"], ecmaFeatures: { modules: true } }, - // never - empty object - { code: "var foo = {};", options: ["never"] }, + // Babel test cases. + { + code: 'export * as x from "mod";', + parser: "babel-eslint", + ecmaFeatures: { modules: true }, + }, + { + code: 'export x from "mod";', + parser: "babel-eslint", + ecmaFeatures: { modules: true }, + }, - // never - objectsInObjects - { code: "var obj = {'foo': {'bar': 1, 'baz': 2} };", options: ["never", {"objectsInObjects": true}]}, + // always - destructuring typed object param + { + code: "function fn({ a,b }:Object){}", + options: ["always"], + parser: "babel-eslint", + ecmaFeatures: { destructuring: true }, + }, - // https://github.com/eslint/eslint/issues/3658 - // Empty cases. - { code: "var {} = foo;", ecmaFeatures: { destructuring: true }}, - { code: "var [] = foo;", ecmaFeatures: { destructuring: true }}, - { code: "var {a: {}} = foo;", ecmaFeatures: { destructuring: true }}, - { code: "var {a: []} = foo;", ecmaFeatures: { destructuring: true }}, - { code: "import {} from 'foo';", ecmaFeatures: { modules: true }}, - { code: "export {} from 'foo';", ecmaFeatures: { modules: true }}, - { code: "export {};", ecmaFeatures: { modules: true }}, - { code: "var {} = foo;", options: ["never"], ecmaFeatures: { destructuring: true }}, - { code: "var [] = foo;", options: ["never"], ecmaFeatures: { destructuring: true }}, - { code: "var {a: {}} = foo;", options: ["never"], ecmaFeatures: { destructuring: true }}, - { code: "var {a: []} = foo;", options: ["never"], ecmaFeatures: { destructuring: true }}, - { code: "import {} from 'foo';", options: ["never"], ecmaFeatures: { modules: true }}, - { code: "export {} from 'foo';", options: ["never"], ecmaFeatures: { modules: true }}, - { code: "export {};", options: ["never"], ecmaFeatures: { modules: true }}, + // never - destructuring typed object param + { + code: "function fn({a,b}: Object){}", + options: ["never"], + parser: "babel-eslint", + ecmaFeatures: { destructuring: true }, + }, + ], - // Babel test cases. - { code: "export * as x from \"mod\";", parser: "babel-eslint", ecmaFeatures: { modules: true } }, - { code: "export x from \"mod\";", parser: "babel-eslint", ecmaFeatures: { modules: true } }, + invalid: [ + { + code: "import {bar} from 'foo.js';", + output: "import { bar } from 'foo.js';", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required after '{'.", + type: "ImportDeclaration", + line: 1, + column: 8, + }, + { + message: "A space is required before '}'.", + type: "ImportDeclaration", + line: 1, + column: 12, + }, + ], + }, + { + code: "import { bar as y} from 'foo.js';", + output: "import { bar as y } from 'foo.js';", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required before '}'.", + type: "ImportDeclaration", + line: 1, + column: 18, + }, + ], + }, + { + code: "import {bar as y} from 'foo.js';", + output: "import { bar as y } from 'foo.js';", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required after '{'.", + type: "ImportDeclaration", + line: 1, + column: 8, + }, + { + message: "A space is required before '}'.", + type: "ImportDeclaration", + line: 1, + column: 17, + }, + ], + }, + { + code: "import { bar} from 'foo.js';", + output: "import { bar } from 'foo.js';", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required before '}'.", + type: "ImportDeclaration", + line: 1, + column: 13, + }, + ], + }, + { + code: "import x, { bar} from 'foo';", + output: "import x, { bar } from 'foo';", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required before '}'.", + type: "ImportDeclaration", + line: 1, + column: 16, + }, + ], + }, + { + code: "import x, { bar, baz} from 'foo';", + output: "import x, { bar, baz } from 'foo';", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required before '}'.", + type: "ImportDeclaration", + line: 1, + column: 21, + }, + ], + }, + { + code: "import x, {bar} from 'foo';", + output: "import x, { bar } from 'foo';", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required after '{'.", + type: "ImportDeclaration", + line: 1, + column: 11, + }, + { + message: "A space is required before '}'.", + type: "ImportDeclaration", + line: 1, + column: 15, + }, + ], + }, + { + code: "import x, {bar, baz} from 'foo';", + output: "import x, { bar, baz } from 'foo';", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required after '{'.", + type: "ImportDeclaration", + line: 1, + column: 11, + }, + { + message: "A space is required before '}'.", + type: "ImportDeclaration", + line: 1, + column: 20, + }, + ], + }, + { + code: "import {bar,} from 'foo';", + output: "import { bar, } from 'foo';", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required after '{'.", + type: "ImportDeclaration", + line: 1, + column: 8, + }, + { + message: "A space is required before '}'.", + type: "ImportDeclaration", + line: 1, + column: 13, + }, + ], + }, + { + code: "import { bar, } from 'foo';", + output: "import {bar,} from 'foo';", + options: ["never"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "There should be no space after '{'.", + type: "ImportDeclaration", + line: 1, + column: 8, + }, + { + message: "There should be no space before '}'.", + type: "ImportDeclaration", + line: 1, + column: 15, + }, + ], + }, + { + code: "export {bar};", + output: "export { bar };", + options: ["always"], + ecmaFeatures: { + modules: true, + }, + errors: [ + { + message: "A space is required after '{'.", + type: "ExportNamedDeclaration", + line: 1, + column: 8, + }, + { + message: "A space is required before '}'.", + type: "ExportNamedDeclaration", + line: 1, + column: 12, + }, + ], + }, - // always - destructuring typed object param - { code: "function fn({ a,b }:Object){}", options: ["always"], parser: "babel-eslint", ecmaFeatures: { destructuring: true } }, + // always - arraysInObjects + { + code: "var obj = { 'foo': [ 1, 2 ] };", + output: "var obj = { 'foo': [ 1, 2 ]};", + options: ["always", { arraysInObjects: false }], + errors: [ + { + message: "There should be no space before '}'.", + type: "ObjectExpression", + }, + ], + }, + { + code: "var obj = { 'foo': [ 1, 2 ] , 'bar': [ 'baz', 'qux' ] };", + output: "var obj = { 'foo': [ 1, 2 ] , 'bar': [ 'baz', 'qux' ]};", + options: ["always", { arraysInObjects: false }], + errors: [ + { + message: "There should be no space before '}'.", + type: "ObjectExpression", + }, + ], + }, - // never - destructuring typed object param - { code: "function fn({a,b}: Object){}", options: ["never"], parser: "babel-eslint", ecmaFeatures: { destructuring: true } }, - ], + // always-objectsInObjects + { + code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 } };", + output: "var obj = { 'foo': { 'bar': 1, 'baz': 2 }};", + options: ["always", { objectsInObjects: false }], + errors: [ + { + message: "There should be no space before '}'.", + type: "ObjectExpression", + line: 1, + column: 43, + }, + ], + }, + { + code: "var obj = { 'foo': [ 1, 2 ] , 'bar': { 'baz': 1, 'qux': 2 } };", + output: "var obj = { 'foo': [ 1, 2 ] , 'bar': { 'baz': 1, 'qux': 2 }};", + options: ["always", { objectsInObjects: false }], + errors: [ + { + message: "There should be no space before '}'.", + type: "ObjectExpression", + line: 1, + column: 61, + }, + ], + }, - invalid: [ + // always-destructuring trailing comma + { + code: "var { a,} = x;", + output: "var { a, } = x;", + options: ["always"], + ecmaFeatures: { destructuring: true }, + errors: [ { - code: "import {bar} from 'foo.js';", - output: "import { bar } from 'foo.js';", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required after '{'.", - type: "ImportDeclaration", - line: 1, - column: 8 - }, - { - message: "A space is required before '}'.", - type: "ImportDeclaration", - line: 1, - column: 12 - } - ] + message: "A space is required before '}'.", + type: "ObjectPattern", + line: 1, + column: 9, + }, + ], + }, + { + code: "var {a, } = x;", + output: "var {a,} = x;", + options: ["never"], + ecmaFeatures: { destructuring: true }, + errors: [ + { + message: "There should be no space before '}'.", + type: "ObjectPattern", + line: 1, + column: 9, + }, + ], + }, + { + code: "var {a:b } = x;", + output: "var {a:b} = x;", + options: ["never"], + ecmaFeatures: { destructuring: true }, + errors: [ + { + message: "There should be no space before '}'.", + type: "ObjectPattern", + line: 1, + column: 10, + }, + ], + }, + { + code: "var { a:b } = x;", + output: "var {a:b} = x;", + options: ["never"], + ecmaFeatures: { destructuring: true }, + errors: [ + { + message: "There should be no space after '{'.", + type: "ObjectPattern", + line: 1, + column: 5, }, { - code: "import { bar as y} from 'foo.js';", - output: "import { bar as y } from 'foo.js';", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required before '}'.", - type: "ImportDeclaration", - line: 1, - column: 18 - } - ] + message: "There should be no space before '}'.", + type: "ObjectPattern", + line: 1, + column: 11, }, - { - code: "import {bar as y} from 'foo.js';", - output: "import { bar as y } from 'foo.js';", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required after '{'.", - type: "ImportDeclaration", - line: 1, - column: 8 - }, - { - message: "A space is required before '}'.", - type: "ImportDeclaration", - line: 1, - column: 17 - } - ] - }, - { - code: "import { bar} from 'foo.js';", - output: "import { bar } from 'foo.js';", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required before '}'.", - type: "ImportDeclaration", - line: 1, - column: 13 - } - ] - }, - { - code: "import x, { bar} from 'foo';", - output: "import x, { bar } from 'foo';", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required before '}'.", - type: "ImportDeclaration", - line: 1, - column: 16 - } + ], + }, - ] - }, + // never-objectsInObjects + { + code: "var obj = {'foo': {'bar': 1, 'baz': 2}};", + output: "var obj = {'foo': {'bar': 1, 'baz': 2} };", + options: ["never", { objectsInObjects: true }], + errors: [ { - code: "import x, { bar, baz} from 'foo';", - output: "import x, { bar, baz } from 'foo';", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required before '}'.", - type: "ImportDeclaration", - line: 1, - column: 21 - } + message: "A space is required before '}'.", + type: "ObjectExpression", + line: 1, + column: 39, + }, + ], + }, + { + code: "var obj = {'foo': [1, 2] , 'bar': {'baz': 1, 'qux': 2}};", + output: "var obj = {'foo': [1, 2] , 'bar': {'baz': 1, 'qux': 2} };", + options: ["never", { objectsInObjects: true }], + errors: [ + { + message: "A space is required before '}'.", + type: "ObjectExpression", + line: 1, + column: 55, + }, + ], + }, - ] + // always & never + { + code: "var obj = {foo: bar, baz: qux};", + output: "var obj = { foo: bar, baz: qux };", + options: ["always"], + errors: [ + { + message: "A space is required after '{'.", + type: "ObjectExpression", + line: 1, + column: 11, }, { - code: "import x, {bar} from 'foo';", - output: "import x, { bar } from 'foo';", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required after '{'.", - type: "ImportDeclaration", - line: 1, - column: 11 - }, - { - message: "A space is required before '}'.", - type: "ImportDeclaration", - line: 1, - column: 15 - } + message: "A space is required before '}'.", + type: "ObjectExpression", + line: 1, + column: 30, + }, + ], + }, + { + code: "var obj = {foo: bar, baz: qux };", + output: "var obj = { foo: bar, baz: qux };", + options: ["always"], + errors: [ + { + message: "A space is required after '{'.", + type: "ObjectExpression", + line: 1, + column: 11, + }, + ], + }, + { + code: "var obj = { foo: bar, baz: qux};", + output: "var obj = { foo: bar, baz: qux };", + options: ["always"], + errors: [ + { + message: "A space is required before '}'.", + type: "ObjectExpression", + line: 1, + column: 31, + }, + ], + }, + { + code: "var obj = { foo: bar, baz: qux };", + output: "var obj = {foo: bar, baz: qux};", + options: ["never"], + errors: [ + { + message: "There should be no space after '{'.", + type: "ObjectExpression", + line: 1, + column: 11, + }, + { + message: "There should be no space before '}'.", + type: "ObjectExpression", + line: 1, + column: 32, + }, + ], + }, + { + code: "var obj = {foo: bar, baz: qux };", + output: "var obj = {foo: bar, baz: qux};", + options: ["never"], + errors: [ + { + message: "There should be no space before '}'.", + type: "ObjectExpression", + line: 1, + column: 31, + }, + ], + }, + { + code: "var obj = { foo: bar, baz: qux};", + output: "var obj = {foo: bar, baz: qux};", + options: ["never"], + errors: [ + { + message: "There should be no space after '{'.", + type: "ObjectExpression", + line: 1, + column: 11, + }, + ], + }, + { + code: "var obj = { foo: { bar: quxx}, baz: qux};", + output: "var obj = {foo: {bar: quxx}, baz: qux};", + options: ["never"], + errors: [ + { + message: "There should be no space after '{'.", + type: "ObjectExpression", + line: 1, + column: 11, + }, + { + message: "There should be no space after '{'.", + type: "ObjectExpression", + line: 1, + column: 18, + }, + ], + }, + { + code: "var obj = {foo: {bar: quxx }, baz: qux };", + output: "var obj = {foo: {bar: quxx}, baz: qux};", + options: ["never"], + errors: [ + { + message: "There should be no space before '}'.", + type: "ObjectExpression", + line: 1, + column: 28, + }, + { + message: "There should be no space before '}'.", + type: "ObjectExpression", + line: 1, + column: 40, + }, + ], + }, + { + code: "export const thing = {value: 1 };", + output: "export const thing = { value: 1 };", + ecmaFeatures: { + modules: true, + blockBindings: true, + }, + options: ["always"], + errors: [ + { + message: "A space is required after '{'.", + type: "ObjectExpression", + line: 1, + column: 22, + }, + ], + }, - ] + // destructuring + { + code: "var {x, y} = y", + output: "var { x, y } = y", + ecmaFeatures: { destructuring: true }, + options: ["always"], + errors: [ + { + message: "A space is required after '{'.", + type: "ObjectPattern", + line: 1, + column: 5, }, { - code: "import x, {bar, baz} from 'foo';", - output: "import x, { bar, baz } from 'foo';", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required after '{'.", - type: "ImportDeclaration", - line: 1, - column: 11 - }, - { - message: "A space is required before '}'.", - type: "ImportDeclaration", - line: 1, - column: 20 - } - ] + message: "A space is required before '}'.", + type: "ObjectPattern", + line: 1, + column: 10, + }, + ], + }, + { + code: "var { x, y} = y", + output: "var { x, y } = y", + ecmaFeatures: { destructuring: true }, + options: ["always"], + errors: [ + { + message: "A space is required before '}'.", + type: "ObjectPattern", + line: 1, + column: 11, + }, + ], + }, + { + code: "var { x, y } = y", + output: "var {x, y} = y", + ecmaFeatures: { destructuring: true }, + options: ["never"], + errors: [ + { + message: "There should be no space after '{'.", + type: "ObjectPattern", + line: 1, + column: 5, }, { - code: "import {bar,} from 'foo';", - output: "import { bar, } from 'foo';", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required after '{'.", - type: "ImportDeclaration", - line: 1, - column: 8 - }, - { - message: "A space is required before '}'.", - type: "ImportDeclaration", - line: 1, - column: 13 - } + message: "There should be no space before '}'.", + type: "ObjectPattern", + line: 1, + column: 12, + }, + ], + }, + { + code: "var {x, y } = y", + output: "var {x, y} = y", + ecmaFeatures: { destructuring: true }, + options: ["never"], + errors: [ + { + message: "There should be no space before '}'.", + type: "ObjectPattern", + line: 1, + column: 11, + }, + ], + }, + { + code: "var { x=10} = y", + output: "var { x=10 } = y", + ecmaFeatures: { destructuring: true }, + options: ["always"], + errors: [ + { + message: "A space is required before '}'.", + type: "ObjectPattern", + line: 1, + column: 11, + }, + ], + }, + { + code: "var {x=10 } = y", + output: "var { x=10 } = y", + ecmaFeatures: { destructuring: true }, + options: ["always"], + errors: [ + { + message: "A space is required after '{'.", + type: "ObjectPattern", + line: 1, + column: 5, + }, + ], + }, - ] - }, + // never - arraysInObjects + { + code: "var obj = {'foo': [1, 2]};", + output: "var obj = {'foo': [1, 2] };", + options: ["never", { arraysInObjects: true }], + errors: [ { - code: "import { bar, } from 'foo';", - output: "import {bar,} from 'foo';", - options: ["never"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "There should be no space after '{'.", - type: "ImportDeclaration", - line: 1, - column: 8 - }, - { - message: "There should be no space before '}'.", - type: "ImportDeclaration", - line: 1, - column: 15 - } - ] + message: "A space is required before '}'.", + type: "ObjectExpression", }, + ], + }, + { + code: "var obj = {'foo': [1, 2] , 'bar': ['baz', 'qux']};", + output: "var obj = {'foo': [1, 2] , 'bar': ['baz', 'qux'] };", + options: ["never", { arraysInObjects: true }], + errors: [ { - code: "export {bar};", - output: "export { bar };", - options: ["always"], - ecmaFeatures: { - modules: true - }, - errors: [ - { - message: "A space is required after '{'.", - type: "ExportNamedDeclaration", - line: 1, - column: 8 - }, - { - message: "A space is required before '}'.", - type: "ExportNamedDeclaration", - line: 1, - column: 12 - } - ] + message: "A space is required before '}'.", + type: "ObjectExpression", }, + ], + }, - // always - arraysInObjects - { - code: "var obj = { 'foo': [ 1, 2 ] };", - output: "var obj = { 'foo': [ 1, 2 ]};", - options: ["always", {"arraysInObjects": false}], - errors: [ - { - message: "There should be no space before '}'.", - type: "ObjectExpression" - } - ] - }, - { - code: "var obj = { 'foo': [ 1, 2 ] , 'bar': [ 'baz', 'qux' ] };", - output: "var obj = { 'foo': [ 1, 2 ] , 'bar': [ 'baz', 'qux' ]};", - options: ["always", {"arraysInObjects": false}], - errors: [ - { - message: "There should be no space before '}'.", - type: "ObjectExpression" - } - ] - }, + // Babel test cases. - // always-objectsInObjects + // always - destructuring typed object param + { + code: "function fn({a,b}: Object){}", + output: "function fn({ a,b }: Object){}", + options: ["always"], + parser: "babel-eslint", + ecmaFeatures: { + destructuring: true, + }, + errors: [ { - code: "var obj = { 'foo': { 'bar': 1, 'baz': 2 } };", - output: "var obj = { 'foo': { 'bar': 1, 'baz': 2 }};", - options: ["always", {"objectsInObjects": false}], - errors: [ - { - message: "There should be no space before '}'.", - type: "ObjectExpression", - line: 1, - column: 43 - } - ] + message: "A space is required after '{'.", + type: "ObjectPattern", + line: 1, + column: 13, }, { - code: "var obj = { 'foo': [ 1, 2 ] , 'bar': { 'baz': 1, 'qux': 2 } };", - output: "var obj = { 'foo': [ 1, 2 ] , 'bar': { 'baz': 1, 'qux': 2 }};", - options: ["always", {"objectsInObjects": false}], - errors: [ - { - message: "There should be no space before '}'.", - type: "ObjectExpression", - line: 1, - column: 61 - } - ] + message: "A space is required before '}'.", + type: "ObjectPattern", + line: 1, + column: 17, }, + ], + }, - // always-destructuring trailing comma + // never - destructuring typed object param + { + code: "function fn({ a,b }: Object){}", + output: "function fn({a,b}: Object){}", + options: ["never"], + parser: "babel-eslint", + ecmaFeatures: { + destructuring: true, + }, + errors: [ { - code: "var { a,} = x;", - output: "var { a, } = x;", - options: ["always"], - ecmaFeatures: { destructuring: true }, - errors: [ - { - message: "A space is required before '}'.", - type: "ObjectPattern", - line: 1, - column: 9 - } - ] + message: "There should be no space after '{'.", + type: "ObjectPattern", + line: 1, + column: 13, }, { - code: "var {a, } = x;", - output: "var {a,} = x;", - options: ["never"], - ecmaFeatures: { destructuring: true }, - errors: [ - { - message: "There should be no space before '}'.", - type: "ObjectPattern", - line: 1, - column: 9 - } - ] + message: "There should be no space before '}'.", + type: "ObjectPattern", + line: 1, + column: 19, }, - { - code: "var {a:b } = x;", - output: "var {a:b} = x;", - options: ["never"], - ecmaFeatures: { destructuring: true }, - errors: [ - { - message: "There should be no space before '}'.", - type: "ObjectPattern", - line: 1, - column: 10 - } - ] - }, - { - code: "var { a:b } = x;", - output: "var {a:b} = x;", - options: ["never"], - ecmaFeatures: { destructuring: true }, - errors: [ - { - message: "There should be no space after '{'.", - type: "ObjectPattern", - line: 1, - column: 5 - }, - { - message: "There should be no space before '}'.", - type: "ObjectPattern", - line: 1, - column: 11 - } - ] - }, - - // never-objectsInObjects - { - code: "var obj = {'foo': {'bar': 1, 'baz': 2}};", - output: "var obj = {'foo': {'bar': 1, 'baz': 2} };", - options: ["never", {"objectsInObjects": true}], - errors: [ - { - message: "A space is required before '}'.", - type: "ObjectExpression", - line: 1, - column: 39 - } - ] - }, - { - code: "var obj = {'foo': [1, 2] , 'bar': {'baz': 1, 'qux': 2}};", - output: "var obj = {'foo': [1, 2] , 'bar': {'baz': 1, 'qux': 2} };", - options: ["never", {"objectsInObjects": true}], - errors: [ - { - message: "A space is required before '}'.", - type: "ObjectExpression", - line: 1, - column: 55 - } - ] - }, - - // always & never - { - code: "var obj = {foo: bar, baz: qux};", - output: "var obj = { foo: bar, baz: qux };", - options: ["always"], - errors: [ - { - message: "A space is required after '{'.", - type: "ObjectExpression", - line: 1, - column: 11 - }, - { - message: "A space is required before '}'.", - type: "ObjectExpression", - line: 1, - column: 30 - } - ] - }, - { - code: "var obj = {foo: bar, baz: qux };", - output: "var obj = { foo: bar, baz: qux };", - options: ["always"], - errors: [ - { - message: "A space is required after '{'.", - type: "ObjectExpression", - line: 1, - column: 11 - } - ] - }, - { - code: "var obj = { foo: bar, baz: qux};", - output: "var obj = { foo: bar, baz: qux };", - options: ["always"], - errors: [ - { - message: "A space is required before '}'.", - type: "ObjectExpression", - line: 1, - column: 31 - } - ] - }, - { - code: "var obj = { foo: bar, baz: qux };", - output: "var obj = {foo: bar, baz: qux};", - options: ["never"], - errors: [ - { - message: "There should be no space after '{'.", - type: "ObjectExpression", - line: 1, - column: 11 - }, - { - message: "There should be no space before '}'.", - type: "ObjectExpression", - line: 1, - column: 32 - } - ] - }, - { - code: "var obj = {foo: bar, baz: qux };", - output: "var obj = {foo: bar, baz: qux};", - options: ["never"], - errors: [ - { - message: "There should be no space before '}'.", - type: "ObjectExpression", - line: 1, - column: 31 - } - ] - }, - { - code: "var obj = { foo: bar, baz: qux};", - output: "var obj = {foo: bar, baz: qux};", - options: ["never"], - errors: [ - { - message: "There should be no space after '{'.", - type: "ObjectExpression", - line: 1, - column: 11 - } - ] - }, - { - code: "var obj = { foo: { bar: quxx}, baz: qux};", - output: "var obj = {foo: {bar: quxx}, baz: qux};", - options: ["never"], - errors: [ - { - message: "There should be no space after '{'.", - type: "ObjectExpression", - line: 1, - column: 11 - }, - { - message: "There should be no space after '{'.", - type: "ObjectExpression", - line: 1, - column: 18 - } - ] - }, - { - code: "var obj = {foo: {bar: quxx }, baz: qux };", - output: "var obj = {foo: {bar: quxx}, baz: qux};", - options: ["never"], - errors: [ - { - message: "There should be no space before '}'.", - type: "ObjectExpression", - line: 1, - column: 28 - }, - { - message: "There should be no space before '}'.", - type: "ObjectExpression", - line: 1, - column: 40 - } - ] - }, - { - code: "export const thing = {value: 1 };", - output: "export const thing = { value: 1 };", - ecmaFeatures: { - modules: true, - blockBindings: true - }, - options: ["always"], - errors: [ - { - message: "A space is required after '{'.", - type: "ObjectExpression", - line: 1, - column: 22 - } - ] - }, - - // destructuring - { - code: "var {x, y} = y", - output: "var { x, y } = y", - ecmaFeatures: {destructuring: true}, - options: ["always"], - errors: [ - { - message: "A space is required after '{'.", - type: "ObjectPattern", - line: 1, - column: 5 - }, - { - message: "A space is required before '}'.", - type: "ObjectPattern", - line: 1, - column: 10 - } - ] - }, - { - code: "var { x, y} = y", - output: "var { x, y } = y", - ecmaFeatures: {destructuring: true}, - options: ["always"], - errors: [ - { - message: "A space is required before '}'.", - type: "ObjectPattern", - line: 1, - column: 11 - } - ] - }, - { - code: "var { x, y } = y", - output: "var {x, y} = y", - ecmaFeatures: {destructuring: true}, - options: ["never"], - errors: [ - { - message: "There should be no space after '{'.", - type: "ObjectPattern", - line: 1, - column: 5 - }, - { - message: "There should be no space before '}'.", - type: "ObjectPattern", - line: 1, - column: 12 - } - ] - }, - { - code: "var {x, y } = y", - output: "var {x, y} = y", - ecmaFeatures: {destructuring: true}, - options: ["never"], - errors: [ - { - message: "There should be no space before '}'.", - type: "ObjectPattern", - line: 1, - column: 11 - } - ] - }, - { - code: "var { x=10} = y", - output: "var { x=10 } = y", - ecmaFeatures: {destructuring: true}, - options: ["always"], - errors: [ - { - message: "A space is required before '}'.", - type: "ObjectPattern", - line: 1, - column: 11 - } - ] - }, - { - code: "var {x=10 } = y", - output: "var { x=10 } = y", - ecmaFeatures: {destructuring: true}, - options: ["always"], - errors: [ - { - message: "A space is required after '{'.", - type: "ObjectPattern", - line: 1, - column: 5 - } - ] - }, - - // never - arraysInObjects - { - code: "var obj = {'foo': [1, 2]};", - output: "var obj = {'foo': [1, 2] };", - options: ["never", {"arraysInObjects": true}], - errors: [ - { - message: "A space is required before '}'.", - type: "ObjectExpression" - } - ] - }, - { - code: "var obj = {'foo': [1, 2] , 'bar': ['baz', 'qux']};", - output: "var obj = {'foo': [1, 2] , 'bar': ['baz', 'qux'] };", - options: ["never", {"arraysInObjects": true}], - errors: [ - { - message: "A space is required before '}'.", - type: "ObjectExpression" - } - ] - }, - - // Babel test cases. - - // always - destructuring typed object param - { - code: "function fn({a,b}: Object){}", - output: "function fn({ a,b }: Object){}", - options: ["always"], - parser: "babel-eslint", - ecmaFeatures: { - destructuring: true - }, - errors: [ - { - message: "A space is required after '{'.", - type: "ObjectPattern", - line: 1, - column: 13 - }, - { - message: "A space is required before '}'.", - type: "ObjectPattern", - line: 1, - column: 17 - } - ] - }, - - // never - destructuring typed object param - { - code: "function fn({ a,b }: Object){}", - output: "function fn({a,b}: Object){}", - options: ["never"], - parser: "babel-eslint", - ecmaFeatures: { - destructuring: true - }, - errors: [ - { - message: "There should be no space after '{'.", - type: "ObjectPattern", - line: 1, - column: 13 - }, - { - message: "There should be no space before '}'.", - type: "ObjectPattern", - line: 1, - column: 19 - } - ] - } - ] + ], + }, + ], }); diff --git a/eslint/babel-eslint-plugin/test/rules/quotes.js b/eslint/babel-eslint-plugin/test/rules/quotes.js index 17fde24b6c..938da649d2 100644 --- a/eslint/babel-eslint-plugin/test/rules/quotes.js +++ b/eslint/babel-eslint-plugin/test/rules/quotes.js @@ -1,320 +1,481 @@ -var rule = require('../../rules/quotes'), - RuleTester = require("../helpers/RuleTester"); +var rule = require("../../src/rules/quotes"), + RuleTester = require("../helpers/RuleTester"); var ruleTester = new RuleTester(); -ruleTester.run('babel/quotes', rule, { - valid: [ - "var foo = \"bar\";", - { code: "var foo = 'bar';", options: ["single"] }, - { code: "var foo = \"bar\";", options: ["double"] }, - { code: "var foo = 1;", options: ["single"] }, - { code: "var foo = 1;", options: ["double"] }, - { code: "var foo = \"'\";", options: ["single", { avoidEscape: true }] }, - { code: "var foo = '\"';", options: ["double", { avoidEscape: true }] }, - { code: "var foo =
Hello world
;", options: ["single"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, - { code: "var foo =
;", options: ["single"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, - { code: "var foo =
Hello world
;", options: ["double"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, - { code: "var foo =
Hello world
;", options: ["double", { avoidEscape: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, - { code: "var foo = `bar`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = `bar 'baz'`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = `bar \"baz\"`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = 1;", options: ["backtick"] }, - { code: "var foo = \"a string containing `backtick` quotes\";", options: ["backtick", { avoidEscape: true }] }, - { code: "var foo =
;", options: ["backtick"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, - { code: "var foo =
Hello world
;", options: ["backtick"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, +ruleTester.run("babel/quotes", rule, { + valid: [ + 'var foo = "bar";', + { code: "var foo = 'bar';", options: ["single"] }, + { code: 'var foo = "bar";', options: ["double"] }, + { code: "var foo = 1;", options: ["single"] }, + { code: "var foo = 1;", options: ["double"] }, + { code: 'var foo = "\'";', options: ["single", { avoidEscape: true }] }, + { code: "var foo = '\"';", options: ["double", { avoidEscape: true }] }, + { + code: "var foo =
Hello world
;", + options: ["single"], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, + }, + { + code: 'var foo =
;', + options: ["single"], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, + }, + { + code: "var foo =
Hello world
;", + options: ["double"], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, + }, + { + code: "var foo =
Hello world
;", + options: ["double", { avoidEscape: true }], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, + }, + { + code: "var foo = `bar`;", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `bar 'baz'`;", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: 'var foo = `bar "baz"`;', + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, + { code: "var foo = 1;", options: ["backtick"] }, + { + code: 'var foo = "a string containing `backtick` quotes";', + options: ["backtick", { avoidEscape: true }], + }, + { + code: 'var foo =
;', + options: ["backtick"], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, + }, + { + code: "var foo =
Hello world
;", + options: ["backtick"], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, + }, - // Backticks are only okay if they have substitutions, contain a line break, or are tagged - { code: "var foo = `back\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = `back\rtick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = `back\u2028tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = `back\u2029tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } }, - { - code: "var foo = `back\\\\\ntick`;", // 2 backslashes followed by a newline - options: ["single"], - parserOptions: { ecmaVersion: 6 } - }, - { code: "var foo = `back\\\\\\\\\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = `\n`;", options: ["single"], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = `back${x}tick`;", options: ["double"], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = tag`backtick`;", options: ["double"], parserOptions: { ecmaVersion: 6 } }, + // Backticks are only okay if they have substitutions, contain a line break, or are tagged + { + code: "var foo = `back\ntick`;", + options: ["single"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `back\rtick`;", + options: ["single"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `back\u2028tick`;", + options: ["single"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `back\u2029tick`;", + options: ["single"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `back\\\\\ntick`;", // 2 backslashes followed by a newline + options: ["single"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `back\\\\\\\\\ntick`;", + options: ["single"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `\n`;", + options: ["single"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `back${x}tick`;", + options: ["double"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = tag`backtick`;", + options: ["double"], + parserOptions: { ecmaVersion: 6 }, + }, - // Backticks are also okay if allowTemplateLiterals - { code: "var foo = `bar 'foo' baz` + 'bar';", options: ["single", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = `bar 'foo' baz` + \"bar\";", options: ["double", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } }, - { code: "var foo = `bar 'foo' baz` + `bar`;", options: ["backtick", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } }, + // Backticks are also okay if allowTemplateLiterals + { + code: "var foo = `bar 'foo' baz` + 'bar';", + options: ["single", { allowTemplateLiterals: true }], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `bar 'foo' baz` + \"bar\";", + options: ["double", { allowTemplateLiterals: true }], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "var foo = `bar 'foo' baz` + `bar`;", + options: ["backtick", { allowTemplateLiterals: true }], + parserOptions: { ecmaVersion: 6 }, + }, - // `backtick` should not warn the directive prologues. - { code: "\"use strict\"; var foo = `backtick`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, - { code: "\"use strict\"; 'use strong'; \"use asm\"; var foo = `backtick`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, - { code: "function foo() { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, - { code: "(function() { 'use strict'; 'use strong'; 'use asm'; var foo = `backtick`; })();", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, - { code: "(() => { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; })();", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, + // `backtick` should not warn the directive prologues. + { + code: '"use strict"; var foo = `backtick`;', + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: '"use strict"; \'use strong\'; "use asm"; var foo = `backtick`;', + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: + 'function foo() { "use strict"; "use strong"; "use asm"; var foo = `backtick`; }', + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: + "(function() { 'use strict'; 'use strong'; 'use asm'; var foo = `backtick`; })();", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: + '(() => { "use strict"; "use strong"; "use asm"; var foo = `backtick`; })();', + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, - // `backtick` should not warn import/export sources. - { code: "import \"a\"; import 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } }, - { code: "import a from \"a\"; import b from 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } }, - { code: "export * from \"a\"; export * from 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } }, + // `backtick` should not warn import/export sources. + { + code: "import \"a\"; import 'b';", + options: ["backtick"], + parserOptions: { sourceType: "module" }, + }, + { + code: "import a from \"a\"; import b from 'b';", + options: ["backtick"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export * from \"a\"; export * from 'b';", + options: ["backtick"], + parserOptions: { sourceType: "module" }, + }, - // `backtick` should not warn property/method names (not computed). - { code: "var obj = {\"key0\": 0, 'key1': 1};", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, - { code: "class Foo { 'bar'(){} }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, - { code: "class Foo { static ''(){} }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, + // `backtick` should not warn property/method names (not computed). + { + code: "var obj = {\"key0\": 0, 'key1': 1};", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "class Foo { 'bar'(){} }", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "class Foo { static ''(){} }", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + }, - // Babel - '<>foo;', - { code: '<>foo;', options: ['single'] }, - { code: '<>foo;', options: ['double'] }, - '<>
;', - { code: '<>
;', options: ['single'] }, - { code: '<>
;', options: ['double'] }, - ], - invalid: [ + // Babel + "<>foo;", + { code: "<>foo;", options: ["single"] }, + { code: "<>foo;", options: ["double"] }, + "<>
;", + { code: "<>
;", options: ["single"] }, + { code: "<>
;", options: ["double"] }, + ], + invalid: [ + { + code: "var foo = 'bar';", + output: 'var foo = "bar";', + errors: [{ message: "Strings must use doublequote.", type: "Literal" }], + }, + { + code: 'var foo = "bar";', + output: "var foo = 'bar';", + options: ["single"], + errors: [{ message: "Strings must use singlequote.", type: "Literal" }], + }, + { + code: "var foo = `bar`;", + output: "var foo = 'bar';", + options: ["single"], + parserOptions: { + ecmaVersion: 6, + }, + errors: [ + { message: "Strings must use singlequote.", type: "TemplateLiteral" }, + ], + }, + { + code: "var foo = 'don\\'t';", + output: 'var foo = "don\'t";', + errors: [{ message: "Strings must use doublequote.", type: "Literal" }], + }, + { + code: 'var msg = "Plugin \'" + name + "\' not found"', + output: "var msg = 'Plugin \\'' + name + '\\' not found'", + options: ["single"], + errors: [ { - code: "var foo = 'bar';", - output: "var foo = \"bar\";", - errors: [{ message: "Strings must use doublequote.", type: "Literal" }] + message: "Strings must use singlequote.", + type: "Literal", + column: 11, }, { - code: "var foo = \"bar\";", - output: "var foo = 'bar';", - options: ["single"], - errors: [{ message: "Strings must use singlequote.", type: "Literal" }] - }, - { - code: "var foo = `bar`;", - output: "var foo = 'bar';", - options: ["single"], - parserOptions: { - ecmaVersion: 6 - }, - errors: [{ message: "Strings must use singlequote.", type: "TemplateLiteral" }] - }, - { - code: "var foo = 'don\\'t';", - output: "var foo = \"don't\";", - errors: [{ message: "Strings must use doublequote.", type: "Literal" }] - }, - { - code: "var msg = \"Plugin '\" + name + \"' not found\"", - output: "var msg = 'Plugin \\'' + name + '\\' not found'", - options: ["single"], - errors: [ - { message: "Strings must use singlequote.", type: "Literal", column: 11 }, - { message: "Strings must use singlequote.", type: "Literal", column: 31 } - ] - }, - { - code: "var foo = 'bar';", - output: "var foo = \"bar\";", - options: ["double"], - errors: [{ message: "Strings must use doublequote.", type: "Literal" }] - }, - { - code: "var foo = `bar`;", - output: "var foo = \"bar\";", - options: ["double"], - parserOptions: { - ecmaVersion: 6 - }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] - }, - { - code: "var foo = \"bar\";", - output: "var foo = 'bar';", - options: ["single", { avoidEscape: true }], - errors: [{ message: "Strings must use singlequote.", type: "Literal" }] - }, - { - code: "var foo = 'bar';", - output: "var foo = \"bar\";", - options: ["double", { avoidEscape: true }], - errors: [{ message: "Strings must use doublequote.", type: "Literal" }] - }, - { - code: "var foo = '\\\\';", - output: "var foo = \"\\\\\";", - options: ["double", { avoidEscape: true }], - errors: [{ message: "Strings must use doublequote.", type: "Literal" }] - }, - { - code: "var foo = \"bar\";", - output: "var foo = 'bar';", - options: ["single", { allowTemplateLiterals: true }], - errors: [{ message: "Strings must use singlequote.", type: "Literal" }] - }, - { - code: "var foo = 'bar';", - output: "var foo = \"bar\";", - options: ["double", { allowTemplateLiterals: true }], - errors: [{ message: "Strings must use doublequote.", type: "Literal" }] - }, - { - code: "var foo = 'bar';", - output: "var foo = `bar`;", - options: ["backtick"], - errors: [{ message: "Strings must use backtick.", type: "Literal" }] - }, - { - code: "var foo = 'b${x}a$r';", - output: "var foo = `b\\${x}a$r`;", - options: ["backtick"], - errors: [{ message: "Strings must use backtick.", type: "Literal" }] - }, - { - code: "var foo = \"bar\";", - output: "var foo = `bar`;", - options: ["backtick"], - errors: [{ message: "Strings must use backtick.", type: "Literal" }] - }, - { - code: "var foo = \"bar\";", - output: "var foo = `bar`;", - options: ["backtick", { avoidEscape: true }], - errors: [{ message: "Strings must use backtick.", type: "Literal" }] - }, - { - code: "var foo = 'bar';", - output: "var foo = `bar`;", - options: ["backtick", { avoidEscape: true }], - errors: [{ message: "Strings must use backtick.", type: "Literal" }] + message: "Strings must use singlequote.", + type: "Literal", + column: 31, }, + ], + }, + { + code: "var foo = 'bar';", + output: 'var foo = "bar";', + options: ["double"], + errors: [{ message: "Strings must use doublequote.", type: "Literal" }], + }, + { + code: "var foo = `bar`;", + output: 'var foo = "bar";', + options: ["double"], + parserOptions: { + ecmaVersion: 6, + }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, + { + code: 'var foo = "bar";', + output: "var foo = 'bar';", + options: ["single", { avoidEscape: true }], + errors: [{ message: "Strings must use singlequote.", type: "Literal" }], + }, + { + code: "var foo = 'bar';", + output: 'var foo = "bar";', + options: ["double", { avoidEscape: true }], + errors: [{ message: "Strings must use doublequote.", type: "Literal" }], + }, + { + code: "var foo = '\\\\';", + output: 'var foo = "\\\\";', + options: ["double", { avoidEscape: true }], + errors: [{ message: "Strings must use doublequote.", type: "Literal" }], + }, + { + code: 'var foo = "bar";', + output: "var foo = 'bar';", + options: ["single", { allowTemplateLiterals: true }], + errors: [{ message: "Strings must use singlequote.", type: "Literal" }], + }, + { + code: "var foo = 'bar';", + output: 'var foo = "bar";', + options: ["double", { allowTemplateLiterals: true }], + errors: [{ message: "Strings must use doublequote.", type: "Literal" }], + }, + { + code: "var foo = 'bar';", + output: "var foo = `bar`;", + options: ["backtick"], + errors: [{ message: "Strings must use backtick.", type: "Literal" }], + }, + { + code: "var foo = 'b${x}a$r';", + output: "var foo = `b\\${x}a$r`;", + options: ["backtick"], + errors: [{ message: "Strings must use backtick.", type: "Literal" }], + }, + { + code: 'var foo = "bar";', + output: "var foo = `bar`;", + options: ["backtick"], + errors: [{ message: "Strings must use backtick.", type: "Literal" }], + }, + { + code: 'var foo = "bar";', + output: "var foo = `bar`;", + options: ["backtick", { avoidEscape: true }], + errors: [{ message: "Strings must use backtick.", type: "Literal" }], + }, + { + code: "var foo = 'bar';", + output: "var foo = `bar`;", + options: ["backtick", { avoidEscape: true }], + errors: [{ message: "Strings must use backtick.", type: "Literal" }], + }, - // "use strict" is *not* a directive prologue in these statements so is subject to the rule - { - code: "var foo = `backtick`; \"use strict\";", - output: "var foo = `backtick`; `use strict`;", - options: ["backtick"], - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use backtick.", type: "Literal" }] - }, - { - code: "{ \"use strict\"; var foo = `backtick`; }", - output: "{ `use strict`; var foo = `backtick`; }", - options: ["backtick"], - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use backtick.", type: "Literal" }] - }, - { - code: "if (1) { \"use strict\"; var foo = `backtick`; }", - output: "if (1) { `use strict`; var foo = `backtick`; }", - options: ["backtick"], - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use backtick.", type: "Literal" }] - }, + // "use strict" is *not* a directive prologue in these statements so is subject to the rule + { + code: 'var foo = `backtick`; "use strict";', + output: "var foo = `backtick`; `use strict`;", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [{ message: "Strings must use backtick.", type: "Literal" }], + }, + { + code: '{ "use strict"; var foo = `backtick`; }', + output: "{ `use strict`; var foo = `backtick`; }", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [{ message: "Strings must use backtick.", type: "Literal" }], + }, + { + code: 'if (1) { "use strict"; var foo = `backtick`; }', + output: "if (1) { `use strict`; var foo = `backtick`; }", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [{ message: "Strings must use backtick.", type: "Literal" }], + }, - // `backtick` should warn computed property names. - { - code: "var obj = {[\"key0\"]: 0, ['key1']: 1};", - output: "var obj = {[`key0`]: 0, [`key1`]: 1};", - options: ["backtick"], - parserOptions: { ecmaVersion: 6 }, - errors: [ - { message: "Strings must use backtick.", type: "Literal" }, - { message: "Strings must use backtick.", type: "Literal" } - ] - }, - { - code: "class Foo { ['a'](){} static ['b'](){} }", - output: "class Foo { [`a`](){} static [`b`](){} }", - options: ["backtick"], - parserOptions: { ecmaVersion: 6 }, - errors: [ - { message: "Strings must use backtick.", type: "Literal" }, - { message: "Strings must use backtick.", type: "Literal" } - ] - }, + // `backtick` should warn computed property names. + { + code: "var obj = {[\"key0\"]: 0, ['key1']: 1};", + output: "var obj = {[`key0`]: 0, [`key1`]: 1};", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" }, + { message: "Strings must use backtick.", type: "Literal" }, + ], + }, + { + code: "class Foo { ['a'](){} static ['b'](){} }", + output: "class Foo { [`a`](){} static [`b`](){} }", + options: ["backtick"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use backtick.", type: "Literal" }, + { message: "Strings must use backtick.", type: "Literal" }, + ], + }, - // https://github.com/eslint/eslint/issues/7084 - { - code: "
", - output: "
", - options: ["single"], - parserOptions: { ecmaFeatures: { jsx: true } }, - errors: [ - { message: "Strings must use singlequote.", type: "Literal" } - ] - }, - { - code: "
", - output: "
", - options: ["double"], - parserOptions: { ecmaFeatures: { jsx: true } }, - errors: [ - { message: "Strings must use doublequote.", type: "Literal" } - ] - }, - { - code: "
", - output: "
", - options: ["backtick"], - parserOptions: { ecmaFeatures: { jsx: true } }, - errors: [ - { message: "Strings must use backtick.", type: "Literal" } - ] - }, + // https://github.com/eslint/eslint/issues/7084 + { + code: '
', + output: "
", + options: ["single"], + parserOptions: { ecmaFeatures: { jsx: true } }, + errors: [{ message: "Strings must use singlequote.", type: "Literal" }], + }, + { + code: "
", + output: '
', + options: ["double"], + parserOptions: { ecmaFeatures: { jsx: true } }, + errors: [{ message: "Strings must use doublequote.", type: "Literal" }], + }, + { + code: "
", + output: "
", + options: ["backtick"], + parserOptions: { ecmaFeatures: { jsx: true } }, + errors: [{ message: "Strings must use backtick.", type: "Literal" }], + }, - // https://github.com/eslint/eslint/issues/7610 - { - code: "`use strict`;", - output: null, - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] - }, - { - code: "function foo() { `use strict`; foo(); }", - output: null, - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] - }, - { - code: "foo = function() { `use strict`; foo(); }", - output: null, - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] - }, - { - code: "() => { `use strict`; foo(); }", - output: null, - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] - }, - { - code: "() => { foo(); `use strict`; }", - output: "() => { foo(); \"use strict\"; }", - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] - }, - { - code: "foo(); `use strict`;", - output: "foo(); \"use strict\";", - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] - }, + // https://github.com/eslint/eslint/issues/7610 + { + code: "`use strict`;", + output: null, + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, + { + code: "function foo() { `use strict`; foo(); }", + output: null, + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, + { + code: "foo = function() { `use strict`; foo(); }", + output: null, + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, + { + code: "() => { `use strict`; foo(); }", + output: null, + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, + { + code: "() => { foo(); `use strict`; }", + output: '() => { foo(); "use strict"; }', + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, + { + code: "foo(); `use strict`;", + output: 'foo(); "use strict";', + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, - // https://github.com/eslint/eslint/issues/7646 + // https://github.com/eslint/eslint/issues/7646 + { + code: "var foo = `foo\\nbar`;", + output: 'var foo = "foo\\nbar";', + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, + { + code: "var foo = `foo\\\nbar`;", // 1 backslash followed by a newline + output: 'var foo = "foo\\\nbar";', + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, + { + code: "var foo = `foo\\\\\\\nbar`;", // 3 backslashes followed by a newline + output: 'var foo = "foo\\\\\\\nbar";', + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Strings must use doublequote.", type: "TemplateLiteral" }, + ], + }, + { + code: "````", + output: '""``', + parserOptions: { ecmaVersion: 6 }, + errors: [ { - code: "var foo = `foo\\nbar`;", - output: "var foo = \"foo\\nbar\";", - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] + message: "Strings must use doublequote.", + type: "TemplateLiteral", + line: 1, + column: 1, }, - { - code: "var foo = `foo\\\nbar`;", // 1 backslash followed by a newline - output: "var foo = \"foo\\\nbar\";", - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] - }, - { - code: "var foo = `foo\\\\\\\nbar`;", // 3 backslashes followed by a newline - output: "var foo = \"foo\\\\\\\nbar\";", - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] - }, - { - code: "````", - output: "\"\"``", - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral", line: 1, column: 1 }] - } - ], + ], + }, + ], }); diff --git a/eslint/babel-eslint-plugin/test/rules/semi.js b/eslint/babel-eslint-plugin/test/rules/semi.js index b76ef6e2e0..ac95f38482 100644 --- a/eslint/babel-eslint-plugin/test/rules/semi.js +++ b/eslint/babel-eslint-plugin/test/rules/semi.js @@ -10,195 +10,686 @@ // Requirements //------------------------------------------------------------------------------ -const rule = require("../../rules/semi"), - RuleTester = require("../helpers/RuleTester"); +const rule = require("../../src/rules/semi"), + RuleTester = require("../helpers/RuleTester"); const ruleTester = new RuleTester(); ruleTester.run("semi", rule, { - valid: [ - "var x = 5;", - "var x =5, y;", - "foo();", - "x = foo();", - "setTimeout(function() {foo = \"bar\"; });", - "setTimeout(function() {foo = \"bar\";});", - "for (var a in b){}", - "for (var i;;){}", - "if (true) {}\n;[global, extended].forEach(function(){});", - "throw new Error('foo');", - { code: "throw new Error('foo')", options: ["never"] }, - { code: "var x = 5", options: ["never"] }, - { code: "var x =5, y", options: ["never"] }, - { code: "foo()", options: ["never"] }, - { code: "debugger", options: ["never"] }, - { code: "for (var a in b){}", options: ["never"] }, - { code: "for (var i;;){}", options: ["never"] }, - { code: "x = foo()", options: ["never"] }, - { code: "if (true) {}\n;[global, extended].forEach(function(){})", options: ["never"] }, - { code: "(function bar() {})\n;(function foo(){})", options: ["never"] }, - { code: ";/foo/.test('bar')", options: ["never"] }, - { code: ";+5", options: ["never"] }, - { code: ";-foo()", options: ["never"] }, - { code: "a++\nb++", options: ["never"] }, - { code: "a++; b++", options: ["never"] }, - { code: "for (let thing of {}) {\n console.log(thing);\n}", parserOptions: { ecmaVersion: 6 } }, - { code: "do{}while(true)", options: ["never"] }, - { code: "do{}while(true);", options: ["always"] }, + valid: [ + "var x = 5;", + "var x =5, y;", + "foo();", + "x = foo();", + 'setTimeout(function() {foo = "bar"; });', + 'setTimeout(function() {foo = "bar";});', + "for (var a in b){}", + "for (var i;;){}", + "if (true) {}\n;[global, extended].forEach(function(){});", + "throw new Error('foo');", + { code: "throw new Error('foo')", options: ["never"] }, + { code: "var x = 5", options: ["never"] }, + { code: "var x =5, y", options: ["never"] }, + { code: "foo()", options: ["never"] }, + { code: "debugger", options: ["never"] }, + { code: "for (var a in b){}", options: ["never"] }, + { code: "for (var i;;){}", options: ["never"] }, + { code: "x = foo()", options: ["never"] }, + { + code: "if (true) {}\n;[global, extended].forEach(function(){})", + options: ["never"], + }, + { code: "(function bar() {})\n;(function foo(){})", options: ["never"] }, + { code: ";/foo/.test('bar')", options: ["never"] }, + { code: ";+5", options: ["never"] }, + { code: ";-foo()", options: ["never"] }, + { code: "a++\nb++", options: ["never"] }, + { code: "a++; b++", options: ["never"] }, + { + code: "for (let thing of {}) {\n console.log(thing);\n}", + parserOptions: { ecmaVersion: 6 }, + }, + { code: "do{}while(true)", options: ["never"] }, + { code: "do{}while(true);", options: ["always"] }, - { code: "if (foo) { bar() }", options: ["always", { omitLastInOneLineBlock: true }] }, - { code: "if (foo) { bar(); baz() }", options: ["always", { omitLastInOneLineBlock: true }] }, + { + code: "if (foo) { bar() }", + options: ["always", { omitLastInOneLineBlock: true }], + }, + { + code: "if (foo) { bar(); baz() }", + options: ["always", { omitLastInOneLineBlock: true }], + }, + // method definitions don't have a semicolon. + { code: "class A { a() {} b() {} }", parserOptions: { ecmaVersion: 6 } }, + { + code: "var A = class { a() {} b() {} };", + parserOptions: { ecmaVersion: 6 }, + }, - // method definitions don't have a semicolon. - { code: "class A { a() {} b() {} }", parserOptions: { ecmaVersion: 6 } }, - { code: "var A = class { a() {} b() {} };", parserOptions: { ecmaVersion: 6 } }, + { + code: "import theDefault, { named1, named2 } from 'src/mylib';", + parserOptions: { sourceType: "module" }, + }, + { + code: "import theDefault, { named1, named2 } from 'src/mylib'", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, - { code: "import theDefault, { named1, named2 } from 'src/mylib';", parserOptions: { sourceType: "module" } }, - { code: "import theDefault, { named1, named2 } from 'src/mylib'", options: ["never"], parserOptions: { sourceType: "module" } }, + // exports, "always" + { code: "export * from 'foo';", parserOptions: { sourceType: "module" } }, + { + code: "export { foo } from 'foo';", + parserOptions: { sourceType: "module" }, + }, + { code: "export { foo };", parserOptions: { sourceType: "module" } }, + { code: "export var foo;", parserOptions: { sourceType: "module" } }, + { + code: "export function foo () { }", + parserOptions: { sourceType: "module" }, + }, + { + code: "export function* foo () { }", + parserOptions: { sourceType: "module" }, + }, + { code: "export class Foo { }", parserOptions: { sourceType: "module" } }, + { code: "export let foo;", parserOptions: { sourceType: "module" } }, + { code: "export const FOO = 42;", parserOptions: { sourceType: "module" } }, + { + code: "export default function() { }", + parserOptions: { sourceType: "module" }, + }, + { + code: "export default function* () { }", + parserOptions: { sourceType: "module" }, + }, + { + code: "export default class { }", + parserOptions: { sourceType: "module" }, + }, + { + code: "export default foo || bar;", + parserOptions: { sourceType: "module" }, + }, + { + code: "export default (foo) => foo.bar();", + parserOptions: { sourceType: "module" }, + }, + { + code: "export default foo = 42;", + parserOptions: { sourceType: "module" }, + }, + { + code: "export default foo += 42;", + parserOptions: { sourceType: "module" }, + }, - // exports, "always" - { code: "export * from 'foo';", parserOptions: { sourceType: "module" } }, - { code: "export { foo } from 'foo';", parserOptions: { sourceType: "module" } }, - { code: "export { foo };", parserOptions: { sourceType: "module" } }, - { code: "export var foo;", parserOptions: { sourceType: "module" } }, - { code: "export function foo () { }", parserOptions: { sourceType: "module" } }, - { code: "export function* foo () { }", parserOptions: { sourceType: "module" } }, - { code: "export class Foo { }", parserOptions: { sourceType: "module" } }, - { code: "export let foo;", parserOptions: { sourceType: "module" } }, - { code: "export const FOO = 42;", parserOptions: { sourceType: "module" } }, - { code: "export default function() { }", parserOptions: { sourceType: "module" } }, - { code: "export default function* () { }", parserOptions: { sourceType: "module" } }, - { code: "export default class { }", parserOptions: { sourceType: "module" } }, - { code: "export default foo || bar;", parserOptions: { sourceType: "module" } }, - { code: "export default (foo) => foo.bar();", parserOptions: { sourceType: "module" } }, - { code: "export default foo = 42;", parserOptions: { sourceType: "module" } }, - { code: "export default foo += 42;", parserOptions: { sourceType: "module" } }, + // exports, "never" + { + code: "export * from 'foo'", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export { foo } from 'foo'", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export { foo }", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export var foo", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export function foo () { }", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export function* foo () { }", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export class Foo { }", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export let foo", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export const FOO = 42", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export default function() { }", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export default function* () { }", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export default class { }", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export default foo || bar", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export default (foo) => foo.bar()", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export default foo = 42", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { + code: "export default foo += 42", + options: ["never"], + parserOptions: { sourceType: "module" }, + }, + { code: "++\nfoo;", options: ["always"] }, + { code: "var a = b;\n+ c", options: ["never"] }, - // exports, "never" - { code: "export * from 'foo'", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export { foo } from 'foo'", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export { foo }", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export var foo", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export function foo () { }", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export function* foo () { }", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export class Foo { }", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export let foo", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export const FOO = 42", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export default function() { }", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export default function* () { }", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export default class { }", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export default foo || bar", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export default (foo) => foo.bar()", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export default foo = 42", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "export default foo += 42", options: ["never"], parserOptions: { sourceType: "module" } }, - { code: "++\nfoo;", options: ["always"] }, - { code: "var a = b;\n+ c", options: ["never"] }, + // https://github.com/eslint/eslint/issues/7782 + { code: "var a = b;\n/foo/.test(c)", options: ["never"] }, + { + code: "var a = b;\n`foo`", + options: ["never"], + parserOptions: { ecmaVersion: 6 }, + }, - // https://github.com/eslint/eslint/issues/7782 - { code: "var a = b;\n/foo/.test(c)", options: ["never"] }, - { code: "var a = b;\n`foo`", options: ["never"], parserOptions: { ecmaVersion: 6 } }, + // babel + "class Foo { bar = 'example'; }", + "class Foo { static bar = 'example'; }", + { + code: + "async function foo() { for await (let thing of {}) { console.log(thing); } }", + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "class Foo { bar = () => {}; }", + options: ["always", { omitLastInOneLineBlock: true }], + }, - // babel - "class Foo { bar = 'example'; }", - "class Foo { static bar = 'example'; }", - { code: "async function foo() { for await (let thing of {}) { console.log(thing); } }", parserOptions: { ecmaVersion: 6 } }, - { code: "class Foo { bar = () => {}; }", options: ["always", { omitLastInOneLineBlock: true }] }, - - // babel, "never" - { code: "class Foo { bar = 'example' }", options: ["never"] }, - { code: "class Foo { static bar = 'example' }", options: ["never"] }, - { code: "class Foo { bar = () => {} }", options: ["never"] }, - ], - invalid: [ - { code: "import * as utils from './utils'", output: "import * as utils from './utils';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration", column: 33 }] }, - { code: "import { square, diag } from 'lib'", output: "import { square, diag } from 'lib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] }, - { code: "import { default as foo } from 'lib'", output: "import { default as foo } from 'lib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] }, - { code: "import 'src/mylib'", output: "import 'src/mylib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] }, - { code: "import theDefault, { named1, named2 } from 'src/mylib'", output: "import theDefault, { named1, named2 } from 'src/mylib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] }, - { code: "function foo() { return [] }", output: "function foo() { return []; }", errors: [{ message: "Missing semicolon.", type: "ReturnStatement" }] }, - { code: "while(true) { break }", output: "while(true) { break; }", errors: [{ message: "Missing semicolon.", type: "BreakStatement" }] }, - { code: "while(true) { continue }", output: "while(true) { continue; }", errors: [{ message: "Missing semicolon.", type: "ContinueStatement" }] }, - { code: "let x = 5", output: "let x = 5;", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "var x = 5", output: "var x = 5;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "var x = 5, y", output: "var x = 5, y;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "debugger", output: "debugger;", errors: [{ message: "Missing semicolon.", type: "DebuggerStatement" }] }, - { code: "foo()", output: "foo();", errors: [{ message: "Missing semicolon.", type: "ExpressionStatement" }] }, - { code: "var x = 5, y", output: "var x = 5, y;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "for (var a in b) var i ", output: "for (var a in b) var i; ", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "for (;;){var i}", output: "for (;;){var i;}", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "for (;;) var i ", output: "for (;;) var i; ", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "for (var j;;) {var i}", output: "for (var j;;) {var i;}", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "var foo = {\n bar: baz\n}", output: "var foo = {\n bar: baz\n};", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration", line: 3 }] }, - { code: "var foo\nvar bar;", output: "var foo;\nvar bar;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration", line: 1 }] }, - { code: "throw new Error('foo')", output: "throw new Error('foo');", errors: [{ message: "Missing semicolon.", type: "ThrowStatement", line: 1 }] }, - { code: "do{}while(true)", output: "do{}while(true);", errors: [{ message: "Missing semicolon.", type: "DoWhileStatement", line: 1 }] }, - - { code: "throw new Error('foo');", output: "throw new Error('foo')", options: ["never"], errors: [{ message: "Extra semicolon.", type: "ThrowStatement", column: 23 }] }, - { code: "function foo() { return []; }", output: "function foo() { return [] }", options: ["never"], errors: [{ message: "Extra semicolon.", type: "ReturnStatement" }] }, - { code: "while(true) { break; }", output: "while(true) { break }", options: ["never"], errors: [{ message: "Extra semicolon.", type: "BreakStatement" }] }, - { code: "while(true) { continue; }", output: "while(true) { continue }", options: ["never"], errors: [{ message: "Extra semicolon.", type: "ContinueStatement" }] }, - { code: "let x = 5;", output: "let x = 5", parserOptions: { ecmaVersion: 6 }, options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "var x = 5;", output: "var x = 5", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "var x = 5, y;", output: "var x = 5, y", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "debugger;", output: "debugger", options: ["never"], errors: [{ message: "Extra semicolon.", type: "DebuggerStatement" }] }, - { code: "foo();", output: "foo()", options: ["never"], errors: [{ message: "Extra semicolon.", type: "ExpressionStatement" }] }, - { code: "var x = 5, y;", output: "var x = 5, y", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "for (var a in b) var i; ", output: "for (var a in b) var i ", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "for (;;){var i;}", output: "for (;;){var i}", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "for (;;) var i; ", output: "for (;;) var i ", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "for (var j;;) {var i;}", output: "for (var j;;) {var i}", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "var foo = {\n bar: baz\n};", output: "var foo = {\n bar: baz\n}", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration", line: 3 }] }, - { code: "import theDefault, { named1, named2 } from 'src/mylib';", output: "import theDefault, { named1, named2 } from 'src/mylib'", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ImportDeclaration" }] }, - { code: "do{}while(true);", output: "do{}while(true)", options: ["never"], errors: [{ message: "Extra semicolon.", type: "DoWhileStatement", line: 1 }] }, - - { code: "if (foo) { bar()\n }", options: ["always", { omitLastInOneLineBlock: true }], errors: [{ message: "Missing semicolon." }] }, - { code: "if (foo) {\n bar() }", options: ["always", { omitLastInOneLineBlock: true }], errors: [{ message: "Missing semicolon." }] }, - { code: "if (foo) {\n bar(); baz() }", options: ["always", { omitLastInOneLineBlock: true }], errors: [{ message: "Missing semicolon." }] }, - { code: "if (foo) { bar(); }", options: ["always", { omitLastInOneLineBlock: true }], errors: [{ message: "Extra semicolon." }] }, - - - // exports, "always" - { code: "export * from 'foo'", output: "export * from 'foo';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportAllDeclaration" }] }, - { code: "export { foo } from 'foo'", output: "export { foo } from 'foo';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportNamedDeclaration" }] }, - { code: "export { foo }", output: "export { foo };", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportNamedDeclaration" }] }, - { code: "export var foo", output: "export var foo;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "export let foo", output: "export let foo;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "export const FOO = 42", output: "export const FOO = 42;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] }, - { code: "export default foo || bar", output: "export default foo || bar;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportDefaultDeclaration" }] }, - { code: "export default (foo) => foo.bar()", output: "export default (foo) => foo.bar();", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportDefaultDeclaration" }] }, - { code: "export default foo = 42", output: "export default foo = 42;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportDefaultDeclaration" }] }, - { code: "export default foo += 42", output: "export default foo += 42;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportDefaultDeclaration" }] }, - - // exports, "never" - { code: "export * from 'foo';", output: "export * from 'foo'", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportAllDeclaration" }] }, - { code: "export { foo } from 'foo';", output: "export { foo } from 'foo'", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportNamedDeclaration" }] }, - { code: "export { foo };", output: "export { foo }", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportNamedDeclaration" }] }, - { code: "export var foo;", output: "export var foo", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "export let foo;", output: "export let foo", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "export const FOO = 42;", output: "export const FOO = 42", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] }, - { code: "export default foo || bar;", output: "export default foo || bar", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] }, - { code: "export default (foo) => foo.bar();", output: "export default (foo) => foo.bar()", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] }, - { code: "export default foo = 42;", output: "export default foo = 42", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] }, - { code: "export default foo += 42;", output: "export default foo += 42", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] }, - { code: "a;\n++b", output: "a\n++b", options: ["never"], errors: [{ message: "Extra semicolon." }] }, - - // babel - { code: "class Foo { bar = 'example' }", errors: [{ message: "Missing semicolon." }] }, - { code: "class Foo { static bar = 'example' }", errors: [{ message: "Missing semicolon." }] }, + // babel, "never" + { code: "class Foo { bar = 'example' }", options: ["never"] }, + { code: "class Foo { static bar = 'example' }", options: ["never"] }, + { code: "class Foo { bar = () => {} }", options: ["never"] }, + ], + invalid: [ + { + code: "import * as utils from './utils'", + output: "import * as utils from './utils';", + parserOptions: { sourceType: "module" }, + errors: [ { - code: "class Foo { bar = () => {} }", - options: ["always", { omitLastInOneLineBlock: true }], - errors: [{ message: "Missing semicolon." }] + message: "Missing semicolon.", + type: "ImportDeclaration", + column: 33, }, + ], + }, + { + code: "import { square, diag } from 'lib'", + output: "import { square, diag } from 'lib';", + parserOptions: { sourceType: "module" }, + errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }], + }, + { + code: "import { default as foo } from 'lib'", + output: "import { default as foo } from 'lib';", + parserOptions: { sourceType: "module" }, + errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }], + }, + { + code: "import 'src/mylib'", + output: "import 'src/mylib';", + parserOptions: { sourceType: "module" }, + errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }], + }, + { + code: "import theDefault, { named1, named2 } from 'src/mylib'", + output: "import theDefault, { named1, named2 } from 'src/mylib';", + parserOptions: { sourceType: "module" }, + errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }], + }, + { + code: "function foo() { return [] }", + output: "function foo() { return []; }", + errors: [{ message: "Missing semicolon.", type: "ReturnStatement" }], + }, + { + code: "while(true) { break }", + output: "while(true) { break; }", + errors: [{ message: "Missing semicolon.", type: "BreakStatement" }], + }, + { + code: "while(true) { continue }", + output: "while(true) { continue; }", + errors: [{ message: "Missing semicolon.", type: "ContinueStatement" }], + }, + { + code: "let x = 5", + output: "let x = 5;", + parserOptions: { ecmaVersion: 6 }, + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "var x = 5", + output: "var x = 5;", + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "var x = 5, y", + output: "var x = 5, y;", + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "debugger", + output: "debugger;", + errors: [{ message: "Missing semicolon.", type: "DebuggerStatement" }], + }, + { + code: "foo()", + output: "foo();", + errors: [{ message: "Missing semicolon.", type: "ExpressionStatement" }], + }, + { + code: "var x = 5, y", + output: "var x = 5, y;", + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "for (var a in b) var i ", + output: "for (var a in b) var i; ", + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "for (;;){var i}", + output: "for (;;){var i;}", + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "for (;;) var i ", + output: "for (;;) var i; ", + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "for (var j;;) {var i}", + output: "for (var j;;) {var i;}", + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "var foo = {\n bar: baz\n}", + output: "var foo = {\n bar: baz\n};", + errors: [ + { message: "Missing semicolon.", type: "VariableDeclaration", line: 3 }, + ], + }, + { + code: "var foo\nvar bar;", + output: "var foo;\nvar bar;", + errors: [ + { message: "Missing semicolon.", type: "VariableDeclaration", line: 1 }, + ], + }, + { + code: "throw new Error('foo')", + output: "throw new Error('foo');", + errors: [ + { message: "Missing semicolon.", type: "ThrowStatement", line: 1 }, + ], + }, + { + code: "do{}while(true)", + output: "do{}while(true);", + errors: [ + { message: "Missing semicolon.", type: "DoWhileStatement", line: 1 }, + ], + }, - // babel, "never" - { code: "class Foo { bar = 'example'; }", options: ["never"], errors: [{ message: "Extra semicolon." }] }, - { code: "class Foo { static bar = 'example'; }", options: ["never"], errors: [{ message: "Extra semicolon." }] }, - { - code: "class Foo { bar = () => {}; }", - options: ["never"], - errors: [{ message: "Extra semicolon." }] - }, - ] + { + code: "throw new Error('foo');", + output: "throw new Error('foo')", + options: ["never"], + errors: [ + { message: "Extra semicolon.", type: "ThrowStatement", column: 23 }, + ], + }, + { + code: "function foo() { return []; }", + output: "function foo() { return [] }", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "ReturnStatement" }], + }, + { + code: "while(true) { break; }", + output: "while(true) { break }", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "BreakStatement" }], + }, + { + code: "while(true) { continue; }", + output: "while(true) { continue }", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "ContinueStatement" }], + }, + { + code: "let x = 5;", + output: "let x = 5", + parserOptions: { ecmaVersion: 6 }, + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "var x = 5;", + output: "var x = 5", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "var x = 5, y;", + output: "var x = 5, y", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "debugger;", + output: "debugger", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "DebuggerStatement" }], + }, + { + code: "foo();", + output: "foo()", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "ExpressionStatement" }], + }, + { + code: "var x = 5, y;", + output: "var x = 5, y", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "for (var a in b) var i; ", + output: "for (var a in b) var i ", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "for (;;){var i;}", + output: "for (;;){var i}", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "for (;;) var i; ", + output: "for (;;) var i ", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "for (var j;;) {var i;}", + output: "for (var j;;) {var i}", + options: ["never"], + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "var foo = {\n bar: baz\n};", + output: "var foo = {\n bar: baz\n}", + options: ["never"], + errors: [ + { message: "Extra semicolon.", type: "VariableDeclaration", line: 3 }, + ], + }, + { + code: "import theDefault, { named1, named2 } from 'src/mylib';", + output: "import theDefault, { named1, named2 } from 'src/mylib'", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [{ message: "Extra semicolon.", type: "ImportDeclaration" }], + }, + { + code: "do{}while(true);", + output: "do{}while(true)", + options: ["never"], + errors: [ + { message: "Extra semicolon.", type: "DoWhileStatement", line: 1 }, + ], + }, + + { + code: "if (foo) { bar()\n }", + options: ["always", { omitLastInOneLineBlock: true }], + errors: [{ message: "Missing semicolon." }], + }, + { + code: "if (foo) {\n bar() }", + options: ["always", { omitLastInOneLineBlock: true }], + errors: [{ message: "Missing semicolon." }], + }, + { + code: "if (foo) {\n bar(); baz() }", + options: ["always", { omitLastInOneLineBlock: true }], + errors: [{ message: "Missing semicolon." }], + }, + { + code: "if (foo) { bar(); }", + options: ["always", { omitLastInOneLineBlock: true }], + errors: [{ message: "Extra semicolon." }], + }, + + // exports, "always" + { + code: "export * from 'foo'", + output: "export * from 'foo';", + parserOptions: { sourceType: "module" }, + errors: [{ message: "Missing semicolon.", type: "ExportAllDeclaration" }], + }, + { + code: "export { foo } from 'foo'", + output: "export { foo } from 'foo';", + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Missing semicolon.", type: "ExportNamedDeclaration" }, + ], + }, + { + code: "export { foo }", + output: "export { foo };", + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Missing semicolon.", type: "ExportNamedDeclaration" }, + ], + }, + { + code: "export var foo", + output: "export var foo;", + parserOptions: { sourceType: "module" }, + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "export let foo", + output: "export let foo;", + parserOptions: { sourceType: "module" }, + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "export const FOO = 42", + output: "export const FOO = 42;", + parserOptions: { sourceType: "module" }, + errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }], + }, + { + code: "export default foo || bar", + output: "export default foo || bar;", + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Missing semicolon.", type: "ExportDefaultDeclaration" }, + ], + }, + { + code: "export default (foo) => foo.bar()", + output: "export default (foo) => foo.bar();", + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Missing semicolon.", type: "ExportDefaultDeclaration" }, + ], + }, + { + code: "export default foo = 42", + output: "export default foo = 42;", + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Missing semicolon.", type: "ExportDefaultDeclaration" }, + ], + }, + { + code: "export default foo += 42", + output: "export default foo += 42;", + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Missing semicolon.", type: "ExportDefaultDeclaration" }, + ], + }, + + // exports, "never" + { + code: "export * from 'foo';", + output: "export * from 'foo'", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [{ message: "Extra semicolon.", type: "ExportAllDeclaration" }], + }, + { + code: "export { foo } from 'foo';", + output: "export { foo } from 'foo'", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [{ message: "Extra semicolon.", type: "ExportNamedDeclaration" }], + }, + { + code: "export { foo };", + output: "export { foo }", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [{ message: "Extra semicolon.", type: "ExportNamedDeclaration" }], + }, + { + code: "export var foo;", + output: "export var foo", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "export let foo;", + output: "export let foo", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "export const FOO = 42;", + output: "export const FOO = 42", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }], + }, + { + code: "export default foo || bar;", + output: "export default foo || bar", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Extra semicolon.", type: "ExportDefaultDeclaration" }, + ], + }, + { + code: "export default (foo) => foo.bar();", + output: "export default (foo) => foo.bar()", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Extra semicolon.", type: "ExportDefaultDeclaration" }, + ], + }, + { + code: "export default foo = 42;", + output: "export default foo = 42", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Extra semicolon.", type: "ExportDefaultDeclaration" }, + ], + }, + { + code: "export default foo += 42;", + output: "export default foo += 42", + options: ["never"], + parserOptions: { sourceType: "module" }, + errors: [ + { message: "Extra semicolon.", type: "ExportDefaultDeclaration" }, + ], + }, + { + code: "a;\n++b", + output: "a\n++b", + options: ["never"], + errors: [{ message: "Extra semicolon." }], + }, + + // babel + { + code: "class Foo { bar = 'example' }", + errors: [{ message: "Missing semicolon." }], + }, + { + code: "class Foo { static bar = 'example' }", + errors: [{ message: "Missing semicolon." }], + }, + { + code: "class Foo { bar = () => {} }", + options: ["always", { omitLastInOneLineBlock: true }], + errors: [{ message: "Missing semicolon." }], + }, + + // babel, "never" + { + code: "class Foo { bar = 'example'; }", + options: ["never"], + errors: [{ message: "Extra semicolon." }], + }, + { + code: "class Foo { static bar = 'example'; }", + options: ["never"], + errors: [{ message: "Extra semicolon." }], + }, + { + code: "class Foo { bar = () => {}; }", + options: ["never"], + errors: [{ message: "Extra semicolon." }], + }, + ], }); diff --git a/eslint/babel-eslint-plugin/test/rules/valid-typeof.js b/eslint/babel-eslint-plugin/test/rules/valid-typeof.js index 6b8bc7c54c..c6092a72f6 100644 --- a/eslint/babel-eslint-plugin/test/rules/valid-typeof.js +++ b/eslint/babel-eslint-plugin/test/rules/valid-typeof.js @@ -9,8 +9,8 @@ // Requirements //------------------------------------------------------------------------------ -const rule = require("../../rules/valid-typeof"), - RuleTester = require("../helpers/RuleTester"); +const rule = require("../../src/rules/valid-typeof"), + RuleTester = require("../helpers/RuleTester"); //------------------------------------------------------------------------------ // Tests @@ -19,175 +19,240 @@ const rule = require("../../rules/valid-typeof"), const ruleTester = new RuleTester(); ruleTester.run("valid-typeof", rule, { - valid: [ - // Original test cases. - "typeof foo === 'string'", - "typeof foo === 'object'", - "typeof foo === 'function'", - "typeof foo === 'undefined'", - "typeof foo === 'boolean'", - "typeof foo === 'number'", - "'string' === typeof foo", - "'object' === typeof foo", - "'function' === typeof foo", - "'undefined' === typeof foo", - "'boolean' === typeof foo", - "'number' === typeof foo", - "typeof foo === typeof bar", - "typeof foo === baz", - "typeof foo !== someType", - "typeof bar != someType", - "someType === typeof bar", - "someType == typeof bar", - "typeof foo == 'string'", - "typeof(foo) === 'string'", - "typeof(foo) !== 'string'", - "typeof(foo) == 'string'", - "typeof(foo) != 'string'", - "var oddUse = typeof foo + 'thing'", - { - code: "typeof foo === 'number'", - options: [{ requireStringLiterals: true }] - }, - { - code: "typeof foo === \"number\"", - options: [{ requireStringLiterals: true }] - }, - { - code: "var baz = typeof foo + 'thing'", - options: [{ requireStringLiterals: true }] - }, - { - code: "typeof foo === typeof bar", - options: [{ requireStringLiterals: true }] - }, - { - code: "typeof foo === `string`", - options: [{ requireStringLiterals: true }], - parserOptions: { ecmaVersion: 6 } - }, - { - code: "`object` === typeof foo", - options: [{ requireStringLiterals: true }], - parserOptions: { ecmaVersion: 6 } - }, - { - code: "typeof foo === `str${somethingElse}`", - parserOptions: { ecmaVersion: 6 } - }, + valid: [ + // Original test cases. + "typeof foo === 'string'", + "typeof foo === 'object'", + "typeof foo === 'function'", + "typeof foo === 'undefined'", + "typeof foo === 'boolean'", + "typeof foo === 'number'", + "'string' === typeof foo", + "'object' === typeof foo", + "'function' === typeof foo", + "'undefined' === typeof foo", + "'boolean' === typeof foo", + "'number' === typeof foo", + "typeof foo === typeof bar", + "typeof foo === baz", + "typeof foo !== someType", + "typeof bar != someType", + "someType === typeof bar", + "someType == typeof bar", + "typeof foo == 'string'", + "typeof(foo) === 'string'", + "typeof(foo) !== 'string'", + "typeof(foo) == 'string'", + "typeof(foo) != 'string'", + "var oddUse = typeof foo + 'thing'", + { + code: "typeof foo === 'number'", + options: [{ requireStringLiterals: true }], + }, + { + code: 'typeof foo === "number"', + options: [{ requireStringLiterals: true }], + }, + { + code: "var baz = typeof foo + 'thing'", + options: [{ requireStringLiterals: true }], + }, + { + code: "typeof foo === typeof bar", + options: [{ requireStringLiterals: true }], + }, + { + code: "typeof foo === `string`", + options: [{ requireStringLiterals: true }], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "`object` === typeof foo", + options: [{ requireStringLiterals: true }], + parserOptions: { ecmaVersion: 6 }, + }, + { + code: "typeof foo === `str${somethingElse}`", + parserOptions: { ecmaVersion: 6 }, + }, - // Babel-specific test cases. + // Babel-specific test cases. + { + code: "typeof BigInt(Number.MAX_SAFE_INTEGER) === 'bigint'", + }, + { + code: "'bigint' === typeof BigInt(Number.MAX_SAFE_INTEGER)", + }, + { + code: "typeof BigInt(Number.MAX_SAFE_INTEGER) === 'bigint'", + options: [{ requireStringLiterals: true }], + }, + ], + invalid: [ + { + code: "typeof foo === 'strnig'", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "'strnig' === typeof foo", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "if (typeof bar === 'umdefined') {}", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "typeof foo !== 'strnig'", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "'strnig' !== typeof foo", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "if (typeof bar !== 'umdefined') {}", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "typeof foo != 'strnig'", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "'strnig' != typeof foo", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "if (typeof bar != 'umdefined') {}", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "typeof foo == 'strnig'", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "'strnig' == typeof foo", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "if (typeof bar == 'umdefined') {}", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "if (typeof bar === `umdefined`) {}", + parserOptions: { ecmaVersion: 6 }, + errors: [ { - code: "typeof BigInt(Number.MAX_SAFE_INTEGER) === 'bigint'" + message: "Invalid typeof comparison value.", + type: "TemplateLiteral", }, + ], + }, + { + code: "typeof foo == 'invalid string'", + options: [{ requireStringLiterals: true }], + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "typeof foo == Object", + options: [{ requireStringLiterals: true }], + errors: [ { - code: "'bigint' === typeof BigInt(Number.MAX_SAFE_INTEGER)" + message: "Typeof comparisons should be to string literals.", + type: "Identifier", }, + ], + }, + { + code: "typeof foo === undefined", + options: [{ requireStringLiterals: true }], + errors: [ { - code: "typeof BigInt(Number.MAX_SAFE_INTEGER) === 'bigint'", - options: [{ requireStringLiterals: true }] + message: "Typeof comparisons should be to string literals.", + type: "Identifier", }, - ], - invalid: [ + ], + }, + { + code: "undefined === typeof foo", + options: [{ requireStringLiterals: true }], + errors: [ { - code: "typeof foo === 'strnig'", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] + message: "Typeof comparisons should be to string literals.", + type: "Identifier", }, + ], + }, + { + code: "undefined == typeof foo", + options: [{ requireStringLiterals: true }], + errors: [ { - code: "'strnig' === typeof foo", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] + message: "Typeof comparisons should be to string literals.", + type: "Identifier", }, + ], + }, + { + code: "typeof foo === `undefined${foo}`", + options: [{ requireStringLiterals: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ { - code: "if (typeof bar === 'umdefined') {}", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] + message: "Typeof comparisons should be to string literals.", + type: "TemplateLiteral", }, + ], + }, + { + code: "typeof foo === `${string}`", + options: [{ requireStringLiterals: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ { - code: "typeof foo !== 'strnig'", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "'strnig' !== typeof foo", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "if (typeof bar !== 'umdefined') {}", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "typeof foo != 'strnig'", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "'strnig' != typeof foo", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "if (typeof bar != 'umdefined') {}", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "typeof foo == 'strnig'", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "'strnig' == typeof foo", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "if (typeof bar == 'umdefined') {}", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "if (typeof bar === `umdefined`) {}", - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Invalid typeof comparison value.", type: "TemplateLiteral" }] - }, - { - code: "typeof foo == 'invalid string'", - options: [{ requireStringLiterals: true }], - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "typeof foo == Object", - options: [{ requireStringLiterals: true }], - errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }] - }, - { - code: "typeof foo === undefined", - options: [{ requireStringLiterals: true }], - errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }] - }, - { - code: "undefined === typeof foo", - options: [{ requireStringLiterals: true }], - errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }] - }, - { - code: "undefined == typeof foo", - options: [{ requireStringLiterals: true }], - errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }] - }, - { - code: "typeof foo === `undefined${foo}`", - options: [{ requireStringLiterals: true }], - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Typeof comparisons should be to string literals.", type: "TemplateLiteral" }] - }, - { - code: "typeof foo === `${string}`", - options: [{ requireStringLiterals: true }], - parserOptions: { ecmaVersion: 6 }, - errors: [{ message: "Typeof comparisons should be to string literals.", type: "TemplateLiteral" }] + message: "Typeof comparisons should be to string literals.", + type: "TemplateLiteral", }, + ], + }, - // Babel-specific test cases. - { - code: "typeof foo === 'bgiint'", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - { - code: "'bignit' === typeof foo", - errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] - }, - ] + // Babel-specific test cases. + { + code: "typeof foo === 'bgiint'", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + { + code: "'bignit' === typeof foo", + errors: [ + { message: "Invalid typeof comparison value.", type: "Literal" }, + ], + }, + ], });