Skip to content

Commit

Permalink
Merge pull request #15 from angular-experts-io/feature/new-output-api
Browse files Browse the repository at this point in the history
Feature/new output api
  • Loading branch information
nivekcode authored Mar 14, 2024
2 parents 36dd8dd + fa628d2 commit e390081
Show file tree
Hide file tree
Showing 4 changed files with 717 additions and 617 deletions.
41 changes: 41 additions & 0 deletions src/parser/shared/parser/field-decorator.parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,45 @@ describe('Field Decorator', function () {
outputs: [],
});
});

it('should parse the new output API', () => {
const ast = tsquery.ast(`
export class MyTestClass {
counterChange = output<number>();
}
`);

const expectedOutputs = [
{
decorator: 'output',
name: 'counterChange',
type: 'number',
field: 'counterChange = output<number>();',
},
];
expect(parseInputsAndOutputs(ast)).toEqual({
inputs: [],
outputs: expectedOutputs,
});
});

it('should parse the new output outputFromObservable API', () => {
const ast = tsquery.ast(`
export class MyTestClass {
counterChange = outputFromObservable(this.change$);
}
`);

const expectedOutputs = [
{
decorator: 'outputFromObservable',
name: 'counterChange',
field: 'counterChange = outputFromObservable(this.change$);',
},
];
expect(parseInputsAndOutputs(ast)).toEqual({
inputs: [],
outputs: expectedOutputs,
});
});
});
36 changes: 35 additions & 1 deletion src/parser/shared/parser/field-decorator.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function parseInputsAndOutputs(ast: ts.SourceFile): {
const parsedPropertyDeclarations = parseDecoratedPropertyDeclarations(ast);
return {
inputs: [...parseDecoratedSetters(ast), ...parsedPropertyDeclarations.inputs, ...parseSignalInputs(ast)],
outputs: [...parsedPropertyDeclarations.outputs],
outputs: [...parsedPropertyDeclarations.outputs, ...parseNewOutputAPI(ast)],
};
}

Expand Down Expand Up @@ -122,3 +122,37 @@ function parseSignalInputs(asti: ts.SourceFile): NgParselFieldDecorator[] {

return signalInputs;
}

function parseNewOutputAPI(ast: ts.SourceFile): NgParselFieldDecorator[] {
const outputNodes = [...tsquery(ast, 'PropertyDeclaration:has(CallExpression:has([name="output"]))')];
const outPutnodesFromObservable = [
...tsquery(ast, 'PropertyDeclaration:has(CallExpression:has([name="outputFromObservable"]))'),
];

const outputs: NgParselFieldDecorator[] = [];

[...outputNodes, ...outPutnodesFromObservable].forEach((node) => {
const field = node.getText();

const isObservableOutput = [...tsquery(field, 'CallExpression:has([name="outputFromObservable"])')].length > 0;
const name = [...tsquery(field, 'BinaryExpression > Identifier')][0]?.getText() || '';
const type = [...tsquery(field, 'CallExpression > *:last-child')][0]?.getText() || '';

if (isObservableOutput) {
outputs.push({
decorator: 'outputFromObservable',
name,
field,
});
} else {
outputs.push({
decorator: 'output',
name,
type,
field,
});
}
});

return outputs;
}
Loading

0 comments on commit e390081

Please sign in to comment.