MelvinVermeer / eslint-plugin-no-relative-import-paths

90 stars 22 forks source link

Add prefix on absolute path #12

Closed richard-lopes-ifood closed 1 year ago

richard-lopes-ifood commented 2 years ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch eslint-plugin-no-relative-import-paths@1.3.4 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/eslint-plugin-no-relative-import-paths/index.js b/node_modules/eslint-plugin-no-relative-import-paths/index.js
index 430fb5e..c13444e 100644
--- a/node_modules/eslint-plugin-no-relative-import-paths/index.js
+++ b/node_modules/eslint-plugin-no-relative-import-paths/index.js
@@ -15,14 +15,15 @@ function isSameFolder(path) {
   return path.startsWith("./");
 }

-function getAbsolutePath(relativePath, context, rootDir) {
-  return path
-    .relative(
+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 +36,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 +52,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 +65,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)
                   );
                 },
               });

This issue body was partially generated by patch-package.

GabriFila commented 2 years ago

This would be very useful 😍

Danones commented 1 year ago

I was looking to have this feature implemented as well.