From ce933132d0f472a1b83cf98432ebdf2c8a366b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Jona=C5=A1?= Date: Sun, 25 Feb 2024 17:59:51 +0100 Subject: [PATCH] cleanup(linter): refactor code for dynamic/static imports check (#20897) --- .../src/rules/enforce-module-boundaries.ts | 10 +++--- .../src/utils/runtime-lint-utils.ts | 34 ++++++++++++++++--- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin/src/rules/enforce-module-boundaries.ts b/packages/eslint-plugin/src/rules/enforce-module-boundaries.ts index 12d6eacff195a..0051a2abf28ae 100644 --- a/packages/eslint-plugin/src/rules/enforce-module-boundaries.ts +++ b/packages/eslint-plugin/src/rules/enforce-module-boundaries.ts @@ -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'; @@ -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( diff --git a/packages/eslint-plugin/src/utils/runtime-lint-utils.ts b/packages/eslint-plugin/src/utils/runtime-lint-utils.ts index e991cd477a750..3ee8f9452f8e3 100644 --- a/packages/eslint-plugin/src/utils/runtime-lint-utils.ts +++ b/packages/eslint-plugin/src/utils/runtime-lint-utils.ts @@ -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 = { @@ -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,