Skip to content

Commit

Permalink
feat: 馃幐 parse additional input types
Browse files Browse the repository at this point in the history
  • Loading branch information
kreuzerk committed May 1, 2024
1 parent 37839c3 commit aabc6cb
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 1 deletion.
156 changes: 156 additions & 0 deletions src/parser/shared/parser/field-decorator.parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,50 @@ describe('Field Decorator', function () {
});
});

it('should parse signal inputs with a number', () => {
const ast = tsquery.ast(`
export class MyTestClass {
test = input(0);
}
`);

const expectedInputs = [
{
decorator: 'input',
name: 'test',
required: false,
initialValue: '0',
field: 'test = input(0);',
},
];
expect(parseInputsAndOutputs(ast)).toEqual({
inputs: expectedInputs,
outputs: [],
});
});

it('should parse signal inputs with a template string', () => {
const ast = tsquery.ast(`
export class MyTestClass {
test = input(\`test\`);
}
`);

const expectedInputs = [
{
decorator: 'input',
name: 'test',
required: false,
initialValue: '`test`',
field: 'test = input(`test`);',
},
];
expect(parseInputsAndOutputs(ast)).toEqual({
inputs: expectedInputs,
outputs: [],
});
});

it('should parse typed required signal inputs', () => {
const ast = tsquery.ast(`
export class MyTestClass {
Expand All @@ -236,6 +280,51 @@ describe('Field Decorator', function () {
outputs: [],
});
});

it('should parse model inputs with a template string', () => {
const ast = tsquery.ast(`
export class MyTestClass {
test = input(\`myvalue\`);
}
`);

const expectedInputs = [
{
decorator: 'input',
name: 'test',
initialValue: '`myvalue`',
required: false,
field: 'test = input(`myvalue`);',
},
];
expect(parseInputsAndOutputs(ast)).toEqual({
inputs: expectedInputs,
outputs: [],
});
});

it('should parse signal inputs with a template string and dynamic variable access', () => {
const foo = 'bar';
const ast = tsquery.ast(`
export class MyTestClass {
test = input(\`myvalue ${foo}\`);
}
`);

const expectedInputs = [
{
decorator: 'input',
name: 'test',
initialValue: '`myvalue bar`',
required: false,
field: 'test = input(`myvalue bar`);',
},
];
expect(parseInputsAndOutputs(ast)).toEqual({
inputs: expectedInputs,
outputs: [],
});
});
});

describe('model inputs', () => {
Expand Down Expand Up @@ -414,6 +503,73 @@ describe('Field Decorator', function () {
outputs: [],
});
});

it('should parse model inputs with a number', () => {
const ast = tsquery.ast(`
export class MyTestClass {
test = model(0);
}
`);

const expectedInputs = [
{
decorator: 'model',
name: 'test',
initialValue: '0',
required: false,
field: 'test = model(0);',
},
];
expect(parseInputsAndOutputs(ast)).toEqual({
inputs: expectedInputs,
outputs: [],
});
});

it('should parse model inputs with a template string', () => {
const ast = tsquery.ast(`
export class MyTestClass {
test = model(\`myvalue\`);
}
`);

const expectedInputs = [
{
decorator: 'model',
name: 'test',
initialValue: '`myvalue`',
required: false,
field: 'test = model(`myvalue`);',
},
];
expect(parseInputsAndOutputs(ast)).toEqual({
inputs: expectedInputs,
outputs: [],
});
});

it('should parse model inputs with a template string and dynamic variable access', () => {
const foo = 'bar';
const ast = tsquery.ast(`
export class MyTestClass {
test = model(\`myvalue ${foo}\`);
}
`);

const expectedInputs = [
{
decorator: 'model',
name: 'test',
initialValue: '`myvalue bar`',
required: false,
field: 'test = model(`myvalue bar`);',
},
];
expect(parseInputsAndOutputs(ast)).toEqual({
inputs: expectedInputs,
outputs: [],
});
});
});

describe('output', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/shared/parser/field-decorator.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function parseSignalInputsAndModels(ast: ts.SourceFile): NgParselFieldDecorator[
[
...tsquery(
field,
'CallExpression > :matches(NullKeyword, ObjectLiteralExpression, ArrayLiteralExpression, TrueKeyword, FalseKeyword, StringLiteral, Identifier[name=undefined])'
'CallExpression > :matches(NullKeyword, ObjectLiteralExpression, ArrayLiteralExpression, TrueKeyword, FalseKeyword, StringLiteral, Identifier[name=undefined], NumericLiteral, TemplateExpression, NoSubstitutionTemplateLiteral)'
),
][0]?.getText() || '';
const type = (required && [...tsquery(field, 'CallExpression > *:last-child')][0]?.getText()) || '';
Expand Down

0 comments on commit aabc6cb

Please sign in to comment.