Skip to content

Commit

Permalink
fix(linter): improve error message for forbidden imports of lazy-load…
Browse files Browse the repository at this point in the history
…ed libs (#17635)

Co-authored-by: Miroslav Jonas <[email protected]>
  • Loading branch information
miluoshi and meeroslav authored Jun 19, 2023
1 parent 5a7d6db commit ef5813a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
21 changes: 17 additions & 4 deletions packages/eslint-plugin/src/rules/enforce-module-boundaries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,20 +1221,33 @@ Violation detected in:
target: 'otherName',
type: DependencyType.dynamic,
},
{
source: 'mylibName',
target: 'otherName',
type: DependencyType.static,
},
],
},
},
{
mylibName: [createFile(`libs/mylib/src/main.ts`)],
mylibName: [
createFile(`libs/mylib/src/main.ts`, [
['otherName', 'static'],
['otherName', 'dynamic'],
]),
],
otherName: [createFile(`libs/other/index.ts`)],
}
);
if (importKind === 'type') {
expect(failures.length).toEqual(0);
} else {
expect(failures[0].message).toEqual(
'Imports of lazy-loaded libraries are forbidden'
);
expect(failures[0].message).toMatchInlineSnapshot(`
"Static imports of lazy-loaded libraries are forbidden.
Library "otherName" is lazy-loaded in these files:
- libs/mylib/src/main.ts"
`);
}
}
);
Expand Down
14 changes: 13 additions & 1 deletion packages/eslint-plugin/src/rules/enforce-module-boundaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { isRelativePath } from 'nx/src/utils/fileutils';
import {
checkCircularPath,
findFilesWithDynamicImports,
findFilesInCircularPath,
} from '../utils/graph-utils';
import {
Expand Down Expand Up @@ -125,7 +126,7 @@ export default createESLintRule<Options, MessageIds>({
noImportsOfE2e: 'Imports of e2e projects are forbidden',
noImportOfNonBuildableLibraries:
'Buildable libraries cannot import or export from non-buildable libraries',
noImportsOfLazyLoadedLibraries: `Imports of lazy-loaded libraries are forbidden`,
noImportsOfLazyLoadedLibraries: `Static imports of lazy-loaded libraries are forbidden.\n\nLibrary "{{targetProjectName}}" is lazy-loaded in these files:\n{{filePaths}}`,
projectWithoutTagsCannotHaveDependencies: `A project without tags matching at least one constraint cannot depend on any libraries`,
bannedExternalImportsViolation: `A project tagged with "{{sourceTag}}" is not allowed to import the "{{package}}" package`,
nestedBannedExternalImportsViolation: `A project tagged with "{{sourceTag}}" is not allowed to import the "{{package}}" package. Nested import found at {{childProjectName}}`,
Expand Down Expand Up @@ -515,7 +516,18 @@ export default createESLintRule<Options, MessageIds>({
[]
)
) {
const filesWithLazyImports = findFilesWithDynamicImports(
projectFileMap,
sourceProject.name,
targetProject.name
);
context.report({
data: {
filePaths: filesWithLazyImports
.map(({ file }) => `- ${file}`)
.join('\n'),
targetProjectName: targetProject.name,
},
node,
messageId: 'noImportsOfLazyLoadedLibraries',
});
Expand Down
28 changes: 27 additions & 1 deletion packages/eslint-plugin/src/utils/graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import type {
ProjectGraph,
ProjectGraphProjectNode,
} from '@nx/devkit';
import { fileDataDepTarget } from 'nx/src/config/project-graph';
import {
DependencyType,
fileDataDepTarget,
fileDataDepType,
} from 'nx/src/config/project-graph';

interface Reach {
graph: ProjectGraph;
Expand Down Expand Up @@ -158,3 +162,25 @@ export function findFilesInCircularPath(

return filePathChain;
}

export function findFilesWithDynamicImports(
projectFileMap: ProjectFileMap,
sourceProjectName: string,
targetProjectName: string
): FileData[] {
const files: FileData[] = [];
projectFileMap[sourceProjectName].forEach((file) => {
if (!file.deps) return;
if (
file.deps.some(
(d) =>
fileDataDepTarget(d) === targetProjectName &&
fileDataDepType(d) === DependencyType.dynamic
)
) {
files.push(file);
}
});

return files;
}

1 comment on commit ef5813a

@vercel
Copy link

@vercel vercel bot commented on ef5813a Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.