diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 4f3eba37296d5..d0ecc1298567e 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -661,8 +661,15 @@ namespace ts { let pendingKind!: CommentKind; let pendingHasTrailingNewLine!: boolean; let hasPendingCommentRange = false; - let collecting = trailing || pos === 0; + let collecting = trailing; let accumulator = initial; + if (pos === 0) { + collecting = true; + const shebang = getShebang(text); + if (shebang) { + pos = shebang.length; + } + } scan: while (pos >= 0 && pos < text.length) { const ch = text.charCodeAt(pos); switch (ch) { diff --git a/src/testRunner/tsconfig.json b/src/testRunner/tsconfig.json index 3b3a477511ed1..849906e24cf7b 100644 --- a/src/testRunner/tsconfig.json +++ b/src/testRunner/tsconfig.json @@ -43,6 +43,7 @@ "unittests/asserts.ts", "unittests/base64.ts", "unittests/builder.ts", + "unittests/comments.ts", "unittests/compilerCore.ts", "unittests/convertToBase64.ts", "unittests/customTransforms.ts", diff --git a/src/testRunner/unittests/comments.ts b/src/testRunner/unittests/comments.ts new file mode 100644 index 0000000000000..59f3020c112bc --- /dev/null +++ b/src/testRunner/unittests/comments.ts @@ -0,0 +1,32 @@ +namespace ts { + describe("comment parsing", () => { + const withShebang = `#! node +/** comment */ +// another one +;`; + const noShebang = `/** comment */ +// another one +;`; + const withTrailing = `;/* comment */ +// another one +`; + it("skips shebang", () => { + const result = getLeadingCommentRanges(withShebang, 0); + assert.isDefined(result); + assert.strictEqual(result!.length, 2); + }); + + it("treats all comments at start of file as leading comments", () => { + const result = getLeadingCommentRanges(noShebang, 0); + assert.isDefined(result); + assert.strictEqual(result!.length, 2); + }); + + it("returns leading comments if position is not 0", () => { + const result = getLeadingCommentRanges(withTrailing, 1); + assert.isDefined(result); + assert.strictEqual(result!.length, 1); + assert.strictEqual(result![0].kind, SyntaxKind.SingleLineCommentTrivia); + }); + }); +} diff --git a/tests/baselines/reference/shebangBeforeReferences.js b/tests/baselines/reference/shebangBeforeReferences.js index 4ebf692e1d513..30e4b7a8633c1 100644 --- a/tests/baselines/reference/shebangBeforeReferences.js +++ b/tests/baselines/reference/shebangBeforeReferences.js @@ -17,6 +17,7 @@ use(x); //// [f.js] #!/usr/bin/env node "use strict"; +/// exports.__esModule = true; var test_1 = require("test"); use(test_1.x);