-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): add rule call-super-on-override #5848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6ec086d
Complete Super Method Checking
mahdi-farnia 31be88c
Update packages/eslint-plugin/docs/rules/call-super-on-override.md
mahdi-farnia 5138d1d
Update packages/eslint-plugin/src/rules/call-super-on-override.ts
mahdi-farnia b8444c3
Update packages/eslint-plugin/src/rules/call-super-on-override.ts
mahdi-farnia a31ade2
Unnecessary ignoreMethods Option Removed
mahdi-farnia 2b65003
Support For Literal Method Names
mahdi-farnia ab81978
Merge branch 'typescript-eslint:main' into main
mahdi-farnia 1fe9429
Merge branch 'typescript-eslint:main' into main
mahdi-farnia 7f588b3
Merge remote-tracking branch 'refs/remotes/origin/main'
mahdi-farnia 35c527c
Merge branch 'typescript-eslint:main' into main
mahdi-farnia e95c87f
Resolve Failed CI Tests & TopLevel Option Removed
mahdi-farnia 4677e9a
Merge branch 'main' into main
bradzacher 93e3b53
Merge branch 'main' into main
mahdi-farnia 43715de
Merge branch 'typescript-eslint:main' into main
mahdi-farnia b7e9812
Merge branch 'typescript-eslint:main' into main
mahdi-farnia a9ed4e0
Update call-super-on-override.md
mahdi-farnia 93e9c16
Merge branch 'main' into main
mahdi-farnia 2b97414
Apply some suggested changes
mahdi-farnia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
packages/eslint-plugin/docs/rules/call-super-on-override.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
description: 'Require overridden methods to call super.method in their body.' | ||
--- | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/call-super-on-override** for documentation. | ||
|
||
This rule enforces that overridden methods call their corresponding `super` method. | ||
Doing so can be useful in architectures where base class methods are meant to always be called by child classes. | ||
|
||
## Rule Details | ||
|
||
Examples of code for this rule: | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
class Foo1 { | ||
bar(param: any): void {} | ||
} | ||
|
||
class Foo2 extends Foo1 { | ||
override bar(param: any): void {} | ||
} | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
class Foo1 { | ||
bar(param: any): void {} | ||
} | ||
|
||
class Foo2 extends Foo1 { | ||
override bar(param: any): void { | ||
super.bar(param); | ||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/eslint-plugin/src/rules/call-super-on-override.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
||
import * as utils from '../util'; | ||
|
||
/** | ||
* TODO: | ||
* 1. Grabbing the type of the extended class | ||
* 2. Checking whether it has a method / function property under the same name | ||
*/ | ||
|
||
export default utils.createRule({ | ||
name: 'call-super-on-override', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'Require overridden methods to call super.method in their body', | ||
recommended: false, | ||
}, | ||
messages: { | ||
missingSuperMethodCall: | ||
"Use 'super{{property}}{{parameterTuple}}' to avoid missing super class method implementations", | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
'MethodDefinition[override=true][kind="method"]'( | ||
node: TSESTree.MethodDefinition, | ||
): void { | ||
const methodName = | ||
node.key.type === AST_NODE_TYPES.Identifier | ||
? node.key.name | ||
: (node.key as TSESTree.Literal).value?.toString() ?? 'null'; | ||
const methodNameIsLiteral = node.key.type === AST_NODE_TYPES.Identifier; | ||
const methodNameIsNull = | ||
node.key.type !== AST_NODE_TYPES.Identifier | ||
? (node.key as TSESTree.Literal).value == null | ||
: false; | ||
|
||
const { computed: isComputed } = node; | ||
const bodyStatements = node.value.body!.body; | ||
|
||
for (const statement of bodyStatements) { | ||
if ( | ||
isSuperMethodCall( | ||
statement, | ||
methodName, | ||
!methodNameIsLiteral && isComputed, | ||
) | ||
) { | ||
return; | ||
} | ||
} | ||
|
||
context.report({ | ||
messageId: 'missingSuperMethodCall', | ||
node: node, | ||
data: { | ||
property: isComputed | ||
? `[${ | ||
methodNameIsLiteral && !methodNameIsNull | ||
? `'${methodName}'` | ||
: methodName | ||
}]` | ||
: `.${methodName}`, | ||
parameterTuple: `(${node.value.params | ||
.map(p => (p as TSESTree.Identifier).name) | ||
.join(', ')})`, | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
const isSuperMethodCall = ( | ||
statement: TSESTree.Statement | undefined, | ||
methodName: string, | ||
methodIsComputedIdentifier: boolean, | ||
): boolean => { | ||
// for edge cases like this -> override [X]() { super.X() } | ||
// we make sure that computed identifier should have computed callback | ||
let calleeIsComputedIdentifier = false; | ||
|
||
const calleeName = | ||
statement?.type === AST_NODE_TYPES.ExpressionStatement && | ||
statement.expression.type === AST_NODE_TYPES.CallExpression && | ||
statement.expression.callee.type === AST_NODE_TYPES.MemberExpression && | ||
statement.expression.callee.object.type === AST_NODE_TYPES.Super && | ||
(statement.expression.callee.property.type === AST_NODE_TYPES.Identifier | ||
? ((calleeIsComputedIdentifier = statement.expression.callee.computed), | ||
statement.expression.callee.property.name) | ||
: statement.expression.callee.property.type === AST_NODE_TYPES.Literal | ||
? statement.expression.callee.property.value?.toString() ?? 'null' | ||
: undefined); | ||
|
||
return methodIsComputedIdentifier | ||
? calleeIsComputedIdentifier | ||
? methodName === calleeName | ||
: false | ||
: methodName === calleeName; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/eslint-plugin/tests/rules/call-super-on-override.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import rule from '../../src/rules/call-super-on-override'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('call-super-on-override', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
class ValidSample { | ||
override x() { | ||
this.y(); | ||
super.x(); | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
class ValidSample { | ||
override ['x-y']() { | ||
super['x-y'](); | ||
} | ||
override ['z']() { | ||
super.z(); | ||
} | ||
override h() { | ||
super['h'](); | ||
} | ||
override [M]() { | ||
super[M](); | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
class InvalidSample { | ||
override x() { | ||
this.x(); | ||
super.x = () => void 0; | ||
super.x; | ||
} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missingSuperMethodCall', | ||
data: { property: '.x', parameterTuple: '()' }, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
class InvalidSample { | ||
override ['x-y-z']() { | ||
this['x-y-z'](); | ||
super['x-y-z'] = () => void 0; | ||
super['x-y-z']; | ||
} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missingSuperMethodCall', | ||
data: { property: "['x-y-z']", parameterTuple: '()' }, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
class InvalidSample { | ||
override x(y: number, z: string) {} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missingSuperMethodCall', | ||
data: { property: '.x', parameterTuple: '(y, z)' }, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
class InvalidSample { | ||
override [M]() { | ||
super.M(); | ||
} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missingSuperMethodCall', | ||
data: { property: '[M]', parameterTuple: '()' }, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
class InvalidSample { | ||
override [null]() {} | ||
override ['null']() {} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missingSuperMethodCall', | ||
data: { property: '[null]', parameterTuple: '()' }, | ||
}, | ||
{ | ||
messageId: 'missingSuperMethodCall', | ||
data: { property: "['null']", parameterTuple: '()' }, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.