Skip to content

Commit 352e078

Browse files
devversionAndrewKushnir
authored andcommitted
feat(core): expose signal input metadata in ComponentMirror (#56402)
This commit starts exposing `isSignal` for inputs in the `ComponentMirror`. We initially had this as a draft when rolling out signal inputs, but there were no good use-cases, so we skipped it. Now, inside G3, for the testing infrastructure and rolling out advancements for signal inputs, having this information is necessary and allows identifying signal inputs without "accessing fields" on the class that may cause side-effects (like triggering setters). PR Close #56402
1 parent c698668 commit 352e078

7 files changed

Lines changed: 71 additions & 31 deletions

File tree

‎goldens/public-api/core/index.api.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ export abstract class ComponentFactory<C> {
276276
propName: string;
277277
templateName: string;
278278
transform?: (value: any) => any;
279+
isSignal: boolean;
279280
}[];
280281
abstract get ngContentSelectors(): string[];
281282
abstract get outputs(): {
@@ -298,6 +299,7 @@ export interface ComponentMirror<C> {
298299
readonly propName: string;
299300
readonly templateName: string;
300301
readonly transform?: (value: any) => any;
302+
readonly isSignal: boolean;
301303
}>;
302304
get isStandalone(): boolean;
303305
get ngContentSelectors(): ReadonlyArray<string>;

‎packages/core/src/linker/component_factory.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export abstract class ComponentFactory<C> {
108108
propName: string;
109109
templateName: string;
110110
transform?: (value: any) => any;
111+
isSignal: boolean;
111112
}[];
112113
/**
113114
* The outputs of the component.

‎packages/core/src/render3/component.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export interface ComponentMirror<C> {
117117
readonly propName: string;
118118
readonly templateName: string;
119119
readonly transform?: (value: any) => any;
120+
readonly isSignal: boolean;
120121
}>;
121122
/**
122123
* The outputs of the component.
@@ -193,6 +194,7 @@ export function reflectComponentType<C>(component: Type<C>): ComponentMirror<C>
193194
propName: string;
194195
templateName: string;
195196
transform?: (value: any) => any;
197+
isSignal: boolean;
196198
}> {
197199
return factory.inputs;
198200
},

‎packages/core/src/render3/component_ref.ts‎

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,23 @@ export class ComponentFactoryResolver extends AbstractComponentFactoryResolver {
107107
}
108108
}
109109

110-
function toRefArray<T>(map: {
111-
[P in keyof T]?: string | [minifiedName: string, flags: InputFlags];
112-
}): {propName: string; templateName: string}[] {
113-
const array: {propName: string; templateName: string}[] = [];
110+
function toRefArray<T>(
111+
map: DirectiveDef<T>['inputs'],
112+
isInputMap: true,
113+
): ComponentFactory<T>['inputs'];
114+
function toRefArray<T>(
115+
map: DirectiveDef<T>['outputs'],
116+
isInput: false,
117+
): ComponentFactory<T>['outputs'];
118+
119+
function toRefArray<
120+
T,
121+
IsInputMap extends boolean,
122+
Return extends IsInputMap extends true
123+
? ComponentFactory<T>['inputs']
124+
: ComponentFactory<T>['outputs'],
125+
>(map: DirectiveDef<T>['inputs'] | DirectiveDef<T>['outputs'], isInputMap: IsInputMap): Return {
126+
const array: Return = [] as unknown as Return;
114127
for (const publicName in map) {
115128
if (!map.hasOwnProperty(publicName)) {
116129
continue;
@@ -121,10 +134,22 @@ function toRefArray<T>(map: {
121134
continue;
122135
}
123136

124-
array.push({
125-
propName: Array.isArray(value) ? value[0] : value,
126-
templateName: publicName,
127-
});
137+
const isArray = Array.isArray(value);
138+
const propName: string = isArray ? value[0] : value;
139+
const flags: InputFlags = isArray ? value[1] : InputFlags.None;
140+
141+
if (isInputMap) {
142+
(array as ComponentFactory<T>['inputs']).push({
143+
propName: propName,
144+
templateName: publicName,
145+
isSignal: (flags & InputFlags.SignalBased) !== 0,
146+
});
147+
} else {
148+
(array as ComponentFactory<T>['outputs']).push({
149+
propName: propName,
150+
templateName: publicName,
151+
});
152+
}
128153
}
129154
return array;
130155
}
@@ -180,15 +205,12 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
180205
override get inputs(): {
181206
propName: string;
182207
templateName: string;
208+
isSignal: boolean;
183209
transform?: (value: any) => any;
184210
}[] {
185211
const componentDef = this.componentDef;
186212
const inputTransforms = componentDef.inputTransforms;
187-
const refArray = toRefArray(componentDef.inputs) as {
188-
propName: string;
189-
templateName: string;
190-
transform?: (value: any) => any;
191-
}[];
213+
const refArray = toRefArray(componentDef.inputs, true);
192214

193215
if (inputTransforms !== null) {
194216
for (const input of refArray) {
@@ -202,7 +224,7 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
202224
}
203225

204226
override get outputs(): {propName: string; templateName: string}[] {
205-
return toRefArray(this.componentDef.outputs);
227+
return toRefArray(this.componentDef.outputs, false);
206228
}
207229

208230
/**

‎packages/core/test/acceptance/component_spec.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
Injectable,
2323
InjectionToken,
2424
Injector,
25+
input,
2526
Input,
2627
NgModule,
2728
OnDestroy,
@@ -968,6 +969,7 @@ describe('component', () => {
968969
})
969970
class StandaloneComponent {
970971
@Input({alias: 'input-alias-c', transform: transformFn}) inputC: unknown;
972+
@Input({isSignal: true} as Input) inputD = input(false);
971973
}
972974

973975
const mirror = reflectComponentType(StandaloneComponent)!;
@@ -976,9 +978,15 @@ describe('component', () => {
976978
expect(mirror.type).toBe(StandaloneComponent);
977979
expect(mirror.isStandalone).toEqual(true);
978980
expect(mirror.inputs).toEqual([
979-
{propName: 'input-a', templateName: 'input-a'},
980-
{propName: 'input-b', templateName: 'input-alias-b'},
981-
{propName: 'inputC', templateName: 'input-alias-c', transform: transformFn},
981+
{propName: 'input-a', templateName: 'input-a', isSignal: false},
982+
{propName: 'input-b', templateName: 'input-alias-b', isSignal: false},
983+
{
984+
propName: 'inputC',
985+
templateName: 'input-alias-c',
986+
transform: transformFn,
987+
isSignal: false,
988+
},
989+
{propName: 'inputD', templateName: 'inputD', isSignal: true},
982990
]);
983991
expect(mirror.outputs).toEqual([
984992
{propName: 'output-a', templateName: 'output-a'},
@@ -1016,9 +1024,14 @@ describe('component', () => {
10161024
expect(mirror.type).toBe(NonStandaloneComponent);
10171025
expect(mirror.isStandalone).toEqual(false);
10181026
expect(mirror.inputs).toEqual([
1019-
{propName: 'input-a', templateName: 'input-a'},
1020-
{propName: 'input-b', templateName: 'input-alias-b'},
1021-
{propName: 'inputC', templateName: 'input-alias-c', transform: transformFn},
1027+
{propName: 'input-a', templateName: 'input-a', isSignal: false},
1028+
{propName: 'input-b', templateName: 'input-alias-b', isSignal: false},
1029+
{
1030+
propName: 'inputC',
1031+
templateName: 'input-alias-c',
1032+
transform: transformFn,
1033+
isSignal: false,
1034+
},
10221035
]);
10231036
expect(mirror.outputs).toEqual([
10241037
{propName: 'output-a', templateName: 'output-a'},

‎packages/core/test/render3/component_ref_spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ describe('ComponentFactory', () => {
9393
expect(cf.selector).toBe('test[foo],bar');
9494

9595
expect(cf.inputs).toEqual([
96-
{propName: 'in1', templateName: 'in1'},
97-
{propName: 'in2', templateName: 'input-attr-2'},
98-
{propName: 'in3', templateName: 'input-attr-3', transform: transformFn},
96+
{propName: 'in1', templateName: 'in1', isSignal: false},
97+
{propName: 'in2', templateName: 'input-attr-2', isSignal: false},
98+
{propName: 'in3', templateName: 'input-attr-3', transform: transformFn, isSignal: false},
9999
]);
100100
expect(cf.outputs).toEqual([
101101
{propName: 'out1', templateName: 'out1'},

‎packages/elements/test/component-factory-strategy_spec.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,15 +472,15 @@ export class FakeComponentFactory<T extends Type<any>> extends ComponentFactory<
472472
override get ngContentSelectors(): string[] {
473473
return ['content-1', 'content-2'];
474474
}
475-
override get inputs(): {propName: string; templateName: string}[] {
475+
override get inputs(): {propName: string; templateName: string; isSignal: boolean}[] {
476476
return [
477-
{propName: 'fooFoo', templateName: 'fooFoo'},
478-
{propName: 'barBar', templateName: 'my-bar-bar'},
479-
{propName: 'falsyUndefined', templateName: 'falsyUndefined'},
480-
{propName: 'falsyNull', templateName: 'falsyNull'},
481-
{propName: 'falsyEmpty', templateName: 'falsyEmpty'},
482-
{propName: 'falsyFalse', templateName: 'falsyFalse'},
483-
{propName: 'falsyZero', templateName: 'falsyZero'},
477+
{propName: 'fooFoo', templateName: 'fooFoo', isSignal: false},
478+
{propName: 'barBar', templateName: 'my-bar-bar', isSignal: false},
479+
{propName: 'falsyUndefined', templateName: 'falsyUndefined', isSignal: false},
480+
{propName: 'falsyNull', templateName: 'falsyNull', isSignal: false},
481+
{propName: 'falsyEmpty', templateName: 'falsyEmpty', isSignal: false},
482+
{propName: 'falsyFalse', templateName: 'falsyFalse', isSignal: false},
483+
{propName: 'falsyZero', templateName: 'falsyZero', isSignal: false},
484484
];
485485
}
486486
override get outputs(): {propName: string; templateName: string}[] {

0 commit comments

Comments
 (0)