forked from vercel/next.js
/
transform_css.rs
482 lines (441 loc) · 16.2 KB
/
transform_css.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use easy_error::{bail, Error};
use std::panic;
use swc_common::util::take::Take;
use swc_common::{source_map::Pos, BytePos, Span, SyntaxContext, DUMMY_SP};
use swc_css::ast::*;
use swc_css::codegen::{
writer::basic::{BasicCssWriter, BasicCssWriterConfig},
CodeGenerator, CodegenConfig, Emit,
};
use swc_css::parser::{parse_str, parse_tokens, parser::ParserConfig};
use swc_css::visit::{VisitMut, VisitMutWith};
use swc_ecmascript::ast::{Expr, Str, StrKind, Tpl, TplElement};
use swc_ecmascript::utils::HANDLER;
use swc_stylis::prefixer::prefixer;
use tracing::{debug, trace};
use super::{hash_string, string_literal_expr, LocalStyle};
pub fn transform_css(
style_info: &LocalStyle,
is_global: bool,
class_name: &Option<String>,
) -> Result<Expr, Error> {
debug!("CSS: \n{}", style_info.css);
let result: Result<Stylesheet, _> = parse_str(
&style_info.css,
style_info.css_span.lo,
style_info.css_span.hi,
ParserConfig {
parse_values: false,
allow_wrong_line_comments: true,
},
// We ignore errors because we inject placeholders for expressions which is
// not a valid css.
&mut vec![],
);
let mut ss = match result {
Ok(ss) => ss,
Err(err) => {
HANDLER.with(|handler| {
// Print css parsing errors
err.to_diagnostics(&handler).emit();
// TODO(kdy1): We may print css so the user can see the error, and report it.
handler
.struct_span_err(
style_info.css_span,
"Failed to parse css in styled jsx component",
)
.note(&format!("Input to the css parser is {}", style_info.css))
.emit()
});
bail!("Failed to parse css");
}
};
// ? Do we need to support optionally prefixing?
ss.visit_mut_with(&mut prefixer());
ss.visit_mut_with(&mut CssPlaceholderFixer);
ss.visit_mut_with(&mut Namespacer {
class_name: match class_name {
Some(s) => s.clone(),
None => format!("jsx-{}", &hash_string(&style_info.hash)),
},
is_global,
is_dynamic: style_info.is_dynamic,
});
let mut s = String::new();
{
let mut wr = BasicCssWriter::new(&mut s, BasicCssWriterConfig { indent: " " });
let mut gen = CodeGenerator::new(&mut wr, CodegenConfig { minify: true });
gen.emit(&ss).unwrap();
}
if style_info.expressions.len() == 0 {
return Ok(string_literal_expr(&s));
}
let mut parts: Vec<&str> = s.split("__styled-jsx-placeholder-").collect();
let mut final_expressions = vec![];
for i in 1..parts.len() {
let (num_len, expression_index) = read_number(&parts[i]);
final_expressions.push(style_info.expressions[expression_index].clone());
let substr = &parts[i][(num_len + 2)..];
parts[i] = substr;
}
Ok(Expr::Tpl(Tpl {
quasis: parts
.iter()
.map(|quasi| {
TplElement {
cooked: None, // ? Do we need cooked as well
raw: Str {
value: (*quasi).into(),
span: DUMMY_SP,
has_escape: false,
kind: StrKind::Synthesized {},
},
span: DUMMY_SP,
tail: false,
}
})
.collect(),
exprs: final_expressions,
span: DUMMY_SP,
}))
}
/// Returns `(length, value)`
fn read_number(s: &str) -> (usize, usize) {
for (idx, c) in s.char_indices() {
if c.is_digit(10) {
continue;
}
// For 10, we reach here after `0`.
let value = s[0..idx].parse().expect("failed to parse");
return (idx, value);
}
unreachable!("read_number(`{}`) is invalid because it is empty", s)
}
/// This fixes invalid css which is created from interpolated expressions.
///
/// `__styled-jsx-placeholder-` is handled at here.
struct CssPlaceholderFixer;
impl VisitMut for CssPlaceholderFixer {
fn visit_mut_media_query(&mut self, q: &mut MediaQuery) {
q.visit_mut_children_with(self);
match q {
MediaQuery::Ident(q) => {
if q.raw.starts_with("__styled-jsx-placeholder-") {
// TODO(kdy1): Remove this once we have CST for media query.
// We need good error recovery for media queries to handle this.
q.raw = format!("({})", &q.value).into();
}
}
_ => {}
}
}
}
struct Namespacer {
class_name: String,
is_global: bool,
is_dynamic: bool,
}
impl VisitMut for Namespacer {
fn visit_mut_complex_selector(&mut self, node: &mut ComplexSelector) {
#[cfg(debug_assertions)]
let _tracing = {
// This will add information to the log messages, only for debug build.
// Note that we use cargo feature to remove all logging on production builds.
let mut code = String::new();
{
let mut wr = BasicCssWriter::new(&mut code, BasicCssWriterConfig { indent: " " });
let mut gen = CodeGenerator::new(&mut wr, CodegenConfig { minify: true });
gen.emit(&*node).unwrap();
}
tracing::span!(
tracing::Level::TRACE,
"Namespacer::visit_mut_complex_selector",
class_name = &*self.class_name,
is_global = self.is_global,
is_dynamic = self.is_dynamic,
input = &*code
)
.entered()
};
let mut new_selectors = vec![];
let mut combinator = None;
for sel in node.children.take() {
match &sel {
ComplexSelectorChildren::CompoundSelector(selector) => {
match self.get_transformed_selectors(combinator, selector.clone()) {
Ok(transformed_selectors) => new_selectors.extend(transformed_selectors),
Err(_) => {
HANDLER.with(|handler| {
handler
.struct_span_err(
selector.span,
"Failed to transform one off global selector",
)
.emit()
});
new_selectors.push(sel);
}
}
combinator = None;
}
ComplexSelectorChildren::Combinator(v) => match v.value {
CombinatorValue::Descendant => {}
CombinatorValue::NextSibling
| CombinatorValue::Child
| CombinatorValue::LaterSibling => {
combinator = Some(v.clone());
new_selectors.push(sel);
}
},
};
}
node.children = new_selectors;
}
}
impl Namespacer {
fn get_transformed_selectors(
&mut self,
combinator: Option<Combinator>,
mut node: CompoundSelector,
) -> Result<Vec<ComplexSelectorChildren>, Error> {
let mut pseudo_index = None;
let empty_tokens = Tokens {
span: node.span,
tokens: vec![],
};
for (i, selector) in node.subclass_selectors.iter().enumerate() {
let (name, args) = match selector {
SubclassSelector::PseudoClass(PseudoClassSelector { name, children, .. }) => {
match children {
Some(PseudoSelectorChildren::Nth(_)) => todo!("nth"),
Some(PseudoSelectorChildren::Tokens(v)) => (name, v),
None => (name, &empty_tokens),
}
}
SubclassSelector::PseudoElement(PseudoElementSelector {
name, children, ..
}) => match children {
Some(children) => (name, children),
None => (name, &empty_tokens),
},
_ => continue,
};
// One off global selector
if &name.value == "global" {
let block_tokens = get_block_tokens(&args);
let mut front_tokens = get_front_selector_tokens(&args);
let mut args = args.clone();
front_tokens.extend(args.tokens);
front_tokens.extend(block_tokens);
args.tokens = front_tokens;
let complex_selectors = panic::catch_unwind(|| {
let x: ComplexSelector = parse_tokens(
&args,
ParserConfig {
parse_values: false,
allow_wrong_line_comments: true,
},
// TODO(kdy1): We might be able to report syntax errors.
&mut vec![],
)
.unwrap();
return x;
});
return match complex_selectors {
Ok(complex_selectors) => {
let mut v = complex_selectors.children[1..]
.iter()
.cloned()
.collect::<Vec<_>>();
match v[0] {
ComplexSelectorChildren::Combinator(Combinator {
value: CombinatorValue::Descendant,
..
}) => {
v.remove(0);
}
_ => {}
}
if v.is_empty() {
bail!("Failed to transform one off global selector");
}
trace!("Combinator: {:?}", combinator);
trace!("v[0]: {:?}", v[0]);
if combinator.is_some() {
match v.get(0) {
Some(ComplexSelectorChildren::Combinator(..)) => {}
Some(..) => {}
_ => {
v.push(ComplexSelectorChildren::Combinator(
combinator.unwrap(),
));
}
}
}
v.iter_mut().for_each(|sel| {
if i < node.subclass_selectors.len() {
match sel {
ComplexSelectorChildren::CompoundSelector(sel) => {
sel.subclass_selectors.extend(
node.subclass_selectors[i + 1..].iter().cloned(),
);
}
_ => {}
}
}
});
Ok(v)
}
Err(_) => bail!("Failed to transform one off global selector"),
};
} else if pseudo_index.is_none() {
pseudo_index = Some(i);
}
}
let subclass_selector = match self.is_dynamic {
true => "__jsx-style-dynamic-selector",
false => &self.class_name,
};
let insert_index = match pseudo_index {
None => node.subclass_selectors.len(),
Some(i) => i,
};
if !self.is_global {
node.subclass_selectors.insert(
insert_index,
SubclassSelector::Class(ClassSelector {
span: DUMMY_SP,
text: Ident {
raw: subclass_selector.into(),
value: subclass_selector.into(),
span: DUMMY_SP,
},
}),
);
}
Ok(vec![ComplexSelectorChildren::CompoundSelector(node)])
}
}
fn get_front_selector_tokens(selector_tokens: &Tokens) -> Vec<TokenAndSpan> {
let start_pos = selector_tokens.span.lo.to_u32() - 2;
vec![
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 0),
hi: BytePos(start_pos + 1),
ctxt: SyntaxContext::empty(),
},
token: Token::Ident {
raw: "a".into(),
value: "a".into(),
},
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 1),
hi: BytePos(start_pos + 2),
ctxt: SyntaxContext::empty(),
},
token: Token::WhiteSpace { value: " ".into() },
},
]
}
fn get_block_tokens(selector_tokens: &Tokens) -> Vec<TokenAndSpan> {
let start_pos = selector_tokens.span.hi.to_u32();
vec![
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 0),
hi: BytePos(start_pos + 1),
ctxt: SyntaxContext::empty(),
},
token: Token::WhiteSpace { value: " ".into() },
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 1),
hi: BytePos(start_pos + 2),
ctxt: SyntaxContext::empty(),
},
token: Token::LBrace,
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 2),
hi: BytePos(start_pos + 3),
ctxt: SyntaxContext::empty(),
},
token: Token::WhiteSpace { value: " ".into() },
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 3),
hi: BytePos(start_pos + 8),
ctxt: SyntaxContext::empty(),
},
token: Token::Ident {
value: "color".into(),
raw: "color".into(),
},
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 8),
hi: BytePos(start_pos + 9),
ctxt: SyntaxContext::empty(),
},
token: Token::Colon,
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 9),
hi: BytePos(start_pos + 10),
ctxt: SyntaxContext::empty(),
},
token: Token::WhiteSpace { value: " ".into() },
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 10),
hi: BytePos(start_pos + 13),
ctxt: SyntaxContext::empty(),
},
token: Token::Ident {
value: "red".into(),
raw: "red".into(),
},
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 13),
hi: BytePos(start_pos + 14),
ctxt: SyntaxContext::empty(),
},
token: Token::Semi,
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 14),
hi: BytePos(start_pos + 15),
ctxt: SyntaxContext::empty(),
},
token: Token::WhiteSpace { value: " ".into() },
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 15),
hi: BytePos(start_pos + 16),
ctxt: SyntaxContext::empty(),
},
token: Token::RBrace,
},
TokenAndSpan {
span: Span {
lo: BytePos(start_pos + 16),
hi: BytePos(start_pos + 17),
ctxt: SyntaxContext::empty(),
},
token: Token::WhiteSpace { value: " ".into() },
},
]
}