diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 88d9d20c4c0..996dac118ef 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4280,25 +4280,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi // TODO (yuisu) : we should not have special cases to condition emitting comments // but have one place to fix check for these conditions. - if (node.kind !== SyntaxKind.MethodDeclaration && - node.kind !== SyntaxKind.MethodSignature && - node.parent && - node.parent.kind !== SyntaxKind.PropertyAssignment && - node.parent.kind !== SyntaxKind.CallExpression && - node.parent.kind !== SyntaxKind.ArrayLiteralExpression) { - // 1. Methods will emit the comments as part of emitting method declaration - + const { kind, parent } = node; + if (kind !== SyntaxKind.MethodDeclaration && + kind !== SyntaxKind.MethodSignature && + parent && + parent.kind !== SyntaxKind.PropertyAssignment && + parent.kind !== SyntaxKind.CallExpression && + parent.kind !== SyntaxKind.ArrayLiteralExpression) { + // 1. Methods will emit comments at their assignment declaration sites. + // // 2. If the function is a property of object literal, emitting leading-comments - // is done by emitNodeWithoutSourceMap which then call this function. - // In particular, we would like to avoid emit comments twice in following case: - // For example: + // is done by emitNodeWithoutSourceMap which then call this function. + // In particular, we would like to avoid emit comments twice in following case: + // // var obj = { // id: // /*comment*/ () => void // } - + // // 3. If the function is an argument in call expression, emitting of comments will be - // taken care of in emit list of arguments inside of emitCallexpression + // taken care of in emit list of arguments inside of 'emitCallExpression'. + // + // 4. If the function is in an array literal, 'emitLinePreservingList' will take care + // of leading comments. emitLeadingComments(node); } @@ -4325,12 +4329,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } emitSignatureAndBody(node); - if (modulekind !== ModuleKind.ES6 && node.kind === SyntaxKind.FunctionDeclaration && node.parent === currentSourceFile && node.name) { + if (modulekind !== ModuleKind.ES6 && kind === SyntaxKind.FunctionDeclaration && parent === currentSourceFile && node.name) { emitExportMemberAssignments((node).name); } emitEnd(node); - if (node.kind !== SyntaxKind.MethodDeclaration && node.kind !== SyntaxKind.MethodSignature) { + if (kind !== SyntaxKind.MethodDeclaration && kind !== SyntaxKind.MethodSignature) { emitTrailingComments(node); } } diff --git a/tests/baselines/reference/decoratorOnClassMethodOverload2.js b/tests/baselines/reference/decoratorOnClassMethodOverload2.js new file mode 100644 index 00000000000..eafa5da7110 --- /dev/null +++ b/tests/baselines/reference/decoratorOnClassMethodOverload2.js @@ -0,0 +1,25 @@ +//// [decoratorOnClassMethodOverload2.ts] +declare function dec(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; + +class C { + method() + @dec + method() { } +} + +//// [decoratorOnClassMethodOverload2.js] +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +var C = (function () { + function C() { + } + C.prototype.method = function () { }; + __decorate([ + dec + ], C.prototype, "method", null); + return C; +}()); diff --git a/tests/baselines/reference/decoratorOnClassMethodOverload2.symbols b/tests/baselines/reference/decoratorOnClassMethodOverload2.symbols new file mode 100644 index 00000000000..f05dd624345 --- /dev/null +++ b/tests/baselines/reference/decoratorOnClassMethodOverload2.symbols @@ -0,0 +1,24 @@ +=== tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload2.ts === +declare function dec(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; +>dec : Symbol(dec, Decl(decoratorOnClassMethodOverload2.ts, 0, 0)) +>T : Symbol(T, Decl(decoratorOnClassMethodOverload2.ts, 0, 21)) +>target : Symbol(target, Decl(decoratorOnClassMethodOverload2.ts, 0, 24)) +>propertyKey : Symbol(propertyKey, Decl(decoratorOnClassMethodOverload2.ts, 0, 36)) +>descriptor : Symbol(descriptor, Decl(decoratorOnClassMethodOverload2.ts, 0, 57)) +>TypedPropertyDescriptor : Symbol(TypedPropertyDescriptor, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(decoratorOnClassMethodOverload2.ts, 0, 21)) +>TypedPropertyDescriptor : Symbol(TypedPropertyDescriptor, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(decoratorOnClassMethodOverload2.ts, 0, 21)) + +class C { +>C : Symbol(C, Decl(decoratorOnClassMethodOverload2.ts, 0, 126)) + + method() +>method : Symbol(method, Decl(decoratorOnClassMethodOverload2.ts, 2, 9), Decl(decoratorOnClassMethodOverload2.ts, 3, 12)) + + @dec +>dec : Symbol(dec, Decl(decoratorOnClassMethodOverload2.ts, 0, 0)) + + method() { } +>method : Symbol(method, Decl(decoratorOnClassMethodOverload2.ts, 2, 9), Decl(decoratorOnClassMethodOverload2.ts, 3, 12)) +} diff --git a/tests/baselines/reference/decoratorOnClassMethodOverload2.types b/tests/baselines/reference/decoratorOnClassMethodOverload2.types new file mode 100644 index 00000000000..7002d74a821 --- /dev/null +++ b/tests/baselines/reference/decoratorOnClassMethodOverload2.types @@ -0,0 +1,24 @@ +=== tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload2.ts === +declare function dec(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; +>dec : (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor +>T : T +>target : any +>propertyKey : string +>descriptor : TypedPropertyDescriptor +>TypedPropertyDescriptor : TypedPropertyDescriptor +>T : T +>TypedPropertyDescriptor : TypedPropertyDescriptor +>T : T + +class C { +>C : C + + method() +>method : () => any + + @dec +>dec : (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor + + method() { } +>method : () => any +} diff --git a/tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload2.ts b/tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload2.ts new file mode 100644 index 00000000000..ee5a3f33bf1 --- /dev/null +++ b/tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload2.ts @@ -0,0 +1,9 @@ +// @target: ES5 +// @experimentaldecorators: true +declare function dec(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; + +class C { + method() + @dec + method() { } +} \ No newline at end of file