Skip to content

Commit

Permalink
Merge pull request #2 from andidev/feature/esling-fix-implementation
Browse files Browse the repository at this point in the history
Feature/esling fix implementation
  • Loading branch information
MelvinVermeer authored Jan 1, 2022
2 parents 979edb1 + 7833e9a commit 3af9ebc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# eslint-plugin-no-relative-import-paths

Moving a file to different folder, could result in changing all imports statement in that file. This will not happen is the import paths are absolute. The eslint rule helps enforcing having absolute import paths.
Moving a file to different folder, could result in changing all imports statement in that file. This will not happen is the import paths are absolute. The eslint rule helps enforcing having absolute import paths.
Support eslint --fix to automatically change imports to absolute paths.

# Installation

Expand Down
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('path');

function isParentFolder(path) {
return path.startsWith("../");
}
Expand All @@ -6,9 +8,17 @@ function isSameFolder(path) {
return path.startsWith("./");
}

function getAbsolutePath(relativePath, context) {
return path.relative(context.getCwd(), path.join(path.dirname(context.getFilename()), relativePath));
}

const message = "import statements should have an absolute path";

module.exports = {
meta: {
type: 'layout',
fixable: 'code',
},
rules: {
"no-relative-import-paths": {
create: function (context) {
Expand All @@ -21,13 +31,19 @@ module.exports = {
context.report({
node,
message: message,
fix: function (fixer) {
return fixer.replaceTextRange([node.source.range[0] + 1, node.source.range[1] - 1], getAbsolutePath(path, context));
},
});
}

if (isSameFolder(path) && !allowSameFolder) {
context.report({
node,
message: message,
fix: function (fixer) {
return fixer.replaceTextRange([node.source.range[0] + 1, node.source.range[1] - 1], getAbsolutePath(path, context));
},
});
}
},
Expand Down

0 comments on commit 3af9ebc

Please sign in to comment.