From 488ade6fdcbc330b6fc9f28b69157eb339e109b1 Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Mon, 3 Apr 2023 10:49:19 +0200 Subject: [PATCH] fix(linter): support ESM js imports in ast utils --- .../eslint-plugin-nx/src/utils/ast-utils.ts | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/eslint-plugin-nx/src/utils/ast-utils.ts b/packages/eslint-plugin-nx/src/utils/ast-utils.ts index afcd492839c22..df3ec305e2f3d 100644 --- a/packages/eslint-plugin-nx/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-nx/src/utils/ast-utils.ts @@ -198,19 +198,33 @@ export function getRelativeImportPath(exportedMember, filePath, basePath) { const modulePath = (exportDeclaration as any).moduleSpecifier.text; - let moduleFilePath = joinPathFragments( - dirname(filePath), - `${modulePath}.ts` - ); - if (!existsSync(moduleFilePath)) { - // might be a tsx file + let moduleFilePath; + if (modulePath.endsWith('.js') || modulePath.endsWith('.jsx')) { + moduleFilePath = joinPathFragments(dirname(filePath), modulePath); + if (!existsSync(moduleFilePath)) { + const tsifiedModulePath = modulePath.replace(/\.js(x?)$/, '.ts$1'); + moduleFilePath = joinPathFragments( + dirname(filePath), + `${tsifiedModulePath}` + ); + } + } else if (modulePath.endsWith('.ts') || modulePath.endsWith('.tsx')) { + moduleFilePath = joinPathFragments(dirname(filePath), modulePath); + } else { moduleFilePath = joinPathFragments( dirname(filePath), - `${modulePath}.tsx` + `${modulePath}.ts` ); + if (!existsSync(moduleFilePath)) { + // might be a tsx file + moduleFilePath = joinPathFragments( + dirname(filePath), + `${modulePath}.tsx` + ); + } } if (!existsSync(moduleFilePath)) { - // might be a index.ts + // might be an index.ts moduleFilePath = joinPathFragments( dirname(filePath), `${modulePath}/index.ts`