Skip to content

Commit 6dd71c6

Browse files
committed
test(linter): port eslint tests to no-unused-expressions (#7611)
1 parent eb825ed commit 6dd71c6

2 files changed

Lines changed: 359 additions & 0 deletions

File tree

crates/oxc_linter/src/rules/typescript/no_unused_expressions.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,57 @@ fn test() {
191191
use crate::tester::Tester;
192192

193193
let pass = vec![
194+
// https://github.com/eslint/eslint/blob/946ae00457265eb298eb169d6d48ca7ec71b9eef/tests/lib/rules/no-unused-expressions.js#L21
195+
("function f(){}", None),
196+
("a = b", None),
197+
("new a", None),
198+
("{}", None),
199+
("f(); g()", None),
200+
("i++", None),
201+
("a()", None),
202+
("a && a()", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
203+
("a() || (b = c)", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
204+
("a ? b() : c()", Some(serde_json::json!([{ "allowTernary": true }]))),
205+
(
206+
"a ? b() || (c = d) : e()",
207+
Some(serde_json::json!([{ "allowShortCircuit": true, "allowTernary": true }])),
208+
),
209+
("delete foo.bar", None),
210+
("void new C", None),
211+
(r#""use strict";"#, None),
212+
(r#""directive one"; "directive two"; f();"#, None),
213+
(r#"function foo() {"use strict"; return true; }"#, None),
214+
(r#"var foo = () => {"use strict"; return true; }"#, None), // { "ecmaVersion": 6 },
215+
(r#"function foo() {"directive one"; "directive two"; f(); }"#, None),
216+
(r#"function foo() { var foo = "use strict"; return true; }"#, None),
217+
("function* foo(){ yield 0; }", None), // { "ecmaVersion": 6 },
218+
("async function foo() { await 5; }", None), // { "ecmaVersion": 8 },
219+
("async function foo() { await foo.bar; }", None), // { "ecmaVersion": 8 },
220+
(
221+
"async function foo() { bar && await baz; }",
222+
Some(serde_json::json!([{ "allowShortCircuit": true }])),
223+
), // { "ecmaVersion": 8 },
224+
(
225+
"async function foo() { foo ? await bar : await baz; }",
226+
Some(serde_json::json!([{ "allowTernary": true }])),
227+
), // { "ecmaVersion": 8 },
228+
(
229+
"tag`tagged template literal`",
230+
Some(serde_json::json!([{ "allowTaggedTemplates": true }])),
231+
), // { "ecmaVersion": 6 },
232+
(
233+
"shouldNotBeAffectedByAllowTemplateTagsOption()",
234+
Some(serde_json::json!([{ "allowTaggedTemplates": true }])),
235+
), // { "ecmaVersion": 6 },
236+
(r#"import("foo")"#, None), // { "ecmaVersion": 11 },
237+
(r#"func?.("foo")"#, None), // { "ecmaVersion": 11 },
238+
(r#"obj?.foo("bar")"#, None), // { "ecmaVersion": 11 },
239+
("<div />", None), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
240+
("<></>", None), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
241+
("var partial = <div />", None), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
242+
("var partial = <div />", Some(serde_json::json!([{ "enforceForJSX": true }]))), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
243+
("var partial = <></>", Some(serde_json::json!([{ "enforceForJSX": true }]))), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
244+
// https://github.com/typescript-eslint/typescript-eslint/blob/32a7a7061abba5bbf1403230526514768d3e2760/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts#L29
194245
(
195246
"
196247
test.age?.toLocaleString();
@@ -285,6 +336,58 @@ fn test() {
285336
];
286337

287338
let fail = vec![
339+
// https://github.com/eslint/eslint/blob/946ae00457265eb298eb169d6d48ca7ec71b9eef/tests/lib/rules/no-unused-expressions.js#L111
340+
("0", None),
341+
("a", None),
342+
("f(), 0", None),
343+
("{0}", None),
344+
("[]", None),
345+
("a && b();", None),
346+
("a() || false", None),
347+
("a || (b = c)", None),
348+
("a ? b() || (c = d) : e", None),
349+
("`untagged template literal`", None), // { "ecmaVersion": 6 },
350+
("tag`tagged template literal`", None), // { "ecmaVersion": 6 },
351+
("a && b()", Some(serde_json::json!([{ "allowTernary": true }]))),
352+
("a ? b() : c()", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
353+
("a || b", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
354+
("a() && b", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
355+
("a ? b : 0", Some(serde_json::json!([{ "allowTernary": true }]))),
356+
("a ? b : c()", Some(serde_json::json!([{ "allowTernary": true }]))),
357+
("foo.bar;", None),
358+
("!a", None),
359+
("+a", None),
360+
(r#""directive one"; f(); "directive two";"#, None),
361+
(r#"function foo() {"directive one"; f(); "directive two"; }"#, None),
362+
(r#"if (0) { "not a directive"; f(); }"#, None),
363+
(r#"function foo() { var foo = true; "use strict"; }"#, None),
364+
(r#"var foo = () => { var foo = true; "use strict"; }"#, None), // { "ecmaVersion": 6 },
365+
(
366+
"`untagged template literal`",
367+
Some(serde_json::json!([{ "allowTaggedTemplates": true }])),
368+
), // { "ecmaVersion": 6 },
369+
(
370+
"`untagged template literal`",
371+
Some(serde_json::json!([{ "allowTaggedTemplates": false }])),
372+
), // { "ecmaVersion": 6 },
373+
(
374+
"tag`tagged template literal`",
375+
Some(serde_json::json!([{ "allowTaggedTemplates": false }])),
376+
), // { "ecmaVersion": 6 },
377+
("obj?.foo", None), // { "ecmaVersion": 2020 },
378+
("obj?.foo.bar", None), // { "ecmaVersion": 2020 },
379+
("obj?.foo().bar", None), // { "ecmaVersion": 2020 },
380+
("<div />", Some(serde_json::json!([{ "enforceForJSX": true }]))), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
381+
("<></>", Some(serde_json::json!([{ "enforceForJSX": true }]))), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
382+
("class C { static { 'use strict'; } }", None), // { "ecmaVersion": 2022 },
383+
(
384+
"class C { static {
385+
'foo'
386+
'bar'
387+
} }",
388+
None,
389+
), // { "ecmaVersion": 2022 }
390+
// https://github.com/typescript-eslint/typescript-eslint/blob/32a7a7061abba5bbf1403230526514768d3e2760/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts#L91
288391
(
289392
"
290393
if (0) 0;

crates/oxc_linter/src/snapshots/typescript_no_unused_expressions.snap

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,262 @@
11
---
22
source: crates/oxc_linter/src/tester.rs
33
---
4+
typescript-eslint(no-unused-expressions): Disallow unused expressions
5+
╭─[no_unused_expressions.tsx:1:1]
6+
10
7+
· ─
8+
╰────
9+
help: Consider removing this expression
10+
11+
typescript-eslint(no-unused-expressions): Disallow unused expressions
12+
╭─[no_unused_expressions.tsx:1:1]
13+
1a
14+
· ─
15+
╰────
16+
help: Consider removing this expression
17+
18+
typescript-eslint(no-unused-expressions): Disallow unused expressions
19+
╭─[no_unused_expressions.tsx:1:1]
20+
1f(), 0
21+
· ──────
22+
╰────
23+
help: Consider removing this expression
24+
25+
typescript-eslint(no-unused-expressions): Disallow unused expressions
26+
╭─[no_unused_expressions.tsx:1:2]
27+
1 │ {0}
28+
· ─
29+
╰────
30+
help: Consider removing this expression
31+
32+
typescript-eslint(no-unused-expressions): Disallow unused expressions
33+
╭─[no_unused_expressions.tsx:1:1]
34+
1 │ []
35+
· ──
36+
╰────
37+
help: Consider removing this expression
38+
39+
typescript-eslint(no-unused-expressions): Disallow unused expressions
40+
╭─[no_unused_expressions.tsx:1:1]
41+
1a && b();
42+
· ─────────
43+
╰────
44+
help: Consider removing this expression
45+
46+
typescript-eslint(no-unused-expressions): Disallow unused expressions
47+
╭─[no_unused_expressions.tsx:1:1]
48+
1a() || false
49+
· ────────────
50+
╰────
51+
help: Consider removing this expression
52+
53+
typescript-eslint(no-unused-expressions): Disallow unused expressions
54+
╭─[no_unused_expressions.tsx:1:1]
55+
1a || (b = c)
56+
· ────────────
57+
╰────
58+
help: Consider removing this expression
59+
60+
typescript-eslint(no-unused-expressions): Disallow unused expressions
61+
╭─[no_unused_expressions.tsx:1:1]
62+
1a ? b() || (c = d) : e
63+
· ──────────────────────
64+
╰────
65+
help: Consider removing this expression
66+
67+
typescript-eslint(no-unused-expressions): Disallow unused expressions
68+
╭─[no_unused_expressions.tsx:1:1]
69+
1`untagged template literal`
70+
· ───────────────────────────
71+
╰────
72+
help: Consider removing this expression
73+
74+
typescript-eslint(no-unused-expressions): Disallow unused expressions
75+
╭─[no_unused_expressions.tsx:1:1]
76+
1tag`tagged template literal`
77+
· ────────────────────────────
78+
╰────
79+
help: Consider removing this expression
80+
81+
typescript-eslint(no-unused-expressions): Disallow unused expressions
82+
╭─[no_unused_expressions.tsx:1:1]
83+
1a && b()
84+
· ────────
85+
╰────
86+
help: Consider removing this expression
87+
88+
typescript-eslint(no-unused-expressions): Disallow unused expressions
89+
╭─[no_unused_expressions.tsx:1:1]
90+
1a ? b() : c()
91+
· ─────────────
92+
╰────
93+
help: Consider removing this expression
94+
95+
typescript-eslint(no-unused-expressions): Disallow unused expressions
96+
╭─[no_unused_expressions.tsx:1:1]
97+
1a || b
98+
· ──────
99+
╰────
100+
help: Consider removing this expression
101+
102+
typescript-eslint(no-unused-expressions): Disallow unused expressions
103+
╭─[no_unused_expressions.tsx:1:1]
104+
1a() && b
105+
· ────────
106+
╰────
107+
help: Consider removing this expression
108+
109+
typescript-eslint(no-unused-expressions): Disallow unused expressions
110+
╭─[no_unused_expressions.tsx:1:1]
111+
1a ? b : 0
112+
· ─────────
113+
╰────
114+
help: Consider removing this expression
115+
116+
typescript-eslint(no-unused-expressions): Disallow unused expressions
117+
╭─[no_unused_expressions.tsx:1:1]
118+
1a ? b : c()
119+
· ───────────
120+
╰────
121+
help: Consider removing this expression
122+
123+
typescript-eslint(no-unused-expressions): Disallow unused expressions
124+
╭─[no_unused_expressions.tsx:1:1]
125+
1foo.bar;
126+
· ────────
127+
╰────
128+
help: Consider removing this expression
129+
130+
typescript-eslint(no-unused-expressions): Disallow unused expressions
131+
╭─[no_unused_expressions.tsx:1:1]
132+
1!a
133+
· ──
134+
╰────
135+
help: Consider removing this expression
136+
137+
typescript-eslint(no-unused-expressions): Disallow unused expressions
138+
╭─[no_unused_expressions.tsx:1:1]
139+
1+a
140+
· ──
141+
╰────
142+
help: Consider removing this expression
143+
144+
typescript-eslint(no-unused-expressions): Disallow unused expressions
145+
╭─[no_unused_expressions.tsx:1:23]
146+
1"directive one"; f(); "directive two";
147+
· ────────────────
148+
╰────
149+
help: Consider removing this expression
150+
151+
typescript-eslint(no-unused-expressions): Disallow unused expressions
152+
╭─[no_unused_expressions.tsx:1:39]
153+
1function foo() {"directive one"; f(); "directive two"; }
154+
· ────────────────
155+
╰────
156+
help: Consider removing this expression
157+
158+
typescript-eslint(no-unused-expressions): Disallow unused expressions
159+
╭─[no_unused_expressions.tsx:1:10]
160+
1if (0) { "not a directive"; f(); }
161+
· ──────────────────
162+
╰────
163+
help: Consider removing this expression
164+
165+
typescript-eslint(no-unused-expressions): Disallow unused expressions
166+
╭─[no_unused_expressions.tsx:1:34]
167+
1function foo() { var foo = true; "use strict"; }
168+
· ─────────────
169+
╰────
170+
help: Consider removing this expression
171+
172+
typescript-eslint(no-unused-expressions): Disallow unused expressions
173+
╭─[no_unused_expressions.tsx:1:35]
174+
1var foo = () => { var foo = true; "use strict"; }
175+
· ─────────────
176+
╰────
177+
help: Consider removing this expression
178+
179+
typescript-eslint(no-unused-expressions): Disallow unused expressions
180+
╭─[no_unused_expressions.tsx:1:1]
181+
1`untagged template literal`
182+
· ───────────────────────────
183+
╰────
184+
help: Consider removing this expression
185+
186+
typescript-eslint(no-unused-expressions): Disallow unused expressions
187+
╭─[no_unused_expressions.tsx:1:1]
188+
1`untagged template literal`
189+
· ───────────────────────────
190+
╰────
191+
help: Consider removing this expression
192+
193+
typescript-eslint(no-unused-expressions): Disallow unused expressions
194+
╭─[no_unused_expressions.tsx:1:1]
195+
1tag`tagged template literal`
196+
· ────────────────────────────
197+
╰────
198+
help: Consider removing this expression
199+
200+
typescript-eslint(no-unused-expressions): Disallow unused expressions
201+
╭─[no_unused_expressions.tsx:1:1]
202+
1obj?.foo
203+
· ────────
204+
╰────
205+
help: Consider removing this expression
206+
207+
typescript-eslint(no-unused-expressions): Disallow unused expressions
208+
╭─[no_unused_expressions.tsx:1:1]
209+
1obj?.foo.bar
210+
· ────────────
211+
╰────
212+
help: Consider removing this expression
213+
214+
typescript-eslint(no-unused-expressions): Disallow unused expressions
215+
╭─[no_unused_expressions.tsx:1:1]
216+
1obj?.foo().bar
217+
· ──────────────
218+
╰────
219+
help: Consider removing this expression
220+
221+
typescript-eslint(no-unused-expressions): Disallow unused expressions
222+
╭─[no_unused_expressions.tsx:1:1]
223+
1<div />
224+
· ───────
225+
╰────
226+
help: Consider removing this expression
227+
228+
typescript-eslint(no-unused-expressions): Disallow unused expressions
229+
╭─[no_unused_expressions.tsx:1:1]
230+
1<></>
231+
· ─────
232+
╰────
233+
help: Consider removing this expression
234+
235+
typescript-eslint(no-unused-expressions): Disallow unused expressions
236+
╭─[no_unused_expressions.tsx:1:20]
237+
1class C { static { 'use strict'; } }
238+
· ─────────────
239+
╰────
240+
help: Consider removing this expression
241+
242+
typescript-eslint(no-unused-expressions): Disallow unused expressions
243+
╭─[no_unused_expressions.tsx:2:4]
244+
1class C { static {
245+
2 │ 'foo'
246+
· ─────
247+
3 │ 'bar'
248+
╰────
249+
help: Consider removing this expression
250+
251+
typescript-eslint(no-unused-expressions): Disallow unused expressions
252+
╭─[no_unused_expressions.tsx:3:4]
253+
2'foo'
254+
3'bar'
255+
· ─────
256+
4 │ } }
257+
╰────
258+
help: Consider removing this expression
259+
4260
typescript-eslint(no-unused-expressions): Disallow unused expressions
5261
╭─[no_unused_expressions.tsx:2:11]
6262
1

0 commit comments

Comments
 (0)