1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | "use strict";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const rule = require("../../rules/semi"),
|
14 | RuleTester = require("../RuleTester");
|
15 |
|
16 | const ruleTester = new RuleTester();
|
17 |
|
18 | ruleTester.run("semi", rule, {
|
19 | valid: [
|
20 | "var x = 5;",
|
21 | "var x =5, y;",
|
22 | "foo();",
|
23 | "x = foo();",
|
24 | "setTimeout(function() {foo = \"bar\"; });",
|
25 | "setTimeout(function() {foo = \"bar\";});",
|
26 | "for (var a in b){}",
|
27 | "for (var i;;){}",
|
28 | "if (true) {}\n;[global, extended].forEach(function(){});",
|
29 | "throw new Error('foo');",
|
30 | { code: "throw new Error('foo')", options: ["never"] },
|
31 | { code: "var x = 5", options: ["never"] },
|
32 | { code: "var x =5, y", options: ["never"] },
|
33 | { code: "foo()", options: ["never"] },
|
34 | { code: "debugger", options: ["never"] },
|
35 | { code: "for (var a in b){}", options: ["never"] },
|
36 | { code: "for (var i;;){}", options: ["never"] },
|
37 | { code: "x = foo()", options: ["never"] },
|
38 | { code: "if (true) {}\n;[global, extended].forEach(function(){})", options: ["never"] },
|
39 | { code: "(function bar() {})\n;(function foo(){})", options: ["never"] },
|
40 | { code: ";/foo/.test('bar')", options: ["never"] },
|
41 | { code: ";+5", options: ["never"] },
|
42 | { code: ";-foo()", options: ["never"] },
|
43 | { code: "a++\nb++", options: ["never"] },
|
44 | { code: "a++; b++", options: ["never"] },
|
45 | { code: "for (let thing of {}) {\n console.log(thing);\n}", parserOptions: { ecmaVersion: 6 } },
|
46 | { code: "do{}while(true)", options: ["never"] },
|
47 | { code: "do{}while(true);", options: ["always"] },
|
48 |
|
49 | { code: "if (foo) { bar() }", options: ["always", { omitLastInOneLineBlock: true }] },
|
50 | { code: "if (foo) { bar(); baz() }", options: ["always", { omitLastInOneLineBlock: true }] },
|
51 |
|
52 |
|
53 |
|
54 | { code: "class A { a() {} b() {} }", parserOptions: { ecmaVersion: 6 } },
|
55 | { code: "var A = class { a() {} b() {} };", parserOptions: { ecmaVersion: 6 } },
|
56 |
|
57 | { code: "import theDefault, { named1, named2 } from 'src/mylib';", parserOptions: { sourceType: "module" } },
|
58 | { code: "import theDefault, { named1, named2 } from 'src/mylib'", options: ["never"], parserOptions: { sourceType: "module" } },
|
59 |
|
60 |
|
61 | { code: "export * from 'foo';", parserOptions: { sourceType: "module" } },
|
62 | { code: "export { foo } from 'foo';", parserOptions: { sourceType: "module" } },
|
63 | { code: "export { foo };", parserOptions: { sourceType: "module" } },
|
64 | { code: "export var foo;", parserOptions: { sourceType: "module" } },
|
65 | { code: "export function foo () { }", parserOptions: { sourceType: "module" } },
|
66 | { code: "export function* foo () { }", parserOptions: { sourceType: "module" } },
|
67 | { code: "export class Foo { }", parserOptions: { sourceType: "module" } },
|
68 | { code: "export let foo;", parserOptions: { sourceType: "module" } },
|
69 | { code: "export const FOO = 42;", parserOptions: { sourceType: "module" } },
|
70 | { code: "export default function() { }", parserOptions: { sourceType: "module" } },
|
71 | { code: "export default function* () { }", parserOptions: { sourceType: "module" } },
|
72 | { code: "export default class { }", parserOptions: { sourceType: "module" } },
|
73 | { code: "export default foo || bar;", parserOptions: { sourceType: "module" } },
|
74 | { code: "export default (foo) => foo.bar();", parserOptions: { sourceType: "module" } },
|
75 | { code: "export default foo = 42;", parserOptions: { sourceType: "module" } },
|
76 | { code: "export default foo += 42;", parserOptions: { sourceType: "module" } },
|
77 |
|
78 |
|
79 | { code: "export * from 'foo'", options: ["never"], parserOptions: { sourceType: "module" } },
|
80 | { code: "export { foo } from 'foo'", options: ["never"], parserOptions: { sourceType: "module" } },
|
81 | { code: "export { foo }", options: ["never"], parserOptions: { sourceType: "module" } },
|
82 | { code: "export var foo", options: ["never"], parserOptions: { sourceType: "module" } },
|
83 | { code: "export function foo () { }", options: ["never"], parserOptions: { sourceType: "module" } },
|
84 | { code: "export function* foo () { }", options: ["never"], parserOptions: { sourceType: "module" } },
|
85 | { code: "export class Foo { }", options: ["never"], parserOptions: { sourceType: "module" } },
|
86 | { code: "export let foo", options: ["never"], parserOptions: { sourceType: "module" } },
|
87 | { code: "export const FOO = 42", options: ["never"], parserOptions: { sourceType: "module" } },
|
88 | { code: "export default function() { }", options: ["never"], parserOptions: { sourceType: "module" } },
|
89 | { code: "export default function* () { }", options: ["never"], parserOptions: { sourceType: "module" } },
|
90 | { code: "export default class { }", options: ["never"], parserOptions: { sourceType: "module" } },
|
91 | { code: "export default foo || bar", options: ["never"], parserOptions: { sourceType: "module" } },
|
92 | { code: "export default (foo) => foo.bar()", options: ["never"], parserOptions: { sourceType: "module" } },
|
93 | { code: "export default foo = 42", options: ["never"], parserOptions: { sourceType: "module" } },
|
94 | { code: "export default foo += 42", options: ["never"], parserOptions: { sourceType: "module" } },
|
95 | { code: "++\nfoo;", options: ["always"] },
|
96 | { code: "var a = b;\n+ c", options: ["never"] },
|
97 |
|
98 |
|
99 | { code: "var a = b;\n/foo/.test(c)", options: ["never"] },
|
100 | { code: "var a = b;\n`foo`", options: ["never"], parserOptions: { ecmaVersion: 6 } },
|
101 |
|
102 |
|
103 | "class Foo { bar = 'example'; }",
|
104 | "class Foo { static bar = 'example'; }",
|
105 | { code: "async function foo() { for await (let thing of {}) { console.log(thing); } }", parserOptions: { ecmaVersion: 6 } },
|
106 | { code: "class Foo { bar = () => {}; }", options: ["always", { omitLastInOneLineBlock: true }] },
|
107 |
|
108 |
|
109 | { code: "class Foo { bar = 'example' }", options: ["never"] },
|
110 | { code: "class Foo { static bar = 'example' }", options: ["never"] },
|
111 | { code: "class Foo { bar = () => {} }", options: ["never"] },
|
112 | ],
|
113 | invalid: [
|
114 | { code: "import * as utils from './utils'", output: "import * as utils from './utils';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration", column: 33 }] },
|
115 | { code: "import { square, diag } from 'lib'", output: "import { square, diag } from 'lib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] },
|
116 | { code: "import { default as foo } from 'lib'", output: "import { default as foo } from 'lib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] },
|
117 | { code: "import 'src/mylib'", output: "import 'src/mylib';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ImportDeclaration" }] },
|
118 | { 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" }] },
|
119 | { code: "function foo() { return [] }", output: "function foo() { return []; }", errors: [{ message: "Missing semicolon.", type: "ReturnStatement" }] },
|
120 | { code: "while(true) { break }", output: "while(true) { break; }", errors: [{ message: "Missing semicolon.", type: "BreakStatement" }] },
|
121 | { code: "while(true) { continue }", output: "while(true) { continue; }", errors: [{ message: "Missing semicolon.", type: "ContinueStatement" }] },
|
122 | { code: "let x = 5", output: "let x = 5;", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
123 | { code: "var x = 5", output: "var x = 5;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
124 | { code: "var x = 5, y", output: "var x = 5, y;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
125 | { code: "debugger", output: "debugger;", errors: [{ message: "Missing semicolon.", type: "DebuggerStatement" }] },
|
126 | { code: "foo()", output: "foo();", errors: [{ message: "Missing semicolon.", type: "ExpressionStatement" }] },
|
127 | { code: "var x = 5, y", output: "var x = 5, y;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
128 | { code: "for (var a in b) var i ", output: "for (var a in b) var i; ", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
129 | { code: "for (;;){var i}", output: "for (;;){var i;}", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
130 | { code: "for (;;) var i ", output: "for (;;) var i; ", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
131 | { code: "for (var j;;) {var i}", output: "for (var j;;) {var i;}", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
132 | { code: "var foo = {\n bar: baz\n}", output: "var foo = {\n bar: baz\n};", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration", line: 3 }] },
|
133 | { code: "var foo\nvar bar;", output: "var foo;\nvar bar;", errors: [{ message: "Missing semicolon.", type: "VariableDeclaration", line: 1 }] },
|
134 | { code: "throw new Error('foo')", output: "throw new Error('foo');", errors: [{ message: "Missing semicolon.", type: "ThrowStatement", line: 1 }] },
|
135 | { code: "do{}while(true)", output: "do{}while(true);", errors: [{ message: "Missing semicolon.", type: "DoWhileStatement", line: 1 }] },
|
136 |
|
137 | { code: "throw new Error('foo');", output: "throw new Error('foo')", options: ["never"], errors: [{ message: "Extra semicolon.", type: "ThrowStatement", column: 23 }] },
|
138 | { code: "function foo() { return []; }", output: "function foo() { return [] }", options: ["never"], errors: [{ message: "Extra semicolon.", type: "ReturnStatement" }] },
|
139 | { code: "while(true) { break; }", output: "while(true) { break }", options: ["never"], errors: [{ message: "Extra semicolon.", type: "BreakStatement" }] },
|
140 | { code: "while(true) { continue; }", output: "while(true) { continue }", options: ["never"], errors: [{ message: "Extra semicolon.", type: "ContinueStatement" }] },
|
141 | { code: "let x = 5;", output: "let x = 5", parserOptions: { ecmaVersion: 6 }, options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
142 | { code: "var x = 5;", output: "var x = 5", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
143 | { code: "var x = 5, y;", output: "var x = 5, y", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
144 | { code: "debugger;", output: "debugger", options: ["never"], errors: [{ message: "Extra semicolon.", type: "DebuggerStatement" }] },
|
145 | { code: "foo();", output: "foo()", options: ["never"], errors: [{ message: "Extra semicolon.", type: "ExpressionStatement" }] },
|
146 | { code: "var x = 5, y;", output: "var x = 5, y", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
147 | { code: "for (var a in b) var i; ", output: "for (var a in b) var i ", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
148 | { code: "for (;;){var i;}", output: "for (;;){var i}", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
149 | { code: "for (;;) var i; ", output: "for (;;) var i ", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
150 | { code: "for (var j;;) {var i;}", output: "for (var j;;) {var i}", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
151 | { code: "var foo = {\n bar: baz\n};", output: "var foo = {\n bar: baz\n}", options: ["never"], errors: [{ message: "Extra semicolon.", type: "VariableDeclaration", line: 3 }] },
|
152 | { 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" }] },
|
153 | { code: "do{}while(true);", output: "do{}while(true)", options: ["never"], errors: [{ message: "Extra semicolon.", type: "DoWhileStatement", line: 1 }] },
|
154 |
|
155 | { code: "if (foo) { bar()\n }", options: ["always", { omitLastInOneLineBlock: true }], errors: [{ message: "Missing semicolon." }] },
|
156 | { code: "if (foo) {\n bar() }", options: ["always", { omitLastInOneLineBlock: true }], errors: [{ message: "Missing semicolon." }] },
|
157 | { code: "if (foo) {\n bar(); baz() }", options: ["always", { omitLastInOneLineBlock: true }], errors: [{ message: "Missing semicolon." }] },
|
158 | { code: "if (foo) { bar(); }", options: ["always", { omitLastInOneLineBlock: true }], errors: [{ message: "Extra semicolon." }] },
|
159 |
|
160 |
|
161 |
|
162 | { code: "export * from 'foo'", output: "export * from 'foo';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportAllDeclaration" }] },
|
163 | { code: "export { foo } from 'foo'", output: "export { foo } from 'foo';", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportNamedDeclaration" }] },
|
164 | { code: "export { foo }", output: "export { foo };", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportNamedDeclaration" }] },
|
165 | { code: "export var foo", output: "export var foo;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
166 | { code: "export let foo", output: "export let foo;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
167 | { code: "export const FOO = 42", output: "export const FOO = 42;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "VariableDeclaration" }] },
|
168 | { code: "export default foo || bar", output: "export default foo || bar;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportDefaultDeclaration" }] },
|
169 | { code: "export default (foo) => foo.bar()", output: "export default (foo) => foo.bar();", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportDefaultDeclaration" }] },
|
170 | { code: "export default foo = 42", output: "export default foo = 42;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportDefaultDeclaration" }] },
|
171 | { code: "export default foo += 42", output: "export default foo += 42;", parserOptions: { sourceType: "module" }, errors: [{ message: "Missing semicolon.", type: "ExportDefaultDeclaration" }] },
|
172 |
|
173 |
|
174 | { code: "export * from 'foo';", output: "export * from 'foo'", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportAllDeclaration" }] },
|
175 | { code: "export { foo } from 'foo';", output: "export { foo } from 'foo'", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportNamedDeclaration" }] },
|
176 | { code: "export { foo };", output: "export { foo }", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportNamedDeclaration" }] },
|
177 | { code: "export var foo;", output: "export var foo", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
178 | { code: "export let foo;", output: "export let foo", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
179 | { code: "export const FOO = 42;", output: "export const FOO = 42", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "VariableDeclaration" }] },
|
180 | { code: "export default foo || bar;", output: "export default foo || bar", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] },
|
181 | { code: "export default (foo) => foo.bar();", output: "export default (foo) => foo.bar()", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] },
|
182 | { code: "export default foo = 42;", output: "export default foo = 42", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] },
|
183 | { code: "export default foo += 42;", output: "export default foo += 42", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] },
|
184 | { code: "a;\n++b", output: "a\n++b", options: ["never"], errors: [{ message: "Extra semicolon." }] },
|
185 |
|
186 |
|
187 | { code: "class Foo { bar = 'example' }", errors: [{ message: "Missing semicolon." }] },
|
188 | { code: "class Foo { static bar = 'example' }", errors: [{ message: "Missing semicolon." }] },
|
189 | {
|
190 | code: "class Foo { bar = () => {} }",
|
191 | options: ["always", { omitLastInOneLineBlock: true }],
|
192 | errors: [{ message: "Missing semicolon." }]
|
193 | },
|
194 |
|
195 |
|
196 | { code: "class Foo { bar = 'example'; }", options: ["never"], errors: [{ message: "Extra semicolon." }] },
|
197 | { code: "class Foo { static bar = 'example'; }", options: ["never"], errors: [{ message: "Extra semicolon." }] },
|
198 | {
|
199 | code: "class Foo { bar = () => {}; }",
|
200 | options: ["never"],
|
201 | errors: [{ message: "Extra semicolon." }]
|
202 | },
|
203 | ]
|
204 | });
|