fix(linter): fix false postive for constructor_super with empty body

This commit is contained in:
Boshen 2023-06-27 22:22:14 +08:00
parent 1768192eeb
commit d643cba1cc
No known key found for this signature in database
GPG key ID: 9C7A8C8AB22BEBD1

View file

@ -1,5 +1,5 @@
use oxc_ast::{
ast::{ClassElement, Expression, MethodDefinitionKind, Statement},
ast::{Expression, MethodDefinitionKind, Statement},
AstKind,
};
use oxc_diagnostics::{
@ -46,15 +46,11 @@ declare_oxc_lint!(
impl Rule for ConstructorSuper {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::Class(class) = node.kind() else { return };
let Some(ctor) = class.body.body.iter().find_map(|el| match el {
ClassElement::MethodDefinition(method_definition)
if method_definition.kind == MethodDefinitionKind::Constructor =>
{
Some(method_definition)
}
_ => None,
}) else { return };
let (ctor, class) = if let AstKind::MethodDefinition(def) = node.kind()
&& def.kind == MethodDefinitionKind::Constructor
&& def.value.body.is_some()
&& let Some(AstKind::Class(class)) = ctx.nodes().parent_kind(node.id()) {
(def, class) } else { return };
// In cases where there's no super-class, calling 'super()' inside the constructor
// is handled by the parser.