-
Notifications
You must be signed in to change notification settings - Fork 651
Expand file tree
/
Copy pathrequire-super-calls-in-overridden-methods.ts
More file actions
80 lines (74 loc) · 2.18 KB
/
require-super-calls-in-overridden-methods.ts
File metadata and controls
80 lines (74 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {createRule} from './utils/ruleCreator.ts';
type MessageIds = 'missingSuperCall';
type Options = [
{
methodNames: String[],
},
];
export default createRule<Options, MessageIds>({
name: 'require-super-calls-in-overridden-methods',
meta: {
type: 'problem',
docs: {
description: 'Checks that overridden methods contain super calls.',
category: 'Possible Errors',
},
messages: {
missingSuperCall: 'Missing call to super.{{ methodName }}() in overridden method {{ methodName }}.',
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
methodNames: {
type: 'array',
},
},
additionalProperties: false,
},
],
},
defaultOptions: [{
methodNames: [],
}],
create: function(context, options) {
return {
MethodDefinition(node) {
if (!node.override || node.key.type !== 'Identifier' || !node.value.body) {
return;
}
const methodName = node.key.name;
if (!options[0].methodNames.includes(methodName)) {
return;
}
const {body} = node.value;
for (const statement of body.body) {
if (statement.type !== 'ExpressionStatement') {
continue;
}
if (statement.expression.type === 'CallExpression' &&
statement.expression.callee.type === 'MemberExpression' &&
statement.expression.callee.object.type === 'Super' &&
statement.expression.callee.property.type === 'Identifier' &&
statement.expression.callee.property.name === methodName) {
return;
}
}
context.report({
node,
messageId: 'missingSuperCall',
data: {methodName},
fix(fixer) {
const range: typeof body.range = [body.range[0], body.range[0] + 1];
const text = ` super.${methodName}();`;
return fixer.insertTextAfterRange(range, text);
},
});
},
};
},
});