From f048ad309405959e276cca898a282e55f3b433ba Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Sun, 14 May 2017 17:45:33 -0400 Subject: [PATCH] (Babylon) Allow `return` outside function (#1608) This makes it easier to format code snippets including a `return` statement, without having to format the entire function. For example, given the following code: ```js function f() { return someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD } ``` a developer could select the line containing `return`, then use prettier to format the code to: ```js function f() { return ( someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD ); } ``` which can then be reindented by the editor. --- Related to https://github.com/prettier/prettier/issues/593 --- src/parser.js | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 13 +++++++++++++ tests/return-outside-function/jsfmt.spec.js | 1 + .../return-outside-function.js | 1 + 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/return-outside-function/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/return-outside-function/jsfmt.spec.js create mode 100644 tests/return-outside-function/return-outside-function.js diff --git a/src/parser.js b/src/parser.js index 889093dc1..9de488dd1 100644 --- a/src/parser.js +++ b/src/parser.js @@ -63,7 +63,7 @@ function parseWithBabylon(text) { const babylonOptions = { sourceType: "module", allowImportExportEverywhere: false, - allowReturnOutsideFunction: false, + allowReturnOutsideFunction: true, plugins: [ "jsx", "flow", diff --git a/tests/return-outside-function/__snapshots__/jsfmt.spec.js.snap b/tests/return-outside-function/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..ae4e8beb1 --- /dev/null +++ b/tests/return-outside-function/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,13 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`return-outside-function.js 1`] = ` +return someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +return ( + someVeryLongStringA && + someVeryLongStringB && + someVeryLongStringC && + someVeryLongStringD +); + +`; diff --git a/tests/return-outside-function/jsfmt.spec.js b/tests/return-outside-function/jsfmt.spec.js new file mode 100644 index 000000000..6a6711aef --- /dev/null +++ b/tests/return-outside-function/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "babylon" }, ["typescript"]); diff --git a/tests/return-outside-function/return-outside-function.js b/tests/return-outside-function/return-outside-function.js new file mode 100644 index 000000000..29e96c8a5 --- /dev/null +++ b/tests/return-outside-function/return-outside-function.js @@ -0,0 +1 @@ +return someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD