Skip to content

Commit

Permalink
test(linter): port eslint tests to no-unused-expressions (#7611)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Dec 3, 2024
1 parent eb825ed commit 6dd71c6
Show file tree
Hide file tree
Showing 2 changed files with 359 additions and 0 deletions.
103 changes: 103 additions & 0 deletions crates/oxc_linter/src/rules/typescript/no_unused_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,57 @@ fn test() {
use crate::tester::Tester;

let pass = vec![
// https://github.com/eslint/eslint/blob/946ae00457265eb298eb169d6d48ca7ec71b9eef/tests/lib/rules/no-unused-expressions.js#L21
("function f(){}", None),
("a = b", None),
("new a", None),
("{}", None),
("f(); g()", None),
("i++", None),
("a()", None),
("a && a()", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
("a() || (b = c)", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
("a ? b() : c()", Some(serde_json::json!([{ "allowTernary": true }]))),
(
"a ? b() || (c = d) : e()",
Some(serde_json::json!([{ "allowShortCircuit": true, "allowTernary": true }])),
),
("delete foo.bar", None),
("void new C", None),
(r#""use strict";"#, None),
(r#""directive one"; "directive two"; f();"#, None),
(r#"function foo() {"use strict"; return true; }"#, None),
(r#"var foo = () => {"use strict"; return true; }"#, None), // { "ecmaVersion": 6 },
(r#"function foo() {"directive one"; "directive two"; f(); }"#, None),
(r#"function foo() { var foo = "use strict"; return true; }"#, None),
("function* foo(){ yield 0; }", None), // { "ecmaVersion": 6 },
("async function foo() { await 5; }", None), // { "ecmaVersion": 8 },
("async function foo() { await foo.bar; }", None), // { "ecmaVersion": 8 },
(
"async function foo() { bar && await baz; }",
Some(serde_json::json!([{ "allowShortCircuit": true }])),
), // { "ecmaVersion": 8 },
(
"async function foo() { foo ? await bar : await baz; }",
Some(serde_json::json!([{ "allowTernary": true }])),
), // { "ecmaVersion": 8 },
(
"tag`tagged template literal`",
Some(serde_json::json!([{ "allowTaggedTemplates": true }])),
), // { "ecmaVersion": 6 },
(
"shouldNotBeAffectedByAllowTemplateTagsOption()",
Some(serde_json::json!([{ "allowTaggedTemplates": true }])),
), // { "ecmaVersion": 6 },
(r#"import("foo")"#, None), // { "ecmaVersion": 11 },
(r#"func?.("foo")"#, None), // { "ecmaVersion": 11 },
(r#"obj?.foo("bar")"#, None), // { "ecmaVersion": 11 },
("<div />", None), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
("<></>", None), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
("var partial = <div />", None), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
("var partial = <div />", Some(serde_json::json!([{ "enforceForJSX": true }]))), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
("var partial = <></>", Some(serde_json::json!([{ "enforceForJSX": true }]))), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
// https://github.com/typescript-eslint/typescript-eslint/blob/32a7a7061abba5bbf1403230526514768d3e2760/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts#L29
(
"
test.age?.toLocaleString();
Expand Down Expand Up @@ -285,6 +336,58 @@ fn test() {
];

let fail = vec![
// https://github.com/eslint/eslint/blob/946ae00457265eb298eb169d6d48ca7ec71b9eef/tests/lib/rules/no-unused-expressions.js#L111
("0", None),
("a", None),
("f(), 0", None),
("{0}", None),
("[]", None),
("a && b();", None),
("a() || false", None),
("a || (b = c)", None),
("a ? b() || (c = d) : e", None),
("`untagged template literal`", None), // { "ecmaVersion": 6 },
("tag`tagged template literal`", None), // { "ecmaVersion": 6 },
("a && b()", Some(serde_json::json!([{ "allowTernary": true }]))),
("a ? b() : c()", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
("a || b", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
("a() && b", Some(serde_json::json!([{ "allowShortCircuit": true }]))),
("a ? b : 0", Some(serde_json::json!([{ "allowTernary": true }]))),
("a ? b : c()", Some(serde_json::json!([{ "allowTernary": true }]))),
("foo.bar;", None),
("!a", None),
("+a", None),
(r#""directive one"; f(); "directive two";"#, None),
(r#"function foo() {"directive one"; f(); "directive two"; }"#, None),
(r#"if (0) { "not a directive"; f(); }"#, None),
(r#"function foo() { var foo = true; "use strict"; }"#, None),
(r#"var foo = () => { var foo = true; "use strict"; }"#, None), // { "ecmaVersion": 6 },
(
"`untagged template literal`",
Some(serde_json::json!([{ "allowTaggedTemplates": true }])),
), // { "ecmaVersion": 6 },
(
"`untagged template literal`",
Some(serde_json::json!([{ "allowTaggedTemplates": false }])),
), // { "ecmaVersion": 6 },
(
"tag`tagged template literal`",
Some(serde_json::json!([{ "allowTaggedTemplates": false }])),
), // { "ecmaVersion": 6 },
("obj?.foo", None), // { "ecmaVersion": 2020 },
("obj?.foo.bar", None), // { "ecmaVersion": 2020 },
("obj?.foo().bar", None), // { "ecmaVersion": 2020 },
("<div />", Some(serde_json::json!([{ "enforceForJSX": true }]))), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
("<></>", Some(serde_json::json!([{ "enforceForJSX": true }]))), // { "parserOptions": { "ecmaFeatures": { "jsx": true } } },
("class C { static { 'use strict'; } }", None), // { "ecmaVersion": 2022 },
(
"class C { static {
'foo'
'bar'
} }",
None,
), // { "ecmaVersion": 2022 }
// https://github.com/typescript-eslint/typescript-eslint/blob/32a7a7061abba5bbf1403230526514768d3e2760/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts#L91
(
"
if (0) 0;
Expand Down
256 changes: 256 additions & 0 deletions crates/oxc_linter/src/snapshots/typescript_no_unused_expressions.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,262 @@
---
source: crates/oxc_linter/src/tester.rs
---
typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
10
· ─
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a
· ─
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1f(), 0
· ──────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:2]
1 │ {0}
· ─
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1 │ []
· ──
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a && b();
· ─────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a() || false
· ────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a || (b = c)
· ────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a ? b() || (c = d) : e
· ──────────────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1`untagged template literal`
· ───────────────────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1tag`tagged template literal`
· ────────────────────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a && b()
· ────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a ? b() : c()
· ─────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a || b
· ──────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a() && b
· ────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a ? b : 0
· ─────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1a ? b : c()
· ───────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1foo.bar;
· ────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1!a
· ──
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1+a
· ──
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:23]
1"directive one"; f(); "directive two";
· ────────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:39]
1function foo() {"directive one"; f(); "directive two"; }
· ────────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:10]
1if (0) { "not a directive"; f(); }
· ──────────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:34]
1function foo() { var foo = true; "use strict"; }
· ─────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:35]
1var foo = () => { var foo = true; "use strict"; }
· ─────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1`untagged template literal`
· ───────────────────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1`untagged template literal`
· ───────────────────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1tag`tagged template literal`
· ────────────────────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1obj?.foo
· ────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1obj?.foo.bar
· ────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1obj?.foo().bar
· ──────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1<div />
· ───────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:1]
1<></>
· ─────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:1:20]
1class C { static { 'use strict'; } }
· ─────────────
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:2:4]
1class C { static {
2 │ 'foo'
· ─────
3 │ 'bar'
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:3:4]
2'foo'
3'bar'
· ─────
4 │ } }
╰────
help: Consider removing this expression

typescript-eslint(no-unused-expressions): Disallow unused expressions
╭─[no_unused_expressions.tsx:2:11]
1
Expand Down

0 comments on commit 6dd71c6

Please sign in to comment.