Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter): check filePath existence first #19749

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions packages/eslint-plugin/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,18 @@ function hasMemberExport(exportedMember, filePath) {
}

export function getRelativeImportPath(exportedMember, filePath, basePath) {
if (lstatSync(filePath).isDirectory()) {
const status = lstatSync(filePath, {
throwIfNoEntry: false,
});
if (!status /*not existed, but probably not full file with an extension*/) {
// try to find an extension that exists
const ext = ['.ts', '.tsx', '.js', '.jsx'].find((ext) =>
lstatSync(filePath + ext, { throwIfNoEntry: false })
);
if (ext) {
filePath += ext;
}
} else if (status.isDirectory()) {
const file = readdirSync(filePath).find((file) =>
/^index\.[jt]sx?$/.exec(file)
);
Expand All @@ -119,18 +130,6 @@ export function getRelativeImportPath(exportedMember, filePath, basePath) {
} else {
return;
}
} else if (
!lstatSync(filePath, {
throwIfNoEntry: false,
}) /*not folder, but probably not full file with an extension either*/
) {
// try to find an extension that exists
const ext = ['.ts', '.tsx', '.js', '.jsx'].find((ext) =>
lstatSync(filePath + ext, { throwIfNoEntry: false })
);
if (ext) {
filePath += ext;
}
}

const fileContent = readFileSync(filePath, 'utf8');
Expand Down