/
githubmirror
/
eslint-plugin
Обзор
Документация
Войти
/
githubmirror
/
eslint-plugin
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lib/rules/noTFileTFolderCast.ts
57 строк
2 KB
saberzero1
refactor: streamline meta.docs.url for static analysis
10 июн 2026, 16:14
10 июн 2026, 16:14
13385bc
Код
Авторство
О чём код?
import { TSESTree } from "@typescript-eslint/utils"; import { docsUrl, ruleCreator } from "../ruleCreator.js"; const BANNED_CAST_TYPES = new Set(["TFile", "TFolder"]); export default ruleCreator({ meta: { type: "suggestion" as const, docs: { description: "Disallow type casting to TFile or TFolder, suggesting instanceof checks instead.", url: docsUrl("no-tfile-tfolder-cast"), }, schema: [], messages: { avoidCast: "Avoid casting to '{{typeName}}'. Use an 'instanceof {{typeName}}' check to safely narrow the type.", }, // This rule is not auto-fixable because the correction requires // changing the code's control flow (e.g., adding an if-block), // which is too complex and potentially breaking for an auto-fix. }, defaultOptions: [], create(context) { // The handler for both TSAsExpression and TSTypeAssertion nodes. // It checks if the type being cast to is TFile or TFolder. const handler = ( node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion, ) => { const typeAnnotation = node.typeAnnotation; // We only care about simple type references like `as TFile` if ( typeAnnotation.type === TSESTree.AST_NODE_TYPES.TSTypeReference && typeAnnotation.typeName.type === TSESTree.AST_NODE_TYPES.Identifier ) { const typeName = typeAnnotation.typeName.name; if (BANNED_CAST_TYPES.has(typeName)) { context.report({ node: typeAnnotation, messageId: "avoidCast", data: { typeName, }, }); } } }; return { // Catches `value as TFile` TSAsExpression: handler, // Catches `<TFile>value` TSTypeAssertion: handler, }; }, });