Merge pull request #28489 from ajafff/shebang-comments

fix comment parsing at start of file
This commit is contained in:
Ron Buckton
2019-01-29 17:45:13 -08:00
committed by GitHub
4 changed files with 42 additions and 1 deletions

View File

@@ -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) {

View File

@@ -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",

View File

@@ -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);
});
});
}

View File

@@ -17,6 +17,7 @@ use(x);
//// [f.js]
#!/usr/bin/env node
"use strict";
/// <reference path="f.d.ts"/>
exports.__esModule = true;
var test_1 = require("test");
use(test_1.x);