Skip to content

Commit 9ac7c32

Browse files
authored
Merge pull request #13599 from Microsoft/getFirstToken-returns-jsdoc
getFirstToken returns jsdoc as single comment
2 parents 82d8bef + 1183129 commit 9ac7c32

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@
421421
LastBinaryOperator = CaretEqualsToken,
422422
FirstNode = QualifiedName,
423423
FirstJSDocNode = JSDocTypeExpression,
424-
LastJSDocNode = JSDocLiteralType,
424+
LastJSDocNode = JSDocNeverKeyword,
425425
FirstJSDocTagNode = JSDocComment,
426426
LastJSDocTagNode = JSDocNeverKeyword
427427
}

src/harness/unittests/jsDocParsing.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,5 +288,19 @@ namespace ts {
288288
*/`);
289289
});
290290
});
291+
describe("getFirstToken", () => {
292+
it("gets jsdoc", () => {
293+
const first = ts.createSourceFile("foo.ts", "/** comment */var a = true;", ts.ScriptTarget.ES5, /*setParentNodes*/ true);
294+
assert.isDefined(first);
295+
assert.equal(first.kind, 263);
296+
});
297+
});
298+
describe("getLastToken", () => {
299+
it("gets jsdoc", () => {
300+
const last = ts.createSourceFile("foo.ts", "var a = true;/** comment */", ts.ScriptTarget.ES5, /*setParentNodes*/ true);
301+
assert.isDefined(last);
302+
assert.equal(last.kind, 263);
303+
});
304+
});
291305
});
292306
}

src/services/services.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ namespace ts {
194194
}
195195

196196
const child = children[0];
197-
198-
return child.kind < SyntaxKind.FirstNode ? child : child.getFirstToken(sourceFile);
197+
return child.kind < SyntaxKind.FirstNode || SyntaxKind.FirstJSDocNode <= child.kind && child.kind <= SyntaxKind.LastJSDocNode ?
198+
child :
199+
child.getFirstToken(sourceFile);
199200
}
200201

201202
public getLastToken(sourceFile?: SourceFile): Node {
@@ -206,7 +207,9 @@ namespace ts {
206207
return undefined;
207208
}
208209

209-
return child.kind < SyntaxKind.FirstNode ? child : child.getLastToken(sourceFile);
210+
return child.kind < SyntaxKind.FirstNode || SyntaxKind.FirstJSDocNode <= child.kind && child.kind <= SyntaxKind.LastJSDocNode ?
211+
child :
212+
child.getLastToken(sourceFile);
210213
}
211214
}
212215

0 commit comments

Comments
 (0)