Skip to content

Commit

Permalink
fix(type-compiler): do not transform {default() {}} expression as fun…
Browse files Browse the repository at this point in the history
…ction name can not be preserved.

fixes #333
  • Loading branch information
marcj committed Aug 1, 2022
1 parent c3242b9 commit 0e5543a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/type-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,19 @@ export class ReflectionTransformer implements CustomTransformer {
if (isMethodDeclaration(node) && node.parent && node.body && isObjectLiteralExpression(node.parent)) {
//replace MethodDeclaration with MethodExpression
// {add(v: number) {}} => {add: function (v: number) {}}
//so that __type can be added
const method = this.decorateFunctionExpression(
this.f.createFunctionExpression(
node.modifiers, node.asteriskToken, isIdentifier(node.name) ? node.name : undefined,
node.typeParameters, node.parameters, node.type, node.body
)
);
node = this.f.createPropertyAssignment(node.name, method);
//so that __type can be added.
//{default(){}} can not be converted without losing the function name, so we skip that for the moment.
let valid = true;
if (node.name.kind === SyntaxKind.Identifier && getIdentifierName(node.name) === 'default') valid = false;
if (valid) {
const method = this.decorateFunctionExpression(
this.f.createFunctionExpression(
node.modifiers, node.asteriskToken, isIdentifier(node.name) ? node.name : undefined,
node.typeParameters, node.parameters, node.type, node.body
)
);
node = this.f.createPropertyAssignment(node.name, method);
}
}

if (isClassDeclaration(node)) {
Expand Down
17 changes: 17 additions & 0 deletions packages/type-compiler/tests/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,20 @@ test('export default async function', () => {

expect(res.app).toContain('export default __assignType(async function (bar: string');
});

test('default function name', () => {
const res = transform({
'app': `
const a = {
default(val: any): any {
console.log('default',val)
return 'default'
}
};
`
});

//`function default(` is invalid syntax.
//as solution we skip that transformation.
expect(res.app).not.toContain('function default(');
});

0 comments on commit 0e5543a

Please sign in to comment.