mirror of
https://github.com/danbulant/oxc
synced 2026-05-22 21:58:36 +00:00
fix(linter): getter-return false positive with TypeScript syntax (#2363)
closes #2349
This commit is contained in:
parent
ca77ccc951
commit
f49ffb2b63
1 changed files with 27 additions and 25 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
use oxc_ast::{
|
use oxc_ast::{
|
||||||
ast::{
|
ast::{
|
||||||
ArrowExpression, ChainElement, Expression, Function, MemberExpression,
|
ChainElement, Expression, MemberExpression, MethodDefinitionKind, ObjectProperty,
|
||||||
MethodDefinitionKind, ObjectProperty, PropertyKind,
|
PropertyKind,
|
||||||
},
|
},
|
||||||
AstKind,
|
AstKind,
|
||||||
};
|
};
|
||||||
|
|
@ -53,6 +53,30 @@ declare_oxc_lint!(
|
||||||
nursery
|
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 {
|
impl GetterReturn {
|
||||||
fn handle_member_expression<'a>(member_expression: &'a MemberExpression<'a>) -> bool {
|
fn handle_member_expression<'a>(member_expression: &'a MemberExpression<'a>) -> bool {
|
||||||
for (a, b) in METHODS_TO_WATCH_FOR {
|
for (a, b) in METHODS_TO_WATCH_FOR {
|
||||||
|
|
@ -263,29 +287,6 @@ enum DefinitelyReturnsOrThrows {
|
||||||
Yes,
|
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]
|
#[test]
|
||||||
fn test() {
|
fn test() {
|
||||||
use crate::tester::Tester;
|
use crate::tester::Tester;
|
||||||
|
|
@ -363,6 +364,7 @@ fn test() {
|
||||||
("foo.defineProperties(null, { bar: { get() {} } });", None),
|
("foo.defineProperties(null, { bar: { get() {} } });", None),
|
||||||
("foo.create(null, { bar: { get() {} } });", None),
|
("foo.create(null, { bar: { get() {} } });", None),
|
||||||
("var foo = { get willThrowSoValid() { throw MyException() } };", None),
|
("var foo = { get willThrowSoValid() { throw MyException() } };", None),
|
||||||
|
("export abstract class Foo { protected abstract get foobar(): number; }", None),
|
||||||
];
|
];
|
||||||
|
|
||||||
let fail = vec![
|
let fail = vec![
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue