Skip to content

Commit

Permalink
properly handle paths (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoux authored Oct 14, 2023
1 parent 60e97fd commit 171114b
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const path = require("path");

function isParentFolder(relativeFilePath, context, rootDir) {
const absoluteRootPath = context.getCwd() + (rootDir !== '' ? path.sep + rootDir : '');
const absoluteRootPath = path.join(context.getCwd(), rootDir);
const absoluteFilePath = path.join(path.dirname(context.getFilename()), relativeFilePath)

return relativeFilePath.startsWith("../") && (
rootDir === '' ||
(absoluteFilePath.startsWith(absoluteRootPath) &&
context.getFilename().startsWith(absoluteRootPath))
context.getFilename().startsWith(absoluteRootPath))
);
}

Expand All @@ -19,11 +19,11 @@ 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)
.relative(
path.join(context.getCwd(), rootDir),
path.join(path.dirname(context.getFilename()), relativePath)
)
.split(path.sep)
].filter(String).join("/");
}

Expand Down Expand Up @@ -53,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 || '', prefix)
getAbsolutePath(path, context, rootDir, prefix)
);
},
});
Expand All @@ -66,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 || '', prefix)
getAbsolutePath(path, context, rootDir, prefix)
);
},
});
Expand Down

0 comments on commit 171114b

Please sign in to comment.