Skip to content

Commit 56ec91a

Browse files
committed
feat(compiler): extract alias from signal options
Parse the `alias` property from the options object passed to input/output/model signals and use it as the public name in generated documentation. Add unit tests for alias extraction in ComponentHelper and integration tests for aliased signal rendering in CLI output. closes #1681
1 parent 6f1b63a commit 56ec91a

4 files changed

Lines changed: 183 additions & 16 deletions

File tree

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,61 @@ export class ComponentHelper {
226226
// argsStr would be "false, { transform: booleanAttribute }" and we only
227227
// want "false" as the default value.
228228
const fullArgs = normalized.slice(pos + 1, argsEnd).trim();
229-
const signalDefaultValue = this.extractFirstSignalArg(fullArgs) || undefined;
230-
231-
return {
229+
const firstArg = this.extractFirstSignalArg(fullArgs) || undefined;
230+
const secondArg = this.extractSignalOptions(fullArgs);
231+
232+
// When there is no second argument but the sole argument starts with '{',
233+
// it is the options object — not a default value.
234+
// e.g. output({ alias: 'x' }) or input.required<T>({ alias: 'x' })
235+
const isSoleArgOptions = !secondArg && !!firstArg?.trimStart().startsWith('{');
236+
const signalDefaultValue = isSoleArgOptions ? undefined : firstArg;
237+
238+
// Extract the alias from the options object (second argument), if present.
239+
// Valid HTML attribute names allow any characters except ASCII control chars,
240+
// space, and "'>/=, so we use a broad character class instead of \w.
241+
const optionsStr = isSoleArgOptions ? firstArg : secondArg;
242+
const aliasRegExp = /alias:\s*['"\`]([^\u0000-\u001F\u007F\u0080-\u009F '"\`>/=]+)['"\`]/;
243+
const alias = optionsStr?.match(aliasRegExp)?.[1];
244+
245+
const result = {
232246
required,
233247
type: this.parseSignalType(signalType),
234248
defaultValue: signalDefaultValue
235249
};
250+
251+
return alias ? { ...result, name: alias } : result;
252+
}
253+
254+
/**
255+
* Extracts the options object string (second argument) from a signal's
256+
* argument string. Returns undefined if there is no second argument.
257+
* For example:
258+
* "'hello', { alias: 'my-alias' }" → "{ alias: 'my-alias' }"
259+
* "'hello'" → undefined
260+
*/
261+
private extractSignalOptions(argsStr: string): string | undefined {
262+
if (!argsStr) return undefined;
263+
264+
let depth = 0;
265+
for (let i = 0; i < argsStr.length; i++) {
266+
const ch = argsStr[i];
267+
if (ch === '"' || ch === "'" || ch === '`') {
268+
const quote = ch;
269+
i++;
270+
while (i < argsStr.length && argsStr[i] !== quote) {
271+
if (argsStr[i] === '\\') i++;
272+
i++;
273+
}
274+
} else if (ch === '(' || ch === '[' || ch === '{') {
275+
depth++;
276+
} else if (ch === ')' || ch === ']' || ch === '}') {
277+
depth--;
278+
} else if (ch === ',' && depth === 0) {
279+
return argsStr.slice(i + 1).trim();
280+
}
281+
}
282+
283+
return undefined;
236284
}
237285

238286
/**

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

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,16 @@ describe('ComponentHelper', () => {
309309
});
310310

311311
it('should parse InputOptionsWithTransform with union types without hanging (issue #1654)', () => {
312-
// This case caused catastrophic backtracking in the old regex
312+
// This case caused catastrophic backtracking in the old regex.
313+
// The options object { transform: ... } is the sole argument and must not
314+
// leak into defaultValue — it should be treated as the options object.
313315
const defaultValue = `input.required<string[], string | string[]>({ transform: v => (Array.isArray(v) ? v : [v]) })`;
314316
const result = componentHelper['getSignalConfig']('input', defaultValue);
315317

316318
expect(result).to.deep.equal({
317319
required: true,
318320
type: 'string[], string | string[]',
319-
defaultValue: '{ transform: v => (Array.isArray(v) ? v : [v]) }'
321+
defaultValue: undefined
320322
});
321323
});
322324

@@ -339,7 +341,8 @@ describe('ComponentHelper', () => {
339341
expect(result).to.deep.equal({
340342
required: false,
341343
type: undefined,
342-
defaultValue: 'null'
344+
defaultValue: 'null',
345+
name: 'aliasedInSignal'
343346
});
344347
});
345348

@@ -353,6 +356,98 @@ describe('ComponentHelper', () => {
353356
defaultValue: 'false'
354357
});
355358
});
359+
360+
// Alias regression tests (issue: signal aliases not detected)
361+
it('should extract alphanumeric alias from input signal options', () => {
362+
const defaultValue = `input<string>('Lorem ipsum', { alias: 'myUnhyphenatedInput' })`;
363+
const result = componentHelper['getSignalConfig']('input', defaultValue);
364+
365+
expect(result).to.deep.equal({
366+
required: false,
367+
type: 'string',
368+
defaultValue: "'Lorem ipsum'",
369+
name: 'myUnhyphenatedInput'
370+
});
371+
});
372+
373+
it('should extract hyphenated alias from input signal options', () => {
374+
const defaultValue = `input<string>('sit amet', { alias: 'my-hyphenated-input' })`;
375+
const result = componentHelper['getSignalConfig']('input', defaultValue);
376+
377+
expect(result).to.deep.equal({
378+
required: false,
379+
type: 'string',
380+
defaultValue: "'sit amet'",
381+
name: 'my-hyphenated-input'
382+
});
383+
});
384+
385+
it('should extract alias from required input signal options', () => {
386+
const defaultValue = `input.required<string>({ alias: 'requiredAlias' })`;
387+
const result = componentHelper['getSignalConfig']('input', defaultValue);
388+
389+
expect(result).to.deep.equal({
390+
required: true,
391+
type: 'string',
392+
defaultValue: undefined,
393+
name: 'requiredAlias'
394+
});
395+
});
396+
397+
it('should extract alphanumeric alias from output signal options', () => {
398+
const defaultValue = `output<string>({ alias: 'myUnhyphenatedOutput' })`;
399+
const result = componentHelper['getSignalConfig']('output', defaultValue);
400+
401+
expect(result).to.deep.equal({
402+
required: false,
403+
type: 'string',
404+
defaultValue: undefined,
405+
name: 'myUnhyphenatedOutput'
406+
});
407+
});
408+
409+
it('should extract hyphenated alias from output signal options', () => {
410+
const defaultValue = `output<string>({ alias: 'my-hyphenated-output' })`;
411+
const result = componentHelper['getSignalConfig']('output', defaultValue);
412+
413+
expect(result).to.deep.equal({
414+
required: false,
415+
type: 'string',
416+
defaultValue: undefined,
417+
name: 'my-hyphenated-output'
418+
});
419+
});
420+
421+
it('should extract alias from model signal options', () => {
422+
const defaultValue = `model<string>('Lorem ipsum', { alias: 'myUnhyphenatedModel' })`;
423+
const result = componentHelper['getSignalConfig']('model', defaultValue);
424+
425+
expect(result).to.deep.equal({
426+
required: false,
427+
type: 'string',
428+
defaultValue: "'Lorem ipsum'",
429+
name: 'myUnhyphenatedModel'
430+
});
431+
});
432+
433+
it('should extract hyphenated alias from model signal options', () => {
434+
const defaultValue = `model<string>('sit amet', { alias: 'my-hyphenated-Model' })`;
435+
const result = componentHelper['getSignalConfig']('model', defaultValue);
436+
437+
expect(result).to.deep.equal({
438+
required: false,
439+
type: 'string',
440+
defaultValue: "'sit amet'",
441+
name: 'my-hyphenated-Model'
442+
});
443+
});
444+
445+
it('should not set name when options object has no alias', () => {
446+
const defaultValue = `input<boolean>(false, { transform: booleanAttribute })`;
447+
const result = componentHelper['getSignalConfig']('input', defaultValue);
448+
449+
expect(result).to.not.have.property('name');
450+
});
356451
});
357452

358453
describe('getInputSignal', () => {
@@ -368,6 +463,18 @@ describe('ComponentHelper', () => {
368463
});
369464
});
370465

466+
it('should override prop name with alias from signal options', () => {
467+
const prop = { name: 'myInput', defaultValue: `input<string>('hello', { alias: 'my-aliased-input' })` };
468+
const result = componentHelper.getInputSignal(prop);
469+
470+
expect(result).to.deep.equal({
471+
name: 'my-aliased-input',
472+
defaultValue: "'hello'",
473+
required: false,
474+
type: 'string'
475+
});
476+
});
477+
371478
it('should return undefined when no input signal found', () => {
372479
const prop = { defaultValue: 'someOtherValue' };
373480
const result = componentHelper.getInputSignal(prop);
@@ -389,6 +496,18 @@ describe('ComponentHelper', () => {
389496
});
390497
});
391498

499+
it('should override prop name with alias from output signal options', () => {
500+
const prop = { name: 'myOutput', defaultValue: `output<string>({ alias: 'my-aliased-output' })` };
501+
const result = componentHelper.getOutputSignal(prop);
502+
503+
expect(result).to.deep.equal({
504+
name: 'my-aliased-output',
505+
defaultValue: undefined,
506+
required: false,
507+
type: 'string'
508+
});
509+
});
510+
392511
it('should return undefined when no output signal found', () => {
393512
const prop = { defaultValue: 'someOtherValue' };
394513
const result = componentHelper.getOutputSignal(prop);

‎test/src/cli/cli-generation-big-app.spec.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,8 @@ describe('CLI simple generation - big app', () => {
11021102
<tbody>
11031103
<tr>
11041104
<td class="col-md-4">
1105-
<a name="inputSignalWithAlias"></a>
1106-
<b>inputSignalWithAlias</b>
1105+
<a name="aliasedSignal"></a>
1106+
<b>aliasedSignal</b>
11071107
</td>
11081108
</tr>
11091109
<tr>
@@ -1351,8 +1351,8 @@ describe('CLI simple generation - big app', () => {
13511351
<tbody>
13521352
<tr>
13531353
<td class="col-md-4">
1354-
<a name="outputSignalWithAlias"></a>
1355-
<b>outputSignalWithAlias</b>
1354+
<a name="aliasedSignal"></a>
1355+
<b>aliasedSignal</b>
13561356
</td>
13571357
</tr>
13581358
<tr>
@@ -1569,8 +1569,8 @@ describe('CLI simple generation - big app', () => {
15691569
<tbody>
15701570
<tr>
15711571
<td class="col-md-4">
1572-
<a name="modelSignalWithAlias"></a>
1573-
<b>modelSignalWithAlias</b>
1572+
<a name="aliasedSignal"></a>
1573+
<b>aliasedSignal</b>
15741574
</td>
15751575
</tr>
15761576
<tr>

‎test/src/cli/cli-generation.spec.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ describe('CLI simple generation', () => {
305305
<tbody>
306306
<tr>
307307
<td class="col-md-4">
308-
<a name="aliasedInputSignal"></a>
309-
<b>aliasedInputSignal</b>
308+
<a name="aliasedInSignal"></a>
309+
<b>aliasedInSignal</b>
310310
</td>
311311
</tr>
312312
<tr>
@@ -457,8 +457,8 @@ describe('CLI simple generation', () => {
457457
<tbody>
458458
<tr>
459459
<td class="col-md-4">
460-
<a name="aliasedOutputSignal"></a>
461-
<b>aliasedOutputSignal</b>
460+
<a name="aliasedOutSignal"></a>
461+
<b>aliasedOutSignal</b>
462462
</td>
463463
</tr>
464464
<tr>

0 commit comments

Comments
 (0)