Skip to content

Commit

Permalink
fix(linter): do not run getter-return in typescript (#3693)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Jun 15, 2024
1 parent 750a534 commit de0690f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 77 deletions.
50 changes: 7 additions & 43 deletions crates/oxc_linter/src/rules/eslint/getter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ declare_oxc_lint!(

impl Rule for GetterReturn {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
// https://eslint.org/docs/latest/rules/getter-return#handled_by_typescript
if ctx.source_type().is_typescript() {
return;
}
match node.kind() {
AstKind::Function(func) if !func.is_typescript_syntax() => {
self.run_diagnostic(node, ctx, func.span);
Expand Down Expand Up @@ -350,54 +354,12 @@ fn test() {
("foo.defineProperties(null, { bar: { get() {} } });", None),
("foo.create(null, { bar: { get() {} } });", None),
("var foo = { get willThrowSoValid() { throw MyException() } };", None),
("export abstract class Foo { protected abstract get foobar(): number; }", None),
(
"class T {
theme: number;
get type(): number {
switch (theme) {
case 1: return 1;
case 2: return 2;
default: return 3;
}
throw new Error('test')
}
}",
None,
),
(
"class T {
theme: number;
get type(): number {
switch (theme) {
case 1: return 1;
case 2: return 2;
default: return 3;
}
}
}",
None,
),
(
"const originalClearTimeout = targetWindow.clearTimeout;
Object.defineProperty(targetWindow, 'vscodeOriginalClearTimeout', { get: () => originalClearTimeout });
",
None,
),
(
"class T {
get width(): number | undefined {
const val = undefined
if (!val) {
return;
}
return val * val;
}
}",
None,
),
("function fn(): void { console.log('test') }", None),
];

let fail = vec![
Expand Down Expand Up @@ -437,5 +399,7 @@ fn test() {
("(Object?.create)(foo, { bar: { get: function (){} } });", Some(serde_json::json!([{ "allowImplicit": true }]))),
];

Tester::new(GetterReturn::NAME, pass, fail).test_and_snapshot();
Tester::new(GetterReturn::NAME, pass, fail)
.change_rule_path_extension("js")
.test_and_snapshot();
}
68 changes: 34 additions & 34 deletions crates/oxc_linter/src/snapshots/getter_return.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,240 +3,240 @@ source: crates/oxc_linter/src/tester.rs
expression: getter_return
---
eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1var foo = { get bar() {} };
· ─────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:2:6]
╭─[getter_return.js:2:6]
1var foo = { get
2 │ bar () {} };
· ─────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1var foo = { get bar(){if(baz) {return true;}} };
· ──────────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1var foo = { get bar() { ~function () {return true;}} };
· ─────────────────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1var foo = { get bar() { return; } };
· ──────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1var foo = { get bar() {} };
· ─────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1var foo = { get bar() {if (baz) {return;}} };
· ───────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1class foo { get bar(){} }
· ────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:3:4]
╭─[getter_return.js:3:4]
2static get
3bar(){} }
· ────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1class foo { get bar(){ if (baz) { return true; }}}
· ──────────────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1class foo { get bar(){ ~function () { return true; }()}}
· ────────────────────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1class foo { get bar(){} }
· ────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:20]
╭─[getter_return.js:1:20]
1class foo { get bar(){if (baz) {return true;} } }
· ────────────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:42]
╭─[getter_return.js:1:42]
1Object.defineProperty(foo, 'bar', { get: function (){}});
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:42]
╭─[getter_return.js:1:42]
1Object.defineProperty(foo, 'bar', { get: function getfoo (){}});
· ────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:40]
╭─[getter_return.js:1:40]
1Object.defineProperty(foo, 'bar', { get(){} });
· ────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:42]
╭─[getter_return.js:1:42]
1Object.defineProperty(foo, 'bar', { get: () => {}});
· ────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:42]
╭─[getter_return.js:1:42]
1Object.defineProperty(foo, "bar", { get: function (){if(bar) {return true;}}});
· ───────────────────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:42]
╭─[getter_return.js:1:42]
1Object.defineProperty(foo, "bar", { get: function (){ ~function () { return true; }()}});
· ─────────────────────────────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:43]
╭─[getter_return.js:1:43]
1Reflect.defineProperty(foo, 'bar', { get: function (){}});
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:34]
╭─[getter_return.js:1:34]
1Object.create(foo, { bar: { get: function() {} } })
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:32]
╭─[getter_return.js:1:32]
1Object.create(foo, { bar: { get() {} } })
· ─────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:34]
╭─[getter_return.js:1:34]
1Object.create(foo, { bar: { get: () => {} } })
· ────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:44]
╭─[getter_return.js:1:44]
1Object.defineProperties(foo, { bar: { get: function () {}} });
· ──────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:44]
╭─[getter_return.js:1:44]
1Object.defineProperties(foo, { bar: { get: function (){if(bar) {return true;}}}});
· ───────────────────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:44]
╭─[getter_return.js:1:44]
1Object.defineProperties(foo, { bar: { get: function () {~function () { return true; }()}} });
· ─────────────────────────────────────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:42]
╭─[getter_return.js:1:42]
1Object.defineProperty(foo, "bar", { get: function (){}});
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:34]
╭─[getter_return.js:1:34]
1Object.create(foo, { bar: { get: function (){} } });
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:43]
╭─[getter_return.js:1:43]
1Reflect.defineProperty(foo, "bar", { get: function (){}});
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:43]
╭─[getter_return.js:1:43]
1Object?.defineProperty(foo, 'bar', { get: function (){} });
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:45]
╭─[getter_return.js:1:45]
1 │ (Object?.defineProperty)(foo, 'bar', { get: function (){} });
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:43]
╭─[getter_return.js:1:43]
1Object?.defineProperty(foo, 'bar', { get: function (){} });
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:45]
╭─[getter_return.js:1:45]
1 │ (Object?.defineProperty)(foo, 'bar', { get: function (){} });
· ─────────────
╰────
help: Return a value from all code paths in getter.

eslint(getter-return): Expected to always return a value in getter.
╭─[getter_return.tsx:1:37]
╭─[getter_return.js:1:37]
1 │ (Object?.create)(foo, { bar: { get: function (){} } });
· ─────────────
╰────
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ impl Tester {
self
}

/// Change the extension of the path
pub fn change_rule_path_extension(mut self, ext: &str) -> Self {
self.rule_path = self.rule_path.with_extension(ext);
self
}

pub fn with_import_plugin(mut self, yes: bool) -> Self {
self.import_plugin = yes;
self
Expand Down

0 comments on commit de0690f

Please sign in to comment.