fix(linter): getter-return false positive with TypeScript syntax (#2363)

closes #2349
This commit is contained in:
Boshen 2024-02-09 18:22:53 +08:00 committed by GitHub
parent ca77ccc951
commit f49ffb2b63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,7 @@
use oxc_ast::{
ast::{
ArrowExpression, ChainElement, Expression, Function, MemberExpression,
MethodDefinitionKind, ObjectProperty, PropertyKind,
ChainElement, Expression, MemberExpression, MethodDefinitionKind, ObjectProperty,
PropertyKind,
},
AstKind,
};
@ -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 {
@ -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;
@ -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![