cleanup(linter): refactor code for dynamic/static imports check (#20897)
This commit is contained in:
parent
26b266faf4
commit
ce933132d0
@ -32,8 +32,8 @@ import {
|
|||||||
isComboDepConstraint,
|
isComboDepConstraint,
|
||||||
isDirectDependency,
|
isDirectDependency,
|
||||||
matchImportWithWildcard,
|
matchImportWithWildcard,
|
||||||
onlyLoadChildren,
|
|
||||||
stringifyTags,
|
stringifyTags,
|
||||||
|
hasStaticImportOfDynamicResource,
|
||||||
} from '../utils/runtime-lint-utils';
|
} from '../utils/runtime-lint-utils';
|
||||||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
||||||
import { basename, dirname, relative } from 'path';
|
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 we import a library using loadChildren, we should not import it using es6imports
|
||||||
if (
|
if (
|
||||||
node.type === AST_NODE_TYPES.ImportDeclaration &&
|
|
||||||
node.importKind !== 'type' &&
|
|
||||||
!checkDynamicDependenciesExceptions.some((a) =>
|
!checkDynamicDependenciesExceptions.some((a) =>
|
||||||
matchImportWithWildcard(a, imp)
|
matchImportWithWildcard(a, imp)
|
||||||
) &&
|
) &&
|
||||||
onlyLoadChildren(
|
hasStaticImportOfDynamicResource(
|
||||||
|
node,
|
||||||
projectGraph,
|
projectGraph,
|
||||||
sourceProject.name,
|
sourceProject.name,
|
||||||
targetProject.name,
|
targetProject.name
|
||||||
[]
|
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
const filesWithLazyImports = findFilesWithDynamicImports(
|
const filesWithLazyImports = findFilesWithDynamicImports(
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import {
|
|||||||
resolveModuleByImport,
|
resolveModuleByImport,
|
||||||
TargetProjectLocator,
|
TargetProjectLocator,
|
||||||
} from '@nx/js/src/internal';
|
} from '@nx/js/src/internal';
|
||||||
|
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
||||||
|
|
||||||
export type Deps = { [projectName: string]: ProjectGraphDependency[] };
|
export type Deps = { [projectName: string]: ProjectGraphDependency[] };
|
||||||
type SingleSourceTagConstraint = {
|
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,
|
graph: ProjectGraph,
|
||||||
sourceProjectName: string,
|
sourceProjectName: string,
|
||||||
targetProjectName: string,
|
targetProjectName: string,
|
||||||
visited: string[]
|
visited: string[]
|
||||||
) {
|
) {
|
||||||
if (visited.indexOf(sourceProjectName) > -1) return false;
|
if (visited.indexOf(sourceProjectName) > -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
(graph.dependencies[sourceProjectName] || []).filter((d) => {
|
(graph.dependencies[sourceProjectName] || []).filter((d) => {
|
||||||
if (d.type !== DependencyType.dynamic) return false;
|
if (d.type !== DependencyType.dynamic) {
|
||||||
if (d.target === targetProjectName) return true;
|
return false;
|
||||||
|
}
|
||||||
|
if (d.target === targetProjectName) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return onlyLoadChildren(graph, d.target, targetProjectName, [
|
return onlyLoadChildren(graph, d.target, targetProjectName, [
|
||||||
...visited,
|
...visited,
|
||||||
sourceProjectName,
|
sourceProjectName,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user