Skip to content

Commit

Permalink
feat: loop over each from element to look for restricted imports
Browse files Browse the repository at this point in the history
hide whitespace changes to ease the review of this commit
  • Loading branch information
AdriAt360 committed Jun 1, 2022
1 parent 9baed5d commit b6916a8
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions src/rules/no-restricted-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,25 @@ module.exports = {
};
}

const makePathValidator = (zoneFrom, zoneExcept = []) => {
const makePathValidators = (zoneFrom, zoneExcept = []) => {
const allZoneFrom = zoneFrom instanceof Array ? zoneFrom : [zoneFrom];
const areGlobPatterns = allZoneFrom.map(isGlob);

if (areBothGlobPatternAndAbsolutePath(areGlobPatterns)) {
return computeMixedGlobAndAbsolutePathValidator();
return [computeMixedGlobAndAbsolutePathValidator()];
}

const absoluteFrom = path.resolve(basePath, zoneFrom);
const isGlobPattern = isGlob(zoneFrom);
const isGlobPattern = areGlobPatterns.includes(true);

if (isGlobPattern) {
return computeGlobPatternPathValidator(absoluteFrom, zoneExcept);
} else {
return computeAbsolutePathValidator(absoluteFrom, zoneExcept);
}
return allZoneFrom.map(singleZoneFrom => {
const absoluteFrom = path.resolve(basePath, singleZoneFrom);

if (isGlobPattern) {
return computeGlobPatternPathValidator(absoluteFrom, zoneExcept);
} else {
return computeAbsolutePathValidator(absoluteFrom, zoneExcept);
}
});
};

const validators = [];
Expand All @@ -185,34 +188,34 @@ module.exports = {

matchingZones.forEach((zone, index) => {
if (!validators[index]) {
validators[index] = makePathValidator(zone.from, zone.except);
validators[index] = makePathValidators(zone.from, zone.except);
}

const {
validators[index].forEach(({
isPathRestricted,
hasValidExceptions,
isPathException,
reportInvalidException,
} = validators[index];

if (!isPathRestricted(absoluteImportPath)) {
return;
}

if (!hasValidExceptions) {
reportInvalidException(node);
return;
}

const pathIsExcepted = isPathException(absoluteImportPath);
if (pathIsExcepted) {
return;
}

context.report({
node,
message: `Unexpected path "{{importPath}}" imported in restricted zone.${zone.message ? ` ${zone.message}` : ''}`,
data: { importPath },
}) => {
if (!isPathRestricted(absoluteImportPath)) {
return;
}

if (!hasValidExceptions) {
reportInvalidException(node);
return;
}

const pathIsExcepted = isPathException(absoluteImportPath);
if (pathIsExcepted) {
return;
}

context.report({
node,
message: `Unexpected path "{{importPath}}" imported in restricted zone.${zone.message ? ` ${zone.message}` : ''}`,
data: { importPath },
});
});
});
}
Expand Down

0 comments on commit b6916a8

Please sign in to comment.