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): support ESM js imports in ast utils #16049

Merged
merged 1 commit into from
Apr 3, 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
30 changes: 22 additions & 8 deletions packages/eslint-plugin-nx/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down