Clean up eslint/* directories (#10729)

This commit is contained in:
Kai Cataldo 2019-11-17 05:01:10 -05:00 committed by Nicolò Ribaudo
parent 401c9bbe83
commit 53a3dac011
36 changed files with 3838 additions and 2394 deletions

View File

@ -0,0 +1,4 @@
src
test
.*
*.log

View File

@ -1,2 +0,0 @@
node_modules
npm-debug.log

View File

@ -0,0 +1,4 @@
src
test
.*
*.log

View File

@ -1 +0,0 @@
package-lock = false

View File

@ -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'

View File

@ -1,7 +0,0 @@
.PHONY: publish-patch
publish-patch:
./node_modules/.bin/mocha
npm version patch
npm publish
git push --follow-tags

View File

@ -20,9 +20,6 @@
"node": ">=6" "node": ">=6"
}, },
"main": "lib/index.js", "main": "lib/index.js",
"files": [
"lib"
],
"peerDependencies": { "peerDependencies": {
"@babel/core": ">=7.2.0", "@babel/core": ">=7.2.0",
"eslint": ">= 4.12.1" "eslint": ">= 4.12.1"

View File

@ -1,2 +1,4 @@
tests/ src
test
.* .*
*.log

View File

@ -13,7 +13,7 @@
"email": "nicolo.ribaudo@gmail.com", "email": "nicolo.ribaudo@gmail.com",
"url": "https://github.com/nicolo-ribaudo" "url": "https://github.com/nicolo-ribaudo"
}, },
"main": "src/index.js", "main": "lib/index.js",
"devDependencies": { "devDependencies": {
"eslint": "^5.9.0" "eslint": "^5.9.0"
}, },

View File

@ -0,0 +1,4 @@
src
test
.*
*.log

View File

@ -1,9 +0,0 @@
---
git:
depth: 1
sudo: false
language: node_js
node_js:
- 6
- 8
- 10

View File

@ -2,7 +2,7 @@
"name": "eslint-plugin-babel", "name": "eslint-plugin-babel",
"version": "5.3.0", "version": "5.3.0",
"description": "an eslint rule plugin companion to babel-eslint", "description": "an eslint rule plugin companion to babel-eslint",
"main": "index.js", "main": "lib/index.js",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/babel/eslint-plugin-babel.git" "url": "git+https://github.com/babel/eslint-plugin-babel.git"

View File

@ -9,7 +9,7 @@
// Requirements // Requirements
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
const rule = require("../../rules/camelcase"), const rule = require("../../src/rules/camelcase"),
RuleTester = require("../helpers/RuleTester"); RuleTester = require("../helpers/RuleTester");
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -21,10 +21,10 @@ const ruleTester = new RuleTester();
ruleTester.run("camelcase", rule, { ruleTester.run("camelcase", rule, {
valid: [ valid: [
// Original test cases. // Original test cases.
"firstName = \"Nicholas\"", 'firstName = "Nicholas"',
"FIRST_NAME = \"Nicholas\"", 'FIRST_NAME = "Nicholas"',
"__myPrivateVariable = \"Patrick\"", '__myPrivateVariable = "Patrick"',
"myPrivateVariable_ = \"Patrick\"", 'myPrivateVariable_ = "Patrick"',
"function doSomething(){}", "function doSomething(){}",
"do_something()", "do_something()",
"new do_something", "new do_something",
@ -42,175 +42,175 @@ ruleTester.run("camelcase", rule, {
"if (foo.bar_baz === boom.bam_pow) { [foo.baz_boom] }", "if (foo.bar_baz === boom.bam_pow) { [foo.baz_boom] }",
{ {
code: "var o = {key: 1}", code: "var o = {key: 1}",
options: [{ properties: "always" }] options: [{ properties: "always" }],
}, },
{ {
code: "var o = {_leading: 1}", code: "var o = {_leading: 1}",
options: [{ properties: "always" }] options: [{ properties: "always" }],
}, },
{ {
code: "var o = {trailing_: 1}", code: "var o = {trailing_: 1}",
options: [{ properties: "always" }] options: [{ properties: "always" }],
}, },
{ {
code: "var o = {bar_baz: 1}", code: "var o = {bar_baz: 1}",
options: [{ properties: "never" }] options: [{ properties: "never" }],
}, },
{ {
code: "var o = {_leading: 1}", code: "var o = {_leading: 1}",
options: [{ properties: "never" }] options: [{ properties: "never" }],
}, },
{ {
code: "var o = {trailing_: 1}", code: "var o = {trailing_: 1}",
options: [{ properties: "never" }] options: [{ properties: "never" }],
}, },
{ {
code: "obj.a_b = 2;", code: "obj.a_b = 2;",
options: [{ properties: "never" }] options: [{ properties: "never" }],
}, },
{ {
code: "obj._a = 2;", code: "obj._a = 2;",
options: [{ properties: "always" }] options: [{ properties: "always" }],
}, },
{ {
code: "obj.a_ = 2;", code: "obj.a_ = 2;",
options: [{ properties: "always" }] options: [{ properties: "always" }],
}, },
{ {
code: "obj._a = 2;", code: "obj._a = 2;",
options: [{ properties: "never" }] options: [{ properties: "never" }],
}, },
{ {
code: "obj.a_ = 2;", code: "obj.a_ = 2;",
options: [{ properties: "never" }] options: [{ properties: "never" }],
}, },
{ {
code: "var obj = {\n a_a: 1 \n};\n obj.a_b = 2;", code: "var obj = {\n a_a: 1 \n};\n obj.a_b = 2;",
options: [{ properties: "never" }] options: [{ properties: "never" }],
}, },
{ {
code: "obj.foo_bar = function(){};", code: "obj.foo_bar = function(){};",
options: [{ properties: "never" }] options: [{ properties: "never" }],
}, },
{ {
code: "var { category_id } = query;", code: "var { category_id } = query;",
options: [{ ignoreDestructuring: true }], options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "var { category_id: category_id } = query;", code: "var { category_id: category_id } = query;",
options: [{ ignoreDestructuring: true }], options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "var { category_id = 1 } = query;", code: "var { category_id = 1 } = query;",
options: [{ ignoreDestructuring: true }], options: [{ ignoreDestructuring: true }],
parserOptions: { ecmaVersion: 6 }, parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "var { category_id: category } = query;", code: "var { category_id: category } = query;",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "var { _leading } = query;", code: "var { _leading } = query;",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "var { trailing_ } = query;", code: "var { trailing_ } = query;",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "import { camelCased } from \"external module\";", code: 'import { camelCased } from "external module";',
parserOptions: { ecmaVersion: 6, sourceType: "module" } parserOptions: { ecmaVersion: 6, sourceType: "module" },
}, },
{ {
code: "import { _leading } from \"external module\";", code: 'import { _leading } from "external module";',
parserOptions: { ecmaVersion: 6, sourceType: "module" } parserOptions: { ecmaVersion: 6, sourceType: "module" },
}, },
{ {
code: "import { trailing_ } from \"external module\";", code: 'import { trailing_ } from "external module";',
parserOptions: { ecmaVersion: 6, sourceType: "module" } parserOptions: { ecmaVersion: 6, sourceType: "module" },
}, },
{ {
code: "import { no_camelcased as camelCased } from \"external-module\";", code: 'import { no_camelcased as camelCased } from "external-module";',
parserOptions: { ecmaVersion: 6, sourceType: "module" } parserOptions: { ecmaVersion: 6, sourceType: "module" },
}, },
{ {
code: "import { no_camelcased as _leading } from \"external-module\";", code: 'import { no_camelcased as _leading } from "external-module";',
parserOptions: { ecmaVersion: 6, sourceType: "module" } parserOptions: { ecmaVersion: 6, sourceType: "module" },
}, },
{ {
code: "import { no_camelcased as trailing_ } from \"external-module\";", code: 'import { no_camelcased as trailing_ } from "external-module";',
parserOptions: { ecmaVersion: 6, sourceType: "module" } parserOptions: { ecmaVersion: 6, sourceType: "module" },
}, },
{ {
code: "import { no_camelcased as camelCased, anoterCamelCased } from \"external-module\";", code:
parserOptions: { ecmaVersion: 6, sourceType: "module" } 'import { no_camelcased as camelCased, anoterCamelCased } from "external-module";',
parserOptions: { ecmaVersion: 6, sourceType: "module" },
}, },
{ {
code: "function foo({ no_camelcased: camelCased }) {};", code: "function foo({ no_camelcased: camelCased }) {};",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "function foo({ no_camelcased: _leading }) {};", code: "function foo({ no_camelcased: _leading }) {};",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "function foo({ no_camelcased: trailing_ }) {};", code: "function foo({ no_camelcased: trailing_ }) {};",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "function foo({ camelCased = 'default value' }) {};", code: "function foo({ camelCased = 'default value' }) {};",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "function foo({ _leading = 'default value' }) {};", code: "function foo({ _leading = 'default value' }) {};",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "function foo({ trailing_ = 'default value' }) {};", code: "function foo({ trailing_ = 'default value' }) {};",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "function foo({ camelCased }) {};", code: "function foo({ camelCased }) {};",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "function foo({ _leading }) {}", code: "function foo({ _leading }) {}",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "function foo({ trailing_ }) {}", code: "function foo({ trailing_ }) {}",
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
// Babel-specific test cases // Babel-specific test cases
{ {
code: "var foo = bar?.a_b;", code: "var foo = bar?.a_b;",
options: [{ properties: "never" }] options: [{ properties: "never" }],
}, },
], ],
invalid: [ invalid: [
{ {
code: "first_name = \"Nicholas\"", code: 'first_name = "Nicholas"',
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "first_name" }, data: { name: "first_name" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "__private_first_name = \"Patrick\"", code: '__private_first_name = "Patrick"',
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "__private_first_name" }, data: { name: "__private_first_name" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "function foo_bar(){}", code: "function foo_bar(){}",
@ -218,9 +218,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "foo_bar" }, data: { name: "foo_bar" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "obj.foo_bar = function(){};", code: "obj.foo_bar = function(){};",
@ -228,9 +228,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "foo_bar" }, data: { name: "foo_bar" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "bar_baz.foo = function(){};", code: "bar_baz.foo = function(){};",
@ -238,9 +238,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "bar_baz" }, data: { name: "bar_baz" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "[foo_bar.baz]", code: "[foo_bar.baz]",
@ -248,9 +248,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "foo_bar" }, data: { name: "foo_bar" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "if (foo.bar_baz === boom.bam_pow) { [foo_bar.baz] }", code: "if (foo.bar_baz === boom.bam_pow) { [foo_bar.baz] }",
@ -258,9 +258,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "foo_bar" }, data: { name: "foo_bar" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "foo.bar_baz = boom.bam_pow", code: "foo.bar_baz = boom.bam_pow",
@ -268,9 +268,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "bar_baz" }, data: { name: "bar_baz" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "var foo = { bar_baz: boom.bam_pow }", code: "var foo = { bar_baz: boom.bam_pow }",
@ -278,9 +278,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "bar_baz" }, data: { name: "bar_baz" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "foo.qux.boom_pow = { bar: boom.bam_pow }", code: "foo.qux.boom_pow = { bar: boom.bam_pow }",
@ -288,9 +288,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "boom_pow" }, data: { name: "boom_pow" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "var o = {bar_baz: 1}", code: "var o = {bar_baz: 1}",
@ -299,9 +299,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "bar_baz" }, data: { name: "bar_baz" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "obj.a_b = 2;", code: "obj.a_b = 2;",
@ -310,9 +310,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "a_b" }, data: { name: "a_b" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "var { category_id: category_alias } = query;", code: "var { category_id: category_alias } = query;",
@ -321,9 +321,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "category_alias" }, data: { name: "category_alias" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "var { category_id: category_alias } = query;", code: "var { category_id: category_alias } = query;",
@ -333,9 +333,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "category_alias" }, data: { name: "category_alias" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "var { category_id: categoryId, ...other_props } = query;", code: "var { category_id: categoryId, ...other_props } = query;",
@ -345,9 +345,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "other_props" }, data: { name: "other_props" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "var { category_id } = query;", code: "var { category_id } = query;",
@ -356,9 +356,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "category_id" }, data: { name: "category_id" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "var { category_id: category_id } = query;", code: "var { category_id: category_id } = query;",
@ -367,9 +367,9 @@ ruleTester.run("camelcase", rule, {
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "category_id" }, data: { name: "category_id" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "var { category_id = 1 } = query;", code: "var { category_id = 1 } = query;",
@ -377,108 +377,111 @@ ruleTester.run("camelcase", rule, {
errors: [ errors: [
{ {
message: "Identifier 'category_id' is not in camel case.", 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" }, parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "no_camelcased" }, 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" }, parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "no_camelcased" }, 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" }, parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "no_camelcased" }, 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" }, parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "no_camel_cased" }, 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" }, parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "no_camel_cased" }, 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" }, parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "no_camelcased" }, 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" }, parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "another_no_camelcased" }, 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" }, parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "no_camelcased" }, 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" }, parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [ errors: [
{ {
messageId: "notCamelCase", messageId: "notCamelCase",
data: { name: "no_camelcased" }, data: { name: "no_camelcased" },
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "function foo({ no_camelcased }) {};", code: "function foo({ no_camelcased }) {};",
@ -486,9 +489,9 @@ ruleTester.run("camelcase", rule, {
errors: [ errors: [
{ {
message: "Identifier 'no_camelcased' is not in camel case.", message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "function foo({ no_camelcased = 'default value' }) {};", code: "function foo({ no_camelcased = 'default value' }) {};",
@ -496,23 +499,24 @@ ruleTester.run("camelcase", rule, {
errors: [ errors: [
{ {
message: "Identifier 'no_camelcased' is not in camel case.", 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 }, parserOptions: { ecmaVersion: 6 },
errors: [ errors: [
{ {
message: "Identifier 'no_camelcased' is not in camel case.", message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier" type: "Identifier",
}, },
{ {
message: "Identifier 'camelcased_value' is not in camel case.", message: "Identifier 'camelcased_value' is not in camel case.",
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "const { bar: no_camelcased } = foo;", code: "const { bar: no_camelcased } = foo;",
@ -520,9 +524,9 @@ ruleTester.run("camelcase", rule, {
errors: [ errors: [
{ {
message: "Identifier 'no_camelcased' is not in camel case.", message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "function foo({ value_1: my_default }) {}", code: "function foo({ value_1: my_default }) {}",
@ -530,9 +534,9 @@ ruleTester.run("camelcase", rule, {
errors: [ errors: [
{ {
message: "Identifier 'my_default' is not in camel case.", message: "Identifier 'my_default' is not in camel case.",
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "function foo({ isCamelcased: no_camelcased }) {};", code: "function foo({ isCamelcased: no_camelcased }) {};",
@ -540,9 +544,9 @@ ruleTester.run("camelcase", rule, {
errors: [ errors: [
{ {
message: "Identifier 'no_camelcased' is not in camel case.", message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "var { foo: bar_baz = 1 } = quz;", code: "var { foo: bar_baz = 1 } = quz;",
@ -550,9 +554,9 @@ ruleTester.run("camelcase", rule, {
errors: [ errors: [
{ {
message: "Identifier 'bar_baz' is not in camel case.", message: "Identifier 'bar_baz' is not in camel case.",
type: "Identifier" type: "Identifier",
} },
] ],
}, },
{ {
code: "const { no_camelcased = false } = bar;", code: "const { no_camelcased = false } = bar;",
@ -560,9 +564,9 @@ ruleTester.run("camelcase", rule, {
errors: [ errors: [
{ {
message: "Identifier 'no_camelcased' is not in camel case.", message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier" type: "Identifier",
} },
] ],
} },
] ],
}); });

View File

@ -3,155 +3,298 @@
* @author Nicholas C. Zakas * @author Nicholas C. Zakas
*/ */
var rule = require('../../rules/new-cap'), var rule = require("../../src/rules/new-cap"),
RuleTester = require("../helpers/RuleTester"); RuleTester = require("../helpers/RuleTester");
var ruleTester = new RuleTester(); var ruleTester = new RuleTester();
ruleTester.run('babel/new-cap', rule, { ruleTester.run("babel/new-cap", rule, {
valid: [ valid: [
// Original test cases. // Original test cases.
"var x = new Constructor();", "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 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 function(){};",
"var x = new _;", "var x = new _;",
"var x = new $;", "var x = new $;",
"var x = new Σ;", "var x = new Σ;",
"var x = new _x;", "var x = new _x;",
"var x = new $x;", "var x = new $x;",
"var x = new this;", "var x = new this;",
"var x = Array(42)", "var x = Array(42)",
"var x = Boolean(42)", "var x = Boolean(42)",
"var x = Date(42)", "var x = Date(42)",
"var x = Date.UTC(2000, 0)", "var x = Date.UTC(2000, 0)",
"var x = Error('error')", "var x = Error('error')",
"var x = Function('return 0')", "var x = Function('return 0')",
"var x = Number(42)", "var x = Number(42)",
"var x = Object(null)", "var x = Object(null)",
"var x = RegExp(42)", "var x = RegExp(42)",
"var x = String(42)", "var x = String(42)",
"var x = Symbol('symbol')", "var x = Symbol('symbol')",
"var x = _();", "var x = _();",
"var x = $();", "var x = $();",
{ code: "var x = Foo(42)", options: [{ capIsNew: false }] }, { code: "var x = Foo(42)", options: [{ capIsNew: false }] },
{ code: "var x = bar.Foo(42)", options: [{ capIsNew: false }] }, { code: "var x = bar.Foo(42)", options: [{ capIsNew: false }] },
{ code: "var x = Foo.bar(42)", options: [{ capIsNew: false }] }, { code: "var x = Foo.bar(42)", options: [{ capIsNew: false }] },
"var x = bar[Foo](42)", "var x = bar[Foo](42)",
{ code: "var x = bar['Foo'](42)", options: [{ capIsNew: false }] }, { code: "var x = bar['Foo'](42)", options: [{ capIsNew: false }] },
"var x = Foo.bar(42)", "var x = Foo.bar(42)",
{ code: "var x = new foo(42)", options: [{ newIsCap: false }] }, { code: "var x = new foo(42)", options: [{ newIsCap: false }] },
"var o = { 1: function() {} }; o[1]();", "var o = { 1: function() {} }; o[1]();",
"var o = { 1: function() {} }; new 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 = Foo(42);",
{ code: "var x = new foo(42);", options: [{ newIsCap: true, newIsCapExceptions: ["foo"] }] }, options: [{ capIsNew: true, capIsNewExceptions: ["Foo"] }],
{ code: "var x = new foo(42);", options: [{ newIsCapExceptionPattern: "^foo" }] }, },
{ code: "var x = Object(42);", options: [{ 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 = Foo.Bar(42);",
{ code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["foo.bar"] }] }, 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 = new foo.bar(42);",
{ code: "var x = Foo.bar(42);", options: [{ properties: false }] }, options: [{ newIsCapExceptionPattern: "^foo\\.." }],
{ code: "var x = foo.Bar(42);", options: [{ capIsNew: false, properties: false }] }, },
{ 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. // Babel-specific test cases.
{ code: "@MyDecorator(123) class MyClass{}", parser: "babel-eslint" }, { code: "@MyDecorator(123) class MyClass{}", parser: "babel-eslint" },
], ],
invalid: [ 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 c();",
{ code: "var x = new a.b.c;", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] }, errors: [
{ 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" }] },
{ {
code: "var a = B.C();", message:
errors: [ "A constructor name should not start with a lowercase letter.",
{ type: "NewExpression",
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 x = new φ;",
errors: [
{ {
code: "var a = B\n.C();", message:
errors: [ "A constructor name should not start with a lowercase letter.",
{ type: "NewExpression",
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 x = new a.b.c;",
errors: [
{ {
code: "var a = new B.c();", message:
errors: [ "A constructor name should not start with a lowercase letter.",
{ type: "NewExpression",
message: "A constructor name should not start with a lowercase letter.",
type: "NewExpression",
line: 1,
column: 15
}
]
}, },
],
},
{
code: "var x = new a.b['c'];",
errors: [
{ {
code: "var a = new B.\nc();", message:
errors: [ "A constructor name should not start with a lowercase letter.",
{ type: "NewExpression",
message: "A constructor name should not start with a lowercase letter.",
type: "NewExpression",
line: 2,
column: 1
}
]
}, },
],
},
{
code: "var b = Foo();",
errors: [
{ {
code: "var a = new c();", message:
errors: [ "A function with a name starting with an uppercase letter should only be used as a constructor.",
{ type: "CallExpression",
message: "A constructor name should not start with a lowercase letter.",
type: "NewExpression",
line: 1,
column: 13
}
]
}, },
],
},
{
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);", type: "CallExpression",
options: [{ capIsNewExceptions: ["Foo"] }], message:
errors: [{ type: "CallExpression", message: "A function with a name starting with an uppercase letter should only be used as a constructor." }] "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\\.." }], options: [{ capIsNewExceptionPattern: "^Foo\\.." }],
errors: [{ type: "CallExpression", message: "A function with a name starting with an uppercase letter should only be used as a constructor." }] errors: [
},
{ {
code: "var x = new foo.bar(42);", type: "CallExpression",
options: [{ newIsCapExceptions: ["foo"] }], message:
errors: [{ type: "NewExpression", message: "A constructor name should not start with a lowercase letter." }] "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\\.." }], options: [{ newIsCapExceptionPattern: "^foo\\.." }],
errors: [{ type: "NewExpression", message: "A constructor name should not start with a lowercase letter." }] errors: [
} {
] type: "NewExpression",
message:
"A constructor name should not start with a lowercase letter.",
},
],
},
],
}); });

File diff suppressed because it is too large Load Diff

View File

@ -9,8 +9,8 @@
// Requirements // Requirements
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
const rule = require("../../rules/no-unused-expressions"), const rule = require("../../src/rules/no-unused-expressions"),
RuleTester = require("../helpers/RuleTester"); RuleTester = require("../helpers/RuleTester");
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Tests // Tests
@ -19,126 +19,372 @@ const rule = require("../../rules/no-unused-expressions"),
const ruleTester = new RuleTester(); const ruleTester = new RuleTester();
ruleTester.run("no-unused-expressions", rule, { ruleTester.run("no-unused-expressions", rule, {
valid: [ valid: [
// Original test cases. // Original test cases.
"function f(){}", "function f(){}",
"a = b", "a = b",
"new a", "new a",
"{}", "{}",
"f(); g()", "f(); g()",
"i++", "i++",
"a()", "a()",
{ code: "a && a()", options: [{ allowShortCircuit: true }] }, { code: "a && a()", options: [{ allowShortCircuit: true }] },
{ code: "a() || (b = c)", options: [{ allowShortCircuit: true }] }, { code: "a() || (b = c)", options: [{ allowShortCircuit: true }] },
{ code: "a ? b() : c()", options: [{ allowTernary: true }] }, { code: "a ? b() : c()", options: [{ allowTernary: true }] },
{ code: "a ? b() || (c = d) : e()", options: [{ allowShortCircuit: true, allowTernary: true }] }, {
"delete foo.bar", code: "a ? b() || (c = d) : e()",
"void new C", options: [{ allowShortCircuit: true, allowTernary: true }],
"\"use strict\";", },
"\"directive one\"; \"directive two\"; f();", "delete foo.bar",
"function foo() {\"use strict\"; return true; }", "void new C",
{ code: "var foo = () => {\"use strict\"; return true; }", parserOptions: { ecmaVersion: 6 } }, '"use strict";',
"function foo() {\"directive one\"; \"directive two\"; f(); }", '"directive one"; "directive two"; f();',
"function foo() { var foo = \"use strict\"; return true; }", 'function foo() {"use strict"; return true; }',
{ {
code: "function* foo(){ yield 0; }", code: 'var foo = () => {"use strict"; return true; }',
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ 'function foo() {"directive one"; "directive two"; f(); }',
code: "async function foo() { await 5; }", 'function foo() { var foo = "use strict"; return true; }',
parserOptions: { ecmaVersion: 8 } {
}, code: "function* foo(){ yield 0; }",
{ parserOptions: { ecmaVersion: 6 },
code: "async function foo() { await foo.bar; }", },
parserOptions: { ecmaVersion: 8 } {
}, code: "async function foo() { await 5; }",
{ parserOptions: { ecmaVersion: 8 },
code: "async function foo() { bar && await baz; }", },
options: [{ allowShortCircuit: true }], {
parserOptions: { ecmaVersion: 8 } code: "async function foo() { await foo.bar; }",
}, parserOptions: { ecmaVersion: 8 },
{ },
code: "async function foo() { foo ? await bar : await baz; }", {
options: [{ allowTernary: true }], code: "async function foo() { bar && await baz; }",
parserOptions: { ecmaVersion: 8 } options: [{ allowShortCircuit: true }],
}, parserOptions: { ecmaVersion: 8 },
{ },
code: "tag`tagged template literal`", {
options: [{ allowTaggedTemplates: true }], code: "async function foo() { foo ? await bar : await baz; }",
parserOptions: { ecmaVersion: 6 } options: [{ allowTernary: true }],
}, parserOptions: { ecmaVersion: 8 },
{ },
code: "shouldNotBeAffectedByAllowTemplateTagsOption()", {
options: [{ allowTaggedTemplates: true }], code: "tag`tagged template literal`",
parserOptions: { ecmaVersion: 6 } options: [{ allowTaggedTemplates: true }],
}, parserOptions: { ecmaVersion: 6 },
},
{
code: "shouldNotBeAffectedByAllowTemplateTagsOption()",
options: [{ allowTaggedTemplates: true }],
parserOptions: { ecmaVersion: 6 },
},
// Babel-specific test cases. // Babel-specific test cases.
"let a = do { if (foo) { foo.bar; } }", "let a = do { if (foo) { foo.bar; } }",
"let a = do { foo; }", "let a = do { foo; }",
"let a = do { let b = 2; foo; }", "let a = do { let b = 2; foo; }",
"let a = do { (foo + 1); }", "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; } } }",
"let a = do { if (foo) { if (foo.bar) { foo.bar; } else if (foo.baz) { foo.baz; } } }", "let a = do { if (foo) { if (foo.bar) { foo.bar; } else if (foo.baz) { foo.baz; } } }",
"foo.bar?.();", "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.",
],
},
], // Babel-specific test cases.
invalid: [ {
{ code: "0", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, code: "let a = do { foo; let b = 2; }",
{ code: "a", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] }, errors: [
{ 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`", message:
parserOptions: { ecmaVersion: 6 }, "Expected an assignment or function call and instead saw an expression.",
errors: ["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`", message:
parserOptions: { ecmaVersion: 6 }, "Expected an assignment or function call and instead saw an expression.",
errors: ["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" }] },
]
}); });

File diff suppressed because it is too large Load Diff

View File

@ -1,320 +1,481 @@
var rule = require('../../rules/quotes'), var rule = require("../../src/rules/quotes"),
RuleTester = require("../helpers/RuleTester"); RuleTester = require("../helpers/RuleTester");
var ruleTester = new RuleTester(); var ruleTester = new RuleTester();
ruleTester.run('babel/quotes', rule, { ruleTester.run("babel/quotes", rule, {
valid: [ valid: [
"var foo = \"bar\";", 'var foo = "bar";',
{ code: "var foo = 'bar';", options: ["single"] }, { code: "var foo = 'bar';", options: ["single"] },
{ code: "var foo = \"bar\";", options: ["double"] }, { code: 'var foo = "bar";', options: ["double"] },
{ code: "var foo = 1;", options: ["single"] }, { code: "var foo = 1;", options: ["single"] },
{ code: "var foo = 1;", options: ["double"] }, { code: "var foo = 1;", options: ["double"] },
{ code: "var foo = \"'\";", options: ["single", { avoidEscape: true }] }, { code: 'var foo = "\'";', options: ["single", { avoidEscape: true }] },
{ code: "var foo = '\"';", options: ["double", { avoidEscape: true }] }, { code: "var foo = '\"';", options: ["double", { avoidEscape: true }] },
{ code: "var foo = <div>Hello world</div>;", options: ["single"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, {
{ code: "var foo = <div id=\"foo\"></div>;", options: ["single"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, code: "var foo = <div>Hello world</div>;",
{ code: "var foo = <div>Hello world</div>;", options: ["double"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, options: ["single"],
{ code: "var foo = <div>Hello world</div>;", options: ["double", { avoidEscape: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: 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 = <div id="foo"></div>;',
{ code: "var foo = 1;", options: ["backtick"] }, options: ["single"],
{ code: "var foo = \"a string containing `backtick` quotes\";", options: ["backtick", { avoidEscape: true }] }, parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
{ code: "var foo = <div id=\"foo\"></div>;", options: ["backtick"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, },
{ code: "var foo = <div>Hello world</div>;", options: ["backtick"], parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, {
code: "var foo = <div>Hello world</div>;",
options: ["double"],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
},
{
code: "var foo = <div>Hello world</div>;",
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 = <div id="foo"></div>;',
options: ["backtick"],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
},
{
code: "var foo = <div>Hello world</div>;",
options: ["backtick"],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
},
// Backticks are only okay if they have substitutions, contain a line break, or are tagged // 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\ntick`;",
{ code: "var foo = `back\u2028tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } }, options: ["single"],
{ code: "var foo = `back\u2029tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } }, parserOptions: { ecmaVersion: 6 },
{ },
code: "var foo = `back\\\\\ntick`;", // 2 backslashes followed by a newline {
options: ["single"], code: "var foo = `back\rtick`;",
parserOptions: { ecmaVersion: 6 } 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 = `back\u2028tick`;",
{ code: "var foo = tag`backtick`;", options: ["double"], parserOptions: { ecmaVersion: 6 } }, 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 // 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';",
{ code: "var foo = `bar 'foo' baz` + `bar`;", options: ["backtick", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } }, 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. // `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: '"use strict"; var foo = `backtick`;',
{ code: "function foo() { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, options: ["backtick"],
{ code: "(function() { 'use strict'; 'use strong'; 'use asm'; var foo = `backtick`; })();", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, parserOptions: { ecmaVersion: 6 },
{ code: "(() => { \"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 },
},
{
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. // `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: "import \"a\"; import 'b';",
{ code: "export * from \"a\"; export * from 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } }, 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). // `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: "var obj = {\"key0\": 0, 'key1': 1};",
{ code: "class Foo { static ''(){} }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } }, options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
{
code: "class Foo { 'bar'(){} }",
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
{
code: "class Foo { static ''(){} }",
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
// Babel // Babel
'<>foo</>;', "<>foo</>;",
{ code: '<>foo</>;', options: ['single'] }, { code: "<>foo</>;", options: ["single"] },
{ code: '<>foo</>;', options: ['double'] }, { code: "<>foo</>;", options: ["double"] },
'<><div /><div /></>;', "<><div /><div /></>;",
{ code: '<><div /><div /></>;', options: ['single'] }, { code: "<><div /><div /></>;", options: ["single"] },
{ code: '<><div /><div /></>;', options: ['double'] }, { code: "<><div /><div /></>;", options: ["double"] },
], ],
invalid: [ 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';", message: "Strings must use singlequote.",
output: "var foo = \"bar\";", type: "Literal",
errors: [{ message: "Strings must use doublequote.", type: "Literal" }] column: 11,
}, },
{ {
code: "var foo = \"bar\";", message: "Strings must use singlequote.",
output: "var foo = 'bar';", type: "Literal",
options: ["single"], column: 31,
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" }]
}, },
],
},
{
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 // "use strict" is *not* a directive prologue in these statements so is subject to the rule
{ {
code: "var foo = `backtick`; \"use strict\";", code: 'var foo = `backtick`; "use strict";',
output: "var foo = `backtick`; `use strict`;", output: "var foo = `backtick`; `use strict`;",
options: ["backtick"], options: ["backtick"],
parserOptions: { ecmaVersion: 6 }, parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use backtick.", type: "Literal" }] errors: [{ message: "Strings must use backtick.", type: "Literal" }],
}, },
{ {
code: "{ \"use strict\"; var foo = `backtick`; }", code: '{ "use strict"; var foo = `backtick`; }',
output: "{ `use strict`; var foo = `backtick`; }", output: "{ `use strict`; var foo = `backtick`; }",
options: ["backtick"], options: ["backtick"],
parserOptions: { ecmaVersion: 6 }, parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use backtick.", type: "Literal" }] errors: [{ message: "Strings must use backtick.", type: "Literal" }],
}, },
{ {
code: "if (1) { \"use strict\"; var foo = `backtick`; }", code: 'if (1) { "use strict"; var foo = `backtick`; }',
output: "if (1) { `use strict`; var foo = `backtick`; }", output: "if (1) { `use strict`; var foo = `backtick`; }",
options: ["backtick"], options: ["backtick"],
parserOptions: { ecmaVersion: 6 }, parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use backtick.", type: "Literal" }] errors: [{ message: "Strings must use backtick.", type: "Literal" }],
}, },
// `backtick` should warn computed property names. // `backtick` should warn computed property names.
{ {
code: "var obj = {[\"key0\"]: 0, ['key1']: 1};", code: "var obj = {[\"key0\"]: 0, ['key1']: 1};",
output: "var obj = {[`key0`]: 0, [`key1`]: 1};", output: "var obj = {[`key0`]: 0, [`key1`]: 1};",
options: ["backtick"], options: ["backtick"],
parserOptions: { ecmaVersion: 6 }, parserOptions: { ecmaVersion: 6 },
errors: [ errors: [
{ message: "Strings must use backtick.", type: "Literal" }, { message: "Strings must use backtick.", type: "Literal" },
{ message: "Strings must use backtick.", type: "Literal" } { message: "Strings must use backtick.", type: "Literal" },
] ],
}, },
{ {
code: "class Foo { ['a'](){} static ['b'](){} }", code: "class Foo { ['a'](){} static ['b'](){} }",
output: "class Foo { [`a`](){} static [`b`](){} }", output: "class Foo { [`a`](){} static [`b`](){} }",
options: ["backtick"], options: ["backtick"],
parserOptions: { ecmaVersion: 6 }, parserOptions: { ecmaVersion: 6 },
errors: [ errors: [
{ message: "Strings must use backtick.", type: "Literal" }, { message: "Strings must use backtick.", type: "Literal" },
{ message: "Strings must use backtick.", type: "Literal" } { message: "Strings must use backtick.", type: "Literal" },
] ],
}, },
// https://github.com/eslint/eslint/issues/7084 // https://github.com/eslint/eslint/issues/7084
{ {
code: "<div blah={\"blah\"} />", code: '<div blah={"blah"} />',
output: "<div blah={'blah'} />", output: "<div blah={'blah'} />",
options: ["single"], options: ["single"],
parserOptions: { ecmaFeatures: { jsx: true } }, parserOptions: { ecmaFeatures: { jsx: true } },
errors: [ errors: [{ message: "Strings must use singlequote.", type: "Literal" }],
{ message: "Strings must use singlequote.", type: "Literal" } },
] {
}, code: "<div blah={'blah'} />",
{ output: '<div blah={"blah"} />',
code: "<div blah={'blah'} />", options: ["double"],
output: "<div blah={\"blah\"} />", parserOptions: { ecmaFeatures: { jsx: true } },
options: ["double"], errors: [{ message: "Strings must use doublequote.", type: "Literal" }],
parserOptions: { ecmaFeatures: { jsx: true } }, },
errors: [ {
{ message: "Strings must use doublequote.", type: "Literal" } code: "<div blah={'blah'} />",
] output: "<div blah={`blah`} />",
}, options: ["backtick"],
{ parserOptions: { ecmaFeatures: { jsx: true } },
code: "<div blah={'blah'} />", errors: [{ message: "Strings must use backtick.", type: "Literal" }],
output: "<div blah={`blah`} />", },
options: ["backtick"],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{ message: "Strings must use backtick.", type: "Literal" }
]
},
// https://github.com/eslint/eslint/issues/7610 // https://github.com/eslint/eslint/issues/7610
{ {
code: "`use strict`;", code: "`use strict`;",
output: null, output: null,
parserOptions: { ecmaVersion: 6 }, parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] errors: [
}, { message: "Strings must use doublequote.", type: "TemplateLiteral" },
{ ],
code: "function foo() { `use strict`; foo(); }", },
output: null, {
parserOptions: { ecmaVersion: 6 }, code: "function foo() { `use strict`; foo(); }",
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] output: null,
}, parserOptions: { ecmaVersion: 6 },
{ errors: [
code: "foo = function() { `use strict`; foo(); }", { message: "Strings must use doublequote.", type: "TemplateLiteral" },
output: null, ],
parserOptions: { ecmaVersion: 6 }, },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] {
}, code: "foo = function() { `use strict`; foo(); }",
{ output: null,
code: "() => { `use strict`; foo(); }", parserOptions: { ecmaVersion: 6 },
output: null, errors: [
parserOptions: { ecmaVersion: 6 }, { message: "Strings must use doublequote.", type: "TemplateLiteral" },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] ],
}, },
{ {
code: "() => { foo(); `use strict`; }", code: "() => { `use strict`; foo(); }",
output: "() => { foo(); \"use strict\"; }", output: null,
parserOptions: { ecmaVersion: 6 }, parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] errors: [
}, { message: "Strings must use doublequote.", type: "TemplateLiteral" },
{ ],
code: "foo(); `use strict`;", },
output: "foo(); \"use strict\";", {
parserOptions: { ecmaVersion: 6 }, code: "() => { foo(); `use strict`; }",
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] 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`;", message: "Strings must use doublequote.",
output: "var foo = \"foo\\nbar\";", type: "TemplateLiteral",
parserOptions: { ecmaVersion: 6 }, line: 1,
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }] 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 }]
}
],
}); });

View File

@ -10,195 +10,686 @@
// Requirements // Requirements
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
const rule = require("../../rules/semi"), const rule = require("../../src/rules/semi"),
RuleTester = require("../helpers/RuleTester"); RuleTester = require("../helpers/RuleTester");
const ruleTester = new RuleTester(); const ruleTester = new RuleTester();
ruleTester.run("semi", rule, { ruleTester.run("semi", rule, {
valid: [ valid: [
"var x = 5;", "var x = 5;",
"var x =5, y;", "var x =5, y;",
"foo();", "foo();",
"x = foo();", "x = foo();",
"setTimeout(function() {foo = \"bar\"; });", 'setTimeout(function() {foo = "bar"; });',
"setTimeout(function() {foo = \"bar\";});", 'setTimeout(function() {foo = "bar";});',
"for (var a in b){}", "for (var a in b){}",
"for (var i;;){}", "for (var i;;){}",
"if (true) {}\n;[global, extended].forEach(function(){});", "if (true) {}\n;[global, extended].forEach(function(){});",
"throw new Error('foo');", "throw new Error('foo');",
{ code: "throw new Error('foo')", options: ["never"] }, { code: "throw new Error('foo')", options: ["never"] },
{ code: "var x = 5", options: ["never"] }, { code: "var x = 5", options: ["never"] },
{ code: "var x =5, y", options: ["never"] }, { code: "var x =5, y", options: ["never"] },
{ code: "foo()", options: ["never"] }, { code: "foo()", options: ["never"] },
{ code: "debugger", options: ["never"] }, { code: "debugger", options: ["never"] },
{ code: "for (var a in b){}", options: ["never"] }, { code: "for (var a in b){}", options: ["never"] },
{ code: "for (var i;;){}", options: ["never"] }, { code: "for (var i;;){}", options: ["never"] },
{ code: "x = foo()", 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: "if (true) {}\n;[global, extended].forEach(function(){})",
{ code: ";/foo/.test('bar')", options: ["never"] }, options: ["never"],
{ code: ";+5", options: ["never"] }, },
{ code: ";-foo()", options: ["never"] }, { code: "(function bar() {})\n;(function foo(){})", options: ["never"] },
{ code: "a++\nb++", options: ["never"] }, { code: ";/foo/.test('bar')", options: ["never"] },
{ code: "a++; b++", options: ["never"] }, { code: ";+5", options: ["never"] },
{ code: "for (let thing of {}) {\n console.log(thing);\n}", parserOptions: { ecmaVersion: 6 } }, { code: ";-foo()", options: ["never"] },
{ code: "do{}while(true)", options: ["never"] }, { code: "a++\nb++", options: ["never"] },
{ code: "do{}while(true);", options: ["always"] }, { 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: "import theDefault, { named1, named2 } from 'src/mylib';",
{ code: "var A = class { a() {} b() {} };", parserOptions: { ecmaVersion: 6 } }, 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" } }, // exports, "always"
{ code: "import theDefault, { named1, named2 } from 'src/mylib'", options: ["never"], parserOptions: { sourceType: "module" } }, { 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" // exports, "never"
{ code: "export * from 'foo';", parserOptions: { sourceType: "module" } }, {
{ code: "export { foo } from 'foo';", parserOptions: { sourceType: "module" } }, code: "export * from 'foo'",
{ code: "export { foo };", parserOptions: { sourceType: "module" } }, options: ["never"],
{ code: "export var foo;", parserOptions: { sourceType: "module" } }, 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 { foo } from 'foo'",
{ code: "export let foo;", parserOptions: { sourceType: "module" } }, options: ["never"],
{ code: "export const FOO = 42;", parserOptions: { sourceType: "module" } }, 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 { foo }",
{ code: "export default foo || bar;", parserOptions: { sourceType: "module" } }, options: ["never"],
{ code: "export default (foo) => foo.bar();", parserOptions: { sourceType: "module" } }, parserOptions: { sourceType: "module" },
{ code: "export default foo = 42;", parserOptions: { sourceType: "module" } }, },
{ code: "export default foo += 42;", 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" // https://github.com/eslint/eslint/issues/7782
{ code: "export * from 'foo'", options: ["never"], parserOptions: { sourceType: "module" } }, { code: "var a = b;\n/foo/.test(c)", options: ["never"] },
{ code: "export { foo } from 'foo'", options: ["never"], parserOptions: { sourceType: "module" } }, {
{ code: "export { foo }", options: ["never"], parserOptions: { sourceType: "module" } }, code: "var a = b;\n`foo`",
{ code: "export var foo", options: ["never"], parserOptions: { sourceType: "module" } }, options: ["never"],
{ code: "export function foo () { }", options: ["never"], parserOptions: { sourceType: "module" } }, parserOptions: { ecmaVersion: 6 },
{ 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 // babel
{ code: "var a = b;\n/foo/.test(c)", options: ["never"] }, "class Foo { bar = 'example'; }",
{ code: "var a = b;\n`foo`", options: ["never"], parserOptions: { ecmaVersion: 6 } }, "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 // babel, "never"
"class Foo { bar = 'example'; }", { code: "class Foo { bar = 'example' }", options: ["never"] },
"class Foo { static bar = 'example'; }", { code: "class Foo { static bar = 'example' }", options: ["never"] },
{ code: "async function foo() { for await (let thing of {}) { console.log(thing); } }", parserOptions: { ecmaVersion: 6 } }, { code: "class Foo { bar = () => {} }", options: ["never"] },
{ code: "class Foo { bar = () => {}; }", options: ["always", { omitLastInOneLineBlock: true }] }, ],
invalid: [
// babel, "never" {
{ code: "class Foo { bar = 'example' }", options: ["never"] }, code: "import * as utils from './utils'",
{ code: "class Foo { static bar = 'example' }", options: ["never"] }, output: "import * as utils from './utils';",
{ code: "class Foo { bar = () => {} }", options: ["never"] }, parserOptions: { sourceType: "module" },
], errors: [
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." }] },
{ {
code: "class Foo { bar = () => {} }", message: "Missing semicolon.",
options: ["always", { omitLastInOneLineBlock: true }], type: "ImportDeclaration",
errors: [{ message: "Missing semicolon." }] 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: "throw new Error('foo');",
{ code: "class Foo { static bar = 'example'; }", options: ["never"], errors: [{ message: "Extra semicolon." }] }, output: "throw new Error('foo')",
{ options: ["never"],
code: "class Foo { bar = () => {}; }", errors: [
options: ["never"], { message: "Extra semicolon.", type: "ThrowStatement", column: 23 },
errors: [{ message: "Extra semicolon." }] ],
}, },
] {
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." }],
},
],
}); });

View File

@ -9,8 +9,8 @@
// Requirements // Requirements
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
const rule = require("../../rules/valid-typeof"), const rule = require("../../src/rules/valid-typeof"),
RuleTester = require("../helpers/RuleTester"); RuleTester = require("../helpers/RuleTester");
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Tests // Tests
@ -19,175 +19,240 @@ const rule = require("../../rules/valid-typeof"),
const ruleTester = new RuleTester(); const ruleTester = new RuleTester();
ruleTester.run("valid-typeof", rule, { ruleTester.run("valid-typeof", rule, {
valid: [ valid: [
// Original test cases. // Original test cases.
"typeof foo === 'string'", "typeof foo === 'string'",
"typeof foo === 'object'", "typeof foo === 'object'",
"typeof foo === 'function'", "typeof foo === 'function'",
"typeof foo === 'undefined'", "typeof foo === 'undefined'",
"typeof foo === 'boolean'", "typeof foo === 'boolean'",
"typeof foo === 'number'", "typeof foo === 'number'",
"'string' === typeof foo", "'string' === typeof foo",
"'object' === typeof foo", "'object' === typeof foo",
"'function' === typeof foo", "'function' === typeof foo",
"'undefined' === typeof foo", "'undefined' === typeof foo",
"'boolean' === typeof foo", "'boolean' === typeof foo",
"'number' === typeof foo", "'number' === typeof foo",
"typeof foo === typeof bar", "typeof foo === typeof bar",
"typeof foo === baz", "typeof foo === baz",
"typeof foo !== someType", "typeof foo !== someType",
"typeof bar != someType", "typeof bar != someType",
"someType === typeof bar", "someType === typeof bar",
"someType == typeof bar", "someType == typeof bar",
"typeof foo == 'string'", "typeof foo == 'string'",
"typeof(foo) === 'string'", "typeof(foo) === 'string'",
"typeof(foo) !== 'string'", "typeof(foo) !== 'string'",
"typeof(foo) == 'string'", "typeof(foo) == 'string'",
"typeof(foo) != 'string'", "typeof(foo) != 'string'",
"var oddUse = typeof foo + 'thing'", "var oddUse = typeof foo + 'thing'",
{ {
code: "typeof foo === 'number'", code: "typeof foo === 'number'",
options: [{ requireStringLiterals: true }] options: [{ requireStringLiterals: true }],
}, },
{ {
code: "typeof foo === \"number\"", code: 'typeof foo === "number"',
options: [{ requireStringLiterals: true }] options: [{ requireStringLiterals: true }],
}, },
{ {
code: "var baz = typeof foo + 'thing'", code: "var baz = typeof foo + 'thing'",
options: [{ requireStringLiterals: true }] options: [{ requireStringLiterals: true }],
}, },
{ {
code: "typeof foo === typeof bar", code: "typeof foo === typeof bar",
options: [{ requireStringLiterals: true }] options: [{ requireStringLiterals: true }],
}, },
{ {
code: "typeof foo === `string`", code: "typeof foo === `string`",
options: [{ requireStringLiterals: true }], options: [{ requireStringLiterals: true }],
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "`object` === typeof foo", code: "`object` === typeof foo",
options: [{ requireStringLiterals: true }], options: [{ requireStringLiterals: true }],
parserOptions: { ecmaVersion: 6 } parserOptions: { ecmaVersion: 6 },
}, },
{ {
code: "typeof foo === `str${somethingElse}`", code: "typeof foo === `str${somethingElse}`",
parserOptions: { ecmaVersion: 6 } 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'", message: "Typeof comparisons should be to string literals.",
options: [{ requireStringLiterals: true }] type: "Identifier",
}, },
], ],
invalid: [ },
{
code: "undefined === typeof foo",
options: [{ requireStringLiterals: true }],
errors: [
{ {
code: "typeof foo === 'strnig'", message: "Typeof comparisons should be to string literals.",
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] type: "Identifier",
}, },
],
},
{
code: "undefined == typeof foo",
options: [{ requireStringLiterals: true }],
errors: [
{ {
code: "'strnig' === typeof foo", message: "Typeof comparisons should be to string literals.",
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] type: "Identifier",
}, },
],
},
{
code: "typeof foo === `undefined${foo}`",
options: [{ requireStringLiterals: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
{ {
code: "if (typeof bar === 'umdefined') {}", message: "Typeof comparisons should be to string literals.",
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] type: "TemplateLiteral",
}, },
],
},
{
code: "typeof foo === `${string}`",
options: [{ requireStringLiterals: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
{ {
code: "typeof foo !== 'strnig'", message: "Typeof comparisons should be to string literals.",
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] type: "TemplateLiteral",
},
{
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" }]
}, },
],
},
// Babel-specific test cases. // Babel-specific test cases.
{ {
code: "typeof foo === 'bgiint'", code: "typeof foo === 'bgiint'",
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] errors: [
}, { message: "Invalid typeof comparison value.", type: "Literal" },
{ ],
code: "'bignit' === typeof foo", },
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }] {
}, code: "'bignit' === typeof foo",
] errors: [
{ message: "Invalid typeof comparison value.", type: "Literal" },
],
},
],
}); });