diff --git a/packages/type-compiler/src/compiler.ts b/packages/type-compiler/src/compiler.ts index 64bffdaec..a88c729c5 100644 --- a/packages/type-compiler/src/compiler.ts +++ b/packages/type-compiler/src/compiler.ts @@ -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)) { diff --git a/packages/type-compiler/tests/transform.spec.ts b/packages/type-compiler/tests/transform.spec.ts index c72511a70..68741fb1b 100644 --- a/packages/type-compiler/tests/transform.spec.ts +++ b/packages/type-compiler/tests/transform.spec.ts @@ -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('); +});