Skip to content

Commit

Permalink
cleanup(linter): refactor code for dynamic/static imports check (#20897)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Feb 25, 2024
1 parent 26b266f commit ce93313
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
10 changes: 4 additions & 6 deletions packages/eslint-plugin/src/rules/enforce-module-boundaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import {
isComboDepConstraint,
isDirectDependency,
matchImportWithWildcard,
onlyLoadChildren,
stringifyTags,
hasStaticImportOfDynamicResource,
} from '../utils/runtime-lint-utils';
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import { basename, dirname, relative } from 'path';
Expand Down Expand Up @@ -566,16 +566,14 @@ export default ESLintUtils.RuleCreator(

// if we import a library using loadChildren, we should not import it using es6imports
if (
node.type === AST_NODE_TYPES.ImportDeclaration &&
node.importKind !== 'type' &&
!checkDynamicDependenciesExceptions.some((a) =>
matchImportWithWildcard(a, imp)
) &&
onlyLoadChildren(
hasStaticImportOfDynamicResource(
node,
projectGraph,
sourceProject.name,
targetProject.name,
[]
targetProject.name
)
) {
const filesWithLazyImports = findFilesWithDynamicImports(
Expand Down
34 changes: 30 additions & 4 deletions packages/eslint-plugin/src/utils/runtime-lint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
resolveModuleByImport,
TargetProjectLocator,
} from '@nx/js/src/internal';
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';

export type Deps = { [projectName: string]: ProjectGraphDependency[] };
type SingleSourceTagConstraint = {
Expand Down Expand Up @@ -202,17 +203,42 @@ export function findConstraintsFor(
});
}

export function onlyLoadChildren(
export function hasStaticImportOfDynamicResource(
node:
| TSESTree.ImportDeclaration
| TSESTree.ImportExpression
| TSESTree.ExportAllDeclaration
| TSESTree.ExportNamedDeclaration,
graph: ProjectGraph,
sourceProjectName: string,
targetProjectName: string
): boolean {
if (
node.type !== AST_NODE_TYPES.ImportDeclaration ||
node.importKind === 'type'
) {
return false;
}
return onlyLoadChildren(graph, sourceProjectName, targetProjectName, []);
}

function onlyLoadChildren(
graph: ProjectGraph,
sourceProjectName: string,
targetProjectName: string,
visited: string[]
) {
if (visited.indexOf(sourceProjectName) > -1) return false;
if (visited.indexOf(sourceProjectName) > -1) {
return false;
}
return (
(graph.dependencies[sourceProjectName] || []).filter((d) => {
if (d.type !== DependencyType.dynamic) return false;
if (d.target === targetProjectName) return true;
if (d.type !== DependencyType.dynamic) {
return false;
}
if (d.target === targetProjectName) {
return true;
}
return onlyLoadChildren(graph, d.target, targetProjectName, [
...visited,
sourceProjectName,
Expand Down

0 comments on commit ce93313

Please sign in to comment.