From 5db394ecd83b17d44106f3b074bbd201e9e33a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Jona=C5=A1?= Date: Fri, 11 Mar 2022 14:52:43 +0100 Subject: [PATCH] fix(linter): use import to check for secondary entrypoint in angular (#9285) --- .../src/rules/enforce-module-boundaries.ts | 4 ++-- .../eslint-plugin-nx/src/utils/angular.ts | 8 ------- .../src/core/target-project-locator.ts | 7 +++++- .../tslint/nxEnforceModuleBoundariesRule.ts | 7 +++++- .../workspace/src/utils/runtime-lint-utils.ts | 23 +++++++++++++++++++ 5 files changed, 37 insertions(+), 12 deletions(-) delete mode 100644 packages/eslint-plugin-nx/src/utils/angular.ts diff --git a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts index 454446e07a9c3..f0dbb8a409b5d 100644 --- a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts +++ b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts @@ -20,6 +20,7 @@ import { hasNoneOfTheseTags, groupImports, MappedProjectGraphNode, + isAngularSecondaryEntrypoint, } from '@nrwl/workspace/src/utils/runtime-lint-utils'; import { AST_NODE_TYPES, @@ -42,7 +43,6 @@ import { findFilesInCircularPath, } from '@nrwl/workspace/src/utils/graph-utils'; import { isRelativePath } from '@nrwl/workspace/src/utilities/fileutils'; -import { isSecondaryEntrypoint as isAngularSecondaryEntrypoint } from '../utils/angular'; import * as chalk from 'chalk'; import { basename, dirname, relative } from 'path'; import { @@ -319,7 +319,7 @@ export default createESLintRule({ if ( !allowCircularSelfDependency && !isRelativePath(imp) && - !isAngularSecondaryEntrypoint(sourceFilePath) + !isAngularSecondaryEntrypoint(targetProjectLocator, imp) ) { context.report({ node, diff --git a/packages/eslint-plugin-nx/src/utils/angular.ts b/packages/eslint-plugin-nx/src/utils/angular.ts deleted file mode 100644 index ecccd8481d2e5..0000000000000 --- a/packages/eslint-plugin-nx/src/utils/angular.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { joinPathFragments } from '@nrwl/devkit'; -import { existsSync } from 'fs'; - -export function isSecondaryEntrypoint(path: string) { - return path.endsWith('src/index.ts') - ? existsSync(joinPathFragments(path, '../../', 'ng-package.json')) - : false; -} diff --git a/packages/workspace/src/core/target-project-locator.ts b/packages/workspace/src/core/target-project-locator.ts index 8d7ee90ee843c..631c2d30346b3 100644 --- a/packages/workspace/src/core/target-project-locator.ts +++ b/packages/workspace/src/core/target-project-locator.ts @@ -86,7 +86,12 @@ export class TargetProjectLocator { return null; } - private findPaths(normalizedImportExpr: string): string[] | undefined { + /** + * Return file paths matching the import relative to the repo root + * @param normalizedImportExpr + * @returns + */ + findPaths(normalizedImportExpr: string): string[] | undefined { if (!this.paths) { return undefined; } diff --git a/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts b/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts index 683e55078723d..c8995cf481e0e 100644 --- a/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts +++ b/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts @@ -24,6 +24,7 @@ import { stringifyTags, hasNoneOfTheseTags, MappedProjectGraphNode, + isAngularSecondaryEntrypoint, } from '../utils/runtime-lint-utils'; import { normalize } from 'path'; import { readNxJson } from '../core/file-utils'; @@ -183,7 +184,11 @@ class EnforceModuleBoundariesWalker extends Lint.RuleWalker { // same project => allow if (sourceProject === targetProject) { - if (!this.allowCircularSelfDependency && !isRelativePath(imp)) { + if ( + !this.allowCircularSelfDependency && + !isRelativePath(imp) && + !isAngularSecondaryEntrypoint(this.targetProjectLocator, imp) + ) { const error = `Projects should use relative imports to import from other files within the same project. Use "./path/to/file" instead of import from "${imp}"`; this.addFailureAt(node.getStart(), node.getWidth(), error); } else { diff --git a/packages/workspace/src/utils/runtime-lint-utils.ts b/packages/workspace/src/utils/runtime-lint-utils.ts index 831b6d81c8889..c1dfbf18491ca 100644 --- a/packages/workspace/src/utils/runtime-lint-utils.ts +++ b/packages/workspace/src/utils/runtime-lint-utils.ts @@ -8,11 +8,13 @@ import { DependencyType, parseJson, ProjectGraphExternalNode, + joinPathFragments, } from '@nrwl/devkit'; import { TargetProjectLocator } from '../core/target-project-locator'; import { join } from 'path'; import { appRootPath } from './app-root'; import { getPath, pathExists } from './graph-utils'; +import { existsSync } from 'fs'; export type MappedProjectGraphNode = ProjectGraphProjectNode & { data: { @@ -340,3 +342,24 @@ export function groupImports( .map((entry) => `import { ${entry.member} } from '${entry.importPath}';`) .join('\n'); } + +/** + * Checks if import points to a secondary entry point in Angular project + * @param targetProjectLocator + * @param importExpr + * @returns + */ +export function isAngularSecondaryEntrypoint( + targetProjectLocator: TargetProjectLocator, + importExpr: string +): boolean { + const targetFiles = targetProjectLocator.findPaths(importExpr); + return ( + targetFiles && + targetFiles.some( + (file) => + file.endsWith('src/index.ts') && + existsSync(joinPathFragments(file, '../../', 'ng-package.json')) + ) + ); +}