/
githubmirror
/
eslint-plugin
Обзор
Документация
Войти
/
githubmirror
/
eslint-plugin
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
0.2.9
lib/rules/preferFileManagerTrashFile.ts
62 строки
2 KB
Michael Naumov
refactor: strongly typed type constants
02 янв 2026, 15:33
02 янв 2026, 15:33
0338c70
Код
Авторство
О чём код?
import { TSESTree, ESLintUtils } from "@typescript-eslint/utils"; import { getParserServices } from "@typescript-eslint/utils/eslint-utils"; const ruleCreator = ESLintUtils.RuleCreator( (name) => `https://github.com/obsidianmd/eslint-plugin/blob/master/docs/rules/${name}.md`, ); const BANNED_METHODS = new Set(["trash", "delete"]); export default ruleCreator({ name: "prefer-file-manager-trash-file", meta: { type: "suggestion" as const, docs: { description: "Prefer FileManager.trashFile() over Vault.trash() or Vault.delete() to respect user settings.", }, schema: [], messages: { preferTrashFile: "Use 'FileManager.trashFile()' instead of 'Vault.{{methodName}}()' to respect the user's file deletion preference.", }, }, defaultOptions: [], create(context) { const services = getParserServices(context); return { CallExpression(node: TSESTree.CallExpression) { const callee = node.callee; if (callee.type !== TSESTree.AST_NODE_TYPES.MemberExpression) { return; } const property = callee.property; if ( property.type !== TSESTree.AST_NODE_TYPES.Identifier || !BANNED_METHODS.has(property.name) ) { return; } // Now, check the type of the object (`vault` in `vault.trash()`) const objectType = services.getTypeAtLocation(callee.object); const symbol = objectType.getSymbol(); // Check if the symbol's name is 'Vault'. This is robust and // works even if the type is imported from 'obsidian'. if (symbol?.name === "Vault") { context.report({ node: property, // Highlight the method name (`trash` or `delete`) messageId: "preferTrashFile", data: { methodName: property.name, }, }); } }, }; }, });