Skip to content

Commit

Permalink
fix(linter): handle paths correctly in enforce-module-boundaries esli…
Browse files Browse the repository at this point in the history
…nt 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
  • Loading branch information
leosvelperez authored Jun 5, 2024
1 parent f4b379f commit ffea1d5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 36 deletions.
6 changes: 3 additions & 3 deletions packages/eslint-plugin/src/rules/enforce-module-boundaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 9 additions & 22 deletions packages/eslint-plugin/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
joinPathFragments,
logger,
ProjectGraphProjectNode,
readJsonFile,
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down
14 changes: 3 additions & 11 deletions packages/eslint-plugin/src/utils/runtime-lint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit ffea1d5

Please sign in to comment.