(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
This commit is contained in:
Joseph Frazier authored and Christopher Chedeau committed 2017-05-12 07:32:27 -07:00
1 parent 36a6a8039e
commit 5ca2117d23
5 files changed
+35 -2

No files matched your search

+15 -2
View File
@@ -60,7 +60,7 @@ function parseWithBabylon(text) {
// Inline the require to avoid loading all the JS if we don't use it // Inline the require to avoid loading all the JS if we don't use it
const babylon = require("babylon"); const babylon = require("babylon");
return babylon.parse(text, { const babylonOptions = {
sourceType: "module", sourceType: "module",
allowImportExportEverywhere: false, allowImportExportEverywhere: false,
allowReturnOutsideFunction: false, allowReturnOutsideFunction: false,
@@ -77,7 +77,20 @@ function parseWithBabylon(text) {
"functionSent", "functionSent",
"dynamicImport" "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) { function parseWithTypeScript(text) {
@@ -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;
`;
+1
View File
@@ -0,0 +1 @@
function f(a,a){return a}
+1
View File
@@ -0,0 +1 @@
run_spec(__dirname, { parser: "babylon" });
+1
View File
@@ -0,0 +1 @@
0777