From fbd89f77f085b3698f5bd88a3c3e1c76e2abe8a3 Mon Sep 17 00:00:00 2001 From: Bobby Piper Date: Sat, 8 Oct 2022 10:57:14 +0100 Subject: [PATCH 1/2] provided prefix option --- README.md | 23 ++++++++++++++++++++++- index.js | 15 +++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c55ec16..e225820 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Add the plugin to the plugins section, and configure the rule options. ... "no-relative-import-paths/no-relative-import-paths": [ "warn", - { "allowSameFolder": true, "rootDir": "src" } + { "allowSameFolder": true, "rootDir": "src", "prefix": "" } ] ... ``` @@ -85,3 +85,24 @@ import Something from "components/something"; // ^- no 'src/' prefix is added ``` +### `prefix` + +Useful when auto-fixing and a prefix should be included in the absolute path. + +Examples of code for this rule: + +```js +// when not configured: +import Something from "../../components/something"; + +// will result in +import Something from "src/components/something"; +``` + +```js +// when configured as { "prefix": "@/" } +import Something from "../../components/something"; + +// will result in +import Something from "@/components/something"; +``` \ No newline at end of file diff --git a/index.js b/index.js index 5195d3d..fff3a24 100644 --- a/index.js +++ b/index.js @@ -15,14 +15,16 @@ function isSameFolder(path) { return path.startsWith("./"); } -function getAbsolutePath(relativePath, context, rootDir) { - return path +function getAbsolutePath(relativePath, context, rootDir, prefix) { + return [ + prefix, + ...path .relative( context.getCwd() + (rootDir !== '' ? path.sep + rootDir : ''), path.join(path.dirname(context.getFilename()), relativePath) ) .split(path.sep) - .join("/"); + ].join("/"); } const message = "import statements should have an absolute path"; @@ -35,9 +37,10 @@ module.exports = { fixable: "code", }, create: function (context) { - const { allowSameFolder, rootDir } = { + const { allowSameFolder, rootDir, prefix } = { allowSameFolder: context.options[0]?.allowSameFolder || false, rootDir: context.options[0]?.rootDir || '', + prefix: context.options[0]?.prefix || '', }; return { @@ -50,7 +53,7 @@ module.exports = { fix: function (fixer) { return fixer.replaceTextRange( [node.source.range[0] + 1, node.source.range[1] - 1], - getAbsolutePath(path, context, rootDir || '') + getAbsolutePath(path, context, rootDir || '', prefix) ); }, }); @@ -63,7 +66,7 @@ module.exports = { fix: function (fixer) { return fixer.replaceTextRange( [node.source.range[0] + 1, node.source.range[1] - 1], - getAbsolutePath(path, context, rootDir || '') + getAbsolutePath(path, context, rootDir || '', prefix) ); }, }); From 10956fae5448605f938417417eb5b1a0e0e8b557 Mon Sep 17 00:00:00 2001 From: Bobby Piper Date: Sat, 8 Oct 2022 10:59:38 +0100 Subject: [PATCH 2/2] readme typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e225820..8e0c92a 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ import Something from "src/components/something"; ``` ```js -// when configured as { "prefix": "@/" } +// when configured as { "prefix": "@" } import Something from "../../components/something"; // will result in