Files
vscode/.eslint-plugin-local/code-no-any-casts.ts
Matt Bierner 360c9fd134 Add lint rule for as any and bulk ignore all existing breaks
For #269213

This adds a new eslint rule for `as any` and `<any>({... })`. We'd like to remove almost all of these, however right now the first goal is to prevent them in new code. That's why with this first PR I simply add `eslint-disable` comments for all breaks

Trying to get this change in soon after branching off for release to hopefully minimize disruption during debt week work
2025-10-02 23:38:33 -07:00

37 lines
1.3 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as eslint from 'eslint';
import { TSESTree } from '@typescript-eslint/utils';
export = new class NoAnyCasts implements eslint.Rule.RuleModule {
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
return {
// Detect TSTypeAssertion: <any>value
'TSTypeAssertion': (node: any) => {
const typeAssertion = node as TSESTree.TSTypeAssertion;
if (typeAssertion.typeAnnotation.type === 'TSAnyKeyword') {
context.report({
node,
message: `Avoid casting to 'any' type. Consider using a more specific type or type guards for better type safety.`
});
}
},
// Detect TSAsExpression: value as any
'TSAsExpression': (node: any) => {
const asExpression = node as TSESTree.TSAsExpression;
if (asExpression.typeAnnotation.type === 'TSAnyKeyword') {
context.report({
node,
message: `Avoid casting to 'any' type. Consider using a more specific type or type guards for better type safety.`
});
}
}
};
}
};