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

View File

@ -1,5 +1,4 @@
import { import {
joinPathFragments,
logger, logger,
ProjectGraphProjectNode, ProjectGraphProjectNode,
readJsonFile, readJsonFile,
@ -8,12 +7,12 @@ import {
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, lstatSync, readdirSync, readFileSync } from 'fs'; import { existsSync, lstatSync, readdirSync, readFileSync } from 'fs';
import { dirname } from 'path'; import { dirname, join } from 'path';
import ts = require('typescript'); import ts = require('typescript');
function tryReadBaseJson() { function tryReadBaseJson() {
try { try {
return readJsonFile(joinPathFragments(workspaceRoot, 'tsconfig.base.json')); return readJsonFile(join(workspaceRoot, 'tsconfig.base.json'));
} catch (e) { } catch (e) {
logger.warn(`Error reading "tsconfig.base.json": \n${JSON.stringify(e)}`); logger.warn(`Error reading "tsconfig.base.json": \n${JSON.stringify(e)}`);
return null; return null;
@ -126,7 +125,7 @@ export function getRelativeImportPath(exportedMember, filePath, basePath) {
/^index\.[jt]sx?$/.exec(file) /^index\.[jt]sx?$/.exec(file)
); );
if (file) { if (file) {
filePath = joinPathFragments(filePath, file); filePath = join(filePath, file);
} else { } else {
return; return;
} }
@ -252,35 +251,23 @@ export function getRelativeImportPath(exportedMember, filePath, basePath) {
let moduleFilePath; let moduleFilePath;
if (modulePath.endsWith('.js') || modulePath.endsWith('.jsx')) { if (modulePath.endsWith('.js') || modulePath.endsWith('.jsx')) {
moduleFilePath = joinPathFragments(dirname(filePath), modulePath); moduleFilePath = join(dirname(filePath), modulePath);
if (!existsSync(moduleFilePath)) { if (!existsSync(moduleFilePath)) {
const tsifiedModulePath = modulePath.replace(/\.js(x?)$/, '.ts$1'); const tsifiedModulePath = modulePath.replace(/\.js(x?)$/, '.ts$1');
moduleFilePath = joinPathFragments( moduleFilePath = join(dirname(filePath), `${tsifiedModulePath}`);
dirname(filePath),
`${tsifiedModulePath}`
);
} }
} else if (modulePath.endsWith('.ts') || modulePath.endsWith('.tsx')) { } else if (modulePath.endsWith('.ts') || modulePath.endsWith('.tsx')) {
moduleFilePath = joinPathFragments(dirname(filePath), modulePath); moduleFilePath = join(dirname(filePath), modulePath);
} else { } else {
moduleFilePath = joinPathFragments( moduleFilePath = join(dirname(filePath), `${modulePath}.ts`);
dirname(filePath),
`${modulePath}.ts`
);
if (!existsSync(moduleFilePath)) { if (!existsSync(moduleFilePath)) {
// might be a tsx file // might be a tsx file
moduleFilePath = joinPathFragments( moduleFilePath = join(dirname(filePath), `${modulePath}.tsx`);
dirname(filePath),
`${modulePath}.tsx`
);
} }
} }
if (!existsSync(moduleFilePath)) { if (!existsSync(moduleFilePath)) {
// might be an index.ts // might be an index.ts
moduleFilePath = joinPathFragments( moduleFilePath = join(dirname(filePath), `${modulePath}/index.ts`);
dirname(filePath),
`${modulePath}/index.ts`
);
} }
if (hasMemberExport(exportedMember, moduleFilePath)) { 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 // we need to find closest existing ng-package.json
// in order to determine if the file matches the secondary entry point // in order to determine if the file matches the secondary entry point
const ngPackageContent = readFileIfExisting( const ngPackageContent = readFileIfExisting(
joinPathFragments(workspaceRoot, parent, 'ng-package.json') path.join(workspaceRoot, parent, 'ng-package.json')
); );
if (ngPackageContent) { if (ngPackageContent) {
// https://github.com/ng-packagr/ng-packagr/blob/23c718d04eea85e015b4c261310b7bd0c39e5311/src/ng-package.schema.json#L54 // 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 { export function appIsMFERemote(project: ProjectGraphProjectNode): boolean {
const mfeConfig = const mfeConfig =
readFileIfExisting( readFileIfExisting(
joinPathFragments( path.join(workspaceRoot, project.data.root, 'module-federation.config.js')
workspaceRoot,
project.data.root,
'module-federation.config.js'
)
) || ) ||
readFileIfExisting( readFileIfExisting(
joinPathFragments( path.join(workspaceRoot, project.data.root, 'module-federation.config.ts')
workspaceRoot,
project.data.root,
'module-federation.config.ts'
)
); );
if (mfeConfig) { if (mfeConfig) {