UNPKG

eslint-plugin-remix

Version:
154 lines (149 loc) 5.06 kB
var __getOwnPropNames = Object.getOwnPropertyNames; var __esm = (fn, res) => function __init() { return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; }; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; // src/rules/node-server-imports/errors.ts var INVALID_NODE_IMPORT, MOVE_TO_SERVER_FILE; var init_errors = __esm({ "src/rules/node-server-imports/errors.ts"() { "use strict"; INVALID_NODE_IMPORT = "Node imports can only exist in .server.{ts,js,tsx,jsx} files"; MOVE_TO_SERVER_FILE = "Move this logic to a .server.ts file and import that file instead."; } }); // src/rules/node-server-imports/index.ts import { ESLintUtils } from "@typescript-eslint/utils"; import builtinModules from "builtin-modules/static"; var nodeServerImports; var init_node_server_imports = __esm({ "src/rules/node-server-imports/index.ts"() { "use strict"; init_errors(); nodeServerImports = ESLintUtils.RuleCreator.withoutDocs({ create: (context) => { return { ImportDeclaration(node) { const value = node.source.value; if (typeof value !== "string") { return; } const isBuiltin = builtinModules.includes(value); if (!isBuiltin) { return; } const filename = context.getFilename(); const isServer = ["ts", "tsx", "js", "jsx"].some((ext) => filename.endsWith(`.server.${ext}`)); if (isServer) { return; } context.report({ node, messageId: "INVALID_NODE_IMPORT", suggest: [ { messageId: "MOVE_TO_SERVER_FILE", fix: (fixer) => { return [fixer.remove(node)]; } } ] }); } }; }, meta: { messages: { INVALID_NODE_IMPORT, MOVE_TO_SERVER_FILE }, hasSuggestions: true, docs: { description: "Ensures that node imports are only used in .server.{ts,js,tsx,jsx} files", recommended: "error" }, type: "problem", schema: [] }, defaultOptions: [] }); } }); // src/rules/use-loader-data-types/index.ts import { AST_NODE_TYPES, ESLintUtils as ESLintUtils2 } from "@typescript-eslint/utils"; function getTypeParameterTypeQueryName(node) { return node.type === AST_NODE_TYPES.TSTypeQuery && node.exprName.type === AST_NODE_TYPES.Identifier && node.exprName.name; } var useLoaderDataTypes; var init_use_loader_data_types = __esm({ "src/rules/use-loader-data-types/index.ts"() { "use strict"; useLoaderDataTypes = ESLintUtils2.RuleCreator.withoutDocs({ create: (context) => { return { 'CallExpression[callee.name="useLoaderData"]'(node) { if (!node.typeParameters) { context.report({ suggest: [ { messageId: "suggestMissingFix", fix: (fixer) => [fixer.insertTextAfter(node.callee, "<typeof loader>")] } ], messageId: "missing", node }); return; } const { typeParameters } = node; if (typeParameters.params.length !== 1 || getTypeParameterTypeQueryName(typeParameters.params[0]) !== "loader") { context.report({ suggest: [ { messageId: "suggestIncorrectFix", fix: (fixer) => [fixer.replaceTextRange(typeParameters.range, "<typeof loader>")] } ], messageId: "incorrect", node }); } } }; }, defaultOptions: [], meta: { messages: { incorrect: "Prefer a `<typeof loader>` type parameter to safely type this call.", missing: "Prefer using an explicit `<typeof loader>` type parameter to safely type this call.", suggestIncorrectFix: "Use `<typeof loader> type parameter to safely type this call.", suggestMissingFix: "Add a `<typeof loader> type parameter to safely type this call." }, hasSuggestions: true, docs: { description: "Ensures that useLoaderData calls have proper type information.", recommended: "warn" }, type: "problem", schema: [] } }); } }); // src/index.ts var require_src = __commonJS({ "src/index.ts"(exports, module) { init_node_server_imports(); init_use_loader_data_types(); var config = { rules: { "use-loader-data-types": useLoaderDataTypes, "node-server-imports": nodeServerImports } }; module.exports = config; } }); export default require_src();