Skip to content

Commit

Permalink
Merge pull request #16 from angular-experts-io/feature/inputdefaults
Browse files Browse the repository at this point in the history
feat: 馃幐 handle all kinds of default valus for Signal inputs
  • Loading branch information
kreuzerk committed Apr 30, 2024
2 parents ce0a900 + d88c1ab commit 0348856
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"bump-version": "rjp package.json version $VERSION",
"start:version": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts --version",
"start:help": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts --help",
"start:parse": "ts-node --esm src/bin/ng-parsel.bin.ts parse --src './test-spa'",
"start:parse": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts parse --src './test-spa'",
"build": "tsc && npm run copy:readme",
"copy:readme": "copyfiles ./README.md ./dist",
"lint": "eslint src/**/*.ts",
Expand Down
134 changes: 133 additions & 1 deletion src/parser/shared/parser/field-decorator.parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Field Decorator', function () {
});
});

it('should parse signal inputs with an initial value', () => {
it('should parse signal inputs with initial string value', () => {
const ast = tsquery.ast(`
export class MyTestClass {
test = input("myValue");
Expand All @@ -80,6 +80,138 @@ describe('Field Decorator', function () {
});
});

it('should parse signal inputs with an initial boolean (true) value', () => {
const ast = tsquery.ast(`
export class MyTestClass {
test = input(true);
}
`);

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

it('should parse signal inputs with an initial boolean (false) value', () => {
const ast = tsquery.ast(`
export class MyTestClass {
test = input(false);
}
`);

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

it('should parse signal inputs with an initial array', () => {
const ast = tsquery.ast(`
export class MyTestClass {
test = input([]);
}
`);

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

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

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

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

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

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

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

it('should parse typed required signal inputs', () => {
const ast = tsquery.ast(`
export class MyTestClass {
Expand Down
11 changes: 8 additions & 3 deletions src/parser/shared/parser/field-decorator.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ function parseDecoratedPropertyDeclarations(ast: ts.SourceFile): {
return inputsAndOutputs;
}

function parseSignalInputs(asti: ts.SourceFile): NgParselFieldDecorator[] {
const inputNodes = [...tsquery(asti, 'PropertyDeclaration:has(CallExpression [name="input"])')];
function parseSignalInputs(ast: ts.SourceFile): NgParselFieldDecorator[] {
const inputNodes = [...tsquery(ast, 'PropertyDeclaration:has(CallExpression [name="input"])')];
const signalInputs: NgParselFieldDecorator[] = [];

function isRequiredSingalInput(file: string): boolean {
Expand All @@ -98,7 +98,12 @@ function parseSignalInputs(asti: ts.SourceFile): NgParselFieldDecorator[] {

const name = [...tsquery(field, 'BinaryExpression > Identifier')][0]?.getText() || '';
const initialValue =
[...tsquery(field, 'CallExpression > StringLiteral, Identifier:last-child')][0]?.getText() || '';
[
...tsquery(
field,
'CallExpression > :matches(NullKeyword, ObjectLiteralExpression, ArrayLiteralExpression, TrueKeyword, FalseKeyword, StringLiteral, Identifier[name=undefined])'
),
][0]?.getText() || '';
const type = (required && [...tsquery(field, 'CallExpression > *:last-child')][0]?.getText()) || '';

if (required) {
Expand Down
9 changes: 8 additions & 1 deletion test-spa/src/app/foo/foo.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter, input, Input, Output } from '@angular/core';
import { Component, EventEmitter, input, Input, model, Output } from '@angular/core';

@Component({
selector: 'foo',
Expand All @@ -12,6 +12,13 @@ export class FooComponent {
}

name = input<string>('Paul Atreides');
firstname = input.required<string>();
flag = input(true);
address = input({
street: 'my street',
});

anotherFlag = model(true);

@Output() fooChanged = new EventEmitter();
foo: string | undefined;
Expand Down

0 comments on commit 0348856

Please sign in to comment.