Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support babel data-type="module" #8394

Merged
merged 2 commits into from May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions changelog_unreleased/html/pr-8173.md
@@ -1,4 +1,9 @@
#### Support legacy HTML-like comments script blocks ([#8173](https://github.com/prettier/prettier/pull/8173) by [@fisker](https://github.com/fisker))
#### Support legacy HTML-like comments script blocks ([#8173](https://github.com/prettier/prettier/pull/8173) by [@fisker](https://github.com/fisker), [#8394](https://github.com/prettier/prettier/pull/8394) by [@fisker](https://github.com/fisker))

Previously we parse html `<script>` blocks as "module"([ECMAScript Module grammar](https://babeljs.io/docs/en/options#sourcetype)), this is why we can't parse comments starts with `<!--`(aka [HTML-like comments](https://tc39.es/ecma262/#sec-html-like-comments)), now we parse `<script>` blocks as "script", unless this `<script>`

1. `type="module"`
2. `type="text/babel"` and `data-type="module"`, [will be introduced in babel@v7.10.0](https://github.com/babel/babel/pull/11466)

<!-- prettier-ignore -->
```html
Expand All @@ -10,7 +15,7 @@ alert("hello" + ' world!')

<!-- Prettier stable -->
SyntaxError: Unexpected token (2:1)
1 |
1 |
> 2 | <!--
| ^
3 | alert("hello" + ' world!')
Expand Down
29 changes: 15 additions & 14 deletions src/language-html/printer-html.js
Expand Up @@ -72,24 +72,25 @@ function embed(path, print, textToDoc, options) {
parser === "markdown"
? dedentString(node.value.replace(/^[^\S\n]*?\n/, ""))
: node.value;
const textToDocOptions = { parser };
if (options.parser === "html" && parser === "babel") {
let sourceType = "script";
const { attrMap } = node.parent;
if (
attrMap &&
(attrMap.type === "module" ||
(attrMap.type === "text/babel" &&
attrMap["data-type"] === "module"))
) {
sourceType = "module";
}
textToDocOptions.__babelSourceType = sourceType;
}
return builders.concat([
concat([
breakParent,
printOpeningTagPrefix(node, options),
stripTrailingHardline(
textToDoc(
value,
options.parser === "html" && parser === "babel"
? {
parser,
__babelSourceType:
node.attrMap && node.attrMap.type === "module"
Copy link
Member Author

@fisker fisker May 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mistake from #8173, should be node.parent.attrMap

? "module"
: "script",
}
: { parser }
)
),
stripTrailingHardline(textToDoc(value, textToDocOptions)),
printClosingTagSuffix(node, options),
]),
]);
Expand Down
36 changes: 36 additions & 0 deletions tests/html/script/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`babel.html format 1`] = `
====================================options=====================================
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
<script type="text/babel" data-presets="react" data-type="module">
import { h,
render } from 'https://unpkg.com/preact?module';
render(
<h1>Hello World!</h1>,
document.body
);
</script>

<script type="text/babel">
<!--
alert(1)
-->
</script>

=====================================output=====================================
<script type="text/babel" data-presets="react" data-type="module">
import { h, render } from "https://unpkg.com/preact?module";
render(<h1>Hello World!</h1>, document.body);
</script>

<script type="text/babel">
<!--
alert(1);
-->
</script>

================================================================================
`;

exports[`legacy.html format 1`] = `
====================================options=====================================
parsers: ["html"]
Expand Down
14 changes: 14 additions & 0 deletions tests/html/script/babel.html
@@ -0,0 +1,14 @@
<script type="text/babel" data-presets="react" data-type="module">
import { h,
render } from 'https://unpkg.com/preact?module';
render(
<h1>Hello World!</h1>,
document.body
);
</script>

<script type="text/babel">
<!--
alert(1)
-->
</script>