fix(linter): handle paths correctly in enforce-module-boundaries eslint rule (#26373)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #21889
This commit is contained in:
Leosvel Pérez Espinosa 2024-06-05 12:38:21 +02:00 committed by GitHub
parent f4b379f459
commit ffea1d5c6d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 36 deletions

View File

@ -13,7 +13,7 @@ import {
} from '@typescript-eslint/utils';
import { isBuiltinModuleImport } from '@nx/js/src/internal';
import { isRelativePath } from 'nx/src/utils/fileutils';
import { basename, dirname, relative } from 'path';
import { basename, dirname, join, relative } from 'path';
import {
getBarrelEntryPointByImportScope,
getBarrelEntryPointProjectNode,
@ -318,7 +318,7 @@ export default ESLintUtils.RuleCreator(
for (const importMember of imports) {
const importPath = getRelativeImportPath(
importMember,
joinPathFragments(workspaceRoot, entryPointPath.path),
join(workspaceRoot, entryPointPath.path),
sourceProject.data.sourceRoot
);
// we cannot remap, so leave it as is
@ -419,7 +419,7 @@ export default ESLintUtils.RuleCreator(
for (const importMember of imports) {
const importPath = getRelativeImportPath(
importMember,
joinPathFragments(workspaceRoot, entryPointPath),
join(workspaceRoot, entryPointPath),
sourceProject.data.sourceRoot
);
if (importPath) {

View File

@ -1,5 +1,4 @@
import {
joinPathFragments,
logger,
ProjectGraphProjectNode,
readJsonFile,
@ -8,12 +7,12 @@ import {
import { findNodes } from '@nx/js';
import { getModifiers } from '@typescript-eslint/type-utils';
import { existsSync, lstatSync, readdirSync, readFileSync } from 'fs';
import { dirname } from 'path';
import { dirname, join } from 'path';
import ts = require('typescript');
function tryReadBaseJson() {
try {
return readJsonFile(joinPathFragments(workspaceRoot, 'tsconfig.base.json'));
return readJsonFile(join(workspaceRoot, 'tsconfig.base.json'));
} catch (e) {
logger.warn(`Error reading "tsconfig.base.json": \n${JSON.stringify(e)}`);
return null;
@ -126,7 +125,7 @@ export function getRelativeImportPath(exportedMember, filePath, basePath) {
/^index\.[jt]sx?$/.exec(file)
);
if (file) {
filePath = joinPathFragments(filePath, file);
filePath = join(filePath, file);
} else {
return;
}
@ -252,35 +251,23 @@ export function getRelativeImportPath(exportedMember, filePath, basePath) {
let moduleFilePath;
if (modulePath.endsWith('.js') || modulePath.endsWith('.jsx')) {
moduleFilePath = joinPathFragments(dirname(filePath), modulePath);
moduleFilePath = join(dirname(filePath), modulePath);
if (!existsSync(moduleFilePath)) {
const tsifiedModulePath = modulePath.replace(/\.js(x?)$/, '.ts$1');
moduleFilePath = joinPathFragments(
dirname(filePath),
`${tsifiedModulePath}`
);
moduleFilePath = join(dirname(filePath), `${tsifiedModulePath}`);
}
} else if (modulePath.endsWith('.ts') || modulePath.endsWith('.tsx')) {
moduleFilePath = joinPathFragments(dirname(filePath), modulePath);
moduleFilePath = join(dirname(filePath), modulePath);
} else {
moduleFilePath = joinPathFragments(
dirname(filePath),
`${modulePath}.ts`
);
moduleFilePath = join(dirname(filePath), `${modulePath}.ts`);
if (!existsSync(moduleFilePath)) {
// might be a tsx file
moduleFilePath = joinPathFragments(
dirname(filePath),
`${modulePath}.tsx`
);
moduleFilePath = join(dirname(filePath), `${modulePath}.tsx`);
}
}
if (!existsSync(moduleFilePath)) {
// might be an index.ts
moduleFilePath = joinPathFragments(
dirname(filePath),
`${modulePath}/index.ts`
);
moduleFilePath = join(dirname(filePath), `${modulePath}/index.ts`);
}
if (hasMemberExport(exportedMember, moduleFilePath)) {

View File

@ -521,7 +521,7 @@ function getAngularEntryPoint(file: string, projectRoot: string): string {
// we need to find closest existing ng-package.json
// in order to determine if the file matches the secondary entry point
const ngPackageContent = readFileIfExisting(
joinPathFragments(workspaceRoot, parent, 'ng-package.json')
path.join(workspaceRoot, parent, 'ng-package.json')
);
if (ngPackageContent) {
// https://github.com/ng-packagr/ng-packagr/blob/23c718d04eea85e015b4c261310b7bd0c39e5311/src/ng-package.schema.json#L54
@ -539,18 +539,10 @@ function getAngularEntryPoint(file: string, projectRoot: string): string {
export function appIsMFERemote(project: ProjectGraphProjectNode): boolean {
const mfeConfig =
readFileIfExisting(
joinPathFragments(
workspaceRoot,
project.data.root,
'module-federation.config.js'
)
path.join(workspaceRoot, project.data.root, 'module-federation.config.js')
) ||
readFileIfExisting(
joinPathFragments(
workspaceRoot,
project.data.root,
'module-federation.config.ts'
)
path.join(workspaceRoot, project.data.root, 'module-federation.config.ts')
);
if (mfeConfig) {