eslint-plugin-export-scope
Version:
Don't leak LOCAL utils, states, components into the global scope
52 lines • 2.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPathFromImport = exports.getScopeDeclarations = void 0;
const utils_1 = require("@typescript-eslint/utils");
const getScopeDeclarations = (comments) => {
return comments.reduce((acc, { type, value, loc }) => {
if (type !== utils_1.AST_TOKEN_TYPES.Block)
return acc;
// Match multiple directives in a single comment block
const regex = /(\s*\*?\s*@)(scope|scopeDefault|scopeException)\s+([^\s\n\r]+)/g;
let match;
while ((match = regex.exec(value)) !== null) {
const [, prefix, scopeType, path] = match;
if (prefix && scopeType && path) {
// Find the line and column of the path within the comment
const beforePath = value.substring(0, match.index + prefix.length + scopeType.length + 1);
const linesBeforePath = beforePath.split('\n');
const pathLineOffset = linesBeforePath.length - 1;
// Calculate column position of path start
const columnOffset = pathLineOffset > 0
? (linesBeforePath[linesBeforePath.length - 1]?.length ?? 0) // Column in the current line
: loc.start.column + 2 + beforePath.length; // +2 for /*
acc.push({
type: scopeType,
path,
loc: {
start: {
line: loc.start.line + pathLineOffset,
column: columnOffset
},
end: {
line: loc.start.line + pathLineOffset,
column: columnOffset + path.length
}
}
});
}
}
return acc;
}, []);
};
exports.getScopeDeclarations = getScopeDeclarations;
const extractPathFromImport = (node) => {
if (node.type === utils_1.AST_NODE_TYPES.ImportDeclaration)
return node.source.value;
if (node.type === utils_1.AST_NODE_TYPES.ImportExpression &&
node.source.type === utils_1.AST_NODE_TYPES.Literal &&
typeof node.source.value === "string")
return node.source.value;
};
exports.extractPathFromImport = extractPathFromImport;
//# sourceMappingURL=esLintUtils.js.map
;