From 529a844e9241627e6524b6951c80551b4ad35778 Mon Sep 17 00:00:00 2001 From: Peter Lenahan Date: Wed, 26 Jun 2024 08:09:14 -0400 Subject: [PATCH] Feature: Add `allowedDepth` option for a limited amount of relative imports (#34) * Add allowedDepth option for allowing certain amounts of relative path specifiers. * Update README for allowedDepth option. --- README.md | 29 ++++++++++++++++++++++++++++- index.js | 35 ++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8e0c92a..6a7bd66 100644 --- a/README.md +++ b/README.md @@ -105,4 +105,31 @@ import Something from "../../components/something"; // will result in import Something from "@/components/something"; -``` \ No newline at end of file +``` + +### `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"; +``` + diff --git a/index.js b/index.js index da5cbe4..709991d 100644 --- a/index.js +++ b/index.js @@ -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, @@ -46,6 +55,7 @@ module.exports = { allowSameFolder: { type: "boolean" }, rootDir: { type: "string" }, prefix: { type: "string" }, + allowedDepth: { type: "number" }, }, additionalProperties: false, }, @@ -53,7 +63,8 @@ module.exports = { }, }, 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 || '', @@ -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) {