Skip to content

Commit

Permalink
Update @babel/parser to 7.12 (#9408)
Browse files Browse the repository at this point in the history
* Install babel/parser 7.12

* Add tests for babel/babel#12161

* Add test for babel/babel#12076

* Add test for babel/babel#12085

* Add test for babel/babel#12108

* Add test for babel/babel#12120

* Add test for babel/babel#12054

* Add test for babel/babel#12061

* Add test babel/babel#12093

* Add test for babel/babel#12065

* Add test for babel/babel#12111

* Add test for babel/babel#12072

* Switch syntax-module-attributes to syntax-import-assertion

* Support "String import/export specifier"

* Remove tests for module-attributes

* Add changelog

* Update to 7.12.3

* Fix by linter

* Fix by spellchecker

* Add tests for module attributes to errors

* Add error test for module string name with import

* Remove TSTypeCastExpression

* Add tests for funny import-assertions

* Update snapshots|

* Add more tests
  • Loading branch information
sosukesuzuki committed Oct 20, 2020
1 parent bdcec2f commit 3b7a7ba
Show file tree
Hide file tree
Showing 53 changed files with 432 additions and 74 deletions.
22 changes: 22 additions & 0 deletions changelog_unreleased/javascript/pr-9408.md
@@ -0,0 +1,22 @@
#### Update to `@babel/parser` 7.12 (#9408 by @sosukesuzuki)

Updated the JavaScript parser to [`@babel/parser` 7.12](https://babeljs.io/blog/2020/10/15/7.12.0). This fixes several bugs and supports some new syntax.

##### Support Import Assertions

[The "module attributes" proposal supported on 2.1](https://prettier.io/blog/2020/08/24/2.1.0.html#support-es-module-attributes-and-json-modules-8436httpsgithubcomprettierprettierpull8436-by-fiskerhttpsgithubcomfisker) has been significantly changed and also renamed to "import assertions".

```js
import json from "./foo.json" assert { type: "json" };
```

##### Support imports and exports with string names

<!-- TODO: Remove if the bug is fixed -->

Due to a [bug in `@babel/parser`](https://github.com/babel/babel/issues/12209), Prettier can only use exports, but that will be fixed.

```js
let happy = "happy";
export { happy as "😃" };
```
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"dependencies": {
"@angular/compiler": "10.1.6",
"@babel/code-frame": "7.10.4",
"@babel/parser": "7.11.5",
"@babel/parser": "7.12.3",
"@glimmer/syntax": "0.62.3",
"@iarna/toml": "2.2.5",
"@typescript-eslint/typescript-estree": "3.10.1",
Expand Down
3 changes: 2 additions & 1 deletion src/language-js/parser-babel.js
Expand Up @@ -35,9 +35,10 @@ function babelOptions({ sourceType, extraPlugins = [] }) {
"partialApplication",
["decorators", { decoratorsBeforeExport: false }],
"privateIn",
["moduleAttributes", { version: "may-2020" }],
"importAssertions",
["recordAndTuple", { syntaxType: "hash" }],
"decimal",
"moduleStringNames",
...extraPlugins,
],
tokens: true,
Expand Down
15 changes: 11 additions & 4 deletions src/language-js/printer-estree.js
Expand Up @@ -809,14 +809,15 @@ function printPathNoParens(path, options, print, args) {
}

return concat(parts);
case "ExportSpecifier":
case "ExportSpecifier": {
parts.push(path.call(print, "local"));

if (n.exported && !hasSameLoc(n.local, n.exported, options)) {
parts.push(" as ", path.call(print, "exported"));
}

return concat(parts);
}
case "ImportNamespaceSpecifier":
parts.push("* as ");
parts.push(path.call(print, "local"));
Expand Down Expand Up @@ -876,8 +877,12 @@ function printPathNoParens(path, options, print, args) {
parts.push(" ", path.call(print, "source"));
}

if (Array.isArray(n.attributes) && n.attributes.length !== 0) {
parts.push(" with ", concat(path.map(print, "attributes")));
if (Array.isArray(n.assertions) && n.assertions.length !== 0) {
parts.push(
" assert { ",
join(", ", path.map(print, "assertions")),
" }"
);
}

parts.push(semi);
Expand Down Expand Up @@ -914,7 +919,8 @@ function printPathNoParens(path, options, print, args) {
parent.type === "DoWhileStatement" ||
parent.type === "DoExpression" ||
(parent.type === "CatchClause" && !parentParent.finalizer) ||
parent.type === "TSModuleDeclaration")
parent.type === "TSModuleDeclaration" ||
parent.type === "TSDeclareFunction")
) {
return "{}";
}
Expand Down Expand Up @@ -3124,6 +3130,7 @@ function printPathNoParens(path, options, print, args) {
n.accessibility ? concat([n.accessibility, " "]) : "",
n.static ? "static " : "",
n.readonly ? "readonly " : "",
n.declare ? "declare " : "",
"[",
n.parameters ? parametersGroup : "",
n.typeAnnotation ? "]: " : "]",
Expand Down
48 changes: 31 additions & 17 deletions tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -683,6 +683,34 @@ iterator.next(2); // Logs "Yield 2"
================================================================================
`;
exports[`import-assertions-dynamic.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
import("./foo.json", { assert: { type: "json" } });
=====================================output=====================================
import("./foo.json", { assert: { type: "json" } });
================================================================================
`;
exports[`import-assertions-static.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
import json from "./foo.json" assert { type: "json" };
=====================================output=====================================
import json from "./foo.json" assert { type: "json" };
================================================================================
`;
exports[`import-meta.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
Expand Down Expand Up @@ -783,30 +811,16 @@ obj.a.b &&= c;
================================================================================
`;
exports[`module-attributes-dynamic.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
import("foo.json", { with: { type: "json" } })
=====================================output=====================================
import("foo.json", { with: { type: "json" } });
================================================================================
`;
exports[`module-attributes-static.js format 1`] = `
exports[`module-string-names.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
import foo from "foo.json" with type: "json";
export { smile as "😄" } from "./emojis.js";
=====================================output=====================================
import foo from "foo.json" with type: "json";
export { smile as "😄" } from "./emojis.js";
================================================================================
`;
Expand Down
1 change: 1 addition & 0 deletions tests/js/babel-plugins/import-assertions-dynamic.js
@@ -0,0 +1 @@
import("./foo.json", { assert: { type: "json" } });
1 change: 1 addition & 0 deletions tests/js/babel-plugins/import-assertions-static.js
@@ -0,0 +1 @@
import json from "./foo.json" assert { type: "json" };
5 changes: 3 additions & 2 deletions tests/js/babel-plugins/jsfmt.spec.js
Expand Up @@ -12,8 +12,8 @@ run_spec(__dirname, ["babel", "babel-ts", "babel-flow"], {
"flow.js",
"function-bind.js",
"function-sent.js",
"module-attributes-dynamic.js",
"module-attributes-static.js",
"import-assertions-dynamic.js",
"import-assertions-static.js",
"partial-application.js",
"pipeline-operator-fsharp.js",
"pipeline-operator-minimal.js",
Expand All @@ -26,6 +26,7 @@ run_spec(__dirname, ["babel", "babel-ts", "babel-flow"], {
"typescript.js",
"v8intrinsic.js",
"optional-chaining.js",
"module-string-names.js",
],
},
});
1 change: 0 additions & 1 deletion tests/js/babel-plugins/module-attributes-dynamic.js

This file was deleted.

1 change: 0 additions & 1 deletion tests/js/babel-plugins/module-attributes-static.js

This file was deleted.

1 change: 1 addition & 0 deletions tests/js/babel-plugins/module-string-names.js
@@ -0,0 +1 @@
export { smile as "😄" } from "./emojis.js";
59 changes: 59 additions & 0 deletions tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,59 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`import-assertions-dynamic.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
import("./foo.json", { assert: { type: "json" } });
=====================================output=====================================
import("./foo.json", { assert: { type: "json" } });
================================================================================
`;

exports[`import-assertions-multi-types.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
import json from "./foo.json" assert { type: "json", type: "bar" };
=====================================output=====================================
import json from "./foo.json" assert { type: "json", type: "bar" };
================================================================================
`;

exports[`import-assertions-static.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
import json from "./foo.json" assert { type: "json" };
=====================================output=====================================
import json from "./foo.json" assert { type: "json" };
================================================================================
`;

exports[`not-import-assertions.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
import "x"
assert ({type: 'json'});
=====================================output=====================================
import "x";
assert({ type: "json" });
================================================================================
`;
1 change: 1 addition & 0 deletions tests/js/import-assertions/import-assertions-dynamic.js
@@ -0,0 +1 @@
import("./foo.json", { assert: { type: "json" } });
@@ -0,0 +1 @@
import json from "./foo.json" assert { type: "json", type: "bar" };
1 change: 1 addition & 0 deletions tests/js/import-assertions/import-assertions-static.js
@@ -0,0 +1 @@
import json from "./foo.json" assert { type: "json" };
9 changes: 9 additions & 0 deletions tests/js/import-assertions/jsfmt.spec.js
@@ -0,0 +1,9 @@
run_spec(__dirname, ["babel"], {
errors: {
espree: [
"import-assertions-dynamic.js",
"import-assertions-multi-types.js",
"import-assertions-static.js",
],
},
});
2 changes: 2 additions & 0 deletions tests/js/import-assertions/not-import-assertions.js
@@ -0,0 +1,2 @@
import "x"
assert ({type: 'json'});
14 changes: 14 additions & 0 deletions tests/js/literal/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`invalid-exponent.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
12.3e
=====================================output=====================================
12.3e;
================================================================================
`;

exports[`number.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
Expand Down
1 change: 1 addition & 0 deletions tests/js/literal/invalid-exponent.js
@@ -0,0 +1 @@
12.3e
4 changes: 3 additions & 1 deletion tests/js/literal/jsfmt.spec.js
@@ -1,2 +1,4 @@
// flow-parser@0.38.0 fails to parse `1.e1`, so use babel here.
run_spec(__dirname, ["babel", "typescript"], { errors: { espree: true } });
run_spec(__dirname, ["babel", "typescript"], {
errors: { espree: true, typescript: ["invalid-exponent.js"] },
});
29 changes: 0 additions & 29 deletions tests/js/module-attributes/__snapshots__/jsfmt.spec.js.snap

This file was deleted.

1 change: 0 additions & 1 deletion tests/js/module-attributes/module-attributes-dynamic.js

This file was deleted.

1 change: 0 additions & 1 deletion tests/js/module-attributes/module-attributes-static.js

This file was deleted.

32 changes: 32 additions & 0 deletions tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`module-string-names.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
export { smile as "smile" } from "./emojis.js";
export { "smile" as smile } from "./emojis.js";
export { "smile" as "smile" } from "./emojis.js";
export { foo, bar as "foo" } from "./emojis.js";
export { foo, bar as "foo" };
export { "學而時習之,不亦說乎?", "吾道一以貫之。" as "忠恕。" } from "Confucius";
export * as "foo", { default as "quux" } from "module-b";
export { "smile" } from "./emojis.js";
=====================================output=====================================
export { smile as "smile" } from "./emojis.js";
export { "smile" as smile } from "./emojis.js";
export { "smile" as "smile" } from "./emojis.js";
export { foo, bar as "foo" } from "./emojis.js";
export { foo, bar as "foo" };
export {
"學而時習之,不亦說乎?",
"吾道一以貫之。" as "忠恕。",
} from "Confucius";
export * as "foo", { default as "quux" } from "module-b";
export { "smile" } from "./emojis.js";
================================================================================
`;
File renamed without changes.
8 changes: 8 additions & 0 deletions tests/js/module-string-names/module-string-names.js
@@ -0,0 +1,8 @@
export { smile as "smile" } from "./emojis.js";
export { "smile" as smile } from "./emojis.js";
export { "smile" as "smile" } from "./emojis.js";
export { foo, bar as "foo" } from "./emojis.js";
export { foo, bar as "foo" };
export { "學而時習之,不亦說乎?", "吾道一以貫之。" as "忠恕。" } from "Confucius";
export * as "foo", { default as "quux" } from "module-b";
export { "smile" } from "./emojis.js";
14 changes: 14 additions & 0 deletions tests/js/objects/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -143,6 +143,20 @@ printWidth: 80
================================================================================
`;

exports[`invalid-setter.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
({ set x(){} });
=====================================output=====================================
({ set x() {} });
================================================================================
`;

exports[`method.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
Expand Down
1 change: 1 addition & 0 deletions tests/js/objects/invalid-setter.js
@@ -0,0 +1 @@
({ set x(){} });

0 comments on commit 3b7a7ba

Please sign in to comment.