eslint-plugin-export-scope
Version:
Don't leak LOCAL utils, states, components into the global scope
255 lines • 11.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateLiteralInScope = exports.validateExceptionsArray = exports.validateExportDefault = exports.validateScopeDeclarations = exports.isInExceptionsArray = void 0;
const utils_1 = require("@typescript-eslint/utils");
const pathUtils_1 = require("../pathUtils");
const esLintUtils_1 = require("./esLintUtils");
const fs_1 = require("fs");
const isInExceptionsArray = (node, parent) => {
let current = parent || node.parent;
while (current) {
if (current.type === utils_1.AST_NODE_TYPES.ArrayExpression) {
if (current.parent?.type === utils_1.AST_NODE_TYPES.VariableDeclarator) {
const declarator = current.parent;
if (declarator.id.type === utils_1.AST_NODE_TYPES.Identifier && declarator.id.name === "exceptions") {
return true;
}
}
}
current = current.parent;
}
return false;
};
exports.isInExceptionsArray = isInExceptionsArray;
const validateScopeDeclarations = (comments, exportDir, onError) => {
const scopeDeclarations = (0, esLintUtils_1.getScopeDeclarations)(comments);
scopeDeclarations.forEach(({ type, path, loc }) => {
if (path === "*")
return;
if (type === "scope" || type === "scopeDefault") {
const validation = (0, pathUtils_1.validateScopePath)(exportDir, path);
if (!validation.isValid) {
onError({
message: "Only parent dirs are allowed for @scope and @scopeDefault",
line: loc.start.line,
column: loc.start.column,
endLine: loc.end.line,
endColumn: loc.end.column
});
}
else if (validation.resolvedPath && !(0, fs_1.existsSync)(validation.resolvedPath)) {
onError({
message: `Invalid scope path: "${validation.resolvedPath}"`,
line: loc.start.line,
column: loc.start.column,
endLine: loc.end.line,
endColumn: loc.end.column
});
}
}
else if (type === "scopeException") {
const validation = (0, pathUtils_1.validateExceptionPath)(exportDir, path);
if (!validation.isValid) {
const fullPath = (0, pathUtils_1.getFullScopePath)(exportDir, path);
if (fullPath) {
onError({
message: `Invalid scope path: "${fullPath}"`,
line: loc.start.line,
column: loc.start.column,
endLine: loc.end.line,
endColumn: loc.end.column
});
}
}
else if (validation.resolvedPath && !(0, fs_1.existsSync)(validation.resolvedPath)) {
onError({
message: `Invalid scope path: "${validation.resolvedPath}"`,
line: loc.start.line,
column: loc.start.column,
endLine: loc.end.line,
endColumn: loc.end.column
});
}
}
});
};
exports.validateScopeDeclarations = validateScopeDeclarations;
const validateExportDefault = (declaration, exportDir, onError) => {
if (declaration.type === utils_1.AST_NODE_TYPES.ArrayExpression) {
for (const element of declaration.elements) {
if (element && element.type === utils_1.AST_NODE_TYPES.Literal && typeof element.value === "string") {
const stringValue = element.value;
if (stringValue === "*")
continue;
const validation = (0, pathUtils_1.validateScopePath)(exportDir, stringValue);
if (!validation.isValid) {
onError({
message: "Only parent dirs are allowed for @scope and @scopeDefault",
line: element.loc.start.line,
column: element.loc.start.column + 2,
endLine: element.loc.end.line,
endColumn: element.loc.end.column
});
}
else if (validation.resolvedPath && !(0, fs_1.existsSync)(validation.resolvedPath)) {
onError({
message: `Invalid scope path: "${validation.resolvedPath}"`,
line: element.loc.start.line,
column: element.loc.start.column + 2,
endLine: element.loc.end.line,
endColumn: element.loc.end.column
});
}
}
}
}
else if (declaration.type === utils_1.AST_NODE_TYPES.Literal && typeof declaration.value === "string") {
const stringValue = declaration.value;
if (stringValue === "*")
return;
const validation = (0, pathUtils_1.validateScopePath)(exportDir, stringValue);
if (!validation.isValid) {
onError({
message: "Only parent dirs are allowed for @scope and @scopeDefault",
line: declaration.loc.start.line,
column: declaration.loc.start.column + 2,
endLine: declaration.loc.end.line,
endColumn: declaration.loc.end.column
});
}
else if (validation.resolvedPath && !(0, fs_1.existsSync)(validation.resolvedPath)) {
onError({
message: `Invalid scope path: "${validation.resolvedPath}"`,
line: declaration.loc.start.line,
column: declaration.loc.start.column + 2,
endLine: declaration.loc.end.line,
endColumn: declaration.loc.end.column
});
}
}
};
exports.validateExportDefault = validateExportDefault;
const validateExceptionsArray = (arrayExpression, exportDir, onError) => {
for (const element of arrayExpression.elements) {
if (element && element.type === utils_1.AST_NODE_TYPES.Literal && typeof element.value === "string") {
const stringValue = element.value;
if (stringValue === "*")
continue;
const validation = (0, pathUtils_1.validateExceptionPath)(exportDir, stringValue);
if (!validation.isValid) {
const fullPath = (0, pathUtils_1.getFullScopePath)(exportDir, stringValue);
if (fullPath) {
onError({
message: `Invalid scope path: "${fullPath}"`,
line: element.loc.start.line,
column: element.loc.start.column + 2,
endLine: element.loc.end.line,
endColumn: element.loc.end.column
});
}
}
else if (validation.resolvedPath && !(0, fs_1.existsSync)(validation.resolvedPath)) {
onError({
message: `Invalid scope path: "${validation.resolvedPath}"`,
line: element.loc.start.line,
column: element.loc.start.column + 1,
endLine: element.loc.end.line,
endColumn: element.loc.end.column - 1
});
}
}
}
};
exports.validateExceptionsArray = validateExceptionsArray;
const validateLiteralInScope = (node, exportDir, onError) => {
if (typeof node.value !== "string")
return;
const stringValue = node.value;
if (stringValue === "*")
return;
// Check if in export default array (e.g., export default ["../path"])
if (node.parent?.type === utils_1.AST_NODE_TYPES.ArrayExpression &&
node.parent.parent?.type === utils_1.AST_NODE_TYPES.ExportDefaultDeclaration) {
const validation = (0, pathUtils_1.validateScopePath)(exportDir, stringValue);
if (!validation.isValid) {
onError({
message: "Only parent dirs are allowed for @scope and @scopeDefault",
line: node.loc.start.line,
column: node.loc.start.column + 2,
endLine: node.loc.end.line,
endColumn: node.loc.end.column
});
}
else if (validation.resolvedPath && !(0, fs_1.existsSync)(validation.resolvedPath)) {
onError({
message: `Invalid scope path: "${validation.resolvedPath}"`,
line: node.loc.start.line,
column: node.loc.start.column + 2,
endLine: node.loc.end.line,
endColumn: node.loc.end.column
});
}
}
// Check if in export default string (e.g., export default "../path")
else if (node.parent?.type === utils_1.AST_NODE_TYPES.ExportDefaultDeclaration) {
const validation = (0, pathUtils_1.validateScopePath)(exportDir, stringValue);
if (!validation.isValid) {
onError({
message: "Only parent dirs are allowed for @scope and @scopeDefault",
line: node.loc.start.line,
column: node.loc.start.column + 2,
endLine: node.loc.end.line,
endColumn: node.loc.end.column
});
}
else if (validation.resolvedPath && !(0, fs_1.existsSync)(validation.resolvedPath)) {
onError({
message: `Invalid scope path: "${validation.resolvedPath}"`,
line: node.loc.start.line,
column: node.loc.start.column + 2,
endLine: node.loc.end.line,
endColumn: node.loc.end.column
});
}
}
// Check if in exceptions array
else if ((0, exports.isInExceptionsArray)(node)) {
const validation = (0, pathUtils_1.validateExceptionPath)(exportDir, stringValue);
if (!validation.isValid) {
const fullPath = (0, pathUtils_1.getFullScopePath)(exportDir, stringValue);
if (fullPath) {
onError({
message: `Invalid scope path: "${fullPath}"`,
line: node.loc.start.line,
column: node.loc.start.column + 2,
endLine: node.loc.end.line,
endColumn: node.loc.end.column
});
}
}
else if (validation.resolvedPath && !(0, fs_1.existsSync)(validation.resolvedPath)) {
onError({
message: `Invalid scope path: "${validation.resolvedPath}"`,
line: node.loc.start.line,
column: node.loc.start.column + 2,
endLine: node.loc.end.line,
endColumn: node.loc.end.column
});
}
}
// Check if in other array expressions
else if (node.parent?.type === utils_1.AST_NODE_TYPES.ArrayExpression) {
const fullPath = (0, pathUtils_1.getFullScopePath)(exportDir, stringValue);
if (fullPath && !(0, fs_1.existsSync)(fullPath)) {
onError({
message: `Invalid scope path: "${fullPath}"`,
line: node.loc.start.line,
column: node.loc.start.column + 2,
endLine: node.loc.end.line,
endColumn: node.loc.end.column
});
}
}
};
exports.validateLiteralInScope = validateLiteralInScope;
//# sourceMappingURL=scopeValidation.js.map