Skip to content

Commit

Permalink
swc emotion prefer cooked tagged template (vercel#40385)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubytree33 committed Sep 14, 2022
1 parent 4f04fb7 commit e3bd19b
Show file tree
Hide file tree
Showing 4 changed files with 569 additions and 314 deletions.
20 changes: 20 additions & 0 deletions errors/invalid-escape-sequence-in-emotion-tagged-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Invalid escape sequence in Emotion tagged template

#### Why This Error Occurred

<!-- Explain why the error occurred. Ensure the description makes it clear why the warning/error exists -->

One or more escape sequence in an Emotion tagged template was incorrect. This prevented correct parsing before transformations were applied.

#### Possible Ways to Fix It

<!-- Explain how to fix the warning/error, potentially by providing alternative approaches. Ensure this section is actionable by users -->

Look for `\\` in the string to find the broken escape sequence(s). For example, `\\u` must be followed by four hex digits.

### Useful Links

<!-- Add links to relevant documentation -->

- [Next.js compiler Emotion configuration](https://nextjs.org/docs/advanced-features/compiler#emotion)
- [ECMAScript 2023 syntax for string literals](https://tc39.es/ecma262/multipage/ecmascript-language-lexical-grammar.html#sec-literals-string-literals)
4 changes: 4 additions & 0 deletions errors/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,10 @@
{
"title": "nonce-contained-invalid-characters",
"path": "/errors/nonce-contained-invalid-characters.md"
},
{
"title": "invalid-escape-sequence-in-emotion-tagged-template",
"path": "/errors/invalid-escape-sequence-in-emotion-tagged-template.md"
}
]
}
Expand Down
18 changes: 16 additions & 2 deletions packages/next-swc/crates/emotion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use regex::Regex;
use serde::{Deserialize, Serialize};
use sourcemap::{RawToken, SourceMap as RawSourcemap};
use swc_core::{
common::{comments::Comments, util::take::Take, BytePos, SourceMapperDyn, DUMMY_SP},
common::{comments::Comments, util::take::Take, BytePos, SourceMapperDyn, DUMMY_SP, errors::HANDLER},
ecma::utils::ExprFactory,
ecma::visit::{Fold, FoldWith},
ecma::{
Expand Down Expand Up @@ -337,7 +337,21 @@ impl<C: Comments> EmotionTransformer<C> {
if index % 2 == 0 {
if let Some(q) = tagged_tpl.quasis.get_mut(i) {
let q = q.take();
let minified = minify_css_string(&q.raw, index == 0, index == args_len - 1);
let input = match q.cooked {
Some(cooked) => cooked,
_ => {
HANDLER.with(|handler| {
handler
.struct_span_err(
q.span,
"This tagged template contains an invalid escape sequence.\nSee: https://nextjs.org/docs/messages/invalid-escape-sequence-in-emotion-tagged-template",
)
.emit()
});
q.raw
},
};
let minified = minify_css_string(&input, index == 0, index == args_len - 1);
// Compress one more spaces into one space
if minified.replace(' ', "").is_empty() {
if index != 0 && index != args_len - 1 {
Expand Down
Loading

0 comments on commit e3bd19b

Please sign in to comment.