From bb8c803bdff17a7045daeb4c7b1f7b7b0df35e41 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Mon, 7 Nov 2022 21:41:07 -0800 Subject: [PATCH] Fix newline normalization credit: @Validark --- src/js_lexer.zig | 11 ++++++----- test/bun.js/transpiler.test.js | 7 +++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/js_lexer.zig b/src/js_lexer.zig index da186eaf4f..1344974212 100644 --- a/src/js_lexer.zig +++ b/src/js_lexer.zig @@ -280,7 +280,8 @@ fn NewLexer_( // include a or sequence. // Convert '\r\n' into '\n' - iter.i += @as(u32, @boolToInt(iter.i < text.len and text[iter.i] == '\n')); + const next_i: usize = iter.i + 1; + iter.i += @as(u32, @boolToInt(next_i < text.len and text[next_i] == '\n')); // Convert '\r' into '\n' buf.append('\n') catch unreachable; @@ -546,11 +547,11 @@ fn NewLexer_( try lexer.syntaxError(); } + // Make sure Windows CRLF counts as a single newline + const next_i: usize = iter.i + 1; + iter.i += @as(u32, @boolToInt(next_i < text.len and text[next_i] == '\n')); + // Ignore line continuations. A line continuation is not an escaped newline. - if (iter.i < text.len and text[iter.i + 1] == '\n') { - // Make sure Windows CRLF counts as a single newline - iter.i += 1; - } continue; }, '\n', 0x2028, 0x2029 => { diff --git a/test/bun.js/transpiler.test.js b/test/bun.js/transpiler.test.js index f8bbd9d447..d34b433304 100644 --- a/test/bun.js/transpiler.test.js +++ b/test/bun.js/transpiler.test.js @@ -65,6 +65,13 @@ describe("Bun.Transpiler", () => { }, }; + it("normalizes \\r\\n", () => { + ts.expectPrinted_( + "console.log(`\r\n\r\n\r\n`)", + "console.log(`\n\n\n`);\n" + ); + }); + describe("TypeScript", () => { it("import Foo = Baz.Bar", () => { ts.expectPrinted_(