From 5ca2117d233bd67bf1f805a967310d86117e2550 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Fri, 12 May 2017 10:32:27 -0400 Subject: [PATCH] (Babylon) Fall back to non-strict mode (#1587) * (Babylon) Fall back to non-strict mode This makes Prettier a little less opinionated about linting. For example, the following can now be formatted: ```js function f(a,a){return a} ``` whereas before it would cause an error: stdin: SyntaxError: Argument name clash in strict mode (1:13) > 1 | function f(a,a){return a} | ^ This also allows octal numbers to be parsed, and therefore fixes https://github.com/prettier/prettier/issues/228 If the code parses neither as strict nor as non-strict, the error from the strict parse is thrown (as it was before this change). --- I noticed this while trying out [eslump] with prettier: eslump | pbcopy; pbpaste | prettier [eslump]: https://github.com/lydell/eslump * Add missing test * Use Object.assign() instead of mutating object --- src/parser.js | 17 +++++++++++++++-- .../non-strict/__snapshots__/jsfmt.spec.js.snap | 17 +++++++++++++++++ tests/non-strict/argument-name-clash.js | 1 + tests/non-strict/jsfmt.spec.js | 1 + tests/non-strict/octal-number.js | 1 + 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/non-strict/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/non-strict/argument-name-clash.js create mode 100644 tests/non-strict/jsfmt.spec.js create mode 100644 tests/non-strict/octal-number.js diff --git a/src/parser.js b/src/parser.js index 90235454a..889093dc1 100644 --- a/src/parser.js +++ b/src/parser.js @@ -60,7 +60,7 @@ function parseWithBabylon(text) { // Inline the require to avoid loading all the JS if we don't use it const babylon = require("babylon"); - return babylon.parse(text, { + const babylonOptions = { sourceType: "module", allowImportExportEverywhere: false, allowReturnOutsideFunction: false, @@ -77,7 +77,20 @@ function parseWithBabylon(text) { "functionSent", "dynamicImport" ] - }); + }; + + try { + return babylon.parse(text, babylonOptions); + } catch (originalError) { + try { + return babylon.parse( + text, + Object.assign({}, babylonOptions, { strictMode: false }) + ); + } catch (nonStrictError) { + throw originalError; + } + } } function parseWithTypeScript(text) { diff --git a/tests/non-strict/__snapshots__/jsfmt.spec.js.snap b/tests/non-strict/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..1addc8d77 --- /dev/null +++ b/tests/non-strict/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`argument-name-clash.js 1`] = ` +function f(a,a){return a} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +function f(a, a) { + return a; +} + +`; + +exports[`octal-number.js 1`] = ` +0777 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +0777; + +`; diff --git a/tests/non-strict/argument-name-clash.js b/tests/non-strict/argument-name-clash.js new file mode 100644 index 000000000..4b7aee9c2 --- /dev/null +++ b/tests/non-strict/argument-name-clash.js @@ -0,0 +1 @@ +function f(a,a){return a} diff --git a/tests/non-strict/jsfmt.spec.js b/tests/non-strict/jsfmt.spec.js new file mode 100644 index 000000000..f974ed6c7 --- /dev/null +++ b/tests/non-strict/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "babylon" }); diff --git a/tests/non-strict/octal-number.js b/tests/non-strict/octal-number.js new file mode 100644 index 000000000..18a912ec9 --- /dev/null +++ b/tests/non-strict/octal-number.js @@ -0,0 +1 @@ +0777