From 609036a2d669572145fb18d96a213fa3f5c6e784 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 8 Apr 2015 17:06:00 -0700 Subject: [PATCH] PR feedback and baseline updates --- src/compiler/utilities.ts | 14 ++++++++------ src/services/formatting/formatting.ts | 4 ++-- src/services/formatting/rules.ts | 14 +++----------- src/services/services.ts | 6 +++--- tests/baselines/reference/APISample_linter.types | 4 ++-- tests/cases/fourslash/formattingDecorators.ts | 14 +++++++------- 6 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 30f392e5ded..a10be6ba225 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -150,20 +150,22 @@ module ts { return !nodeIsMissing(node); } - export function getTokenPosOfNode(node: Node, sourceFile?: SourceFile, skipDecorators?: boolean): number { + export function getTokenPosOfNode(node: Node, sourceFile?: SourceFile): number { // With nodes that have no width (i.e. 'Missing' nodes), we actually *don't* // want to skip trivia because this will launch us forward to the next token. if (nodeIsMissing(node)) { return node.pos; } - let pos = node.pos; - if (skipDecorators && node.decorators) { - // Skip past decorators - pos = node.decorators.end; + return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos); + } + + export function getNonDecoratorTokenPosOfNode(node: Node, sourceFile?: SourceFile): number { + if (nodeIsMissing(node) || !node.decorators) { + return getTokenPosOfNode(node, sourceFile); } - return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, pos); + return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.decorators.end); } export function getSourceTextOfNodeFromSourceFile(sourceFile: SourceFile, node: Node): string { diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index ddf2f4cad27..0f5f49f3d8f 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -330,7 +330,7 @@ module ts.formatting { let startLine = sourceFile.getLineAndCharacterOfPosition(enclosingNode.getStart(sourceFile)).line; let undecoratedStartLine = startLine; if (enclosingNode.decorators) { - undecoratedStartLine = sourceFile.getLineAndCharacterOfPosition(enclosingNode.getStart(sourceFile, /*skipDecorators*/ true)).line; + undecoratedStartLine = sourceFile.getLineAndCharacterOfPosition(getNonDecoratorTokenPosOfNode(enclosingNode, sourceFile)).line; } let delta = getOwnOrInheritedDelta(enclosingNode, options, sourceFile); @@ -561,7 +561,7 @@ module ts.formatting { let undecoratedChildStartLine = childStartLine; if (child.decorators) { - undecoratedChildStartLine = sourceFile.getLineAndCharacterOfPosition(child.getStart(sourceFile, /*skipDecorators*/ true)).line; + undecoratedChildStartLine = sourceFile.getLineAndCharacterOfPosition(getNonDecoratorTokenPosOfNode(child, sourceFile)).line; } // if child is a list item - try to get its indentation diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index ac3d7fe6783..f5cb8cb4e64 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -670,18 +670,10 @@ module ts.formatting { } static NodeIsInDecoratorContext(node: Node): boolean { - if (node.parserContextFlags & ParserContextFlags.Decorator) { - return true; + while (isExpression(node)) { + node = node.parent; } - while (node) { - if (isExpression(node)) { - node = node.parent; - } - else { - return node.kind === SyntaxKind.Decorator; - } - } - return false; + return node.kind === SyntaxKind.Decorator; } static IsStartOfVariableDeclarationList(context: FormattingContext): boolean { diff --git a/src/services/services.ts b/src/services/services.ts index 042e2f4f01e..86c323f24bf 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -19,7 +19,7 @@ module ts { getChildCount(sourceFile?: SourceFile): number; getChildAt(index: number, sourceFile?: SourceFile): Node; getChildren(sourceFile?: SourceFile): Node[]; - getStart(sourceFile?: SourceFile, skipDecorators?: boolean): number; + getStart(sourceFile?: SourceFile): number; getFullStart(): number; getEnd(): number; getWidth(sourceFile?: SourceFile): number; @@ -149,8 +149,8 @@ module ts { return getSourceFileOfNode(this); } - public getStart(sourceFile?: SourceFile, skipDecorators?: boolean): number { - return getTokenPosOfNode(this, sourceFile, skipDecorators); + public getStart(sourceFile?: SourceFile): number { + return getTokenPosOfNode(this, sourceFile); } public getFullStart(): number { diff --git a/tests/baselines/reference/APISample_linter.types b/tests/baselines/reference/APISample_linter.types index 5db938d63bd..609f08f3a76 100644 --- a/tests/baselines/reference/APISample_linter.types +++ b/tests/baselines/reference/APISample_linter.types @@ -237,9 +237,9 @@ export function delint(sourceFile: ts.SourceFile) { >sourceFile : ts.SourceFile >getLineAndCharacterOfPosition : (pos: number) => ts.LineAndCharacter >node.getStart() : number ->node.getStart : (sourceFile?: ts.SourceFile, skipDecorators?: boolean) => number +>node.getStart : (sourceFile?: ts.SourceFile) => number >node : ts.Node ->getStart : (sourceFile?: ts.SourceFile, skipDecorators?: boolean) => number +>getStart : (sourceFile?: ts.SourceFile) => number console.log(`${sourceFile.fileName} (${line + 1},${character + 1}): ${message}`); >console.log(`${sourceFile.fileName} (${line + 1},${character + 1}): ${message}`) : any diff --git a/tests/cases/fourslash/formattingDecorators.ts b/tests/cases/fourslash/formattingDecorators.ts index 9fc63d9d47b..366b9cb7729 100644 --- a/tests/cases/fourslash/formattingDecorators.ts +++ b/tests/cases/fourslash/formattingDecorators.ts @@ -1,11 +1,11 @@ /// -/////*1*/ @ decorator1 +/////*1*/ @ decorator1 /////*2*/ @ decorator2 /////*3*/ @decorator3 /////*4*/ @ decorator4 @ decorator5 /////*5*/class C { -/////*6*/ @ decorator6 +/////*6*/ @ decorator6 /////*7*/ @ decorator7 /////*8*/ @decorator8 /////*9*/ method1() { } @@ -13,7 +13,7 @@ /////*10*/ @ decorator9 @ decorator10 @decorator11 method2() { } //// //// method3( -/////*11*/ @ decorator12 +/////*11*/ @ decorator12 /////*12*/ @ decorator13 /////*13*/ @decorator14 /////*14*/ x) { } @@ -21,21 +21,21 @@ //// method4( /////*15*/ @ decorator15 @ decorator16 @decorator17 x) { } //// -/////*16*/ @ decorator18 +/////*16*/ @ decorator18 /////*17*/ @ decorator19 -/////*18*/ @decorator20 +/////*18*/ @decorator20 /////*19*/ ["computed1"]() { } //// /////*20*/ @ decorator21 @ decorator22 @decorator23 ["computed2"]() { } //// -/////*21*/ @ decorator24 +/////*21*/ @ decorator24 /////*22*/ @ decorator25 /////*23*/ @decorator26 /////*24*/ get accessor1() { } //// /////*25*/ @ decorator27 @ decorator28 @decorator29 get accessor2() { } //// -/////*26*/ @ decorator30 +/////*26*/ @ decorator30 /////*27*/ @ decorator31 /////*28*/ @decorator32 /////*29*/ property1;