mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-21 13:14:43 -06:00
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
This commit is contained in:
parent
fe1ba9bee3
commit
b55b6e2f6b
@ -642,8 +642,18 @@ 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) {
|
||||
if (collecting) {
|
||||
return accumulator;
|
||||
}
|
||||
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) {
|
||||
|
||||
@ -45,6 +45,7 @@
|
||||
"unittests/builder.ts",
|
||||
"unittests/cancellableLanguageServiceOperations.ts",
|
||||
"unittests/commandLineParsing.ts",
|
||||
"unittests/comments.ts",
|
||||
"unittests/compileOnSave.ts",
|
||||
"unittests/compilerCore.ts",
|
||||
"unittests/configurationExtension.ts",
|
||||
|
||||
49
src/testRunner/unittests/comments.ts
Normal file
49
src/testRunner/unittests/comments.ts
Normal file
@ -0,0 +1,49 @@
|
||||
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);
|
||||
});
|
||||
|
||||
it("returns no trailing comments at start of file", () => {
|
||||
const result = getTrailingCommentRanges(noShebang, 0);
|
||||
assert.isUndefined(result);
|
||||
});
|
||||
|
||||
it("returns no trailing comments at start of file if shebang is present", () => {
|
||||
const result = getTrailingCommentRanges(withShebang, 0);
|
||||
assert.isUndefined(result);
|
||||
});
|
||||
|
||||
it("returns trailing comments if position is not 0", () => {
|
||||
const result = getTrailingCommentRanges(withTrailing, 1);
|
||||
assert.isDefined(result);
|
||||
assert.strictEqual(result!.length, 1);
|
||||
assert.strictEqual(result![0].kind, SyntaxKind.MultiLineCommentTrivia);
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -40,8 +40,7 @@ var temp = 10;
|
||||
|
||||
//// [exponentiationOperatorSyntaxError1.js]
|
||||
// Error: early syntax error using ES7 SimpleUnaryExpression on left-hand side without ()
|
||||
Math.pow(// Error: early syntax error using ES7 SimpleUnaryExpression on left-hand side without ()
|
||||
-1, 2);
|
||||
Math.pow(-1, 2);
|
||||
Math.pow(+1, 2);
|
||||
Math.pow(1, Math.pow(-2, 3));
|
||||
Math.pow(1, Math.pow(-2, -3));
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user