From 1183129bda17ff969334b1f1a055d463f8028535 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 20 Jan 2017 10:17:11 -0800 Subject: [PATCH] getFirstToken returns jsdoc as single comment This is a bit odd, but it's the way that 2.0 and earlier behaved. 2.1 broke it. --- src/compiler/types.ts | 2 +- src/harness/unittests/jsDocParsing.ts | 14 ++++++++++++++ src/services/services.ts | 9 ++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 51347c8d3df5b..f0e20ecd4bf10 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -421,7 +421,7 @@ LastBinaryOperator = CaretEqualsToken, FirstNode = QualifiedName, FirstJSDocNode = JSDocTypeExpression, - LastJSDocNode = JSDocLiteralType, + LastJSDocNode = JSDocNeverKeyword, FirstJSDocTagNode = JSDocComment, LastJSDocTagNode = JSDocNeverKeyword } diff --git a/src/harness/unittests/jsDocParsing.ts b/src/harness/unittests/jsDocParsing.ts index addd01b0e0a08..3cafc9d49aebb 100644 --- a/src/harness/unittests/jsDocParsing.ts +++ b/src/harness/unittests/jsDocParsing.ts @@ -288,5 +288,19 @@ namespace ts { */`); }); }); + describe("getFirstToken", () => { + it("gets jsdoc", () => { + const first = ts.createSourceFile("foo.ts", "/** comment */var a = true;", ts.ScriptTarget.ES5, /*setParentNodes*/ true); + assert.isDefined(first); + assert.equal(first.kind, 263); + }); + }); + describe("getLastToken", () => { + it("gets jsdoc", () => { + const last = ts.createSourceFile("foo.ts", "var a = true;/** comment */", ts.ScriptTarget.ES5, /*setParentNodes*/ true); + assert.isDefined(last); + assert.equal(last.kind, 263); + }); + }); }); } diff --git a/src/services/services.ts b/src/services/services.ts index 26374eccbfddb..cdd789166bcbc 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -194,8 +194,9 @@ namespace ts { } const child = children[0]; - - return child.kind < SyntaxKind.FirstNode ? child : child.getFirstToken(sourceFile); + return child.kind < SyntaxKind.FirstNode || SyntaxKind.FirstJSDocNode <= child.kind && child.kind <= SyntaxKind.LastJSDocNode ? + child : + child.getFirstToken(sourceFile); } public getLastToken(sourceFile?: SourceFile): Node { @@ -206,7 +207,9 @@ namespace ts { return undefined; } - return child.kind < SyntaxKind.FirstNode ? child : child.getLastToken(sourceFile); + return child.kind < SyntaxKind.FirstNode || SyntaxKind.FirstJSDocNode <= child.kind && child.kind <= SyntaxKind.LastJSDocNode ? + child : + child.getLastToken(sourceFile); } }