Skip to content

Commit 22a3bc7

Browse files
committed
fix: parsing of union type in input signal
1 parent b07571e commit 22a3bc7

File tree

5 files changed

+503
-219
lines changed

5 files changed

+503
-219
lines changed

src/app/compiler/angular/deps/helpers/component-helper.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ export class ComponentHelper {
220220

221221
const result = {
222222
required: !!required,
223-
type,
223+
type: this.parseSignalType(type),
224224
defaultValue
225225
};
226226

@@ -235,6 +235,32 @@ export class ComponentHelper {
235235
}
236236
}
237237

238+
public parseSignalType(type: string) {
239+
if (!type) {
240+
return type;
241+
}
242+
243+
// adjust union string expression like: 'foo' | 'bar' | 'test'
244+
// which should be outputed as: "foo" | "bar" | "test"
245+
246+
const unionTypeRegex = /^'([\w-]+)'\s?\|\s?('([\w-]+)'|.*)$/
247+
let typeRest = type;
248+
let newType = "";
249+
let typeMatch: RegExpMatchArray;
250+
while ((typeMatch = typeRest.match(unionTypeRegex))) {
251+
const [, first, rest, second] = typeMatch;
252+
if (second) {
253+
newType += `"${first}" | "${second}"`;
254+
type = newType;
255+
break;
256+
}
257+
newType += `"${first}" | `;
258+
typeRest = rest;
259+
}
260+
261+
return type;
262+
}
263+
238264
public getComponentStandalone(
239265
props: ReadonlyArray<ts.ObjectLiteralElementLike>,
240266
srcFile: ts.SourceFile

test/fixtures/sample-files/foo.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ export class FooComponent {
4444
/**
4545
* An example aliased input using the object syntax
4646
*/
47-
@Input({ alias: 'aliasedInput' }) objectAliasedInput: string;
47+
@Input({ alias: 'aliasedInputObjectSyntax' }) objectAliasedInput: string;
4848

4949
/**
5050
* An example aliased required input using the object syntax
5151
*/
52-
@Input({ alias: 'aliasedInput', required: true }) aliasedAndRequired: string;
52+
@Input({ alias: 'aliasedAndRequiredInput', required: true }) aliasedAndRequired: string;
5353

5454
/**
5555
* An example output

test/fixtures/todomvc-ng2/src/app/about/compodoc/compodoc.component.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,10 @@ export class CompodocComponent {
5050
*/
5151

5252
outputSignal = output();
53-
outputSignalWithDefaultValue = output(this.defaultValue);
54-
outputSignalWithDefaultStringValue = output('value');
55-
outputSignalWithAlias = output(0, { alias: 'aliasedSignal' });
53+
outputSignalWithAlias = output({ alias: 'aliasedSignal' });
5654

57-
requiredOutputSignal = output.required();
58-
requiredOutputSignalWithType = output.required<number>();
59-
60-
outputSignalWithType = output<string>('value');
61-
outputSignalWithStringType = output<'value'>('value');
62-
outputSignalWithMultipleTypes = output<string | number>(0);
63-
outputSignalWithMultipleMixedTypes = output<'asc' | 'dsc' | number>('asc');
55+
outputSignalWithType = output<string>();
56+
outputSignalWithStringType = output<'value'>();
57+
outputSignalWithMultipleTypes = output<string | number>();
58+
outputSignalWithMultipleMixedTypes = output<'asc' | 'dsc' | number>();
6459
}

0 commit comments

Comments
 (0)