Skip to content

Commit f4d1b11

Browse files
committed
Fix all any casts in lint rules
For microsoft#269213 Also bumps eslint versions to fix some typing issues
1 parent 6711af1 commit f4d1b11

9 files changed

Lines changed: 219 additions & 206 deletions

.eslint-plugin-local/code-no-observable-get-in-reactive-context.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as eslint from 'eslint';
76
import { TSESTree } from '@typescript-eslint/utils';
8-
import * as ESTree from 'estree';
7+
import * as eslint from 'eslint';
98
import * as visitorKeys from 'eslint-visitor-keys';
9+
import * as ESTree from 'estree';
1010

1111
export = new class NoObservableGetInReactiveContext implements eslint.Rule.RuleModule {
1212
meta: eslint.Rule.RuleMetaData = {
@@ -61,13 +61,11 @@ function checkFunctionForObservableGetCalls(
6161
if (node.type === 'CallExpression' && isObservableGetCall(node)) {
6262
// Flag .get() calls since we're always in a reactive context here
6363
context.report({
64-
// eslint-disable-next-line local/code-no-any-casts
65-
node: node as any as ESTree.Node,
64+
node: node,
6665
message: `Observable '.get()' should not be used in reactive context. Use '.read(${readerName})' instead to properly track dependencies or '.read(undefined)' to be explicit about an untracked read.`,
6766
fix: (fixer) => {
6867
const memberExpression = node.callee as TSESTree.MemberExpression;
69-
// eslint-disable-next-line local/code-no-any-casts
70-
return fixer.replaceText(node as any, `${context.getSourceCode().getText(memberExpression.object as any)}.read(undefined)`);
68+
return fixer.replaceText(node, `${context.getSourceCode().getText(memberExpression.object as ESTree.Node)}.read(undefined)`);
7169
}
7270
});
7371
}
@@ -133,8 +131,7 @@ function isReactiveFunctionWithReader(callee: TSESTree.Node): boolean {
133131
function walkChildren(node: TSESTree.Node, cb: (child: TSESTree.Node) => void) {
134132
const keys = visitorKeys.KEYS[node.type] || [];
135133
for (const key of keys) {
136-
// eslint-disable-next-line local/code-no-any-casts
137-
const child = (node as any)[key];
134+
const child = (node as Record<string, any>)[key];
138135
if (Array.isArray(child)) {
139136
for (const item of child) {
140137
if (item && typeof item === 'object' && item.type) {

.eslint-plugin-local/code-no-reader-after-await.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as eslint from 'eslint';
76
import { TSESTree } from '@typescript-eslint/utils';
8-
import * as ESTree from 'estree';
7+
import * as eslint from 'eslint';
98

109
export = new class NoReaderAfterAwait implements eslint.Rule.RuleModule {
1110
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
@@ -59,8 +58,7 @@ function checkFunctionForAwaitBeforeReader(
5958
if (awaitPositions.length > 0) {
6059
const methodName = getMethodName(node);
6160
context.report({
62-
// eslint-disable-next-line local/code-no-any-casts
63-
node: node as any as ESTree.Node,
61+
node: node,
6462
message: `Reader method '${methodName}' should not be called after 'await'. The reader becomes invalid after async operations.`
6563
});
6664
}

.eslint-plugin-local/code-no-static-self-ref.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as eslint from 'eslint';
7+
import * as ESTree from 'estree';
78
import { TSESTree } from '@typescript-eslint/utils';
89

910
/**
@@ -28,16 +29,14 @@ export = new class implements eslint.Rule.RuleModule {
2829

2930
const classCtor = classDeclaration.body.body.find(node => node.type === 'MethodDefinition' && node.kind === 'constructor');
3031

31-
if (!classCtor) {
32+
if (!classCtor || classCtor.type === 'StaticBlock') {
3233
return;
3334
}
3435

3536
const name = classDeclaration.id.name;
36-
// eslint-disable-next-line local/code-no-any-casts
37-
const valueText = context.sourceCode.getText(<any>propertyDefinition.value);
37+
const valueText = context.sourceCode.getText(propertyDefinition.value as ESTree.Node);
3838

3939
if (valueText.includes(name + '.')) {
40-
4140
if (classCtor.value?.type === 'FunctionExpression' && !classCtor.value.params.find((param: any) => param.type === 'TSParameterProperty' && param.decorators?.length > 0)) {
4241
return;
4342
}

.eslint-plugin-local/code-no-test-async-suite.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export = new class NoAsyncSuite implements eslint.Rule.RuleModule {
2020
function hasAsyncSuite(node: any) {
2121
if (isCallExpression(node) && node.arguments.length >= 2 && isFunctionExpression(node.arguments[1]) && node.arguments[1].async) {
2222
return context.report({
23-
// eslint-disable-next-line local/code-no-any-casts
24-
node: node as any,
23+
node: node,
2524
message: 'suite factory function should never be async'
2625
});
2726
}

.eslint-plugin-local/code-no-unexternalized-strings.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
67
import * as eslint from 'eslint';
7-
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/utils';
8+
import * as ESTree from 'estree';
89

9-
function isStringLiteral(node: TSESTree.Node | null | undefined): node is TSESTree.StringLiteral {
10+
function isStringLiteral(node: TSESTree.Node | ESTree.Node | null | undefined): node is TSESTree.StringLiteral {
1011
return !!node && node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string';
1112
}
1213

@@ -33,7 +34,7 @@ export = new class NoUnexternalizedStrings implements eslint.Rule.RuleModule {
3334
const externalizedStringLiterals = new Map<string, { call: TSESTree.CallExpression; message: TSESTree.Node }[]>();
3435
const doubleQuotedStringLiterals = new Set<TSESTree.Node>();
3536

36-
function collectDoubleQuotedStrings(node: TSESTree.Literal) {
37+
function collectDoubleQuotedStrings(node: ESTree.Literal) {
3738
if (isStringLiteral(node) && isDoubleQuoted(node)) {
3839
doubleQuotedStringLiterals.add(node);
3940
}
@@ -42,7 +43,7 @@ export = new class NoUnexternalizedStrings implements eslint.Rule.RuleModule {
4243
function visitLocalizeCall(node: TSESTree.CallExpression) {
4344

4445
// localize(key, message)
45-
const [keyNode, messageNode] = (<TSESTree.CallExpression>node).arguments;
46+
const [keyNode, messageNode] = node.arguments;
4647

4748
// (1)
4849
// extract key so that it can be checked later
@@ -81,8 +82,7 @@ export = new class NoUnexternalizedStrings implements eslint.Rule.RuleModule {
8182
context.report({
8283
loc: messageNode.loc,
8384
messageId: 'badMessage',
84-
// eslint-disable-next-line local/code-no-any-casts
85-
data: { message: context.getSourceCode().getText(<any>node) }
85+
data: { message: context.getSourceCode().getText(node as ESTree.Node) }
8686
});
8787
}
8888
}
@@ -129,8 +129,7 @@ export = new class NoUnexternalizedStrings implements eslint.Rule.RuleModule {
129129
// report all invalid duplicates (same key, different message)
130130
if (values.length > 1) {
131131
for (let i = 1; i < values.length; i++) {
132-
// eslint-disable-next-line local/code-no-any-casts
133-
if (context.getSourceCode().getText(<any>values[i - 1].message) !== context.getSourceCode().getText(<any>values[i].message)) {
132+
if (context.getSourceCode().getText(values[i - 1].message as ESTree.Node) !== context.getSourceCode().getText(values[i].message as ESTree.Node)) {
134133
context.report({ loc: values[i].call.loc, messageId: 'duplicateKey', data: { key } });
135134
}
136135
}
@@ -139,23 +138,23 @@ export = new class NoUnexternalizedStrings implements eslint.Rule.RuleModule {
139138
}
140139

141140
return {
142-
['Literal']: (node: any) => collectDoubleQuotedStrings(node),
143-
['ExpressionStatement[directive] Literal:exit']: (node: any) => doubleQuotedStringLiterals.delete(node),
141+
['Literal']: (node: ESTree.Literal) => collectDoubleQuotedStrings(node),
142+
['ExpressionStatement[directive] Literal:exit']: (node: TSESTree.Literal) => doubleQuotedStringLiterals.delete(node),
144143

145144
// localize(...)
146-
['CallExpression[callee.type="MemberExpression"][callee.object.name="nls"][callee.property.name="localize"]:exit']: (node: any) => visitLocalizeCall(node),
145+
['CallExpression[callee.type="MemberExpression"][callee.object.name="nls"][callee.property.name="localize"]:exit']: (node: TSESTree.CallExpression) => visitLocalizeCall(node),
147146

148147
// localize2(...)
149-
['CallExpression[callee.type="MemberExpression"][callee.object.name="nls"][callee.property.name="localize2"]:exit']: (node: any) => visitLocalizeCall(node),
148+
['CallExpression[callee.type="MemberExpression"][callee.object.name="nls"][callee.property.name="localize2"]:exit']: (node: TSESTree.CallExpression) => visitLocalizeCall(node),
150149

151150
// vscode.l10n.t(...)
152-
['CallExpression[callee.type="MemberExpression"][callee.object.property.name="l10n"][callee.property.name="t"]:exit']: (node: any) => visitL10NCall(node),
151+
['CallExpression[callee.type="MemberExpression"][callee.object.property.name="l10n"][callee.property.name="t"]:exit']: (node: TSESTree.CallExpression) => visitL10NCall(node),
153152

154153
// l10n.t(...)
155-
['CallExpression[callee.object.name="l10n"][callee.property.name="t"]:exit']: (node: any) => visitL10NCall(node),
154+
['CallExpression[callee.object.name="l10n"][callee.property.name="t"]:exit']: (node: TSESTree.CallExpression) => visitL10NCall(node),
156155

157-
['CallExpression[callee.name="localize"][arguments.length>=2]:exit']: (node: any) => visitLocalizeCall(node),
158-
['CallExpression[callee.name="localize2"][arguments.length>=2]:exit']: (node: any) => visitLocalizeCall(node),
156+
['CallExpression[callee.name="localize"][arguments.length>=2]:exit']: (node: TSESTree.CallExpression) => visitLocalizeCall(node),
157+
['CallExpression[callee.name="localize2"][arguments.length>=2]:exit']: (node: TSESTree.CallExpression) => visitLocalizeCall(node),
159158
['Program:exit']: reportBadStringsAndBadKeys,
160159
};
161160
}

.eslint-plugin-local/vscode-dts-provider-naming.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ export = new class ApiProviderNaming implements eslint.Rule.RuleModule {
2424

2525
return {
2626
['TSInterfaceDeclaration[id.name=/.+Provider/] TSMethodSignature']: (node: any) => {
27-
28-
2927
const interfaceName = (<TSESTree.TSInterfaceDeclaration>(<TSESTree.Identifier>node).parent?.parent).id.name;
3028
if (allowed.has(interfaceName)) {
3129
// allowed
3230
return;
3331
}
3432

35-
// eslint-disable-next-line local/code-no-any-casts
36-
const methodName = (<any>(<TSESTree.TSMethodSignatureNonComputedName>node).key).name;
33+
const methodName = ((<TSESTree.TSMethodSignatureNonComputedName>node).key as TSESTree.Identifier).name;
3734

3835
if (!ApiProviderNaming._providerFunctionNames.test(methodName)) {
3936
context.report({

.eslint-plugin-local/vscode-dts-vscode-in-comments.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as eslint from 'eslint';
7-
import type * as estree from 'estree';
87

98
export = new class ApiVsCodeInComments implements eslint.Rule.RuleModule {
109

@@ -40,10 +39,8 @@ export = new class ApiVsCodeInComments implements eslint.Rule.RuleModule {
4039
}
4140

4241
// Types for eslint seem incorrect
43-
// eslint-disable-next-line local/code-no-any-casts
44-
const start = sourceCode.getLocFromIndex(startIndex + match.index) as any as estree.Position;
45-
// eslint-disable-next-line local/code-no-any-casts
46-
const end = sourceCode.getLocFromIndex(startIndex + match.index + match[0].length) as any as estree.Position;
42+
const start = sourceCode.getLocFromIndex(startIndex + match.index);
43+
const end = sourceCode.getLocFromIndex(startIndex + match.index + match[0].length);
4744
context.report({
4845
messageId: 'comment',
4946
loc: { start, end }

0 commit comments

Comments
 (0)