Skip to content

Commit

Permalink
fix(linter): getter-return false positive with TypeScript syntax (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored and IWANABETHATGUY committed May 29, 2024
1 parent 74ca20e commit a5a9e56
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions crates/oxc_linter/src/rules/eslint/getter_return.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::{
ast::{
ArrowExpression, ChainElement, Expression, Function, MemberExpression,
MethodDefinitionKind, ObjectProperty, PropertyKind,
ChainElement, Expression, MemberExpression, MethodDefinitionKind, ObjectProperty,
PropertyKind,
},
AstKind,
};
Expand Down Expand Up @@ -53,6 +53,30 @@ declare_oxc_lint!(
nursery
);

impl Rule for GetterReturn {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
AstKind::Function(func) if !func.is_typescript_syntax() => {
self.run_diagnostic(node, ctx, func.span);
}
AstKind::ArrowExpression(expr) => {
self.run_diagnostic(node, ctx, expr.span);
}
_ => {}
}
}

fn from_configuration(value: serde_json::Value) -> Self {
let allow_implicit = value
.get(0)
.and_then(|config| config.get("allowImplicit"))
.and_then(serde_json::Value::as_bool)
.unwrap_or(false);

Self { allow_implicit }
}
}

impl GetterReturn {
fn handle_member_expression<'a>(member_expression: &'a MemberExpression<'a>) -> bool {
for (a, b) in METHODS_TO_WATCH_FOR {
Expand Down Expand Up @@ -263,29 +287,6 @@ enum DefinitelyReturnsOrThrows {
Yes,
}

impl Rule for GetterReturn {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
AstKind::Function(Function { span, .. })
| AstKind::ArrowExpression(ArrowExpression { span, .. }) => {
self.run_diagnostic(node, ctx, *span);
}

_ => {}
}
}

fn from_configuration(value: serde_json::Value) -> Self {
let allow_implicit = value
.get(0)
.and_then(|config| config.get("allowImplicit"))
.and_then(serde_json::Value::as_bool)
.unwrap_or(false);

Self { allow_implicit }
}
}

#[test]
fn test() {
use crate::tester::Tester;
Expand Down Expand Up @@ -363,6 +364,7 @@ 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),
];

let fail = vec![
Expand Down

0 comments on commit a5a9e56

Please sign in to comment.