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"
},
"main": "lib/index.js",
"files": [
"lib"
],
"peerDependencies": {
"@babel/core": ">=7.2.0",
"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",
"url": "https://github.com/nicolo-ribaudo"
},
"main": "src/index.js",
"main": "lib/index.js",
"devDependencies": {
"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",
"version": "5.3.0",
"description": "an eslint rule plugin companion to babel-eslint",
"main": "index.js",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/babel/eslint-plugin-babel.git"

View File

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

View File

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

View File

@ -10,7 +10,7 @@
//------------------------------------------------------------------------------
const cloneDeep = require("lodash.clonedeep");
const rule = require("../../rules/no-invalid-this"),
const rule = require("../../src/rules/no-invalid-this"),
RuleTester = require("../helpers/RuleTester");
//------------------------------------------------------------------------------
@ -32,7 +32,7 @@ function NORMAL(pattern) {
* @returns {void}
*/
function USE_STRICT(pattern) {
pattern.code = "\"use strict\"; " + pattern.code;
pattern.code = '"use strict"; ' + pattern.code;
}
/**
@ -64,7 +64,6 @@ function MODULES(pattern) {
* @returns {Object[]} Test patterns.
*/
function extractPatterns(patterns, type) {
// Clone and apply the pattern environment.
const patternsList = patterns.map(function(pattern) {
return pattern[type].map(function(applyCondition) {
@ -89,44 +88,43 @@ function extractPatterns(patterns, type) {
return Array.prototype.concat.apply([], patternsList);
}
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const errors = [
{message: "Unexpected 'this'.", type: "ThisExpression"},
{message: "Unexpected 'this'.", type: "ThisExpression"}
{ message: "Unexpected 'this'.", type: "ThisExpression" },
{ message: "Unexpected 'this'.", type: "ThisExpression" },
];
const patterns = [
// Global.
{
code: "console.log(this); z(x => console.log(x, this));",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "console.log(this); z(x => console.log(x, this));",
parserOptions: {
ecmaVersion: 6,
ecmaFeatures: {globalReturn: true}
ecmaFeatures: { globalReturn: true },
},
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
// IIFE.
{
code: "(function() { console.log(this); z(x => console.log(x, this)); })();",
code:
"(function() { console.log(this); z(x => console.log(x, this)); })();",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
// Just functions.
@ -135,91 +133,102 @@ const patterns = [
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "function foo() { \"use strict\"; console.log(this); z(x => console.log(x, this)); }",
code:
'function foo() { "use strict"; console.log(this); z(x => console.log(x, this)); }',
parserOptions: { ecmaVersion: 6 },
errors,
valid: [],
invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "return function() { console.log(this); z(x => console.log(x, this)); };",
code:
"return function() { console.log(this); z(x => console.log(x, this)); };",
parserOptions: {
ecmaVersion: 6,
ecmaFeatures: {globalReturn: true}
ecmaFeatures: { globalReturn: true },
},
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT] // modules cannot return on global.
invalid: [USE_STRICT, IMPLIED_STRICT], // modules cannot return on global.
},
{
code: "var foo = (function() { console.log(this); z(x => console.log(x, this)); }).bar(obj);",
code:
"var foo = (function() { console.log(this); z(x => console.log(x, this)); }).bar(obj);",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
// Functions in methods.
{
code: "var obj = {foo: function() { function foo() { console.log(this); z(x => console.log(x, this)); } foo(); }};",
code:
"var obj = {foo: function() { function foo() { console.log(this); z(x => console.log(x, this)); } foo(); }};",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "var obj = {foo() { function foo() { console.log(this); z(x => console.log(x, this)); } foo(); }};",
code:
"var obj = {foo() { function foo() { console.log(this); z(x => console.log(x, this)); } foo(); }};",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "var obj = {foo: function() { return function() { console.log(this); z(x => console.log(x, this)); }; }};",
code:
"var obj = {foo: function() { return function() { console.log(this); z(x => console.log(x, this)); }; }};",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "var obj = {foo: function() { \"use strict\"; return function() { console.log(this); z(x => console.log(x, this)); }; }};",
code:
'var obj = {foo: function() { "use strict"; return function() { console.log(this); z(x => console.log(x, this)); }; }};',
parserOptions: { ecmaVersion: 6 },
errors,
valid: [],
invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "obj.foo = function() { return function() { console.log(this); z(x => console.log(x, this)); }; };",
code:
"obj.foo = function() { return function() { console.log(this); z(x => console.log(x, this)); }; };",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "obj.foo = function() { \"use strict\"; return function() { console.log(this); z(x => console.log(x, this)); }; };",
code:
'obj.foo = function() { "use strict"; return function() { console.log(this); z(x => console.log(x, this)); }; };',
parserOptions: { ecmaVersion: 6 },
errors,
valid: [],
invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "class A { foo() { return function() { console.log(this); z(x => console.log(x, this)); }; } }",
code:
"class A { foo() { return function() { console.log(this); z(x => console.log(x, this)); }; } }",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [],
invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
},
// Class Static methods.
{
code: "class A {static foo() { console.log(this); z(x => console.log(x, this)); }};",
code:
"class A {static foo() { console.log(this); z(x => console.log(x, this)); }};",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
// Constructors.
@ -227,292 +236,335 @@ const patterns = [
code: "function Foo() { console.log(this); z(x => console.log(x, this)); }",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "var Foo = function Foo() { console.log(this); z(x => console.log(x, this)); };",
code:
"var Foo = function Foo() { console.log(this); z(x => console.log(x, this)); };",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "class A {constructor() { console.log(this); z(x => console.log(x, this)); }};",
code:
"class A {constructor() { console.log(this); z(x => console.log(x, this)); }};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
// On a property.
{
code: "var obj = {foo: function() { console.log(this); z(x => console.log(x, this)); }};",
code:
"var obj = {foo: function() { console.log(this); z(x => console.log(x, this)); }};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "var obj = {foo() { console.log(this); z(x => console.log(x, this)); }};",
code:
"var obj = {foo() { console.log(this); z(x => console.log(x, this)); }};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "var obj = {foo: foo || function() { console.log(this); z(x => console.log(x, this)); }};",
code:
"var obj = {foo: foo || function() { console.log(this); z(x => console.log(x, this)); }};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "var obj = {foo: hasNative ? foo : function() { console.log(this); z(x => console.log(x, this)); }};",
code:
"var obj = {foo: hasNative ? foo : function() { console.log(this); z(x => console.log(x, this)); }};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "var obj = {foo: (function() { return function() { console.log(this); z(x => console.log(x, this)); }; })()};",
code:
"var obj = {foo: (function() { return function() { console.log(this); z(x => console.log(x, this)); }; })()};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "Object.defineProperty(obj, \"foo\", {value: function() { console.log(this); z(x => console.log(x, this)); }})",
code:
'Object.defineProperty(obj, "foo", {value: function() { console.log(this); z(x => console.log(x, this)); }})',
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "Object.defineProperties(obj, {foo: {value: function() { console.log(this); z(x => console.log(x, this)); }}})",
code:
"Object.defineProperties(obj, {foo: {value: function() { console.log(this); z(x => console.log(x, this)); }}})",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
// Assigns to a property.
{
code: "obj.foo = function() { console.log(this); z(x => console.log(x, this)); };",
code:
"obj.foo = function() { console.log(this); z(x => console.log(x, this)); };",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "obj.foo = foo || function() { console.log(this); z(x => console.log(x, this)); };",
code:
"obj.foo = foo || function() { console.log(this); z(x => console.log(x, this)); };",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "obj.foo = foo ? bar : function() { console.log(this); z(x => console.log(x, this)); };",
code:
"obj.foo = foo ? bar : function() { console.log(this); z(x => console.log(x, this)); };",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "obj.foo = (function() { return function() { console.log(this); z(x => console.log(x, this)); }; })();",
code:
"obj.foo = (function() { return function() { console.log(this); z(x => console.log(x, this)); }; })();",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
// Class Instance Methods.
{
code: "class A {foo() { console.log(this); z(x => console.log(x, this)); }};",
code:
"class A {foo() { console.log(this); z(x => console.log(x, this)); }};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
// Bind/Call/Apply
{
code: "var foo = function() { console.log(this); z(x => console.log(x, this)); }.bind(obj);",
code:
"var foo = function() { console.log(this); z(x => console.log(x, this)); }.bind(obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "var foo = function() { console.log(this); z(x => console.log(x, this)); }.bind(null);",
code:
"var foo = function() { console.log(this); z(x => console.log(x, this)); }.bind(null);",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "(function() { console.log(this); z(x => console.log(x, this)); }).call(obj);",
code:
"(function() { console.log(this); z(x => console.log(x, this)); }).call(obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "(function() { console.log(this); z(x => console.log(x, this)); }).call(undefined);",
code:
"(function() { console.log(this); z(x => console.log(x, this)); }).call(undefined);",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "(function() { console.log(this); z(x => console.log(x, this)); }).apply(obj);",
code:
"(function() { console.log(this); z(x => console.log(x, this)); }).apply(obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "(function() { console.log(this); z(x => console.log(x, this)); }).apply(void 0);",
code:
"(function() { console.log(this); z(x => console.log(x, this)); }).apply(void 0);",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "Reflect.apply(function() { console.log(this); z(x => console.log(x, this)); }, obj, []);",
code:
"Reflect.apply(function() { console.log(this); z(x => console.log(x, this)); }, obj, []);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
// Array methods.
{
code: "Array.from([], function() { console.log(this); z(x => console.log(x, this)); });",
code:
"Array.from([], function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "foo.every(function() { console.log(this); z(x => console.log(x, this)); });",
code:
"foo.every(function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "foo.filter(function() { console.log(this); z(x => console.log(x, this)); });",
code:
"foo.filter(function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "foo.find(function() { console.log(this); z(x => console.log(x, this)); });",
code:
"foo.find(function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "foo.findIndex(function() { console.log(this); z(x => console.log(x, this)); });",
code:
"foo.findIndex(function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "foo.forEach(function() { console.log(this); z(x => console.log(x, this)); });",
code:
"foo.forEach(function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "foo.map(function() { console.log(this); z(x => console.log(x, this)); });",
code:
"foo.map(function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "foo.some(function() { console.log(this); z(x => console.log(x, this)); });",
code:
"foo.some(function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "Array.from([], function() { console.log(this); z(x => console.log(x, this)); }, obj);",
code:
"Array.from([], function() { console.log(this); z(x => console.log(x, this)); }, obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "foo.every(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
code:
"foo.every(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "foo.filter(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
code:
"foo.filter(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "foo.find(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
code:
"foo.find(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "foo.findIndex(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
code:
"foo.findIndex(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "foo.forEach(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
code:
"foo.forEach(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "foo.map(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
code:
"foo.map(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "foo.some(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
code:
"foo.some(function() { console.log(this); z(x => console.log(x, this)); }, obj);",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "foo.forEach(function() { console.log(this); z(x => console.log(x, this)); }, null);",
code:
"foo.forEach(function() { console.log(this); z(x => console.log(x, this)); }, null);",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
// @this tag.
{
code: "/** @this Obj */ function foo() { console.log(this); z(x => console.log(x, this)); }",
code:
"/** @this Obj */ function foo() { console.log(this); z(x => console.log(x, this)); }",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "/**\n * @returns {void}\n * @this Obj\n */\nfunction foo() { console.log(this); z(x => console.log(x, this)); }",
code:
"/**\n * @returns {void}\n * @this Obj\n */\nfunction foo() { console.log(this); z(x => console.log(x, this)); }",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "/** @returns {void} */ function foo() { console.log(this); z(x => console.log(x, this)); }",
code:
"/** @returns {void} */ function foo() { console.log(this); z(x => console.log(x, this)); }",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "/** @this Obj */ foo(function() { console.log(this); z(x => console.log(x, this)); });",
code:
"/** @this Obj */ foo(function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "foo(/* @this Obj */ function() { console.log(this); z(x => console.log(x, this)); });",
code:
"foo(/* @this Obj */ function() { console.log(this); z(x => console.log(x, this)); });",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
// https://github.com/eslint/eslint/issues/3254
@ -521,69 +573,78 @@ const patterns = [
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
// https://github.com/eslint/eslint/issues/3287
{
code: "function foo() { /** @this Obj*/ return function bar() { console.log(this); z(x => console.log(x, this)); }; }",
code:
"function foo() { /** @this Obj*/ return function bar() { console.log(this); z(x => console.log(x, this)); }; }",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
// https://github.com/eslint/eslint/issues/6824
{
code: "var Ctor = function() { console.log(this); z(x => console.log(x, this)); }",
code:
"var Ctor = function() { console.log(this); z(x => console.log(x, this)); }",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "var func = function() { console.log(this); z(x => console.log(x, this)); }",
code:
"var func = function() { console.log(this); z(x => console.log(x, this)); }",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "Ctor = function() { console.log(this); z(x => console.log(x, this)); }",
code:
"Ctor = function() { console.log(this); z(x => console.log(x, this)); }",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "func = function() { console.log(this); z(x => console.log(x, this)); }",
code:
"func = function() { console.log(this); z(x => console.log(x, this)); }",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "function foo(Ctor = function() { console.log(this); z(x => console.log(x, this)); }) {}",
code:
"function foo(Ctor = function() { console.log(this); z(x => console.log(x, this)); }) {}",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "function foo(func = function() { console.log(this); z(x => console.log(x, this)); }) {}",
code:
"function foo(func = function() { console.log(this); z(x => console.log(x, this)); }) {}",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
{
code: "[obj.method = function() { console.log(this); z(x => console.log(x, this)); }] = a",
code:
"[obj.method = function() { console.log(this); z(x => console.log(x, this)); }] = a",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "[func = function() { console.log(this); z(x => console.log(x, this)); }] = a",
code:
"[func = function() { console.log(this); z(x => console.log(x, this)); }] = a",
parserOptions: { ecmaVersion: 6 },
errors,
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
},
// babel/no-invalid-this
@ -593,14 +654,14 @@ const patterns = [
code: "class A {a = this.b;};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "class A {a = () => {return this.b;};};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
// Class Private Instance Properties.
@ -608,14 +669,14 @@ const patterns = [
code: "class A {#a = this.b;};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
{
code: "class A {#a = () => {return this.b;};};",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
invalid: [],
},
];
@ -623,5 +684,5 @@ const ruleTester = new RuleTester();
ruleTester.run("no-invalid-this", rule, {
valid: extractPatterns(patterns, "valid"),
invalid: extractPatterns(patterns, "invalid")
invalid: extractPatterns(patterns, "invalid"),
});

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../rules/no-unused-expressions"),
const rule = require("../../src/rules/no-unused-expressions"),
RuleTester = require("../helpers/RuleTester");
//------------------------------------------------------------------------------
@ -31,46 +31,52 @@ ruleTester.run("no-unused-expressions", rule, {
{ code: "a && a()", options: [{ allowShortCircuit: true }] },
{ code: "a() || (b = c)", options: [{ allowShortCircuit: true }] },
{ code: "a ? b() : c()", options: [{ allowTernary: true }] },
{ code: "a ? b() || (c = d) : e()", options: [{ allowShortCircuit: true, allowTernary: true }] },
{
code: "a ? b() || (c = d) : e()",
options: [{ allowShortCircuit: true, allowTernary: true }],
},
"delete foo.bar",
"void new C",
"\"use strict\";",
"\"directive one\"; \"directive two\"; f();",
"function foo() {\"use strict\"; return true; }",
{ code: "var foo = () => {\"use strict\"; return true; }", parserOptions: { ecmaVersion: 6 } },
"function foo() {\"directive one\"; \"directive two\"; f(); }",
"function foo() { var foo = \"use strict\"; return true; }",
'"use strict";',
'"directive one"; "directive two"; f();',
'function foo() {"use strict"; return true; }',
{
code: 'var foo = () => {"use strict"; return true; }',
parserOptions: { ecmaVersion: 6 },
},
'function foo() {"directive one"; "directive two"; f(); }',
'function foo() { var foo = "use strict"; return true; }',
{
code: "function* foo(){ yield 0; }",
parserOptions: { ecmaVersion: 6 }
parserOptions: { ecmaVersion: 6 },
},
{
code: "async function foo() { await 5; }",
parserOptions: { ecmaVersion: 8 }
parserOptions: { ecmaVersion: 8 },
},
{
code: "async function foo() { await foo.bar; }",
parserOptions: { ecmaVersion: 8 }
parserOptions: { ecmaVersion: 8 },
},
{
code: "async function foo() { bar && await baz; }",
options: [{ allowShortCircuit: true }],
parserOptions: { ecmaVersion: 8 }
parserOptions: { ecmaVersion: 8 },
},
{
code: "async function foo() { foo ? await bar : await baz; }",
options: [{ allowTernary: true }],
parserOptions: { ecmaVersion: 8 }
parserOptions: { ecmaVersion: 8 },
},
{
code: "tag`tagged template literal`",
options: [{ allowTaggedTemplates: true }],
parserOptions: { ecmaVersion: 6 }
parserOptions: { ecmaVersion: 6 },
},
{
code: "shouldNotBeAffectedByAllowTemplateTagsOption()",
options: [{ allowTaggedTemplates: true }],
parserOptions: { ecmaVersion: 6 }
parserOptions: { ecmaVersion: 6 },
},
// Babel-specific test cases.
@ -81,64 +87,304 @@ ruleTester.run("no-unused-expressions", rule, {
"let a = do { if (foo) { if (foo.bar) { foo.bar; } } }",
"let a = do { if (foo) { if (foo.bar) { foo.bar; } else if (foo.baz) { foo.baz; } } }",
"foo.bar?.();",
],
invalid: [
{ code: "0", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "a", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "f(), 0", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "{0}", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "[]", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "a && b();", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "a() || false", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "a || (b = c)", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "a ? b() || (c = d) : e", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{
code: "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."]
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."]
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: "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."]
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."]
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."]
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" }] },
]
{
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,205 +1,347 @@
var rule = require('../../rules/quotes'),
var rule = require("../../src/rules/quotes"),
RuleTester = require("../helpers/RuleTester");
var ruleTester = new RuleTester();
ruleTester.run('babel/quotes', rule, {
ruleTester.run("babel/quotes", rule, {
valid: [
"var foo = \"bar\";",
'var foo = "bar";',
{ 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: ["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 = <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>;", 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 = <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>;",
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 } } },
{
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
{ code: "var foo = `back\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = `back\rtick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = `back\u2028tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = `back\u2029tick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
{
code: "var foo = `back\ntick`;",
options: ["single"],
parserOptions: { ecmaVersion: 6 },
},
{
code: "var foo = `back\rtick`;",
options: ["single"],
parserOptions: { ecmaVersion: 6 },
},
{
code: "var foo = `back\u2028tick`;",
options: ["single"],
parserOptions: { ecmaVersion: 6 },
},
{
code: "var foo = `back\u2029tick`;",
options: ["single"],
parserOptions: { ecmaVersion: 6 },
},
{
code: "var foo = `back\\\\\ntick`;", // 2 backslashes followed by a newline
options: ["single"],
parserOptions: { ecmaVersion: 6 }
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 },
},
{ code: "var foo = `back\\\\\\\\\ntick`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = `\n`;", options: ["single"], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = `back${x}tick`;", options: ["double"], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = tag`backtick`;", options: ["double"], parserOptions: { ecmaVersion: 6 } },
// Backticks are also okay if allowTemplateLiterals
{ code: "var foo = `bar 'foo' baz` + 'bar';", options: ["single", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = `bar 'foo' baz` + \"bar\";", options: ["double", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = `bar 'foo' baz` + `bar`;", options: ["backtick", { allowTemplateLiterals: true }], parserOptions: { ecmaVersion: 6 } },
{
code: "var foo = `bar 'foo' baz` + 'bar';",
options: ["single", { allowTemplateLiterals: true }],
parserOptions: { ecmaVersion: 6 },
},
{
code: "var foo = `bar 'foo' baz` + \"bar\";",
options: ["double", { allowTemplateLiterals: true }],
parserOptions: { ecmaVersion: 6 },
},
{
code: "var foo = `bar 'foo' baz` + `bar`;",
options: ["backtick", { allowTemplateLiterals: true }],
parserOptions: { ecmaVersion: 6 },
},
// `backtick` should not warn the directive prologues.
{ code: "\"use strict\"; var foo = `backtick`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
{ code: "\"use strict\"; 'use strong'; \"use asm\"; var foo = `backtick`;", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo() { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
{ code: "(function() { 'use strict'; 'use strong'; 'use asm'; var foo = `backtick`; })();", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
{ code: "(() => { \"use strict\"; \"use strong\"; \"use asm\"; var foo = `backtick`; })();", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
{
code: '"use strict"; var foo = `backtick`;',
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
{
code: '"use strict"; \'use strong\'; "use asm"; var foo = `backtick`;',
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
{
code:
'function foo() { "use strict"; "use strong"; "use asm"; var foo = `backtick`; }',
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
{
code:
"(function() { 'use strict'; 'use strong'; 'use asm'; var foo = `backtick`; })();",
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
{
code:
'(() => { "use strict"; "use strong"; "use asm"; var foo = `backtick`; })();',
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
// `backtick` should not warn import/export sources.
{ code: "import \"a\"; import 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } },
{ code: "import a from \"a\"; import b from 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } },
{ code: "export * from \"a\"; export * from 'b';", options: ["backtick"], parserOptions: { sourceType: "module" } },
{
code: "import \"a\"; import 'b';",
options: ["backtick"],
parserOptions: { sourceType: "module" },
},
{
code: "import a from \"a\"; import b from 'b';",
options: ["backtick"],
parserOptions: { sourceType: "module" },
},
{
code: "export * from \"a\"; export * from 'b';",
options: ["backtick"],
parserOptions: { sourceType: "module" },
},
// `backtick` should not warn property/method names (not computed).
{ code: "var obj = {\"key0\": 0, 'key1': 1};", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
{ code: "class Foo { 'bar'(){} }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
{ code: "class Foo { static ''(){} }", options: ["backtick"], parserOptions: { ecmaVersion: 6 } },
{
code: "var obj = {\"key0\": 0, 'key1': 1};",
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
{
code: "class Foo { 'bar'(){} }",
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
{
code: "class Foo { static ''(){} }",
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
},
// Babel
'<>foo</>;',
{ code: '<>foo</>;', options: ['single'] },
{ code: '<>foo</>;', options: ['double'] },
'<><div /><div /></>;',
{ code: '<><div /><div /></>;', options: ['single'] },
{ code: '<><div /><div /></>;', options: ['double'] },
"<>foo</>;",
{ code: "<>foo</>;", options: ["single"] },
{ code: "<>foo</>;", options: ["double"] },
"<><div /><div /></>;",
{ code: "<><div /><div /></>;", options: ["single"] },
{ code: "<><div /><div /></>;", options: ["double"] },
],
invalid: [
{
code: "var foo = 'bar';",
output: "var foo = \"bar\";",
errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
output: 'var foo = "bar";',
errors: [{ message: "Strings must use doublequote.", type: "Literal" }],
},
{
code: "var foo = \"bar\";",
code: 'var foo = "bar";',
output: "var foo = 'bar';",
options: ["single"],
errors: [{ message: "Strings must use singlequote.", type: "Literal" }]
errors: [{ message: "Strings must use singlequote.", type: "Literal" }],
},
{
code: "var foo = `bar`;",
output: "var foo = 'bar';",
options: ["single"],
parserOptions: {
ecmaVersion: 6
ecmaVersion: 6,
},
errors: [{ message: "Strings must use singlequote.", type: "TemplateLiteral" }]
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" }]
output: 'var foo = "don\'t";',
errors: [{ message: "Strings must use doublequote.", type: "Literal" }],
},
{
code: "var msg = \"Plugin '\" + name + \"' not found\"",
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 }
]
{
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\";",
output: 'var foo = "bar";',
options: ["double"],
errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
errors: [{ message: "Strings must use doublequote.", type: "Literal" }],
},
{
code: "var foo = `bar`;",
output: "var foo = \"bar\";",
output: 'var foo = "bar";',
options: ["double"],
parserOptions: {
ecmaVersion: 6
ecmaVersion: 6,
},
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
errors: [
{ message: "Strings must use doublequote.", type: "TemplateLiteral" },
],
},
{
code: "var foo = \"bar\";",
code: 'var foo = "bar";',
output: "var foo = 'bar';",
options: ["single", { avoidEscape: true }],
errors: [{ message: "Strings must use singlequote.", type: "Literal" }]
errors: [{ message: "Strings must use singlequote.", type: "Literal" }],
},
{
code: "var foo = 'bar';",
output: "var foo = \"bar\";",
output: 'var foo = "bar";',
options: ["double", { avoidEscape: true }],
errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
errors: [{ message: "Strings must use doublequote.", type: "Literal" }],
},
{
code: "var foo = '\\\\';",
output: "var foo = \"\\\\\";",
output: 'var foo = "\\\\";',
options: ["double", { avoidEscape: true }],
errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
errors: [{ message: "Strings must use doublequote.", type: "Literal" }],
},
{
code: "var foo = \"bar\";",
code: 'var foo = "bar";',
output: "var foo = 'bar';",
options: ["single", { allowTemplateLiterals: true }],
errors: [{ message: "Strings must use singlequote.", type: "Literal" }]
errors: [{ message: "Strings must use singlequote.", type: "Literal" }],
},
{
code: "var foo = 'bar';",
output: "var foo = \"bar\";",
output: 'var foo = "bar";',
options: ["double", { allowTemplateLiterals: true }],
errors: [{ message: "Strings must use doublequote.", type: "Literal" }]
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" }]
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" }]
errors: [{ message: "Strings must use backtick.", type: "Literal" }],
},
{
code: "var foo = \"bar\";",
code: 'var foo = "bar";',
output: "var foo = `bar`;",
options: ["backtick"],
errors: [{ message: "Strings must use backtick.", type: "Literal" }]
errors: [{ message: "Strings must use backtick.", type: "Literal" }],
},
{
code: "var foo = \"bar\";",
code: 'var foo = "bar";',
output: "var foo = `bar`;",
options: ["backtick", { avoidEscape: true }],
errors: [{ message: "Strings must use backtick.", type: "Literal" }]
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" }]
errors: [{ message: "Strings must use backtick.", type: "Literal" }],
},
// "use strict" is *not* a directive prologue in these statements so is subject to the rule
{
code: "var foo = `backtick`; \"use strict\";",
code: 'var foo = `backtick`; "use strict";',
output: "var foo = `backtick`; `use strict`;",
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use backtick.", type: "Literal" }]
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`; }",
options: ["backtick"],
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`; }",
options: ["backtick"],
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.
@ -210,8 +352,8 @@ ruleTester.run('babel/quotes', rule, {
parserOptions: { ecmaVersion: 6 },
errors: [
{ 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'](){} }",
@ -220,37 +362,31 @@ ruleTester.run('babel/quotes', rule, {
parserOptions: { ecmaVersion: 6 },
errors: [
{ 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
{
code: "<div blah={\"blah\"} />",
code: '<div blah={"blah"} />',
output: "<div blah={'blah'} />",
options: ["single"],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{ message: "Strings must use singlequote.", type: "Literal" }
]
errors: [{ message: "Strings must use singlequote.", type: "Literal" }],
},
{
code: "<div blah={'blah'} />",
output: "<div blah={\"blah\"} />",
output: '<div blah={"blah"} />',
options: ["double"],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{ message: "Strings must use doublequote.", type: "Literal" }
]
errors: [{ message: "Strings must use doublequote.", type: "Literal" }],
},
{
code: "<div blah={'blah'} />",
output: "<div blah={`blah`} />",
options: ["backtick"],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [
{ message: "Strings must use backtick.", type: "Literal" }
]
errors: [{ message: "Strings must use backtick.", type: "Literal" }],
},
// https://github.com/eslint/eslint/issues/7610
@ -258,63 +394,88 @@ ruleTester.run('babel/quotes', rule, {
code: "`use strict`;",
output: null,
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 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
errors: [
{ message: "Strings must use doublequote.", type: "TemplateLiteral" },
],
},
{
code: "foo = function() { `use strict`; foo(); }",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
errors: [
{ message: "Strings must use doublequote.", type: "TemplateLiteral" },
],
},
{
code: "() => { `use strict`; foo(); }",
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
errors: [
{ message: "Strings must use doublequote.", type: "TemplateLiteral" },
],
},
{
code: "() => { foo(); `use strict`; }",
output: "() => { foo(); \"use strict\"; }",
output: '() => { foo(); "use strict"; }',
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\";",
output: 'foo(); "use strict";',
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
errors: [
{ message: "Strings must use doublequote.", type: "TemplateLiteral" },
],
},
// https://github.com/eslint/eslint/issues/7646
{
code: "var foo = `foo\\nbar`;",
output: "var foo = \"foo\\nbar\";",
output: 'var foo = "foo\\nbar";',
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
errors: [
{ message: "Strings must use doublequote.", type: "TemplateLiteral" },
],
},
{
code: "var foo = `foo\\\nbar`;", // 1 backslash followed by a newline
output: "var foo = \"foo\\\nbar\";",
output: 'var foo = "foo\\\nbar";',
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
errors: [
{ message: "Strings must use doublequote.", type: "TemplateLiteral" },
],
},
{
code: "var foo = `foo\\\\\\\nbar`;", // 3 backslashes followed by a newline
output: "var foo = \"foo\\\\\\\nbar\";",
output: 'var foo = "foo\\\\\\\nbar";',
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral" }]
errors: [
{ message: "Strings must use doublequote.", type: "TemplateLiteral" },
],
},
{
code: "````",
output: "\"\"``",
output: '""``',
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Strings must use doublequote.", type: "TemplateLiteral", line: 1, column: 1 }]
}
errors: [
{
message: "Strings must use doublequote.",
type: "TemplateLiteral",
line: 1,
column: 1,
},
],
},
],
});

View File

@ -10,7 +10,7 @@
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../rules/semi"),
const rule = require("../../src/rules/semi"),
RuleTester = require("../helpers/RuleTester");
const ruleTester = new RuleTester();
@ -21,8 +21,8 @@ ruleTester.run("semi", rule, {
"var x =5, y;",
"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 i;;){}",
"if (true) {}\n;[global, extended].forEach(function(){});",
@ -35,75 +35,201 @@ ruleTester.run("semi", rule, {
{ code: "for (var a in b){}", options: ["never"] },
{ code: "for (var i;;){}", options: ["never"] },
{ code: "x = foo()", options: ["never"] },
{ code: "if (true) {}\n;[global, extended].forEach(function(){})", options: ["never"] },
{
code: "if (true) {}\n;[global, extended].forEach(function(){})",
options: ["never"],
},
{ code: "(function bar() {})\n;(function foo(){})", options: ["never"] },
{ code: ";/foo/.test('bar')", options: ["never"] },
{ code: ";+5", options: ["never"] },
{ code: ";-foo()", options: ["never"] },
{ code: "a++\nb++", options: ["never"] },
{ code: "a++; b++", options: ["never"] },
{ code: "for (let thing of {}) {\n console.log(thing);\n}", parserOptions: { ecmaVersion: 6 } },
{
code: "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 } },
{
code: "var A = class { a() {} b() {} };",
parserOptions: { ecmaVersion: 6 },
},
{ code: "import theDefault, { named1, named2 } from 'src/mylib';", parserOptions: { sourceType: "module" } },
{ code: "import theDefault, { named1, named2 } from 'src/mylib'", options: ["never"], parserOptions: { sourceType: "module" } },
{
code: "import theDefault, { named1, named2 } from 'src/mylib';",
parserOptions: { sourceType: "module" },
},
{
code: "import theDefault, { named1, named2 } from 'src/mylib'",
options: ["never"],
parserOptions: { sourceType: "module" },
},
// exports, "always"
{ code: "export * from 'foo';", parserOptions: { sourceType: "module" } },
{ code: "export { foo } from 'foo';", parserOptions: { sourceType: "module" } },
{
code: "export { foo } 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 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" } },
{
code: "export default function() { }",
parserOptions: { sourceType: "module" },
},
{
code: "export default function* () { }",
parserOptions: { sourceType: "module" },
},
{
code: "export default class { }",
parserOptions: { sourceType: "module" },
},
{
code: "export default foo || bar;",
parserOptions: { sourceType: "module" },
},
{
code: "export default (foo) => foo.bar();",
parserOptions: { sourceType: "module" },
},
{
code: "export default foo = 42;",
parserOptions: { sourceType: "module" },
},
{
code: "export default foo += 42;",
parserOptions: { sourceType: "module" },
},
// exports, "never"
{ code: "export * from 'foo'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export { foo } from 'foo'", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export { foo }", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export var foo", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export function foo () { }", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export function* foo () { }", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export class Foo { }", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export let foo", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export const FOO = 42", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export default function() { }", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export default function* () { }", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export default class { }", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export default foo || bar", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export default (foo) => foo.bar()", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export default foo = 42", options: ["never"], parserOptions: { sourceType: "module" } },
{ code: "export default foo += 42", options: ["never"], parserOptions: { sourceType: "module" } },
{
code: "export * from 'foo'",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export { foo } from 'foo'",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export { foo }",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export var foo",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export function foo () { }",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export function* foo () { }",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export class Foo { }",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export let foo",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export const FOO = 42",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export default function() { }",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export default function* () { }",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export default class { }",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export default foo || bar",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export default (foo) => foo.bar()",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export default foo = 42",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{
code: "export default foo += 42",
options: ["never"],
parserOptions: { sourceType: "module" },
},
{ code: "++\nfoo;", options: ["always"] },
{ code: "var a = b;\n+ c", options: ["never"] },
// https://github.com/eslint/eslint/issues/7782
{ code: "var a = b;\n/foo/.test(c)", options: ["never"] },
{ code: "var a = b;\n`foo`", options: ["never"], parserOptions: { ecmaVersion: 6 } },
{
code: "var a = b;\n`foo`",
options: ["never"],
parserOptions: { ecmaVersion: 6 },
},
// babel
"class Foo { bar = 'example'; }",
"class Foo { static bar = 'example'; }",
{ code: "async function foo() { for await (let thing of {}) { console.log(thing); } }", parserOptions: { ecmaVersion: 6 } },
{ code: "class Foo { bar = () => {}; }", options: ["always", { omitLastInOneLineBlock: true }] },
{
code:
"async function foo() { for await (let thing of {}) { console.log(thing); } }",
parserOptions: { ecmaVersion: 6 },
},
{
code: "class Foo { bar = () => {}; }",
options: ["always", { omitLastInOneLineBlock: true }],
},
// babel, "never"
{ code: "class Foo { bar = 'example' }", options: ["never"] },
@ -111,94 +237,459 @@ ruleTester.run("semi", rule, {
{ code: "class Foo { bar = () => {} }", options: ["never"] },
],
invalid: [
{ code: "import * as utils from './utils'", output: "import * as utils from './utils';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration", column: 33 }] },
{ code: "import { square, diag } from 'lib'", output: "import { square, diag } from 'lib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] },
{ code: "import { default as foo } from 'lib'", output: "import { default as foo } from 'lib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] },
{ code: "import 'src/mylib'", output: "import 'src/mylib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] },
{ code: "import theDefault, { named1, named2 } from 'src/mylib'", output: "import theDefault, { named1, named2 } from 'src/mylib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] },
{ code: "function foo() { return [] }", output: "function foo() { return []; }", errors: [{ message: "Missing semicolon.", type: "ReturnStatement" }] },
{ code: "while(true) { break }", output: "while(true) { break; }", errors: [{ message: "Missing semicolon.", type: "BreakStatement" }] },
{ code: "while(true) { continue }", output: "while(true) { continue; }", errors: [{ message: "Missing semicolon.", type: "ContinueStatement" }] },
{ code: "let x = 5", output: "let x = 5;", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
{ code: "var x = 5", output: "var x = 5;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
{ code: "var x = 5, y", output: "var x = 5, y;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
{ code: "debugger", output: "debugger;", errors: [{ message: "Missing semicolon.", type: "DebuggerStatement" }] },
{ code: "foo()", output: "foo();", errors: [{ message: "Missing semicolon.", type: "ExpressionStatement" }] },
{ code: "var x = 5, y", output: "var x = 5, y;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
{ code: "for (var a in b) var i ", output: "for (var a in b) var i; ", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
{ code: "for (;;){var i}", output: "for (;;){var i;}", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
{ code: "for (;;) var i ", output: "for (;;) var i; ", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
{ code: "for (var j;;) {var i}", output: "for (var j;;) {var i;}", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
{ code: "var foo = {\n bar: baz\n}", output: "var foo = {\n bar: baz\n};", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration", line: 3 }] },
{ code: "var foo\nvar bar;", output: "var foo;\nvar bar;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration", line: 1 }] },
{ code: "throw new Error('foo')", output: "throw new Error('foo');", errors: [{ message: "Missing semicolon.", type: "ThrowStatement", line: 1 }] },
{ code: "do{}while(true)", output: "do{}while(true);", errors: [{ message: "Missing semicolon.", type: "DoWhileStatement", line: 1 }] },
{
code: "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." }] },
{
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" }] },
{
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." }] },
{
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 = '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." }]
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 = '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." }]
errors: [{ message: "Extra semicolon." }],
},
]
],
});

View File

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