refactor(core): rename ngModuleDef to ɵmod (#33142)

Module defs are not considered public API, so the property
that contains them should be prefixed with Angular's marker
for "private" ('ɵ') to discourage apps from relying on def
APIs directly.

This commit adds the prefix and shortens the name from
ngModuleDef to mod. This is because property names
cannot be minified by Uglify without turning on property
mangling (which most apps have turned off) and are thus
size-sensitive.

PR Close #33142
This commit is contained in:
Kara Erickson
2019-10-14 07:20:26 -07:00
committed by Miško Hevery
parent d62eff7316
commit fc93dafab1
24 changed files with 87 additions and 89 deletions

View File

@ -268,14 +268,14 @@ A.ɵdir = ɵngcc0.ɵɵdefineDirective({ type: A, selectors: [["", "a", ""]] });
decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses);
const addDefinitionsSpy = testFormatter.addDefinitions as jasmine.Spy;
const definitions: string = addDefinitionsSpy.calls.first().args[2];
const ngModuleDef = definitions.indexOf('ngModuleDef');
expect(ngModuleDef).not.toEqual(-1, 'ngModuleDef should exist');
const ngModuleDef = definitions.indexOf('ɵmod');
expect(ngModuleDef).not.toEqual(-1, 'ɵmod should exist');
const ngInjectorDef = definitions.indexOf('ngInjectorDef');
expect(ngInjectorDef).not.toEqual(-1, 'ngInjectorDef should exist');
const setClassMetadata = definitions.indexOf('setClassMetadata');
expect(setClassMetadata).not.toEqual(-1, 'setClassMetadata call should exist');
expect(setClassMetadata)
.toBeGreaterThan(ngModuleDef, 'setClassMetadata should follow ngModuleDef');
.toBeGreaterThan(ngModuleDef, 'setClassMetadata should follow ɵmod');
expect(setClassMetadata)
.toBeGreaterThan(ngInjectorDef, 'setClassMetadata should follow ngInjectorDef');
});

View File

@ -24,7 +24,7 @@ import {ReferencesRegistry} from './references_registry';
import {combineResolvers, findAngularDecorator, forwardRefResolver, getValidConstructorDependencies, isExpressionForwardReference, toR3Reference, unwrapExpression} from './util';
export interface NgModuleAnalysis {
ngModuleDef: R3NgModuleMetadata;
ɵmod: R3NgModuleMetadata;
ngInjectorDef: R3InjectorMetadata;
metadataStmt: Statement|null;
declarations: Reference<ClassDeclaration>[];
@ -236,8 +236,7 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
return {
analysis: {
id,
ngModuleDef,
ngInjectorDef,
ɵmod: ngModuleDef, ngInjectorDef,
declarations: declarationRefs,
exports: exportRefs,
metadataStmt: generateSetClassMetadataCall(
@ -282,7 +281,7 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
compile(node: ClassDeclaration, analysis: NgModuleAnalysis): CompileResult[] {
const ngInjectorDef = compileInjector(analysis.ngInjectorDef);
const ngModuleDef = compileNgModule(analysis.ngModuleDef);
const ngModuleDef = compileNgModule(analysis.ɵmod);
const ngModuleStatements = ngModuleDef.additionalStatements;
if (analysis.metadataStmt !== null) {
ngModuleStatements.push(analysis.metadataStmt);
@ -309,7 +308,7 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
}
const res: CompileResult[] = [
{
name: 'ngModuleDef',
name: 'ɵmod',
initializer: ngModuleDef.expression,
statements: ngModuleStatements,
type: ngModuleDef.type,

View File

@ -77,7 +77,7 @@ runInEachFileSystem(() => {
if (detected === undefined) {
return fail('Failed to recognize @NgModule');
}
const moduleDef = handler.analyze(TestModule, detected.metadata).analysis !.ngModuleDef;
const moduleDef = handler.analyze(TestModule, detected.metadata).analysis !.ɵmod;
expect(getReferenceIdentifierTexts(moduleDef.declarations)).toEqual(['TestComp']);
expect(getReferenceIdentifierTexts(moduleDef.exports)).toEqual(['TestComp']);

View File

@ -38,7 +38,7 @@ export class DtsMetadataReader implements MetadataReader {
// This operation is explicitly not memoized, as it depends on `ref.ownedByModuleGuess`.
// TODO(alxhub): investigate caching of .d.ts module metadata.
const ngModuleDef = this.reflector.getMembersOfClass(clazz).find(
member => member.name === 'ngModuleDef' && member.isStatic);
member => member.name === 'ɵmod' && member.isStatic);
if (ngModuleDef === undefined) {
return null;
} else if (

View File

@ -90,7 +90,7 @@ runInEachFileSystem(() => {
}
export declare class Module {
static ngModuleDef: ModuleMeta<Module, [typeof Dir], never, [typeof Dir]>;
static ɵmod: ModuleMeta<Module, [typeof Dir], never, [typeof Dir]>;
}
`
});
@ -107,11 +107,11 @@ runInEachFileSystem(() => {
}
export declare class ModuleA {
static ngModuleDef: ModuleMeta<ModuleA, [typeof Dir], never, [typeof Dir]>;
static ɵmod: ModuleMeta<ModuleA, [typeof Dir], never, [typeof Dir]>;
}
export declare class ModuleB {
static ngModuleDef: ModuleMeta<ModuleB, never, never, [typeof ModuleA]>;
static ɵmod: ModuleMeta<ModuleB, never, never, [typeof ModuleA]>;
}
`
});
@ -128,14 +128,14 @@ runInEachFileSystem(() => {
}
export declare class ModuleA {
static ngModuleDef: ModuleMeta<ModuleA, [typeof Dir], never, [typeof Dir]>;
static ɵmod: ModuleMeta<ModuleA, [typeof Dir], never, [typeof Dir]>;
}
`,
'exported': `
import * as d from 'declaration';
export declare class ModuleB {
static ngModuleDef: ModuleMeta<ModuleB, never, never, [typeof d.ModuleA]>;
static ɵmod: ModuleMeta<ModuleB, never, never, [typeof d.ModuleA]>;
}
`
});
@ -156,7 +156,7 @@ runInEachFileSystem(() => {
}
export declare class DeepModule {
static ngModuleDef: ModuleMeta<DeepModule, [typeof DeepDir], never, [typeof DeepDir]>;
static ɵmod: ModuleMeta<DeepModule, [typeof DeepDir], never, [typeof DeepDir]>;
}
`,
'middle': `
@ -167,7 +167,7 @@ runInEachFileSystem(() => {
}
export declare class MiddleModule {
static ngModuleDef: ModuleMeta<MiddleModule, [typeof MiddleDir], never, [typeof MiddleDir, typeof deep.DeepModule]>;
static ɵmod: ModuleMeta<MiddleModule, [typeof MiddleDir], never, [typeof MiddleDir, typeof deep.DeepModule]>;
}
`,
'shallow': `
@ -178,7 +178,7 @@ runInEachFileSystem(() => {
}
export declare class ShallowModule {
static ngModuleDef: ModuleMeta<ShallowModule, [typeof ShallowDir], never, [typeof ShallowDir, typeof middle.MiddleModule]>;
static ɵmod: ModuleMeta<ShallowModule, [typeof ShallowDir], never, [typeof ShallowDir, typeof middle.MiddleModule]>;
}
`,
},
@ -206,7 +206,7 @@ runInEachFileSystem(() => {
}
export declare class DeepModule {
static ngModuleDef: ModuleMeta<DeepModule, [typeof DeepDir], never, [typeof DeepDir]>;
static ɵmod: ModuleMeta<DeepModule, [typeof DeepDir], never, [typeof DeepDir]>;
}
`,
'middle': `
@ -217,7 +217,7 @@ runInEachFileSystem(() => {
}
export declare class MiddleModule {
static ngModuleDef: ModuleMeta<MiddleModule, [typeof MiddleDir], [typeof deep.DeepModule], [typeof MiddleDir, typeof deep.DeepDir]>;
static ɵmod: ModuleMeta<MiddleModule, [typeof MiddleDir], [typeof deep.DeepModule], [typeof MiddleDir, typeof deep.DeepDir]>;
}
`,
'shallow': `
@ -228,7 +228,7 @@ runInEachFileSystem(() => {
}
export declare class ShallowModule {
static ngModuleDef: ModuleMeta<ShallowModule, [typeof ShallowDir], never, [typeof ShallowDir, typeof middle.MiddleModule]>;
static ɵmod: ModuleMeta<ShallowModule, [typeof ShallowDir], never, [typeof ShallowDir, typeof middle.MiddleModule]>;
}
`,
},
@ -257,11 +257,11 @@ runInEachFileSystem(() => {
}
export declare class DeepModule {
static ngModuleDef: ModuleMeta<DeepModule, [typeof DeepDir], never, [typeof DeepDir]>;
static ɵmod: ModuleMeta<DeepModule, [typeof DeepDir], never, [typeof DeepDir]>;
}
export declare class DeepExportModule {
static ngModuleDef: ModuleMeta<DeepExportModule, never, never, [typeof DeepModule]>;
static ɵmod: ModuleMeta<DeepExportModule, never, never, [typeof DeepModule]>;
}
`,
},

View File

@ -24,7 +24,7 @@ const R3_DEF_NAME_PATTERN = [
'ɵdir',
'ngInjectableDef',
'ngInjectorDef',
'ngModuleDef',
'ɵmod',
'ɵpipe',
'ɵfac',
].join('|');

View File

@ -376,7 +376,7 @@ runInEachFileSystem(os => {
expect(jsContents).toContain('TestDirective.ɵdir = i0.ɵɵdefineDirective');
expect(jsContents).toContain('TestPipe.ɵpipe = i0.ɵɵdefinePipe');
expect(jsContents).toContain('TestInjectable.ngInjectableDef = i0.ɵɵdefineInjectable');
expect(jsContents).toContain('MyModule.ngModuleDef = i0.ɵɵdefineNgModule');
expect(jsContents).toContain('MyModule.ɵmod = i0.ɵɵdefineNgModule');
expect(jsContents).toContain('MyModule.ngInjectorDef = i0.ɵɵdefineInjector');
expect(jsContents).toContain('inputs: { input: "input" }');
expect(jsContents).toContain('outputs: { output: "output" }');
@ -512,7 +512,7 @@ runInEachFileSystem(os => {
'static ɵcmp: i0.ɵɵComponentDefWithMeta<TestCmp, "test-cmp", never, {}, {}, never>');
expect(dtsContents)
.toContain(
'static ngModuleDef: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], never, never>');
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], never, never>');
expect(dtsContents).not.toContain('__decorate');
});
@ -607,7 +607,7 @@ runInEachFileSystem(os => {
declare class RouterModule {
static forRoot(): ModuleWithProviders<RouterModule>;
static ngModuleDef: ɵɵNgModuleDefWithMeta<RouterModule, [typeof RouterComp], never, [typeof RouterComp]>;
static ɵmod: ɵɵNgModuleDefWithMeta<RouterModule, [typeof RouterComp], never, [typeof RouterComp]>;
}
`);
@ -657,7 +657,7 @@ runInEachFileSystem(os => {
const dtsContents = env.getContents('test.d.ts');
expect(dtsContents)
.toContain(
'static ngModuleDef: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
expect(dtsContents).toContain('static ngInjectorDef: i0.ɵɵInjectorDef');
});
@ -697,7 +697,7 @@ runInEachFileSystem(os => {
const dtsContents = env.getContents('test.d.ts');
expect(dtsContents)
.toContain(
'static ngModuleDef: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
expect(dtsContents).toContain('static ngInjectorDef: i0.ɵɵInjectorDef');
});
@ -741,7 +741,7 @@ runInEachFileSystem(os => {
const dtsContents = env.getContents('test.d.ts');
expect(dtsContents)
.toContain(
'static ngModuleDef: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
expect(dtsContents).toContain('static ngInjectorDef: i0.ɵɵInjectorDef');
});
@ -1180,7 +1180,7 @@ runInEachFileSystem(os => {
expect(jsContents).toContain('TestCmp.ɵcmp =');
expect(jsContents).toContain('TestDir.ɵdir =');
expect(jsContents).toContain('TestPipe.ɵpipe =');
expect(jsContents).toContain('TestNgModule.ngModuleDef =');
expect(jsContents).toContain('TestNgModule.ɵmod =');
// Validate that each class also has an injectable definition.
expect(jsContents).toContain('TestCmp.ngInjectableDef =');
@ -1453,7 +1453,7 @@ runInEachFileSystem(os => {
declare class RouterModule {
static forRoot(): ModuleWithProviders<RouterModule>;
static ngModuleDef: ɵɵNgModuleDefWithMeta<RouterModule, never, never, never>;
static ɵmod: ɵɵNgModuleDefWithMeta<RouterModule, never, never, never>;
}
`);
@ -1492,7 +1492,7 @@ runInEachFileSystem(os => {
env.write('node_modules/router/internal.d.ts', `
import {ɵɵNgModuleDefWithMeta} from '@angular/core';
export declare class InternalRouterModule {
static ngModuleDef: ɵɵNgModuleDefWithMeta<InternalRouterModule, never, never, never>;
static ɵmod: ɵɵNgModuleDefWithMeta<InternalRouterModule, never, never, never>;
}
`);
@ -1508,14 +1508,14 @@ runInEachFileSystem(os => {
'i0.ɵɵNgModuleDefWithMeta<TestModule, never, [typeof i1.InternalRouterModule], never>');
});
it('should not reference a constant with a ModuleWithProviders value in ngModuleDef imports',
it('should not reference a constant with a ModuleWithProviders value in module def imports',
() => {
env.write('dep.d.ts', `
import {ModuleWithProviders, ɵɵNgModuleDefWithMeta as ɵɵNgModuleDefWithMeta} from '@angular/core';
export declare class DepModule {
static forRoot(arg1: any, arg2: any): ModuleWithProviders<DepModule>;
static ngModuleDef: ɵɵNgModuleDefWithMeta<DepModule, never, never, never>;
static ɵmod: ɵɵNgModuleDefWithMeta<DepModule, never, never, never>;
}
`);
env.write('test.ts', `
@ -1555,7 +1555,7 @@ runInEachFileSystem(os => {
declare class RouterModule {
static forRoot(): (MyType)&{ngModule:RouterModule};
static ngModuleDef: ɵɵNgModuleDefWithMeta<RouterModule, never, never, never>;
static ɵmod: ɵɵNgModuleDefWithMeta<RouterModule, never, never, never>;
}
`);
@ -1587,7 +1587,7 @@ runInEachFileSystem(os => {
declare class RouterModule {
static forRoot(): core.ModuleWithProviders<RouterModule>;
static ngModuleDef: ɵɵNgModuleDefWithMeta<RouterModule, never, never, never>;
static ɵmod: ɵɵNgModuleDefWithMeta<RouterModule, never, never, never>;
}
`);
@ -2947,7 +2947,7 @@ runInEachFileSystem(os => {
}
export declare class ExternalModule {
static ngModuleDef: ɵɵNgModuleDefWithMeta<ExternalModule, [typeof ExternalDir], never, [typeof ExternalDir]>;
static ɵmod: ɵɵNgModuleDefWithMeta<ExternalModule, [typeof ExternalDir], never, [typeof ExternalDir]>;
}
`);
env.write('test.ts', `
@ -2981,7 +2981,7 @@ runInEachFileSystem(os => {
export {InternalDir as ExternalDir} from './internal';
export declare class ExternalModule {
static ngModuleDef: ɵɵNgModuleDefWithMeta<ExternalModule, [typeof InternalDir], never, [typeof InternalDir]>;
static ɵmod: ɵɵNgModuleDefWithMeta<ExternalModule, [typeof InternalDir], never, [typeof InternalDir]>;
}
`);
env.write('node_modules/external/internal.d.ts', `
@ -3234,7 +3234,7 @@ runInEachFileSystem(os => {
}
export declare class ExternalModule {
static ngModuleDef: ɵɵNgModuleDefWithMeta<ExternalModule, [typeof ExternalDir], never, [typeof ExternalDir]>;
static ɵmod: ɵɵNgModuleDefWithMeta<ExternalModule, [typeof ExternalDir], never, [typeof ExternalDir]>;
}
`);
@ -3543,7 +3543,7 @@ runInEachFileSystem(os => {
export declare class RouterModule {
static forRoot(arg1: any, arg2: any): ModuleWithProviders<RouterModule>;
static forChild(arg1: any): ModuleWithProviders<RouterModule>;
static ngModuleDef: ɵɵNgModuleDefWithMeta<RouterModule, never, never, never>;
static ɵmod: ɵɵNgModuleDefWithMeta<RouterModule, never, never, never>;
}
`);
});
@ -4180,7 +4180,7 @@ export const Foo = Foo__PRE_R3__;
}
export declare class AlphaModule {
static ngModuleDef: ɵɵNgModuleDefWithMeta<AlphaModule, [typeof ExternalDir], never, [typeof ExternalDir]>;
static ɵmod: ɵɵNgModuleDefWithMeta<AlphaModule, [typeof ExternalDir], never, [typeof ExternalDir]>;
}
`);
@ -4190,7 +4190,7 @@ export const Foo = Foo__PRE_R3__;
import {AlphaModule} from './alpha';
export declare class BetaModule {
static ngModuleDef: ɵɵNgModuleDefWithMeta<AlphaModule, never, never, [typeof AlphaModule]>;
static ɵmod: ɵɵNgModuleDefWithMeta<AlphaModule, never, never, [typeof AlphaModule]>;
}
`);
@ -4231,7 +4231,7 @@ export const Foo = Foo__PRE_R3__;
}
export declare class ExternalModule {
static ngModuleDef: ɵɵNgModuleDefWithMeta<ExternalModule, [typeof ExternalDir], never, [typeof ExternalDir, typeof LibModule]>;
static ɵmod: ɵɵNgModuleDefWithMeta<ExternalModule, [typeof ExternalDir], never, [typeof ExternalDir, typeof LibModule]>;
}
`);
env.write('lib.d.ts', `
@ -4242,7 +4242,7 @@ export const Foo = Foo__PRE_R3__;
}
export declare class LibModule {
static ngModuleDef: ɵɵNgModuleDefWithMeta<LibModule, [typeof LibDir], never, [typeof LibDir]>;
static ɵmod: ɵɵNgModuleDefWithMeta<LibModule, [typeof LibDir], never, [typeof LibDir]>;
}
`);
env.write('foo.ts', `

View File

@ -51,7 +51,7 @@ runInEachFileSystem(() => {
const dtsContents = env.getContents('test.d.ts');
expect(dtsContents)
.toContain(
'static ngModuleDef: i0.ɵɵNgModuleDefWithMeta<TestModule, never, [typeof OtherModule], never>');
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, never, [typeof OtherModule], never>');
});
it('should produce an error when an invalid class is imported', () => {
@ -112,7 +112,7 @@ runInEachFileSystem(() => {
const dtsContents = env.getContents('test.d.ts');
expect(dtsContents)
.toContain(
'static ngModuleDef: i0.ɵɵNgModuleDefWithMeta<TestModule, never, never, [typeof OtherModule]>');
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, never, never, [typeof OtherModule]>');
});
it('should produce an error when a non-NgModule class is exported', () => {

View File

@ -56,7 +56,7 @@ export declare class NgIf {
}
export declare class CommonModule {
static ngModuleDef: i0.ɵɵNgModuleDefWithMeta<CommonModule, [typeof NgIf, typeof NgForOf, typeof IndexPipe], never, [typeof NgIf, typeof NgForOf, typeof IndexPipe]>;
static ɵmod: i0.ɵɵNgModuleDefWithMeta<CommonModule, [typeof NgIf, typeof NgForOf, typeof IndexPipe], never, [typeof NgIf, typeof NgForOf, typeof IndexPipe]>;
}
`);
});