fix(ivy): align NgModule assertions with ViewEngine (#31543)
Aligns Ivy's `NgModule` assertion messages with the ones from `ViewEngine` and adds a few that hadn't been implemented. PR Close #31543
This commit is contained in:
@ -109,6 +109,11 @@ export function compileNgModuleDefs(
|
||||
configurable: true,
|
||||
get: () => {
|
||||
if (ngModuleDef === null) {
|
||||
if (ngDevMode && ngModule.imports && ngModule.imports.indexOf(moduleType) > -1) {
|
||||
// We need to assert this immediately, because allowing it to continue will cause it to
|
||||
// go into an infinite loop before we've reached the point where we throw all the errors.
|
||||
throw new Error(`'${stringifyForError(moduleType)}' module can't import itself`);
|
||||
}
|
||||
ngModuleDef = getCompilerFacade().compileNgModule(
|
||||
angularCoreEnv, `ng:///${moduleType.name}/ngModuleDef.js`, {
|
||||
type: moduleType,
|
||||
@ -156,17 +161,28 @@ export function compileNgModuleDefs(
|
||||
}
|
||||
|
||||
function verifySemanticsOfNgModuleDef(
|
||||
moduleType: NgModuleType, allowDuplicateDeclarationsInRoot: boolean): void {
|
||||
moduleType: NgModuleType, allowDuplicateDeclarationsInRoot: boolean,
|
||||
importingModule?: NgModuleType): void {
|
||||
if (verifiedNgModule.get(moduleType)) return;
|
||||
verifiedNgModule.set(moduleType, true);
|
||||
moduleType = resolveForwardRef(moduleType);
|
||||
const ngModuleDef = getNgModuleDef(moduleType, true);
|
||||
let ngModuleDef: NgModuleDef<any>;
|
||||
if (importingModule) {
|
||||
ngModuleDef = getNgModuleDef(moduleType) !;
|
||||
if (!ngModuleDef) {
|
||||
throw new Error(
|
||||
`Unexpected value '${moduleType.name}' imported by the module '${importingModule.name}'. Please add a @NgModule annotation.`);
|
||||
}
|
||||
} else {
|
||||
ngModuleDef = getNgModuleDef(moduleType, true);
|
||||
}
|
||||
const errors: string[] = [];
|
||||
const declarations = maybeUnwrapFn(ngModuleDef.declarations);
|
||||
const imports = maybeUnwrapFn(ngModuleDef.imports);
|
||||
flatten(imports)
|
||||
.map(unwrapModuleWithProvidersImports)
|
||||
.forEach(mod => verifySemanticsOfNgModuleDef(mod, false));
|
||||
flatten(imports).map(unwrapModuleWithProvidersImports).forEach(mod => {
|
||||
verifySemanticsOfNgModuleImport(mod, moduleType);
|
||||
verifySemanticsOfNgModuleDef(mod, false, moduleType);
|
||||
});
|
||||
const exports = maybeUnwrapFn(ngModuleDef.exports);
|
||||
declarations.forEach(verifyDeclarationsHaveDefinitions);
|
||||
const combinedDeclarations: Type<any>[] = [
|
||||
@ -180,9 +196,10 @@ function verifySemanticsOfNgModuleDef(
|
||||
const ngModule = getAnnotation<NgModule>(moduleType, 'NgModule');
|
||||
if (ngModule) {
|
||||
ngModule.imports &&
|
||||
flatten(ngModule.imports)
|
||||
.map(unwrapModuleWithProvidersImports)
|
||||
.forEach(mod => verifySemanticsOfNgModuleDef(mod, false));
|
||||
flatten(ngModule.imports).map(unwrapModuleWithProvidersImports).forEach(mod => {
|
||||
verifySemanticsOfNgModuleImport(mod, moduleType);
|
||||
verifySemanticsOfNgModuleDef(mod, false, moduleType);
|
||||
});
|
||||
ngModule.bootstrap && ngModule.bootstrap.forEach(verifyCorrectBootstrapType);
|
||||
ngModule.bootstrap && ngModule.bootstrap.forEach(verifyComponentIsPartOfNgModule);
|
||||
ngModule.entryComponents && ngModule.entryComponents.forEach(verifyComponentIsPartOfNgModule);
|
||||
@ -260,6 +277,20 @@ function verifySemanticsOfNgModuleDef(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function verifySemanticsOfNgModuleImport(type: Type<any>, importingModule: Type<any>) {
|
||||
type = resolveForwardRef(type);
|
||||
|
||||
if (getComponentDef(type) || getDirectiveDef(type)) {
|
||||
throw new Error(
|
||||
`Unexpected directive '${type.name}' imported by the module '${importingModule.name}'. Please add a @NgModule annotation.`);
|
||||
}
|
||||
|
||||
if (getPipeDef(type)) {
|
||||
throw new Error(
|
||||
`Unexpected pipe '${type.name}' imported by the module '${importingModule.name}'. Please add a @NgModule annotation.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function unwrapModuleWithProvidersImports(
|
||||
|
Reference in New Issue
Block a user