refactor(core): rename ngInjectorDef to ɵinj (#33151)

Injector 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
ngInjectorDef to inj. 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 #33151
This commit is contained in:
Kara Erickson
2019-10-14 15:28:01 -07:00
committed by Matias Niemelä
parent 3e14c2d02c
commit cda9248b33
25 changed files with 79 additions and 77 deletions

View File

@ -270,14 +270,14 @@ A.ɵdir = ɵngcc0.ɵɵdefineDirective({ type: A, selectors: [["", "a", ""]] });
const definitions: string = addDefinitionsSpy.calls.first().args[2];
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 ngInjectorDef = definitions.indexOf('ɵinj');
expect(ngInjectorDef).not.toEqual(-1, 'ɵinj should exist');
const setClassMetadata = definitions.indexOf('setClassMetadata');
expect(setClassMetadata).not.toEqual(-1, 'setClassMetadata call should exist');
expect(setClassMetadata)
.toBeGreaterThan(ngModuleDef, 'setClassMetadata should follow ɵmod');
expect(setClassMetadata)
.toBeGreaterThan(ngInjectorDef, 'setClassMetadata should follow ngInjectorDef');
.toBeGreaterThan(ngInjectorDef, 'setClassMetadata should follow ɵinj');
});
it('should render classes without decorators if handler matches', () => {

View File

@ -24,8 +24,8 @@ import {ReferencesRegistry} from './references_registry';
import {combineResolvers, findAngularDecorator, forwardRefResolver, getValidConstructorDependencies, isExpressionForwardReference, toR3Reference, unwrapExpression} from './util';
export interface NgModuleAnalysis {
ɵmod: R3NgModuleMetadata;
ngInjectorDef: R3InjectorMetadata;
mod: R3NgModuleMetadata;
inj: R3InjectorMetadata;
metadataStmt: Statement|null;
declarations: Reference<ClassDeclaration>[];
exports: Reference<ClassDeclaration>[];
@ -236,7 +236,8 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
return {
analysis: {
id,
ɵmod: ngModuleDef, ngInjectorDef,
mod: ngModuleDef,
inj: ngInjectorDef,
declarations: declarationRefs,
exports: exportRefs,
metadataStmt: generateSetClassMetadataCall(
@ -256,7 +257,7 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
const context = getSourceFile(node);
for (const exportRef of analysis.exports) {
if (isNgModule(exportRef.node, scope.compilation)) {
analysis.ngInjectorDef.imports.push(this.refEmitter.emit(exportRef, context));
analysis.inj.imports.push(this.refEmitter.emit(exportRef, context));
}
}
@ -280,8 +281,8 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
}
compile(node: ClassDeclaration, analysis: NgModuleAnalysis): CompileResult[] {
const ngInjectorDef = compileInjector(analysis.ngInjectorDef);
const ngModuleDef = compileNgModule(analysis.ɵmod);
const ngInjectorDef = compileInjector(analysis.inj);
const ngModuleDef = compileNgModule(analysis.mod);
const ngModuleStatements = ngModuleDef.additionalStatements;
if (analysis.metadataStmt !== null) {
ngModuleStatements.push(analysis.metadataStmt);
@ -314,7 +315,7 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
type: ngModuleDef.type,
},
{
name: 'ngInjectorDef',
name: 'ɵinj',
initializer: ngInjectorDef.expression,
statements: ngInjectorDef.statements,
type: ngInjectorDef.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 !.ɵmod;
const moduleDef = handler.analyze(TestModule, detected.metadata).analysis !.mod;
expect(getReferenceIdentifierTexts(moduleDef.declarations)).toEqual(['TestComp']);
expect(getReferenceIdentifierTexts(moduleDef.exports)).toEqual(['TestComp']);

View File

@ -23,7 +23,7 @@ const R3_DEF_NAME_PATTERN = [
'ɵcmp',
'ɵdir',
'ngInjectableDef',
'ngInjectorDef',
'ɵinj',
'ɵmod',
'ɵpipe',
'ɵfac',

View File

@ -377,7 +377,7 @@ runInEachFileSystem(os => {
expect(jsContents).toContain('TestPipe.ɵpipe = i0.ɵɵdefinePipe');
expect(jsContents).toContain('TestInjectable.ngInjectableDef = i0.ɵɵdefineInjectable');
expect(jsContents).toContain('MyModule.ɵmod = i0.ɵɵdefineNgModule');
expect(jsContents).toContain('MyModule.ngInjectorDef = i0.ɵɵdefineInjector');
expect(jsContents).toContain('MyModule.ɵinj = i0.ɵɵdefineInjector');
expect(jsContents).toContain('inputs: { input: "input" }');
expect(jsContents).toContain('outputs: { output: "output" }');
});
@ -650,7 +650,7 @@ runInEachFileSystem(os => {
expect(jsContents).toContain('i0.ɵɵdefineNgModule({ type: TestModule });');
expect(jsContents)
.toContain(
`TestModule.ngInjectorDef = i0.ɵɵdefineInjector({ factory: ` +
`TestModule.ɵinj = i0.ɵɵdefineInjector({ factory: ` +
`function TestModule_Factory(t) { return new (t || TestModule)(); }, providers: [{ provide: ` +
`Token, useValue: 'test' }], imports: [[OtherModule]] });`);
@ -658,7 +658,7 @@ runInEachFileSystem(os => {
expect(dtsContents)
.toContain(
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
expect(dtsContents).toContain('static ngInjectorDef: i0.ɵɵInjectorDef');
expect(dtsContents).toContain('static ɵinj: i0.ɵɵInjectorDef');
});
it('should compile NgModules with factory providers without errors', () => {
@ -690,7 +690,7 @@ runInEachFileSystem(os => {
expect(jsContents).toContain('i0.ɵɵdefineNgModule({ type: TestModule });');
expect(jsContents)
.toContain(
`TestModule.ngInjectorDef = i0.ɵɵdefineInjector({ factory: ` +
`TestModule.ɵinj = i0.ɵɵdefineInjector({ factory: ` +
`function TestModule_Factory(t) { return new (t || TestModule)(); }, providers: [{ provide: ` +
`Token, useFactory: function () { return new Token(); } }], imports: [[OtherModule]] });`);
@ -698,7 +698,7 @@ runInEachFileSystem(os => {
expect(dtsContents)
.toContain(
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
expect(dtsContents).toContain('static ngInjectorDef: i0.ɵɵInjectorDef');
expect(dtsContents).toContain('static ɵinj: i0.ɵɵInjectorDef');
});
it('should compile NgModules with factory providers and deps without errors', () => {
@ -734,7 +734,7 @@ runInEachFileSystem(os => {
expect(jsContents).toContain('i0.ɵɵdefineNgModule({ type: TestModule });');
expect(jsContents)
.toContain(
`TestModule.ngInjectorDef = i0.ɵɵdefineInjector({ factory: ` +
`TestModule.ɵinj = i0.ɵɵdefineInjector({ factory: ` +
`function TestModule_Factory(t) { return new (t || TestModule)(); }, providers: [{ provide: ` +
`Token, useFactory: function (dep) { return new Token(dep); }, deps: [Dep] }], imports: [[OtherModule]] });`);
@ -742,7 +742,7 @@ runInEachFileSystem(os => {
expect(dtsContents)
.toContain(
'static ɵmod: i0.ɵɵNgModuleDefWithMeta<TestModule, [typeof TestCmp], [typeof OtherModule], never>');
expect(dtsContents).toContain('static ngInjectorDef: i0.ɵɵInjectorDef');
expect(dtsContents).toContain('static ɵinj: i0.ɵɵInjectorDef');
});
it('should compile NgModules with references to local components', () => {

View File

@ -10,7 +10,7 @@ import {nocollapseHack} from '../../src/transformers/nocollapse_hack';
describe('@nocollapse hack', () => {
it('should add @nocollapse to a basic class', () => {
const decl = `Foo.ngInjectorDef = define(...);`;
const decl = `Foo.ɵinj = define(...);`;
expect(nocollapseHack(decl)).toEqual('/** @nocollapse */ ' + decl);
});
@ -18,9 +18,9 @@ describe('@nocollapse hack', () => {
const decl = `
if (false) {
/** @type {?} */
Foo.ngInjectorDef;
Foo.ɵinj;
}
`;
expect(nocollapseHack(decl)).toContain('/** @nocollapse @type {?} */');
});
});
});