Skip to content

Commit

Permalink
Feature: Add allowedDepth option for a limited amount of relative i…
Browse files Browse the repository at this point in the history
…mports (#34)

* Add allowedDepth option for allowing certain amounts of relative path specifiers.

* Update README for allowedDepth option.
  • Loading branch information
peterlenahan-apollo authored Jun 26, 2024
1 parent b3c0800 commit 529a844
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,31 @@ import Something from "../../components/something";

// will result in
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";
```

35 changes: 24 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ function isSameFolder(path) {
return path.startsWith("./");
}

function getRelativePathDepth(path) {
let depth = 0;
while (path.startsWith('../')) {
depth += 1;
path = path.substring(3)
}
return depth;
}

function getAbsolutePath(relativePath, context, rootDir, prefix) {
return [
prefix,
Expand Down Expand Up @@ -46,14 +55,16 @@ module.exports = {
allowSameFolder: { type: "boolean" },
rootDir: { type: "string" },
prefix: { type: "string" },
allowedDepth: { type: "number" },
},
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,
rootDir: context.options[0]?.rootDir || '',
prefix: context.options[0]?.prefix || '',
Expand All @@ -63,16 +74,18 @@ module.exports = {
ImportDeclaration: function (node) {
const path = node.source.value;
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)
);
},
});
}
}

if (isSameFolder(path) && !allowSameFolder) {
Expand Down

0 comments on commit 529a844

Please sign in to comment.