Skip to content

Commit

Permalink
Fix nested classes case in no-ember-super-in-es-classes (#2071)
Browse files Browse the repository at this point in the history
* A failing test for no-ember-super-in-es-classes

This rule incorrectly fails on the following code:

```js
class Foo {
  bar() {
    Baz.reopen({
      quux() {
        this._super();
      },
    });
  }
}
```

* Add a fix proposal

* boolean instead

* lint

* fixup
  • Loading branch information
CvX authored Feb 13, 2024
1 parent 7928510 commit 4326b03
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/rules/no-ember-super-in-es-classes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
'use strict';

function isDirectlyInClass(node) {
let currentNode = node.parent;
let inFunctionAlready = false;

while (currentNode) {
if (currentNode.type === 'MethodDefinition') {
return true;
} else if (currentNode.type === 'FunctionExpression') {
if (inFunctionAlready) {
return false;
} else {
inFunctionAlready = true;
}
}

currentNode = currentNode.parent;
}

return false;
}

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
Expand All @@ -19,6 +40,10 @@ module.exports = {
'MethodDefinition MemberExpression[object.type="ThisExpression"][property.name="_super"]'(
node
) {
if (!isDirectlyInClass(node)) {
return;
}

context.report({
node,
message: "Don't use `this._super` in ES classes; instead, you should use `super`",
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/no-ember-super-in-es-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ eslintTester.run('no-ember-super-in-es-classes', rule, {
'EmberObject.extend({ init() { this._super(); } })',
'EmberObject.extend({ init(a, b) { this._super(a, b); } })',
'EmberObject.extend({ init() { this._super(...arguments); } })',
'class Foo { bar() { Baz.reopen({ quux() { this._super(); } }); } }',
'class Foo { bar() { return function() { this._super(); }; } }',
'class Foo { bar() { return { baz() { this._super() } }; } }',
],
invalid: [
{
Expand Down Expand Up @@ -70,5 +73,19 @@ eslintTester.run('no-ember-super-in-es-classes', rule, {
{ message: "Don't use `this._super` in ES classes; instead, you should use `super`" },
],
},
{
code: 'class Foo { init() { return { a: this._super() }; } }',
output: 'class Foo { init() { return { a: super.init() }; } }',
errors: [
{ message: "Don't use `this._super` in ES classes; instead, you should use `super`" },
],
},
{
code: 'class Foo { init() { return () => { this._super(); }; } }',
output: 'class Foo { init() { return () => { super.init(); }; } }',
errors: [
{ message: "Don't use `this._super` in ES classes; instead, you should use `super`" },
],
},
],
});

0 comments on commit 4326b03

Please sign in to comment.