diff --git a/src/util.js b/src/util.js index 29f28f77..ecf786f1 100644 --- a/src/util.js +++ b/src/util.js @@ -166,20 +166,23 @@ function skipNewline(text, index, opts) { const backwards = opts && opts.backwards; if (index === false) { return false; - } else if (backwards) { - if (text.charAt(index) === "\n") { + } + + const atIndex = text.charAt(index); + if (backwards) { + if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") { return index - 1; } - if (text.charAt(index - 1) === "\r" && text.charAt(index) === "\n") { + if (text.charAt(index - 1) === "\r" && atIndex === "\n") { return index - 2; } } else { - if (text.charAt(index) === "\n") { - return index + 1; - } - if (text.charAt(index) === "\r" && text.charAt(index + 1) === "\n") { + if (atIndex === "\r" && text.charAt(index + 1) === "\n") { return index + 2; } + if (atIndex === "\n" || atIndex === "\r" || atIndex === "\u2028" || atIndex === "\u2029") { + return index + 1; + } } return index; diff --git a/tests/newline/__snapshots__/jsfmt.spec.js.snap b/tests/newline/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..be2ad3d0 --- /dev/null +++ b/tests/newline/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`backslash_2028.js 1`] = ` +"1;/*a*///b
/*c*/2 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +1; /*a*/ //b
/*c*/2 +" +`; + +exports[`backslash_2029.js 1`] = ` +"1;/*a*///b
/*c*/2 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +1; /*a*/ //b
/*c*/2 +" +`; diff --git a/tests/newline/backslash_2028.js b/tests/newline/backslash_2028.js new file mode 100644 index 00000000..93728af0 --- /dev/null +++ b/tests/newline/backslash_2028.js @@ -0,0 +1 @@ +1;/*a*///b
/*c*/2 diff --git a/tests/newline/backslash_2029.js b/tests/newline/backslash_2029.js new file mode 100644 index 00000000..5c8c07b8 --- /dev/null +++ b/tests/newline/backslash_2029.js @@ -0,0 +1 @@ +1;/*a*///b
/*c*/2 diff --git a/tests/newline/jsfmt.spec.js b/tests/newline/jsfmt.spec.js new file mode 100644 index 00000000..989047bc --- /dev/null +++ b/tests/newline/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname);