restrict-imports-loader
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -5,3 +5,4 @@ import * as core from "./core"; | ||
export declare function matchedBy(r: RegExp): core.AsyncDeciderFunction; | ||
export declare function climbingUpwardsMoreThan(levels: number): core.AsyncDeciderFunction; | ||
export declare const everythingInside: (dirs: readonly string[]) => LoaderFunctionDecider; | ||
export declare const everythingOutside: (dirs: readonly string[]) => LoaderFunctionDecider; |
@@ -11,2 +11,4 @@ "use strict"; | ||
const path = __importStar(require("path")); | ||
const UP_ONE_LEVEL_LENGTH = 3; | ||
const REGEX_UP_LEVELS = new RegExp(String.raw `(?:\.\.\/)+`, "g"); | ||
function everythingInPackage(packageName) { | ||
@@ -20,2 +22,21 @@ return matchedBy(new RegExp(String.raw `^${packageName}(\/.*)?$`)); | ||
exports.matchedBy = matchedBy; | ||
function climbingUpwardsMoreThan(levels) { | ||
return importPath => { | ||
const maxLength = lengthOfLongestMatch(normalize(importPath).match(REGEX_UP_LEVELS)); | ||
const maxClimbs = maxLength / UP_ONE_LEVEL_LENGTH; | ||
return Promise.resolve({ | ||
restricted: maxClimbs > levels, | ||
info: `(consecutive "../"s: ${maxClimbs})`, | ||
}); | ||
}; | ||
} | ||
exports.climbingUpwardsMoreThan = climbingUpwardsMoreThan; | ||
function lengthOfLongestMatch(matches) { | ||
return (matches === null | ||
? 0 | ||
: matches.reduce((acc, m) => Math.max(acc, m.length), 0)); | ||
} | ||
function normalize(importPath) { | ||
return importPath.replace(/\/+/g, "/").replace(/\/\.\//g, "/"); | ||
} | ||
exports.everythingInside = everything(true); | ||
@@ -22,0 +43,0 @@ exports.everythingOutside = everything(false); |
@@ -5,4 +5,4 @@ import * as webpack from "webpack"; | ||
export { LoaderDecider, LoaderOptions, Severity } from "./loader"; | ||
export { everythingInPackage, everythingOutside, everythingInside, matchedBy } from "./deciders"; | ||
export { climbingUpwardsMoreThan, everythingInPackage, everythingInside, everythingOutside, matchedBy, } from "./deciders"; | ||
export default function (this: webpack.loader.LoaderContext, source: string): void; | ||
export declare type AsyncDecider = RegExp | core.AsyncDeciderFunction; |
@@ -14,5 +14,6 @@ "use strict"; | ||
var deciders_1 = require("./deciders"); | ||
exports.climbingUpwardsMoreThan = deciders_1.climbingUpwardsMoreThan; | ||
exports.everythingInPackage = deciders_1.everythingInPackage; | ||
exports.everythingInside = deciders_1.everythingInside; | ||
exports.everythingOutside = deciders_1.everythingOutside; | ||
exports.everythingInside = deciders_1.everythingInside; | ||
exports.matchedBy = deciders_1.matchedBy; | ||
@@ -19,0 +20,0 @@ function default_1(source) { |
{ | ||
"name": "restrict-imports-loader", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "A Webpack loader to restrict imports in ES and TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -229,2 +229,30 @@ # restrict-imports-loader | ||
### Restricting excessive directory climbing | ||
You can restrict the number of consecutive `../`s an import may contain. | ||
(`.././../` counts as two consecutive `../`s.) | ||
```typescript | ||
const { climbingUpwardsMoreThan } = require("restrict-imports-loader"); | ||
module.exports = { | ||
// ... | ||
{ | ||
loader: "restrict-imports-loader", | ||
options: { | ||
severity: "warning", | ||
rules: [ | ||
{ | ||
restricted: climbingUpwardsMoreThan(1), | ||
info: `These imports climb the directory tree excessively:`, | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
``` | ||
This example allows `"../foo"` but restricts `"../../foo"`. | ||
### Limitations | ||
@@ -231,0 +259,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42797
586
281