From 22e300471989322b913158bbdce118a87a82a0c7 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 2 Apr 2020 05:43:52 +0800 Subject: [PATCH] Fix empty file format (#7921) --- src/language-js/printer-estree.js | 5 +++- .../js_empty/__snapshots__/jsfmt.spec.js.snap | 28 ++++++++++++++++++- tests/js_empty/empty.js | 1 + tests/js_empty/many-semicolons.js | 2 ++ tests_config/run_spec.js | 1 - 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/js_empty/empty.js create mode 100644 tests/js_empty/many-semicolons.js diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 2f2b6064adfe..15352c0be8b4 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -494,7 +494,10 @@ function printPathNoParens(path, options, print, args) { ); // Only force a trailing newline if there were any contents. - if (n.body.length || n.comments) { + if ( + !n.body.every(({ type }) => type === "EmptyStatement") || + n.comments + ) { parts.push(hardline); } diff --git a/tests/js_empty/__snapshots__/jsfmt.spec.js.snap b/tests/js_empty/__snapshots__/jsfmt.spec.js.snap index 9c42bd2a580b..4c04af20d385 100644 --- a/tests/js_empty/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js_empty/__snapshots__/jsfmt.spec.js.snap @@ -1,15 +1,41 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`semicolon.js 1`] = ` +exports[`empty.js 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + +================================================================================ +`; + +exports[`many-semicolons.js 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] printWidth: 80 | printWidth =====================================input====================================== +;; ; =====================================output===================================== +================================================================================ +`; + +exports[`semicolon.js 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +; + +=====================================output===================================== ================================================================================ `; diff --git a/tests/js_empty/empty.js b/tests/js_empty/empty.js new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/tests/js_empty/empty.js @@ -0,0 +1 @@ + diff --git a/tests/js_empty/many-semicolons.js b/tests/js_empty/many-semicolons.js new file mode 100644 index 000000000000..6b6add353868 --- /dev/null +++ b/tests/js_empty/many-semicolons.js @@ -0,0 +1,2 @@ +;; +; diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index 4c9767ee7e7b..296f26fe0e36 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -34,7 +34,6 @@ const unstableTests = new Map( "html_angular/attributes.component.html", (options) => options.printWidth === 1, ], - "js_empty/semicolon.js", "markdown_footnoteDefinition/multiline.md", "markdown_spec/example-234.md", "markdown_spec/example-235.md",