From b55b6e2f6b69798ace74b28122931ac302f41870 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Mon, 12 Nov 2018 21:45:36 +0100 Subject: [PATCH] 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 --- src/compiler/scanner.ts | 12 ++++- src/testRunner/tsconfig.json | 1 + src/testRunner/unittests/comments.ts | 49 +++++++++++++++++++ .../exponentiationOperatorSyntaxError1.js | 3 +- .../reference/shebangBeforeReferences.js | 1 + 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/testRunner/unittests/comments.ts diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index b8d4d248077..65e9d359836 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -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) { diff --git a/src/testRunner/tsconfig.json b/src/testRunner/tsconfig.json index 32772ed311b..7f164df95c4 100644 --- a/src/testRunner/tsconfig.json +++ b/src/testRunner/tsconfig.json @@ -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", diff --git a/src/testRunner/unittests/comments.ts b/src/testRunner/unittests/comments.ts new file mode 100644 index 00000000000..60ae4afd312 --- /dev/null +++ b/src/testRunner/unittests/comments.ts @@ -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); + }); + }); +} diff --git a/tests/baselines/reference/exponentiationOperatorSyntaxError1.js b/tests/baselines/reference/exponentiationOperatorSyntaxError1.js index 667abed731c..bddd4880ee2 100644 --- a/tests/baselines/reference/exponentiationOperatorSyntaxError1.js +++ b/tests/baselines/reference/exponentiationOperatorSyntaxError1.js @@ -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)); diff --git a/tests/baselines/reference/shebangBeforeReferences.js b/tests/baselines/reference/shebangBeforeReferences.js index 4ebf692e1d5..30e4b7a8633 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);