UNPKG

eslint-plugin-export-scope

Version:

Don't leak LOCAL utils, states, components into the global scope

68 lines 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createImportValidator = void 0; const utils_1 = require("@typescript-eslint/utils"); const checkIsImportable_1 = require("../checkIsImportable"); const typescript_1 = require("typescript"); const esLintUtils_1 = require("./esLintUtils"); const validateProgram_1 = require("./validateProgram"); const createImportValidator = (context) => { const compilerOptions = context.program.getCompilerOptions(); const resolvePath = (relativePath) => (0, typescript_1.resolveModuleName)(relativePath, context.filename, compilerOptions, typescript_1.sys).resolvedModule?.resolvedFileName; const checkNode = (node, exportName, relExportPath) => { if (!relExportPath) return; const exportPath = resolvePath(relExportPath); if (!(0, checkIsImportable_1.checkIsImportable)({ tsProgram: context.program, importPath: context.filename, exportPath, exportName })) { context.report(node, exportName, relExportPath); } }; const lintNode = (node, relExportPath) => { const isPromise = node.type === utils_1.AST_NODE_TYPES.AwaitExpression && node.parent; node = isPromise ? node.parent : node; const { type } = node; if (type === utils_1.AST_NODE_TYPES.Identifier) { checkNode(node, node.name, relExportPath); } if (type === utils_1.AST_NODE_TYPES.MemberExpression && node.property.type === utils_1.AST_NODE_TYPES.Identifier) { checkNode(node.property, node.property.name, relExportPath); } const lintObjectPattern = (node) => { node.properties.forEach((property) => { if (property.type === utils_1.AST_NODE_TYPES.Property && property.key.type === utils_1.AST_NODE_TYPES.Identifier) { checkNode(property.key, property.key.name, relExportPath); } }); }; if (type === utils_1.AST_NODE_TYPES.VariableDeclarator && node.id.type === utils_1.AST_NODE_TYPES.ObjectPattern) { lintObjectPattern(node.id); } if (type === utils_1.AST_NODE_TYPES.ObjectPattern) { lintObjectPattern(node); } if (type === utils_1.AST_NODE_TYPES.TSQualifiedName) { checkNode(node.right, node.right.name, relExportPath); } }; return { ImportSpecifier: (node) => "name" in node.imported && checkNode(node, node.imported.name, (0, esLintUtils_1.extractPathFromImport)(node.parent)), ImportDefaultSpecifier: (node) => checkNode(node, "default", (0, esLintUtils_1.extractPathFromImport)(node.parent)), ImportDeclaration: (node) => !node.specifiers.length && checkNode(node, undefined, (0, esLintUtils_1.extractPathFromImport)(node)), ImportExpression: (node) => { const relExportPath = (0, esLintUtils_1.extractPathFromImport)(node); const parent = node.parent; if (parent.parent?.type === utils_1.AST_NODE_TYPES.Program || (parent?.type === utils_1.AST_NODE_TYPES.AwaitExpression && parent.parent.parent?.type === utils_1.AST_NODE_TYPES.Program)) { return checkNode(node, undefined, relExportPath); } }, Program: (node) => (0, validateProgram_1.validateProgram)(context, node, lintNode), }; }; exports.createImportValidator = createImportValidator; //# sourceMappingURL=importValidation.js.map