fix(compiler): report an error for recursive module references
Modules that directly or indirectly export or imported themselves reports an error instead of generating a stack fault. Fixes: #19979
This commit is contained in:

committed by
Alex Rickabaugh

parent
5efea2f6a0
commit
33c0ee3441
@ -444,11 +444,12 @@ export class CompileMetadataResolver {
|
||||
this._ngModuleResolver.isNgModule(type);
|
||||
}
|
||||
|
||||
getNgModuleSummary(moduleType: any): cpl.CompileNgModuleSummary|null {
|
||||
getNgModuleSummary(moduleType: any, alreadyCollecting: Set<any>|null = null):
|
||||
cpl.CompileNgModuleSummary|null {
|
||||
let moduleSummary: cpl.CompileNgModuleSummary|null =
|
||||
<cpl.CompileNgModuleSummary>this._loadSummary(moduleType, cpl.CompileSummaryKind.NgModule);
|
||||
if (!moduleSummary) {
|
||||
const moduleMeta = this.getNgModuleMetadata(moduleType, false);
|
||||
const moduleMeta = this.getNgModuleMetadata(moduleType, false, alreadyCollecting);
|
||||
moduleSummary = moduleMeta ? moduleMeta.toSummary() : null;
|
||||
if (moduleSummary) {
|
||||
this._summaryCache.set(moduleType, moduleSummary);
|
||||
@ -476,7 +477,9 @@ export class CompileMetadataResolver {
|
||||
return Promise.all(loading);
|
||||
}
|
||||
|
||||
getNgModuleMetadata(moduleType: any, throwIfNotFound = true): cpl.CompileNgModuleMetadata|null {
|
||||
getNgModuleMetadata(
|
||||
moduleType: any, throwIfNotFound = true,
|
||||
alreadyCollecting: Set<any>|null = null): cpl.CompileNgModuleMetadata|null {
|
||||
moduleType = resolveForwardRef(moduleType);
|
||||
let compileMeta = this._ngModuleCache.get(moduleType);
|
||||
if (compileMeta) {
|
||||
@ -514,7 +517,18 @@ export class CompileMetadataResolver {
|
||||
|
||||
if (importedModuleType) {
|
||||
if (this._checkSelfImport(moduleType, importedModuleType)) return;
|
||||
const importedModuleSummary = this.getNgModuleSummary(importedModuleType);
|
||||
if (!alreadyCollecting) alreadyCollecting = new Set();
|
||||
if (alreadyCollecting.has(importedModuleType)) {
|
||||
this._reportError(
|
||||
syntaxError(
|
||||
`${this._getTypeDescriptor(importedModuleType)} '${stringifyType(importedType)}' is imported recursively by the module '${stringifyType(moduleType)}'.`),
|
||||
moduleType);
|
||||
return;
|
||||
}
|
||||
alreadyCollecting.add(importedModuleType);
|
||||
const importedModuleSummary =
|
||||
this.getNgModuleSummary(importedModuleType, alreadyCollecting);
|
||||
alreadyCollecting.delete(importedModuleType);
|
||||
if (!importedModuleSummary) {
|
||||
this._reportError(
|
||||
syntaxError(
|
||||
@ -542,7 +556,17 @@ export class CompileMetadataResolver {
|
||||
moduleType);
|
||||
return;
|
||||
}
|
||||
const exportedModuleSummary = this.getNgModuleSummary(exportedType);
|
||||
if (!alreadyCollecting) alreadyCollecting = new Set();
|
||||
if (alreadyCollecting.has(exportedType)) {
|
||||
this._reportError(
|
||||
syntaxError(
|
||||
`${this._getTypeDescriptor(exportedType)} '${stringify(exportedType)}' is exported recursively by the module '${stringifyType(moduleType)}'`),
|
||||
moduleType);
|
||||
return;
|
||||
}
|
||||
alreadyCollecting.add(exportedType);
|
||||
const exportedModuleSummary = this.getNgModuleSummary(exportedType, alreadyCollecting);
|
||||
alreadyCollecting.delete(exportedType);
|
||||
if (exportedModuleSummary) {
|
||||
exportedModules.push(exportedModuleSummary);
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user