fix(linter): support directories for relative path fix (#16854)

This commit is contained in:
Miroslav Jonaš 2023-05-08 16:57:10 +02:00 committed by GitHub
parent 7920fb6a20
commit 62fb39de02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ import {
} from '@nx/devkit'; } from '@nx/devkit';
import { findNodes } from '@nx/js'; import { findNodes } from '@nx/js';
import { getModifiers } from '@typescript-eslint/type-utils'; import { getModifiers } from '@typescript-eslint/type-utils';
import { existsSync, readFileSync } from 'fs'; import { existsSync, lstatSync, readdirSync, readFileSync } from 'fs';
import { dirname } from 'path'; import { dirname } from 'path';
import ts = require('typescript'); import ts = require('typescript');
@ -81,6 +81,16 @@ function hasMemberExport(exportedMember, filePath) {
} }
export function getRelativeImportPath(exportedMember, filePath, basePath) { export function getRelativeImportPath(exportedMember, filePath, basePath) {
if (lstatSync(filePath).isDirectory()) {
const file = readdirSync(filePath).find((file) =>
/^index\.[jt]sx?$/.exec(file)
);
if (file) {
filePath = joinPathFragments(filePath, file);
} else {
return;
}
}
const fileContent = readFileSync(filePath, 'utf8'); const fileContent = readFileSync(filePath, 'utf8');
// use the TypeScript AST to find the path to the file where exportedMember is defined // use the TypeScript AST to find the path to the file where exportedMember is defined