eslint-plugin-no-relative-import-paths
Advanced tools
Comparing version 1.5.4 to 1.5.5
35
index.js
@@ -18,2 +18,11 @@ const path = require("path"); | ||
function getRelativePathDepth(path) { | ||
let depth = 0; | ||
while (path.startsWith('../')) { | ||
depth += 1; | ||
path = path.substring(3) | ||
} | ||
return depth; | ||
} | ||
function getAbsolutePath(relativePath, context, rootDir, prefix) { | ||
@@ -50,2 +59,3 @@ return [ | ||
prefix: { type: "string" }, | ||
allowedDepth: { type: "number" }, | ||
}, | ||
@@ -58,3 +68,4 @@ additionalProperties: false, | ||
create: function (context) { | ||
const { allowSameFolder, rootDir, prefix } = { | ||
const { allowedDepth, allowSameFolder, rootDir, prefix } = { | ||
allowedDepth: context.options[0]?.allowedDepth, | ||
allowSameFolder: context.options[0]?.allowSameFolder || false, | ||
@@ -69,12 +80,14 @@ rootDir: context.options[0]?.rootDir || '', | ||
if (isParentFolder(path, context, rootDir)) { | ||
context.report({ | ||
node, | ||
message: message, | ||
fix: function (fixer) { | ||
return fixer.replaceTextRange( | ||
[node.source.range[0] + 1, node.source.range[1] - 1], | ||
getAbsolutePath(path, context, rootDir, prefix) | ||
); | ||
}, | ||
}); | ||
if (typeof allowedDepth === 'undefined' || getRelativePathDepth(path) > allowedDepth) { | ||
context.report({ | ||
node, | ||
message: message, | ||
fix: function (fixer) { | ||
return fixer.replaceTextRange( | ||
[node.source.range[0] + 1, node.source.range[1] - 1], | ||
getAbsolutePath(path, context, rootDir, prefix) | ||
); | ||
}, | ||
}); | ||
} | ||
} | ||
@@ -81,0 +94,0 @@ |
{ | ||
"name": "eslint-plugin-no-relative-import-paths", | ||
"version": "v1.5.4", | ||
"version": "v1.5.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -108,2 +108,29 @@ # eslint-plugin-no-relative-import-paths | ||
import Something from "@/components/something"; | ||
``` | ||
``` | ||
### `allowedDepth` | ||
Used to allow some relative imports of certain depths. | ||
Examples of code for this rule: | ||
```js | ||
// when configured as { "allowedDepth": 1 } | ||
// will NOT generate a warning | ||
import Something from "../components/something"; | ||
// will generate a warning | ||
import Something from "../../components/something"; | ||
``` | ||
```js | ||
// when configured as { "allowedDepth": 2 } | ||
// will NOT generate a warning | ||
import Something from "../../components/something"; | ||
// will generate a warning | ||
import Something from "../../../components/something"; | ||
``` | ||
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
8156
99
136