Skip to content

Commit 4326b03

Browse files
authored
Fix nested classes case in no-ember-super-in-es-classes (#2071)
* 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
1 parent 7928510 commit 4326b03

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/rules/no-ember-super-in-es-classes.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
'use strict';
22

3+
function isDirectlyInClass(node) {
4+
let currentNode = node.parent;
5+
let inFunctionAlready = false;
6+
7+
while (currentNode) {
8+
if (currentNode.type === 'MethodDefinition') {
9+
return true;
10+
} else if (currentNode.type === 'FunctionExpression') {
11+
if (inFunctionAlready) {
12+
return false;
13+
} else {
14+
inFunctionAlready = true;
15+
}
16+
}
17+
18+
currentNode = currentNode.parent;
19+
}
20+
21+
return false;
22+
}
23+
324
/** @type {import('eslint').Rule.RuleModule} */
425
module.exports = {
526
meta: {
@@ -19,6 +40,10 @@ module.exports = {
1940
'MethodDefinition MemberExpression[object.type="ThisExpression"][property.name="_super"]'(
2041
node
2142
) {
43+
if (!isDirectlyInClass(node)) {
44+
return;
45+
}
46+
2247
context.report({
2348
node,
2449
message: "Don't use `this._super` in ES classes; instead, you should use `super`",

tests/lib/rules/no-ember-super-in-es-classes.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ eslintTester.run('no-ember-super-in-es-classes', rule, {
1818
'EmberObject.extend({ init() { this._super(); } })',
1919
'EmberObject.extend({ init(a, b) { this._super(a, b); } })',
2020
'EmberObject.extend({ init() { this._super(...arguments); } })',
21+
'class Foo { bar() { Baz.reopen({ quux() { this._super(); } }); } }',
22+
'class Foo { bar() { return function() { this._super(); }; } }',
23+
'class Foo { bar() { return { baz() { this._super() } }; } }',
2124
],
2225
invalid: [
2326
{
@@ -70,5 +73,19 @@ eslintTester.run('no-ember-super-in-es-classes', rule, {
7073
{ message: "Don't use `this._super` in ES classes; instead, you should use `super`" },
7174
],
7275
},
76+
{
77+
code: 'class Foo { init() { return { a: this._super() }; } }',
78+
output: 'class Foo { init() { return { a: super.init() }; } }',
79+
errors: [
80+
{ message: "Don't use `this._super` in ES classes; instead, you should use `super`" },
81+
],
82+
},
83+
{
84+
code: 'class Foo { init() { return () => { this._super(); }; } }',
85+
output: 'class Foo { init() { return () => { super.init(); }; } }',
86+
errors: [
87+
{ message: "Don't use `this._super` in ES classes; instead, you should use `super`" },
88+
],
89+
},
7390
],
7491
});

0 commit comments

Comments
 (0)