diff --git a/src/doc-printer.js b/src/doc-printer.js index ac6747ec..d78101d4 100644 --- a/src/doc-printer.js +++ b/src/doc-printer.js @@ -284,10 +284,12 @@ function printDocToString(doc, options) { out.pop(); } - out[out.length - 1] = out[out.length - 1].replace( - /[^\S\n]*$/, - "" - ); + if (out.length) { + out[out.length - 1] = out[out.length - 1].replace( + /[^\S\n]*$/, + "" + ); + } } let length = ind.indent * options.tabWidth + ind.align.spaces; diff --git a/tests/empty/__snapshots__/jsfmt.spec.js.snap b/tests/empty/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..874fa35e --- /dev/null +++ b/tests/empty/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`empty.js 1`] = ` +; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + +`; diff --git a/tests/empty/empty.js b/tests/empty/empty.js new file mode 100644 index 00000000..092bc2b0 --- /dev/null +++ b/tests/empty/empty.js @@ -0,0 +1 @@ +; diff --git a/tests/empty/jsfmt.spec.js b/tests/empty/jsfmt.spec.js new file mode 100644 index 00000000..989047bc --- /dev/null +++ b/tests/empty/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname);