From a9b6d7624cc0e26168edd833669c09dc03be00ca Mon Sep 17 00:00:00 2001 From: JounQin Date: Fri, 20 Oct 2023 09:14:13 +0000 Subject: [PATCH] fix(linter): check filePath existence first fix #19748 --- packages/eslint-plugin/src/utils/ast-utils.ts | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/eslint-plugin/src/utils/ast-utils.ts b/packages/eslint-plugin/src/utils/ast-utils.ts index 69000a91b176e..f212efcc26365 100644 --- a/packages/eslint-plugin/src/utils/ast-utils.ts +++ b/packages/eslint-plugin/src/utils/ast-utils.ts @@ -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) ); @@ -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');