Skip to content

Commit

Permalink
fix(linter): use import to check for secondary entrypoint in angular (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Mar 11, 2022
1 parent a32d46c commit 5db394e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
Expand Up @@ -20,6 +20,7 @@ import {
hasNoneOfTheseTags,
groupImports,
MappedProjectGraphNode,
isAngularSecondaryEntrypoint,
} from '@nrwl/workspace/src/utils/runtime-lint-utils';
import {
AST_NODE_TYPES,
Expand All @@ -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 {
Expand Down Expand Up @@ -319,7 +319,7 @@ export default createESLintRule<Options, MessageIds>({
if (
!allowCircularSelfDependency &&
!isRelativePath(imp) &&
!isAngularSecondaryEntrypoint(sourceFilePath)
!isAngularSecondaryEntrypoint(targetProjectLocator, imp)
) {
context.report({
node,
Expand Down
8 changes: 0 additions & 8 deletions packages/eslint-plugin-nx/src/utils/angular.ts

This file was deleted.

7 changes: 6 additions & 1 deletion packages/workspace/src/core/target-project-locator.ts
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -24,6 +24,7 @@ import {
stringifyTags,
hasNoneOfTheseTags,
MappedProjectGraphNode,
isAngularSecondaryEntrypoint,
} from '../utils/runtime-lint-utils';
import { normalize } from 'path';
import { readNxJson } from '../core/file-utils';
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions packages/workspace/src/utils/runtime-lint-utils.ts
Expand Up @@ -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<T = any> = ProjectGraphProjectNode<T> & {
data: {
Expand Down Expand Up @@ -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'))
)
);
}

0 comments on commit 5db394e

Please sign in to comment.