Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter): use import to check for secondary entrypoint in angular #9285

Merged
merged 1 commit into from Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'))
)
);
}