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

Fix css embed in JSX when using (only) a spread attribute #15896

Merged
merged 6 commits into from Jan 14, 2024
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
8 changes: 8 additions & 0 deletions changelog_unreleased/javascript/15896.md
@@ -0,0 +1,8 @@
#### Fix crash when parsing template literal CSS in a JSX style tag using a spread attribute (#15896 by @eelco)

For example this code would crash before:

<!-- prettier-ignore -->
```jsx
<style {...spread}>{`.{}`}</style>
```
3 changes: 2 additions & 1 deletion src/language-js/embed/css.js
Expand Up @@ -75,7 +75,8 @@ function isStyledJsx({ node, parent, grandparent }) {
grandparent.type === "JSXElement" &&
grandparent.openingElement.name.name === "style" &&
grandparent.openingElement.attributes.some(
(attribute) => attribute.name.name === "jsx",
(attribute) =>
attribute.type === "JSXAttribute" && attribute.name.name === "jsx",
)) ||
(parent?.type === "TaggedTemplateExpression" &&
parent.tag.type === "Identifier" &&
Expand Down
15 changes: 15 additions & 0 deletions tests/format/jsx/embed/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`css-embed.js format 1`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
| printWidth
=====================================input======================================
<style {...spread}>{\`.{}\`}</style>;

=====================================output=====================================
<style {...spread}>{\`.{}\`}</style>;

================================================================================
`;
1 change: 1 addition & 0 deletions tests/format/jsx/embed/css-embed.js
@@ -0,0 +1 @@
<style {...spread}>{`.{}`}</style>;
1 change: 1 addition & 0 deletions tests/format/jsx/embed/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(import.meta, ["flow", "babel"]);