From 68170dfbdba7b0c45b0a81b7e525ffb308ce67b6 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Thu, 7 Apr 2016 16:17:58 -0700 Subject: [PATCH] Add a source mapping to the closing `}` for functions, and wire in positions for class transfomtaion --- src/compiler/printer.ts | 14 ++++++-------- src/compiler/transformers/es6.ts | 16 ++++++++++------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/compiler/printer.ts b/src/compiler/printer.ts index 4193ac6ef75..23220b25f42 100644 --- a/src/compiler/printer.ts +++ b/src/compiler/printer.ts @@ -1095,7 +1095,7 @@ const _super = (function (geti, seti) { } function emitPrefixUnaryExpression(node: PrefixUnaryExpression) { - writeToken(node.operator); + writeTokenText(node.operator); if (shouldEmitWhitespaceBeforeOperand(node)) { write(" "); } @@ -1123,7 +1123,7 @@ const _super = (function (geti, seti) { function emitPostfixUnaryExpression(node: PostfixUnaryExpression) { emitExpression(node.operand); - writeToken(node.operator); + writeTokenText(node.operator); } function emitBinaryExpression(node: BinaryExpression) { @@ -1419,8 +1419,7 @@ const _super = (function (geti, seti) { emitSignatureHead(node); write(" {"); emitBlockFunctionBody(node, body); - write("}"); - + writeToken(SyntaxKind.CloseBraceToken, node.end) if (indentedFlag) { decreaseIndent(); } @@ -1826,7 +1825,7 @@ const _super = (function (geti, seti) { function emitHeritageClause(node: HeritageClause) { write(" "); - writeToken(node.token); + writeTokenText(node.token); write(" "); emitList(node, node.types, ListFormat.HeritageClauseTypes); } @@ -2275,10 +2274,9 @@ const _super = (function (geti, seti) { } } - function writeToken(token: SyntaxKind, pos?: number) { - const tokenStartPos = skipTrivia(currentText, pos); + function writeToken(token: SyntaxKind, tokenStartPos: number) { emitPos(tokenStartPos); - const tokenEndPos = writeTokenText(token, pos); + const tokenEndPos = writeTokenText(token, tokenStartPos); emitPos(tokenEndPos); return tokenEndPos; } diff --git a/src/compiler/transformers/es6.ts b/src/compiler/transformers/es6.ts index d1c443f8401..98c94d7c6a9 100644 --- a/src/compiler/transformers/es6.ts +++ b/src/compiler/transformers/es6.ts @@ -609,12 +609,14 @@ namespace ts { if (node.name) { enableSubstitutionsForBlockScopedBindings(); } + const closingBraceLocation = { pos: node.end - 1, end: node.end }; const baseTypeNode = getClassExtendsHeritageClauseElement(node); const classFunction = createFunctionExpression( /*asteriskToken*/ undefined, /*name*/ undefined, baseTypeNode ? [createParameter("_super")] : [], - transformClassBody(node, baseTypeNode !== undefined) + transformClassBody(node, baseTypeNode !== undefined, closingBraceLocation), + closingBraceLocation ); // To preserve the behavior of the old emitter, we explicitly indent @@ -629,8 +631,10 @@ namespace ts { classFunction, baseTypeNode ? [visitNode(baseTypeNode.expression, visitor, isExpression)] - : [] - ) + : [], + closingBraceLocation + ), + closingBraceLocation ); } @@ -640,13 +644,13 @@ namespace ts { * @param node A ClassExpression or ClassDeclaration node. * @param hasExtendsClause A value indicating whether the class has an `extends` clause. */ - function transformClassBody(node: ClassExpression | ClassDeclaration, hasExtendsClause: boolean): Block { + function transformClassBody(node: ClassExpression | ClassDeclaration, hasExtendsClause: boolean, closingBraceLocation: TextRange): Block { const statements: Statement[] = []; startLexicalEnvironment(); addExtendsHelperIfNeeded(statements, node, hasExtendsClause); addConstructor(statements, node, hasExtendsClause); addClassMembers(statements, node); - statements.push(createReturn(getDeclarationName(node))); + statements.push(createReturn(getDeclarationName(node), /*location*/ closingBraceLocation)); addRange(statements, endLexicalEnvironment()); return createBlock(statements, /*location*/ undefined, /*multiLine*/ true); } @@ -685,7 +689,7 @@ namespace ts { getDeclarationName(node), transformConstructorParameters(constructor, hasSynthesizedSuper), transformConstructorBody(constructor, hasExtendsClause, hasSynthesizedSuper), - /*location*/ constructor + /*location*/ constructor || node ) ); }