fix(rome_js_analyzer): `noInvalidConstructorSuper` false positive for class expressions
MODIFY
crates/rome_js_analyze/src/analyzers/nursery/no_invalid_constructor_super.rs
crates/rome_js_analyze/src/analyzers/nursery/no_invalid_constructor_super.rs
@@ -2,8 +2,7 @@ use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
use rome_console::{markup, MarkupBuf};
use rome_js_syntax::{
- JsAnyExpression, JsAssignmentOperator, JsClassDeclaration, JsConstructorClassMember,
- JsLogicalOperator,
+ JsAnyClass, JsAnyExpression, JsAssignmentOperator, JsConstructorClassMember, JsLogicalOperator,
};
use rome_rowan::{AstNode, AstNodeList, TextRange};
@@ -29,6 +28,22 @@ declare_rule! {
/// }
/// ```
///
+ /// ### Valid
+ ///
+ /// ```js
+ /// export default class A extends B {
+ /// constructor() {
+ /// super();
+ /// }
+ /// }
+ /// ```
+ ///
+ /// ```js
+ /// export class A {
+ /// constructor() {}
+ /// }
+ /// ```
+ ///
pub(crate) NoInvalidConstructorSuper {
version: "10.0.0",
name: "noInvalidConstructorSuper",
@@ -103,7 +118,7 @@ impl Rule for NoInvalidConstructorSuper {
let extends_clause = node
.syntax()
.ancestors()
- .find_map(|node| JsClassDeclaration::cast(node)?.extends_clause());
+ .find_map(|node| JsAnyClass::cast(node)?.extends_clause());
match (super_expression, extends_clause) {
(Some(super_expression), Some(extends_clause)) => {

MODIFY
crates/rome_js_analyze/tests/specs/nursery/noInvalidConstructorSuper.js
crates/rome_js_analyze/tests/specs/nursery/noInvalidConstructorSuper.js
@@ -24,6 +24,20 @@ class A extends (B -= C) { constructor() { super(); } }
class A extends (B **= C) { constructor() { super(); } }
class A extends (B |= C) { constructor() { super(); } }
class A extends undefined { constructor() { super(); } }
+module.exports = class A extends B {
+ constructor() {
+ }
+}
+export class A extends B {
+ constructor() {
+ missing_super();
+ }
+}
+export default class A extends null {
+ constructor() {
+ super();
+ }
+}
// valid
class A extends B {
constructor() {
@@ -37,3 +51,18 @@ class A extends null { constructor() { return a; } }
class A extends Object { constructor() { super() } }
class A extends (5 && B) { constructor() { super(); } }
class A extends (B || C) { constructor() { super(); } }
+module.exports = class A extends B {
+ constructor() {
+ super();
+ }
+}
+export class A extends B {
+ constructor() {
+ super();
+ }
+}
+export default class A extends B {
+ constructor() {
+ super();
+ }
+}

MODIFY
website/docs/src/lint/rules/noInvalidConstructorSuper.md
website/docs/src/lint/rules/noInvalidConstructorSuper.md
@@ -49,3 +49,19 @@ class A {
</code></pre>{% endraw %}
+### Valid
+
+```jsx
+export default class A extends B {
+ constructor() {
+ super();
+ }
+}
+```
+
+```jsx
+export class A {
+ constructor() {}
+}
+```
+

fix(rome_js_analyzer): `noInvalidConstructorSuper` false positive for class expressions (#3561)