Skip to content

Commit 7530005

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[cleanup] Remove implicit any from rules
A start to removing the implicit any from EsLint rules. After all are remove the flag will be flipped in the tsconfig.json Bug: none Change-Id: Id5b699b03284b8492a607b52613f2e294d182fa7 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6732760 Auto-Submit: Nikolay Vitkov <[email protected]> Commit-Queue: Nikolay Vitkov <[email protected]> Reviewed-by: Alex Rudenko <[email protected]>
1 parent 631f0ea commit 7530005

File tree

4 files changed

+104
-80
lines changed

4 files changed

+104
-80
lines changed

scripts/eslint_rules/lib/prefer-assert-is-ok.ts

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,40 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import type {TSESTree} from '@typescript-eslint/utils';
6+
57
import {createRule} from './utils/ruleCreator.ts';
68

79
const FALSY_ASSERTIONS = new Set(['isFalse', 'isNotOk', 'isNotTrue', 'notOk']);
810
const TRUTHY_ASSERTIONS = new Set(['isNotFalse', 'isOk', 'isTrue', 'ok']);
911

10-
export default createRule({
12+
type MessageIds =
13+
'useAssertIsOk'|'useAssertIsOkInsteadOfNegation'|'useAssertIsNotOk'|'useAssertIsNotOkInsteadOfNegation';
14+
15+
function isAssertOk(node: TSESTree.Expression) {
16+
return node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'assert' &&
17+
node.property.type === 'Identifier' && node.property.name === 'ok';
18+
}
19+
20+
function isAssertNotOk(node: TSESTree.Expression) {
21+
return node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'assert' &&
22+
node.property.type === 'Identifier' && node.property.name === 'notOk';
23+
}
24+
25+
function isTruthyAssertion(node: TSESTree.Expression) {
26+
if (node.type === 'Identifier' && node.name === 'assert') {
27+
return true;
28+
}
29+
return node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'assert' &&
30+
node.property.type === 'Identifier' && TRUTHY_ASSERTIONS.has(node.property.name);
31+
}
32+
33+
function isFalsyAssertion(node: TSESTree.Expression) {
34+
return node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'assert' &&
35+
node.property.type === 'Identifier' && FALSY_ASSERTIONS.has(node.property.name);
36+
}
37+
38+
export default createRule<unknown[], MessageIds>({
1139
name: 'prefer-assert-is-ok',
1240
meta: {
1341
type: 'suggestion',
@@ -26,34 +54,12 @@ export default createRule({
2654
},
2755
defaultOptions: [],
2856
create: function(context) {
29-
function isAssertOk(calleeNode) {
30-
return calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
31-
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier' &&
32-
calleeNode.property.name === 'ok';
33-
}
34-
35-
function isAssertNotOk(calleeNode) {
36-
return calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
37-
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier' &&
38-
calleeNode.property.name === 'notOk';
39-
}
40-
41-
function isTruthyAssertion(calleeNode) {
42-
if (calleeNode.type === 'Identifier' && calleeNode.name === 'assert') {
43-
return true;
44-
}
45-
return calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
46-
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier' &&
47-
TRUTHY_ASSERTIONS.has(calleeNode.property.name);
48-
}
49-
50-
function isFalsyAssertion(calleeNode) {
51-
return calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
52-
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier' &&
53-
FALSY_ASSERTIONS.has(calleeNode.property.name);
54-
}
55-
56-
function reportError(node, calleeText, firstArgNode, messageId) {
57+
function reportError(
58+
node: TSESTree.CallExpression,
59+
calleeText: string,
60+
firstArgNode: TSESTree.CallExpressionArgument,
61+
messageId: MessageIds,
62+
) {
5763
context.report({
5864
node,
5965
messageId,
@@ -69,26 +75,29 @@ export default createRule({
6975

7076
return {
7177
CallExpression(node) {
72-
if (node.arguments.length >= 1) {
73-
const [argumentNode] = node.arguments;
74-
if (argumentNode.type === 'UnaryExpression' && argumentNode.operator === '!') {
75-
if (isTruthyAssertion(node.callee)) {
76-
reportError(node, 'assert.isNotOk', argumentNode.argument, 'useAssertIsNotOkInsteadOfNegation');
77-
return;
78-
}
79-
if (isFalsyAssertion(node.callee)) {
80-
reportError(node, 'assert.isOk', argumentNode.argument, 'useAssertIsOkInsteadOfNegation');
81-
return;
82-
}
83-
}
78+
if (node.arguments.length < 1) {
79+
return;
80+
}
8481

85-
if (isAssertOk(node.callee)) {
86-
reportError(node, 'assert.isOk', argumentNode, 'useAssertIsOk');
87-
} else if (isAssertNotOk(node.callee)) {
88-
reportError(node, 'assert.isNotOk', argumentNode, 'useAssertIsNotOk');
82+
const [argumentNode] = node.arguments;
83+
if (argumentNode.type === 'UnaryExpression' && argumentNode.operator === '!') {
84+
if (isTruthyAssertion(node.callee)) {
85+
reportError(node, 'assert.isNotOk', argumentNode.argument, 'useAssertIsNotOkInsteadOfNegation');
86+
return;
87+
}
88+
if (isFalsyAssertion(node.callee)) {
89+
reportError(node, 'assert.isOk', argumentNode.argument, 'useAssertIsOkInsteadOfNegation');
90+
return;
8991
}
9092
}
93+
94+
if (isAssertOk(node.callee)) {
95+
reportError(node, 'assert.isOk', argumentNode, 'useAssertIsOk');
96+
} else if (isAssertNotOk(node.callee)) {
97+
reportError(node, 'assert.isNotOk', argumentNode, 'useAssertIsNotOk');
98+
}
9199
}
100+
92101
};
93102
},
94103
});

scripts/eslint_rules/lib/prefer-private-class-members.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import type {TSESTree} from '@typescript-eslint/utils';
6+
57
import {createRule} from './utils/ruleCreator.ts';
68

79
export default createRule({
@@ -13,15 +15,20 @@ export default createRule({
1315
category: 'Possible Errors',
1416
},
1517
schema: [], // no options
16-
messages: {do_not_use_private: 'Use private properties (starting with `#`) rather than the `private` modifier.'},
18+
messages: {
19+
doNotUsePrivate: 'Use private properties (starting with `#`) rather than the `private` modifier.',
20+
},
1721
},
1822
defaultOptions: [],
1923
create: function(context) {
20-
function isTypeScriptPrivate(node) {
21-
if (node.accessibility === 'private' && node.kind !== 'constructor') {
24+
function isTypeScriptPrivate(node: TSESTree.MethodDefinition|TSESTree.PropertyDefinition) {
25+
if (node.type === 'MethodDefinition' && node.kind === 'constructor') {
26+
return;
27+
}
28+
if (node.accessibility === 'private') {
2229
context.report({
2330
node: node.key,
24-
messageId: 'do_not_use_private',
31+
messageId: 'doNotUsePrivate',
2532
});
2633
}
2734
}

scripts/eslint_rules/lib/prefer-sinon-assert.ts

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,37 @@ import type {TSESTree} from '@typescript-eslint/utils';
66

77
import {createRule} from './utils/ruleCreator.ts';
88

9-
export default createRule({
9+
function isAssert(calleeNode: TSESTree.Expression) {
10+
if (calleeNode.type === 'Identifier' && calleeNode.name === 'assert') {
11+
return true;
12+
}
13+
if (calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
14+
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier') {
15+
return ['isNotFalse', 'isOk', 'isTrue', 'ok'].includes(calleeNode.property.name);
16+
}
17+
return false;
18+
}
19+
20+
function isAssertFalsy(node: TSESTree.Expression) {
21+
if (node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'assert' &&
22+
node.property.type === 'Identifier') {
23+
return ['isFalse', 'isNotOk', 'isNotTrue', 'notOk'].includes(node.property.name);
24+
}
25+
return false;
26+
}
27+
28+
function isAssertEquality(node: TSESTree.Expression) {
29+
if (node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'assert' &&
30+
node.property.type === 'Identifier') {
31+
return ['deepEqual', 'equal', 'strictEqual'].includes(node.property.name);
32+
}
33+
return false;
34+
}
35+
36+
type MessageIds = 'useSinonAssertInsteadOfAssert'|'useSinonAssertCalledInsteadOfAssert'|
37+
'useSinonAssertNotCalledInsteadOfAssert'|'useSinonAssertCallCountInsteadOfAssert';
38+
39+
export default createRule<unknown[], MessageIds>({
1040
name: 'prefer-sinon-assert',
1141
meta: {
1242
type: 'suggestion',
@@ -30,34 +60,12 @@ export default createRule({
3060
},
3161
defaultOptions: [],
3262
create: function(context) {
33-
function isAssert(calleeNode) {
34-
if (calleeNode.type === 'Identifier' && calleeNode.name === 'assert') {
35-
return true;
36-
}
37-
if (calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
38-
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier') {
39-
return ['isNotFalse', 'isOk', 'isTrue', 'ok'].includes(calleeNode.property.name);
40-
}
41-
return false;
42-
}
43-
44-
function isAssertFalsy(calleeNode) {
45-
if (calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
46-
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier') {
47-
return ['isFalse', 'isNotOk', 'isNotTrue', 'notOk'].includes(calleeNode.property.name);
48-
}
49-
return false;
50-
}
51-
52-
function isAssertEquality(calleeNode) {
53-
if (calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
54-
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier') {
55-
return ['deepEqual', 'equal', 'strictEqual'].includes(calleeNode.property.name);
56-
}
57-
return false;
58-
}
59-
60-
function reportError(node, methodName: string, firstArgNodes: TSESTree.Node|TSESTree.Node[], messageId) {
63+
function reportError(
64+
node: TSESTree.CallExpression,
65+
methodName: string,
66+
firstArgNodes: TSESTree.Node|TSESTree.Node[],
67+
messageId: MessageIds,
68+
) {
6169
context.report({
6270
node,
6371
messageId,

scripts/eslint_rules/tests/prefer-private-class-members.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ new RuleTester().run('prefer-private-class-members', rule, {
4545
}
4646
`,
4747
filename: 'test/e2e/folder/file.ts',
48-
errors: [{messageId: 'do_not_use_private'}],
48+
errors: [{messageId: 'doNotUsePrivate'}],
4949
},
5050
{
5151
code: `class Foo {
5252
private field: string;
5353
}
5454
`,
5555
filename: 'test/e2e/folder/file.ts',
56-
errors: [{messageId: 'do_not_use_private'}],
56+
errors: [{messageId: 'doNotUsePrivate'}],
5757
},
5858
],
5959
});

0 commit comments

Comments
 (0)