Babel: support moduleAttributes (#8436)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-06-02 20:31:33 +08:00
1 parent 7222be12f4
commit 7d2e57c67f
20 files changed
+152 -3

No files matched your search

@@ -0,0 +1,17 @@
#### Support ES Module Attributes and JSON modules ([#8436](https://github.com/prettier/prettier/pull/8436) by [@fisker](https://github.com/fisker))
Support Stage-1 proposal [ES Module Attributes and JSON modules](https://github.com/tc39/proposal-module-attributes).
<!-- prettier-ignore -->
```js
// Input
import foo from "foo.json" with type: "json";
// Prettier stable
SyntaxError: Unexpected token, expected ";" (1:28)
> 1 | import foo from "foo.json" with type: "json";
| ^
// Prettier master
import foo from "foo.json" with type: "json";
```
+12 -2
View File
@@ -31,8 +31,9 @@ function babelOptions({ sourceType, extraPlugins = [] }) {
"classPrivateMethods",
"v8intrinsic",
"partialApplication",
"privateIn",
["decorators", { decoratorsBeforeExport: false }],
"privateIn",
["moduleAttributes", { version: "may-2020" }],
...extraPlugins,
],
};
@@ -129,7 +130,16 @@ function rethrowSomeRecoveredErrors(ast) {
for (const error of ast.errors) {
if (
typeof error.message === "string" &&
error.message.startsWith("Did not expect a type annotation here.")
(error.message.startsWith(
// UnexpectedTypeAnnotation
// https://github.com/babel/babel/blob/2f31ecf85d85cb100fa08d4d9a09de0fe4a117e4/packages/babel-parser/src/plugins/typescript/index.js#L88
"Did not expect a type annotation here."
) ||
error.message.startsWith(
// ModuleAttributeDifferentFromType
// https://github.com/babel/babel/blob/bda759ac3dce548f021ca24e9182b6e6f7c218e3/packages/babel-parser/src/parser/location.js#L99
"The only accepted module attribute is `type`"
))
) {
throw error;
}
+6 -1
View File
@@ -1022,11 +1022,16 @@ 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")));
}
parts.push(semi);
return concat(parts);
}
case "ImportAttribute":
return concat([path.call(print, "key"), ": ", path.call(print, "value")]);
case "Import":
return "import";
case "TSModuleBlock":
@@ -117,6 +117,20 @@ async function foo() {
================================================================================
`;
exports[`module-attributes.html format 1`] = `
====================================options=====================================
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
<script src="foo.wasm" type="module" withtype="webassembly"></script>
=====================================output=====================================
<script src="foo.wasm" type="module" withtype="webassembly"></script>
================================================================================
`;
exports[`script.html format 1`] = `
====================================options=====================================
parsers: ["html"]
+1
View File
@@ -0,0 +1 @@
<script src="foo.wasm" type="module" withtype="webassembly"></script>
@@ -720,6 +720,34 @@ 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`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
import foo from "foo.json" with type: "json";
=====================================output=====================================
import foo from "foo.json" with type: "json";
================================================================================
`;
exports[`nullish-coalescing-operator.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
@@ -0,0 +1 @@
import("foo.json", { with: { type: "json" } })
@@ -0,0 +1 @@
import foo from "foo.json" with type: "json";
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`module-attributes-dynamic.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
import("foo.json", { with: { type: "json" } })
=====================================output=====================================
import("foo.json", { with: { type: "json" } });
================================================================================
`;
exports[`module-attributes-static.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
import foo from "foo.json" with type: "json";
=====================================output=====================================
import foo from "foo.json" with type: "json";
================================================================================
`;
+1
View File
@@ -0,0 +1 @@
run_spec(__dirname, ["babel"]);
@@ -0,0 +1 @@
import("foo.json", { with: { type: "json" } })
@@ -0,0 +1 @@
import foo from "foo.json" with type: "json";
@@ -6,3 +6,17 @@ exports[`dynamic-import.js error test 1`] = `
| ^
2 | "
`;
exports[`module-attributes-dynamic.js error test 1`] = `
"Unexpected token \`,\`, expected the token \`)\` (1:18)
> 1 | import(\\"foo.json\\", { with: { type: \\"json\\" } })
| ^
2 | "
`;
exports[`module-attributes-static.js error test 1`] = `
"Unexpected token \`with\`, expected the token \`;\` (1:28)
> 1 | import foo from \\"foo.json\\" with type: \\"json\\";
| ^^^^
2 | "
`;
@@ -0,0 +1 @@
import("foo.json", { with: { type: "json" } })
@@ -0,0 +1 @@
import foo from "foo.json" with type: "json";
@@ -11,6 +11,13 @@ exports[`html-like-comments.js error test 1`] = `
6 | "
`;
exports[`module-attributes.js error test 1`] = `
"The only accepted module attribute is \`type\` (1:33)
> 1 | import foo from \\"foo.json\\" with foo: \\"json\\";
| ^
2 | "
`;
exports[`partial-template-strings.js error test 1`] = `
"Unexpected token (6:42)
4 |
@@ -0,0 +1 @@
import foo from "foo.json" with foo: "json";
@@ -16,6 +16,20 @@ exports[`invalid-jsx-1.ts error test 1`] = `
4 | "
`;
exports[`module-attributes-dynamic.ts error test 1`] = `
"Dynamic import must have one specifier as an argument. (1:8)
> 1 | import(\\"foo.json\\", { with: { type: \\"json\\" } })
| ^
2 | "
`;
exports[`module-attributes-static.ts error test 1`] = `
"';' expected. (1:28)
> 1 | import foo from \\"foo.json\\" with type: \\"json\\";
| ^
2 | "
`;
exports[`snippet: #0 error test 1`] = `
"Unexpected token. Did you mean \`{'}'}\` or \`&rbrace;\`? (1:15)
> 1 | let x1 = <div>}</div>;
@@ -0,0 +1 @@
import("foo.json", { with: { type: "json" } })
@@ -0,0 +1 @@
import foo from "foo.json" with type: "json";