From 2a07d3f8db9e4bf8da8a74f8c3d184082a1d5e57 Mon Sep 17 00:00:00 2001 From: Yui T Date: Sun, 15 Mar 2015 12:33:29 -0700 Subject: [PATCH] Address code review: do not emit default constructor --- src/compiler/emitter.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 96c34ae743d..b3b305dd6c4 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4578,15 +4578,29 @@ module ts { tempParameters = undefined; var popFrame = enterNameScope(); + // Check if we have property assignment inside class declaration. + // If there is property assignment, we need to emit constructor whether users define it or not + // If there is no property assignment, we can omit constructor if users do not define it + var hasPropertyAssignment = false; // Emit the constructor overload pinned comments forEach(node.members, member => { if (member.kind === SyntaxKind.Constructor && !(member).body) { emitPinnedOrTripleSlashComments(member); } + if (member.kind === SyntaxKind.PropertyDeclaration && (member).initializer) { + hasPropertyAssignment = true; + } }); var ctor = getFirstConstructorWithBody(node); + + // For target ES6 and above, if there is no user-defined constructor and there is no property assignment + // do not emit constructor in class declaration. + if (languageVersion >= ScriptTarget.ES6 && !ctor && !hasPropertyAssignment) { + return; + } + if (ctor) { emitLeadingComments(ctor); } @@ -4617,6 +4631,7 @@ module ts { } } } + write(" {"); scopeEmitStart(node, "constructor"); increaseIndent();