-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathcheck-was-shown-methods.ts
57 lines (52 loc) · 1.85 KB
/
check-was-shown-methods.ts
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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';
export default createRule({
name: 'check-was-shown-methods',
meta: {
type: 'problem',
docs: {
description: 'Checks wasShown() method definitions call super.wasShown();',
category: 'Possible Errors',
},
messages: {
superFirstCall: 'Please make sure the first call in wasShown is to super.wasShown().',
},
fixable: 'code',
schema: [], // no options
},
defaultOptions: [],
create: function(context) {
return {
MethodDefinition(node) {
if (node.key.type !== 'Identifier') {
return;
}
const nodeName = node.key.name;
if (nodeName !== 'wasShown') {
return;
}
const ancestorClass = node.parent.parent;
if (ancestorClass.type !== 'ClassDeclaration') {
return;
}
if (ancestorClass.superClass?.type === 'MemberExpression' &&
ancestorClass.superClass.property.type === 'Identifier' &&
ancestorClass.superClass.property.name === 'Widget') {
const topBodyNode = node.value.body?.body[0];
if (!topBodyNode) {
return;
}
if (!(topBodyNode.type === 'ExpressionStatement' && topBodyNode.expression.type === 'CallExpression' &&
topBodyNode.expression.callee.type === 'MemberExpression' &&
topBodyNode.expression.callee.object.type === 'Super' &&
topBodyNode.expression.callee.property.type === 'Identifier' &&
topBodyNode.expression.callee.property.name === 'wasShown')) {
context.report({node, messageId: 'superFirstCall'});
}
}
},
};
},
});