Skip to content

Commit

Permalink
fix: no-relative-import resolve path not correct when used in sub dir…
Browse files Browse the repository at this point in the history
…ectory
  • Loading branch information
tjx666 committed Aug 27, 2024
1 parent a297f7f commit 57d525b
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions packages/eslint-plugin/rules/no-relative-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ function has(map, path) {
return true;
}

function findDirWithFile(filename) {
let dir = path.resolve(filename);
function findDirWithFile(filename, startDir) {
let dir = path.resolve(startDir);
const root = path.parse(dir).root;

do {
while (dir !== root) {
if (fs.existsSync(path.join(dir, filename))) {
return dir;
}
dir = path.dirname(dir);
} while (!fs.existsSync(path.join(dir, filename)) && dir !== '/');

if (!fs.existsSync(path.join(dir, filename))) {
return;
}

return dir;
if (fs.existsSync(path.join(root, filename))) {
return root;
}
}

function getImportPrefixToAlias(paths) {
Expand Down Expand Up @@ -152,14 +154,15 @@ const optionsSchema = {
};

let cachedBaseDir, cachedBaseUrl, cachedPaths, cachedImportPrefixToAlias;
function generateRule(context) {
function create(context) {
const options = context.options[0] || {};
const onlyPathAliases = options.onlyPathAliases || false;
const onlyAbsoluteImports = options.onlyAbsoluteImports || false;
const respectAliasOrder = options.respectAliasOrder || false;

if (!cachedBaseDir) {
cachedBaseDir = findDirWithFile('package.json');
const filename = context.getFilename();
cachedBaseDir = findDirWithFile('package.json', path.dirname(filename));
[cachedBaseUrl, cachedPaths] = getBaseUrlAndPaths(cachedBaseDir);
cachedImportPrefixToAlias = getImportPrefixToAlias(cachedPaths);
}
Expand Down Expand Up @@ -203,7 +206,5 @@ module.exports = {
fixable: true,
schema: [optionsSchema],
},
create(context) {
return generateRule(context);
},
create,
};

0 comments on commit 57d525b

Please sign in to comment.