Skip to content

Commit b55b6e2

Browse files
committed
fix comment parsing at start of file
* skip shebang if present (fixes: #28477) * don't parse trailing comments as they are also treated like leading comments
1 parent fe1ba9b commit b55b6e2

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

src/compiler/scanner.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,18 @@ namespace ts {
642642
let pendingKind!: CommentKind;
643643
let pendingHasTrailingNewLine!: boolean;
644644
let hasPendingCommentRange = false;
645-
let collecting = trailing || pos === 0;
645+
let collecting = trailing;
646646
let accumulator = initial;
647+
if (pos === 0) {
648+
if (collecting) {
649+
return accumulator;
650+
}
651+
collecting = true;
652+
const shebang = getShebang(text);
653+
if (shebang) {
654+
pos = shebang.length;
655+
}
656+
}
647657
scan: while (pos >= 0 && pos < text.length) {
648658
const ch = text.charCodeAt(pos);
649659
switch (ch) {

src/testRunner/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"unittests/builder.ts",
4646
"unittests/cancellableLanguageServiceOperations.ts",
4747
"unittests/commandLineParsing.ts",
48+
"unittests/comments.ts",
4849
"unittests/compileOnSave.ts",
4950
"unittests/compilerCore.ts",
5051
"unittests/configurationExtension.ts",

src/testRunner/unittests/comments.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace ts {
2+
describe("comment parsing", () => {
3+
const withShebang = `#! node
4+
/** comment */
5+
// another one
6+
;`;
7+
const noShebang = `/** comment */
8+
// another one
9+
;`;
10+
const withTrailing = `;/* comment */
11+
// another one
12+
`
13+
it("skips shebang", () => {
14+
const result = getLeadingCommentRanges(withShebang, 0);
15+
assert.isDefined(result);
16+
assert.strictEqual(result!.length, 2);
17+
});
18+
19+
it("treats all comments at start of file as leading comments", () => {
20+
const result = getLeadingCommentRanges(noShebang, 0);
21+
assert.isDefined(result);
22+
assert.strictEqual(result!.length, 2);
23+
});
24+
25+
it("returns leading comments if position is not 0", () => {
26+
const result = getLeadingCommentRanges(withTrailing, 1);
27+
assert.isDefined(result);
28+
assert.strictEqual(result!.length, 1);
29+
assert.strictEqual(result![0].kind, SyntaxKind.SingleLineCommentTrivia);
30+
});
31+
32+
it("returns no trailing comments at start of file", () => {
33+
const result = getTrailingCommentRanges(noShebang, 0);
34+
assert.isUndefined(result);
35+
});
36+
37+
it("returns no trailing comments at start of file if shebang is present", () => {
38+
const result = getTrailingCommentRanges(withShebang, 0);
39+
assert.isUndefined(result);
40+
});
41+
42+
it("returns trailing comments if position is not 0", () => {
43+
const result = getTrailingCommentRanges(withTrailing, 1);
44+
assert.isDefined(result);
45+
assert.strictEqual(result!.length, 1);
46+
assert.strictEqual(result![0].kind, SyntaxKind.MultiLineCommentTrivia);
47+
});
48+
});
49+
}

tests/baselines/reference/exponentiationOperatorSyntaxError1.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ var temp = 10;
4040

4141
//// [exponentiationOperatorSyntaxError1.js]
4242
// Error: early syntax error using ES7 SimpleUnaryExpression on left-hand side without ()
43-
Math.pow(// Error: early syntax error using ES7 SimpleUnaryExpression on left-hand side without ()
44-
-1, 2);
43+
Math.pow(-1, 2);
4544
Math.pow(+1, 2);
4645
Math.pow(1, Math.pow(-2, 3));
4746
Math.pow(1, Math.pow(-2, -3));

tests/baselines/reference/shebangBeforeReferences.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use(x);
1717
//// [f.js]
1818
#!/usr/bin/env node
1919
"use strict";
20+
/// <reference path="f.d.ts"/>
2021
exports.__esModule = true;
2122
var test_1 = require("test");
2223
use(test_1.x);

0 commit comments

Comments
 (0)