refactor(core): rename ngDirectiveDef to ɵdir (#33110)

Directive 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
ngDirectiveDef to dir. 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.

Note that the other "defs" (ngFactoryDef, etc) will be
prefixed and shortened in follow-up PRs, in an attempt to
limit how large and conflict-y this change is.

PR Close #33110
This commit is contained in:
Kara Erickson
2019-10-11 12:28:12 -07:00
committed by Miško Hevery
parent d8249d1230
commit 1a67d70bf8
47 changed files with 150 additions and 156 deletions

View File

@ -69,7 +69,7 @@
"name": "NG_COMP_DEF"
},
{
"name": "NG_DIRECTIVE_DEF"
"name": "NG_DIR_DEF"
},
{
"name": "NG_ELEMENT_ID"

View File

@ -63,7 +63,7 @@
"name": "NG_COMP_DEF"
},
{
"name": "NG_DIRECTIVE_DEF"
"name": "NG_DIR_DEF"
},
{
"name": "NG_ELEMENT_ID"

View File

@ -126,7 +126,7 @@
"name": "NG_COMP_DEF"
},
{
"name": "NG_DIRECTIVE_DEF"
"name": "NG_DIR_DEF"
},
{
"name": "NG_ELEMENT_ID"

View File

@ -15,7 +15,7 @@ export const NgForOf: DirectiveType<NgForOfDef<any>> = NgForOfDef as any;
export const NgIf: DirectiveType<NgIfDef> = NgIfDef as any;
export const NgTemplateOutlet: DirectiveType<NgTemplateOutletDef> = NgTemplateOutletDef as any;
NgForOf.ngDirectiveDef = ɵɵdefineDirective({
NgForOf.ɵdir = ɵɵdefineDirective({
type: NgForOfDef,
selectors: [['', 'ngForOf', '']],
inputs: {
@ -29,7 +29,7 @@ NgForOf.ngFactoryDef = () => new NgForOfDef(
ɵɵdirectiveInject(ViewContainerRef as any), ɵɵdirectiveInject(TemplateRef as any),
ɵɵdirectiveInject(IterableDiffers));
NgIf.ngDirectiveDef = ɵɵdefineDirective({
NgIf.ɵdir = ɵɵdefineDirective({
type: NgIfDef,
selectors: [['', 'ngIf', '']],
inputs: {ngIf: 'ngIf', ngIfThen: 'ngIfThen', ngIfElse: 'ngIfElse'}
@ -38,7 +38,7 @@ NgIf.ngDirectiveDef = ɵɵdefineDirective({
NgIf.ngFactoryDef = () =>
new NgIfDef(ɵɵdirectiveInject(ViewContainerRef as any), ɵɵdirectiveInject(TemplateRef as any));
NgTemplateOutlet.ngDirectiveDef = ɵɵdefineDirective({
NgTemplateOutlet.ɵdir = ɵɵdefineDirective({
type: NgTemplateOutletDef,
selectors: [['', 'ngTemplateOutlet', '']],
features: [ɵɵNgOnChangesFeature()],

View File

@ -437,8 +437,7 @@ describe('recursive components', () => {
}
}
(NgIfTree.ɵcmp as ComponentDef<NgIfTree>).directiveDefs =
() => [NgIfTree.ɵcmp, NgIf.ngDirectiveDef];
(NgIfTree.ɵcmp as ComponentDef<NgIfTree>).directiveDefs = () => [NgIfTree.ɵcmp, NgIf.ɵdir];
function _buildTree(currDepth: number): TreeNode {
const children = currDepth < 2 ? _buildTree(currDepth + 1) : null;

View File

@ -29,7 +29,7 @@ describe('di', () => {
value = 'DirB';
static ngFactoryDef = () => new DirB();
static ngDirectiveDef =
static ɵdir =
ɵɵdefineDirective({selectors: [['', 'dirB', '']], type: DirB, inputs: {value: 'value'}});
}
@ -40,7 +40,7 @@ describe('di', () => {
value !: string;
static ngFactoryDef = () => new DirB();
static ngDirectiveDef =
static ɵdir =
ɵɵdefineDirective({type: DirB, selectors: [['', 'dirB', '']], inputs: {value: 'dirB'}});
}
@ -56,7 +56,7 @@ describe('di', () => {
return dirA;
}
static ngDirectiveDef = ɵɵdefineDirective({type: DirA, selectors: [['', 'dirA', '']]});
static ɵdir = ɵɵdefineDirective({type: DirA, selectors: [['', 'dirA', '']]});
}
beforeEach(() => dirA = null);
@ -82,7 +82,7 @@ describe('di', () => {
constructor(@Self() public dirB: DirB) {}
static ngFactoryDef = () => dirA = new DirA(ɵɵdirectiveInject(DirB, InjectFlags.Self));
static ngDirectiveDef = ɵɵdefineDirective({type: DirA, selectors: [['', 'dirA', '']]});
static ɵdir = ɵɵdefineDirective({type: DirA, selectors: [['', 'dirA', '']]});
}
const DirC = createDirective('dirC');

View File

@ -346,7 +346,7 @@ describe('component animations', () => {
// it('should allow host binding animations to be picked up and rendered', () => {
// class ChildCompWithAnim {
// static ngFactoryDef = () => new ChildCompWithAnim();
// static ngDirectiveDef = ɵɵdefineDirective({
// static ɵdir = ɵɵdefineDirective({
// type: ChildCompWithAnim,
// selectors: [['child-comp-with-anim']],
// hostBindings: function(rf: RenderFlags, ctx: any, elementIndex: number): void {
@ -837,20 +837,17 @@ describe('element discovery', () => {
class MyDir1 {
static ngFactoryDef = () => myDir1Instance = new MyDir1();
static ngDirectiveDef =
ɵɵdefineDirective({type: MyDir1, selectors: [['', 'my-dir-1', '']]});
static ɵdir = ɵɵdefineDirective({type: MyDir1, selectors: [['', 'my-dir-1', '']]});
}
class MyDir2 {
static ngFactoryDef = () => myDir2Instance = new MyDir2();
static ngDirectiveDef =
ɵɵdefineDirective({type: MyDir2, selectors: [['', 'my-dir-2', '']]});
static ɵdir = ɵɵdefineDirective({type: MyDir2, selectors: [['', 'my-dir-2', '']]});
}
class MyDir3 {
static ngFactoryDef = () => myDir3Instance = new MyDir2();
static ngDirectiveDef =
ɵɵdefineDirective({type: MyDir3, selectors: [['', 'my-dir-3', '']]});
static ɵdir = ɵɵdefineDirective({type: MyDir3, selectors: [['', 'my-dir-3', '']]});
}
class StructuredComp {
@ -921,14 +918,12 @@ describe('element discovery', () => {
class MyDir1 {
static ngFactoryDef = () => myDir1Instance = new MyDir1();
static ngDirectiveDef =
ɵɵdefineDirective({type: MyDir1, selectors: [['', 'my-dir-1', '']]});
static ɵdir = ɵɵdefineDirective({type: MyDir1, selectors: [['', 'my-dir-1', '']]});
}
class MyDir2 {
static ngFactoryDef = () => myDir2Instance = new MyDir2();
static ngDirectiveDef =
ɵɵdefineDirective({type: MyDir2, selectors: [['', 'my-dir-2', '']]});
static ɵdir = ɵɵdefineDirective({type: MyDir2, selectors: [['', 'my-dir-2', '']]});
}
class ChildComp {
@ -1114,7 +1109,7 @@ describe('sanitization', () => {
cite: any = 'http://cite-dir-value';
static ngFactoryDef = () => hostBindingDir = new UnsafeUrlHostBindingDir();
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: UnsafeUrlHostBindingDir,
selectors: [['', 'unsafeUrlHostBindingDir', '']],
hostBindings: (rf: RenderFlags, ctx: any) => {

View File

@ -281,8 +281,8 @@ ivyEnabled && describe('render3 jit', () => {
}
const InputDirAny = InputDir as any;
expect(InputDirAny.ngDirectiveDef.inputs).toEqual({publicName: 'privateName'});
expect(InputDirAny.ngDirectiveDef.declaredInputs).toEqual({publicName: 'privateName'});
expect(InputDirAny.ɵdir.inputs).toEqual({publicName: 'privateName'});
expect(InputDirAny.ɵdir.declaredInputs).toEqual({publicName: 'privateName'});
});
it('should compile ContentChildren query with string predicate on a directive', () => {
@ -291,7 +291,7 @@ ivyEnabled && describe('render3 jit', () => {
@ContentChildren('foo') foos: QueryList<ElementRef>|undefined;
}
expect((TestDirective as any).ngDirectiveDef.contentQueries).not.toBeNull();
expect((TestDirective as any).ɵdir.contentQueries).not.toBeNull();
});
it('should compile ContentChild query with string predicate on a directive', () => {
@ -300,7 +300,7 @@ ivyEnabled && describe('render3 jit', () => {
@ContentChild('foo', {static: false}) foo: ElementRef|undefined;
}
expect((TestDirective as any).ngDirectiveDef.contentQueries).not.toBeNull();
expect((TestDirective as any).ɵdir.contentQueries).not.toBeNull();
});
it('should compile ContentChildren query with type predicate on a directive', () => {
@ -311,7 +311,7 @@ ivyEnabled && describe('render3 jit', () => {
@ContentChildren(SomeDir) dirs: QueryList<SomeDir>|undefined;
}
expect((TestDirective as any).ngDirectiveDef.contentQueries).not.toBeNull();
expect((TestDirective as any).ɵdir.contentQueries).not.toBeNull();
});
it('should compile ContentChild query with type predicate on a directive', () => {
@ -322,7 +322,7 @@ ivyEnabled && describe('render3 jit', () => {
@ContentChild(SomeDir, {static: false}) dir: SomeDir|undefined;
}
expect((TestDirective as any).ngDirectiveDef.contentQueries).not.toBeNull();
expect((TestDirective as any).ɵdir.contentQueries).not.toBeNull();
});
it('should compile ViewChild query on a component', () => {

View File

@ -71,7 +71,7 @@ describe('lifecycles', () => {
ngOnInit() { events.push('dir'); }
static ngFactoryDef = () => new Directive();
static ngDirectiveDef = ɵɵdefineDirective({type: Directive, selectors: [['', 'dir', '']]});
static ɵdir = ɵɵdefineDirective({type: Directive, selectors: [['', 'dir', '']]});
}
const directives = [Comp, Parent, ProjectedComp, Directive, NgIf];

View File

@ -101,7 +101,7 @@ describe('event listeners', () => {
static ngFactoryDef = function HostListenerDir_Factory() {
return new GlobalHostListenerDir();
};
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: GlobalHostListenerDir,
selectors: [['', 'hostListenerDir', '']],
hostBindings: function HostListenerDir_HostBindings(
@ -581,7 +581,7 @@ describe('event listeners', () => {
onClick() { events.push('click!'); }
static ngFactoryDef = function HostListenerDir_Factory() { return new HostListenerDir(); };
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: HostListenerDir,
selectors: [['', 'hostListenerDir', '']],
hostBindings: function HostListenerDir_HostBindings(

View File

@ -38,7 +38,7 @@ describe('outputs', () => {
changeStream = new EventEmitter();
static ngFactoryDef = () => otherDir = new OtherDir;
static ngDirectiveDef = ɵɵdefineDirective(
static ɵdir = ɵɵdefineDirective(
{type: OtherDir, selectors: [['', 'otherDir', '']], outputs: {changeStream: 'change'}});
}

View File

@ -1459,7 +1459,7 @@ function expectProvidersScenario(defs: {
class ViewChildDirective {
static ngFactoryDef = () => testDirectiveInjection(defs.viewChild, new ViewChildDirective());
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: ViewChildDirective,
selectors: [['view-child']],
features: defs.viewChild && [ɵɵProvidersFeature(defs.viewChild.directiveProviders || [])],
@ -1490,7 +1490,7 @@ function expectProvidersScenario(defs: {
static ngFactoryDef =
() => { return testDirectiveInjection(defs.contentChild, new ContentChildDirective()); }
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: ContentChildDirective,
selectors: [['content-child']],
features:
@ -1519,7 +1519,7 @@ function expectProvidersScenario(defs: {
class ParentDirective {
static ngFactoryDef = () => testDirectiveInjection(defs.parent, new ParentDirective());
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: ParentDirective,
selectors: [['parent']],
features: defs.parent && [ɵɵProvidersFeature(defs.parent.directiveProviders || [])],
@ -1528,7 +1528,7 @@ function expectProvidersScenario(defs: {
class ParentDirective2 {
static ngFactoryDef = () => testDirectiveInjection(defs.parent, new ParentDirective2());
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: ParentDirective2,
selectors: [['parent']],
features: defs.parent && [ɵɵProvidersFeature(defs.parent.directive2Providers || [])],

View File

@ -223,7 +223,7 @@ describe('query', () => {
static ngFactoryDef = function MyDirective_Factory() {
return directive = new MyDirective(ɵɵdirectiveInject(Service));
};
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: MyDirective,
selectors: [['', 'myDir', '']],
features: [ɵɵProvidersFeature([Service, {provide: Alias, useExisting: Service}])],
@ -1423,7 +1423,7 @@ describe('query', () => {
static ngFactoryDef = () => new SomeDir(
ɵɵdirectiveInject(ViewContainerRef as any), ɵɵdirectiveInject(TemplateRef as any))
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: SomeDir,
selectors: [['', 'someDir', '']],
});
@ -1482,7 +1482,7 @@ describe('query', () => {
}
static ngFactoryDef = () => withContentInstance = new WithContentDirective();
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: WithContentDirective,
selectors: [['', 'with-content', '']],
contentQueries: (rf: RenderFlags, ctx: any, dirIndex: number) => {
@ -1680,7 +1680,7 @@ describe('query', () => {
class QueryDirective {
fooBars: any;
static ngFactoryDef = () => new QueryDirective();
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: QueryDirective,
selectors: [['', 'query', '']],
exportAs: ['query'],
@ -1746,7 +1746,7 @@ describe('query', () => {
class QueryDirective {
fooBars: any;
static ngFactoryDef = () => new QueryDirective();
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: QueryDirective,
selectors: [['', 'query', '']],
exportAs: ['query'],
@ -1803,7 +1803,7 @@ describe('query', () => {
class QueryDirective {
fooBars: any;
static ngFactoryDef = () => new QueryDirective();
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: QueryDirective,
selectors: [['', 'query', '']],
exportAs: ['query'],
@ -1864,7 +1864,7 @@ describe('query', () => {
class ShallowQueryDirective {
foos: any;
static ngFactoryDef = () => new ShallowQueryDirective();
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: ShallowQueryDirective,
selectors: [['', 'shallow-query', '']],
exportAs: ['shallow-query'],
@ -1885,7 +1885,7 @@ describe('query', () => {
class DeepQueryDirective {
foos: any;
static ngFactoryDef = () => new DeepQueryDirective();
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: DeepQueryDirective,
selectors: [['', 'deep-query', '']],
exportAs: ['deep-query'],
@ -1947,7 +1947,7 @@ describe('query', () => {
value !: string;
static ngFactoryDef = () => new TextDirective();
static ngDirectiveDef = ɵɵdefineDirective(
static ɵdir = ɵɵdefineDirective(
{type: TextDirective, selectors: [['', 'text', '']], inputs: {value: 'text'}});
}

View File

@ -395,7 +395,7 @@ export function createDirective(
name: string, {exportAs}: {exportAs?: string[]} = {}): DirectiveType<any> {
return class Directive {
static ngFactoryDef = () => new Directive();
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: Directive,
selectors: [['', name, '']],
exportAs: exportAs,

View File

@ -32,7 +32,7 @@ describe('ViewContainerRef', () => {
static ngFactoryDef = () => directiveInstance = new DirectiveWithVCRef(
ɵɵdirectiveInject(ViewContainerRef as any), injectComponentFactoryResolver())
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: DirectiveWithVCRef,
selectors: [['', 'vcref', '']],
inputs: {tplRef: 'tplRef', name: 'name'}
@ -68,7 +68,7 @@ describe('ViewContainerRef', () => {
return instance;
}
static ngDirectiveDef = ɵɵdefineDirective({
static ɵdir = ɵɵdefineDirective({
type: TestDirective,
selectors: [['', 'testdir', '']],
});
@ -140,7 +140,7 @@ describe('ViewContainerRef', () => {
static ngFactoryDef = () => directiveInstance = new TestDirective(
ɵɵdirectiveInject(ViewContainerRef as any), ɵɵdirectiveInject(TemplateRef as any))
static ngDirectiveDef =
static ɵdir =
ɵɵdefineDirective({type: TestDirective, selectors: [['', 'testdir', '']]});
constructor(private _vcRef: ViewContainerRef, private _tplRef: TemplateRef<{}>) {}

View File

@ -657,8 +657,8 @@ describe('TestBed', () => {
expect(ComponentWithNoAnnotations.hasOwnProperty('ɵcmp')).toBeTruthy();
expect(SomeComponent.hasOwnProperty('ɵcmp')).toBeTruthy();
expect(DirectiveWithNoAnnotations.hasOwnProperty('ngDirectiveDef')).toBeTruthy();
expect(SomeDirective.hasOwnProperty('ngDirectiveDef')).toBeTruthy();
expect(DirectiveWithNoAnnotations.hasOwnProperty('ɵdir')).toBeTruthy();
expect(SomeDirective.hasOwnProperty('ɵdir')).toBeTruthy();
expect(PipeWithNoAnnotations.hasOwnProperty('ngPipeDef')).toBeTruthy();
expect(SomePipe.hasOwnProperty('ngPipeDef')).toBeTruthy();
@ -667,12 +667,12 @@ describe('TestBed', () => {
// ng defs should be removed from classes with no annotations
expect(ComponentWithNoAnnotations.hasOwnProperty('ɵcmp')).toBeFalsy();
expect(DirectiveWithNoAnnotations.hasOwnProperty('ngDirectiveDef')).toBeFalsy();
expect(DirectiveWithNoAnnotations.hasOwnProperty('ɵdir')).toBeFalsy();
expect(PipeWithNoAnnotations.hasOwnProperty('ngPipeDef')).toBeFalsy();
// ng defs should be preserved on super types
expect(SomeComponent.hasOwnProperty('ɵcmp')).toBeTruthy();
expect(SomeDirective.hasOwnProperty('ngDirectiveDef')).toBeTruthy();
expect(SomeDirective.hasOwnProperty('ɵdir')).toBeTruthy();
expect(SomePipe.hasOwnProperty('ngPipeDef')).toBeTruthy();
});