Skip to content

Commit 03e7b5b

Browse files
authored
Merge pull request #22 from angular-experts-io/feature/improveTypedetection
feat: 🎸 improve type detection
2 parents 93cd0cd + 974e74e commit 03e7b5b

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

src/parser/shared/model/decorator.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface NgParselFieldDecorator {
22
decorator: string;
33
name: string;
4-
type?: string;
4+
type?: string | 'inferred';
55
required?: boolean;
66
initializer?: string;
77
initialValue?: string | null;

src/parser/shared/parser/field-decorator.parser.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ describe('Field Decorator', function () {
6161
});
6262

6363
describe('signal inputs', () => {
64+
it('should parse a custom type type of an optional input', () => {
65+
const ast = tsquery.ast(`
66+
export class MyTestClass {
67+
test = input<myIcon>();
68+
}
69+
`);
70+
71+
const expectedInputs = [
72+
{
73+
decorator: 'input',
74+
name: 'test',
75+
initialValue: '',
76+
type: 'myIcon',
77+
required: false,
78+
field: 'test = input<myIcon>();',
79+
},
80+
];
81+
expect(parseInputsAndOutputs(ast)).toEqual({
82+
inputs: expectedInputs,
83+
outputs: [],
84+
});
85+
});
86+
6487
it('should parse signal inputs with initial string value', () => {
6588
const ast = tsquery.ast(`
6689
export class MyTestClass {
@@ -74,6 +97,7 @@ describe('Field Decorator', function () {
7497
name: 'test',
7598
required: false,
7699
initialValue: '"myValue"',
100+
type: 'inferred',
77101
field: 'test = input("myValue");',
78102
},
79103
];
@@ -95,6 +119,7 @@ describe('Field Decorator', function () {
95119
decorator: 'input',
96120
name: 'test',
97121
required: false,
122+
type: 'inferred',
98123
initialValue: 'true',
99124
field: 'test = input(true);',
100125
},
@@ -118,6 +143,7 @@ describe('Field Decorator', function () {
118143
name: 'test',
119144
required: false,
120145
initialValue: 'false',
146+
type: 'inferred',
121147
field: 'test = input(false);',
122148
},
123149
];
@@ -139,6 +165,7 @@ describe('Field Decorator', function () {
139165
decorator: 'input',
140166
name: 'test',
141167
required: false,
168+
type: 'inferred',
142169
initialValue: '[]',
143170
field: 'test = input([]);',
144171
},
@@ -161,6 +188,7 @@ describe('Field Decorator', function () {
161188
decorator: 'input',
162189
name: 'test',
163190
required: false,
191+
type: 'inferred',
164192
initialValue: '{}',
165193
field: 'test = input({});',
166194
},
@@ -183,6 +211,7 @@ describe('Field Decorator', function () {
183211
decorator: 'input',
184212
name: 'test',
185213
required: false,
214+
type: 'null',
186215
initialValue: 'null',
187216
field: 'test = input(null);',
188217
},
@@ -205,6 +234,7 @@ describe('Field Decorator', function () {
205234
decorator: 'input',
206235
name: 'test',
207236
required: false,
237+
type: 'inferred',
208238
initialValue: 'undefined',
209239
field: 'test = input(undefined);',
210240
},
@@ -227,6 +257,7 @@ describe('Field Decorator', function () {
227257
decorator: 'input',
228258
name: 'test',
229259
required: false,
260+
type: 'inferred',
230261
initialValue: '0',
231262
field: 'test = input(0);',
232263
},
@@ -249,6 +280,7 @@ describe('Field Decorator', function () {
249280
decorator: 'input',
250281
name: 'test',
251282
required: false,
283+
type: 'inferred',
252284
initialValue: '`test`',
253285
field: 'test = input(`test`);',
254286
},
@@ -293,6 +325,7 @@ describe('Field Decorator', function () {
293325
decorator: 'input',
294326
name: 'test',
295327
initialValue: '`myvalue`',
328+
type: 'inferred',
296329
required: false,
297330
field: 'test = input(`myvalue`);',
298331
},
@@ -315,6 +348,7 @@ describe('Field Decorator', function () {
315348
{
316349
decorator: 'input',
317350
name: 'test',
351+
type: 'inferred',
318352
initialValue: '`myvalue bar`',
319353
required: false,
320354
field: 'test = input(`myvalue bar`);',
@@ -339,6 +373,7 @@ describe('Field Decorator', function () {
339373
{
340374
decorator: 'model',
341375
name: 'test',
376+
type: 'inferred',
342377
required: false,
343378
initialValue: '"myValue"',
344379
field: 'test = model("myValue");',
@@ -362,6 +397,7 @@ describe('Field Decorator', function () {
362397
decorator: 'model',
363398
name: 'test',
364399
required: false,
400+
type: 'inferred',
365401
initialValue: 'true',
366402
field: 'test = model(true);',
367403
},
@@ -384,6 +420,7 @@ describe('Field Decorator', function () {
384420
decorator: 'model',
385421
name: 'test',
386422
required: false,
423+
type: 'inferred',
387424
initialValue: 'false',
388425
field: 'test = model(false);',
389426
},
@@ -406,6 +443,7 @@ describe('Field Decorator', function () {
406443
decorator: 'model',
407444
name: 'test',
408445
required: false,
446+
type: 'inferred',
409447
initialValue: '[]',
410448
field: 'test = model([]);',
411449
},
@@ -428,6 +466,7 @@ describe('Field Decorator', function () {
428466
decorator: 'model',
429467
name: 'test',
430468
required: false,
469+
type: 'inferred',
431470
initialValue: '{}',
432471
field: 'test = model({});',
433472
},
@@ -450,6 +489,7 @@ describe('Field Decorator', function () {
450489
decorator: 'model',
451490
name: 'test',
452491
required: false,
492+
type: 'null',
453493
initialValue: 'null',
454494
field: 'test = model(null);',
455495
},
@@ -472,6 +512,7 @@ describe('Field Decorator', function () {
472512
decorator: 'model',
473513
name: 'test',
474514
required: false,
515+
type: 'inferred',
475516
initialValue: 'undefined',
476517
field: 'test = model(undefined);',
477518
},
@@ -515,6 +556,7 @@ describe('Field Decorator', function () {
515556
{
516557
decorator: 'model',
517558
name: 'test',
559+
type: 'inferred',
518560
initialValue: '0',
519561
required: false,
520562
field: 'test = model(0);',
@@ -537,6 +579,7 @@ describe('Field Decorator', function () {
537579
{
538580
decorator: 'model',
539581
name: 'test',
582+
type: 'inferred',
540583
initialValue: '`myvalue`',
541584
required: false,
542585
field: 'test = model(`myvalue`);',
@@ -560,6 +603,7 @@ describe('Field Decorator', function () {
560603
{
561604
decorator: 'model',
562605
name: 'test',
606+
type: 'inferred',
563607
initialValue: '`myvalue bar`',
564608
required: false,
565609
field: 'test = model(`myvalue bar`);',

src/parser/shared/parser/field-decorator.parser.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ function parseDecoratedPropertyDeclarations(ast: ts.SourceFile): {
4646
outputs: NgParselFieldDecorator[];
4747
} {
4848
/*
49-
This is afaik the only way to get the Decorator name
50-
- getDecorators() returns nothing
51-
- canHaveDecorators() returns false
52-
*/
49+
This is afaik the only way to get the Decorator name
50+
- getDecorators() returns nothing
51+
- canHaveDecorators() returns false
52+
*/
5353
const decoratorPropertyDecorator = [...tsquery(ast, 'PropertyDeclaration:has(Decorator) > Decorator')];
5454
const decoratorPropertyDeclaration = [...tsquery(ast, 'PropertyDeclaration:has(Decorator)')];
5555

@@ -113,7 +113,14 @@ function parseSignalInputsAndModels(ast: ts.SourceFile): NgParselFieldDecorator[
113113
'CallExpression > :matches(NullKeyword, ObjectLiteralExpression, ArrayLiteralExpression, TrueKeyword, FalseKeyword, StringLiteral, Identifier[name=undefined], NumericLiteral, TemplateExpression, NoSubstitutionTemplateLiteral)'
114114
),
115115
][0]?.getText() || '';
116-
const type = (required && [...tsquery(field, 'CallExpression > *:last-child')][0]?.getText()) || '';
116+
117+
const type =
118+
[
119+
...tsquery(
120+
field,
121+
'CallExpression > :matches(BooleanKeyword, AnyKeyword, TypeReference, StringKeyword, LiteralType, TypeLiteral, NullKeyword, UndefinedKeyword, Identifier[name=Array], ArrayType)'
122+
),
123+
][0]?.getText() || 'inferred';
117124

118125
if (required) {
119126
signalInputs.push({
@@ -127,8 +134,9 @@ function parseSignalInputsAndModels(ast: ts.SourceFile): NgParselFieldDecorator[
127134
signalInputs.push({
128135
decorator,
129136
required,
130-
name,
131137
initialValue,
138+
name,
139+
type,
132140
field,
133141
});
134142
}

0 commit comments

Comments
 (0)