]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for no-unused-expressions rule. | |
3 | * @author Michael Ficarra | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/no-unused-expressions"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | ||
21 | ruleTester.run("no-unused-expressions", rule, { | |
22 | valid: [ | |
23 | "function f(){}", | |
24 | "a = b", | |
25 | "new a", | |
26 | "{}", | |
27 | "f(); g()", | |
28 | "i++", | |
29 | "a()", | |
30 | { code: "a && a()", options: [{ allowShortCircuit: true }] }, | |
31 | { code: "a() || (b = c)", options: [{ allowShortCircuit: true }] }, | |
32 | { code: "a ? b() : c()", options: [{ allowTernary: true }] }, | |
33 | { code: "a ? b() || (c = d) : e()", options: [{ allowShortCircuit: true, allowTernary: true }] }, | |
34 | "delete foo.bar", | |
35 | "void new C", | |
36 | "\"use strict\";", | |
37 | "\"directive one\"; \"directive two\"; f();", | |
38 | "function foo() {\"use strict\"; return true; }", | |
39 | { code: "var foo = () => {\"use strict\"; return true; }", parserOptions: { ecmaVersion: 6 } }, | |
40 | "function foo() {\"directive one\"; \"directive two\"; f(); }", | |
41 | "function foo() { var foo = \"use strict\"; return true; }", | |
42 | { | |
43 | code: "function* foo(){ yield 0; }", | |
44 | parserOptions: { ecmaVersion: 6 } | |
45 | }, | |
46 | { | |
47 | code: "async function foo() { await 5; }", | |
48 | parserOptions: { ecmaVersion: 8 } | |
49 | }, | |
50 | { | |
51 | code: "async function foo() { await foo.bar; }", | |
52 | parserOptions: { ecmaVersion: 8 } | |
53 | }, | |
54 | { | |
55 | code: "async function foo() { bar && await baz; }", | |
56 | options: [{ allowShortCircuit: true }], | |
57 | parserOptions: { ecmaVersion: 8 } | |
58 | }, | |
59 | { | |
60 | code: "async function foo() { foo ? await bar : await baz; }", | |
61 | options: [{ allowTernary: true }], | |
62 | parserOptions: { ecmaVersion: 8 } | |
63 | }, | |
64 | { | |
65 | code: "tag`tagged template literal`", | |
66 | options: [{ allowTaggedTemplates: true }], | |
67 | parserOptions: { ecmaVersion: 6 } | |
68 | }, | |
69 | { | |
70 | code: "shouldNotBeAffectedByAllowTemplateTagsOption()", | |
71 | options: [{ allowTaggedTemplates: true }], | |
72 | parserOptions: { ecmaVersion: 6 } | |
d3726936 TL |
73 | }, |
74 | { | |
75 | code: "import(\"foo\")", | |
76 | parserOptions: { ecmaVersion: 11 } | |
6f036462 TL |
77 | }, |
78 | { | |
79 | code: "func?.(\"foo\")", | |
80 | parserOptions: { ecmaVersion: 11 } | |
81 | }, | |
82 | { | |
83 | code: "obj?.foo(\"bar\")", | |
84 | parserOptions: { ecmaVersion: 11 } | |
eb39fafa DC |
85 | } |
86 | ], | |
87 | invalid: [ | |
88 | { code: "0", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
89 | { code: "a", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
90 | { code: "f(), 0", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
91 | { code: "{0}", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
92 | { code: "[]", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
93 | { code: "a && b();", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
94 | { code: "a() || false", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
95 | { code: "a || (b = c)", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
96 | { code: "a ? b() || (c = d) : e", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
97 | { | |
98 | code: "`untagged template literal`", | |
99 | parserOptions: { ecmaVersion: 6 }, | |
100 | errors: [{ messageId: "unusedExpression" }] | |
101 | }, | |
102 | { | |
103 | code: "tag`tagged template literal`", | |
104 | parserOptions: { ecmaVersion: 6 }, | |
105 | errors: [{ messageId: "unusedExpression" }] | |
106 | }, | |
107 | { code: "a && b()", options: [{ allowTernary: true }], errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
108 | { code: "a ? b() : c()", options: [{ allowShortCircuit: true }], errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
109 | { code: "a || b", options: [{ allowShortCircuit: true }], errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
110 | { code: "a() && b", options: [{ allowShortCircuit: true }], errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
111 | { code: "a ? b : 0", options: [{ allowTernary: true }], errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
112 | { code: "a ? b : c()", options: [{ allowTernary: true }], errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
113 | { code: "foo.bar;", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
114 | { code: "!a", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
115 | { code: "+a", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
116 | { code: "\"directive one\"; f(); \"directive two\";", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
117 | { code: "function foo() {\"directive one\"; f(); \"directive two\"; }", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
118 | { code: "if (0) { \"not a directive\"; f(); }", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
119 | { code: "function foo() { var foo = true; \"use strict\"; }", errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
120 | { code: "var foo = () => { var foo = true; \"use strict\"; }", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] }, | |
121 | { | |
122 | code: "`untagged template literal`", | |
123 | options: [{ allowTaggedTemplates: true }], | |
124 | parserOptions: { ecmaVersion: 6 }, | |
125 | errors: [{ messageId: "unusedExpression" }] | |
126 | }, | |
127 | { | |
128 | code: "`untagged template literal`", | |
129 | options: [{ allowTaggedTemplates: false }], | |
130 | parserOptions: { ecmaVersion: 6 }, | |
131 | errors: [{ messageId: "unusedExpression" }] | |
132 | }, | |
133 | { | |
134 | code: "tag`tagged template literal`", | |
135 | options: [{ allowTaggedTemplates: false }], | |
136 | parserOptions: { ecmaVersion: 6 }, | |
137 | errors: [{ messageId: "unusedExpression" }] | |
6f036462 TL |
138 | }, |
139 | ||
140 | // Optional chaining | |
141 | { | |
142 | code: "obj?.foo", | |
143 | parserOptions: { ecmaVersion: 2020 }, | |
144 | errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] | |
145 | }, | |
146 | { | |
147 | code: "obj?.foo.bar", | |
148 | parserOptions: { ecmaVersion: 2020 }, | |
149 | errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] | |
150 | }, | |
151 | { | |
152 | code: "obj?.foo().bar", | |
153 | parserOptions: { ecmaVersion: 2020 }, | |
154 | errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }] | |
eb39fafa DC |
155 | } |
156 | ] | |
157 | }); |