Skip to content

Commit

Permalink
refactor(css/parser): Refactor even more (#6288)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 29, 2022
1 parent b1a72db commit 6cafeb2
Show file tree
Hide file tree
Showing 337 changed files with 1,738 additions and 1,613 deletions.
@@ -1,4 +1,10 @@

x Unexpected duplicate '@import' rule 'a.css'.
,-[$DIR/tests/rules/fail/no-duplicate-at-import-rules/11/input.css:2:1]
2 | @import 'a.css' layer();
: ^^^^^^^^^^^^^^^^^^^^^^^^
`----

x Unexpected duplicate '@import' rule 'a.css'.
,-[$DIR/tests/rules/fail/no-duplicate-at-import-rules/11/input.css:3:1]
3 | @import 'a.css' layer(default);
Expand Down
Expand Up @@ -137,8 +137,8 @@ st.css');
@import url("./test.css") layer(default);
@import url("./test.css") layer(default) supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") layer supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") layer() supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") layer();
@import url("./test.css") layer supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") layer;
@import url("http://example.com/style.css") supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") layer(default) supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") screen and (min-width: 400px);
Expand Down
94 changes: 13 additions & 81 deletions crates/swc_css_parser/src/parser/at_rules/mod.rs
Expand Up @@ -184,53 +184,16 @@ where
Token::Function { value, .. }
if *value.to_ascii_lowercase() == *"layer" =>
{
let span = self.input.cur_span();

let ctx = Ctx {
in_import_at_rule: true,
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
..self.ctx
};

let func = self.with_ctx(ctx).parse_as::<Function>()?;

self.input.skip_ws();

if func.value.len() != 1 {
return Err(Error::new(
span,
ErrorKind::Expected(
"layer function inside @import expected to have exactly \
one ident argument",
),
));
} else if let ComponentValue::LayerName(LayerName {
name: name_raw,
..
}) = &func.value[0]
{
self.input.skip_ws();

if name_raw.is_empty() {
return Err(Error::new(
span,
ErrorKind::Expected(
"layer function inside @import expected to have \
exactly one ident argument",
),
));
} else {
Some(Box::new(ImportPreludeLayerName::Function(func)))
}
} else {
return Err(Error::new(
span,
ErrorKind::Expected(
"layer function inside @import expected to have exactly \
one ident argument",
),
));
}
Some(Box::new(ImportPreludeLayerName::Function(func)))
}
_ => None,
}
Expand Down Expand Up @@ -516,7 +479,6 @@ where
in_container_at_rule: true,
..self.ctx
};

let style_blocks = self.with_ctx(ctx).parse_as::<Vec<StyleBlock>>()?;
let style_blocks: Vec<ComponentValue> = style_blocks
.into_iter()
Expand Down Expand Up @@ -586,12 +548,11 @@ where
declaration_list
}
js_word!("font-feature-values") => {
let declaration_list = self
.with_ctx(Ctx {
in_font_feature_values_at_rule: true,
..self.ctx
})
.parse_as::<Vec<DeclarationOrAtRule>>()?;
let ctx = Ctx {
in_font_feature_values_at_rule: true,
..self.ctx
};
let declaration_list = self.with_ctx(ctx).parse_as::<Vec<DeclarationOrAtRule>>()?;
let declaration_list: Vec<ComponentValue> = declaration_list
.into_iter()
.map(ComponentValue::DeclarationOrAtRule)
Expand Down Expand Up @@ -642,7 +603,6 @@ where
..self.ctx
};
let rule_list = self.with_ctx(ctx).parse_as::<Vec<Rule>>()?;

let rule_list: Vec<ComponentValue> = rule_list
.into_iter()
.map(|rule| match rule {
Expand Down Expand Up @@ -1234,20 +1194,15 @@ where
match cur!(self) {
tok!("function") => {
let ctx = Ctx {
block_contents_grammar: BlockContentsGrammar::NoGrammar,
block_contents_grammar: BlockContentsGrammar::DeclarationList,
..self.ctx
};

let function = self.with_ctx(ctx).parse_as::<Function>()?;

Ok(GeneralEnclosed::Function(function))
}
tok!("(") => {
let ctx = Ctx {
block_contents_grammar: BlockContentsGrammar::NoGrammar,
..self.ctx
};
let block = self.with_ctx(ctx).parse_as::<SimpleBlock>()?;
let block = self.parse_as::<SimpleBlock>()?;

if let Some(first) = block.value.get(0) {
match first {
Expand Down Expand Up @@ -1310,7 +1265,6 @@ where
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
..self.ctx
};

let function = self.with_ctx(ctx).parse_as::<Function>()?;

Ok(DocumentPreludeMatchingFunction::Function(function))
Expand Down Expand Up @@ -1897,10 +1851,9 @@ where
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
..self.ctx
};
let function = self.with_ctx(ctx).parse_as::<Function>()?;

Ok(MediaFeatureValue::Function(
self.with_ctx(ctx).parse_as::<Function>()?,
))
Ok(MediaFeatureValue::Function(function))
}
_ => Err(Error::new(
span,
Expand Down Expand Up @@ -2051,34 +2004,14 @@ where
let start = self.input.cur_span().lo;
let mut name = vec![];

let entered = is!(self, Ident);

while is!(self, Ident) {
let span = self.input.cur_span();
let token = bump!(self);
let ident = match token {
Token::Ident { value, raw } => Ident {
span,
value,
raw: Some(raw),
},
_ => {
unreachable!();
}
};

name.push(ident);
name.push(self.parse()?);

if is!(self, ".") {
eat!(self, ".");
}
}

if !entered {
// if first argument is not ident, without bump! will cause infinite loop
bump!(self);
}

Ok(LayerName {
name,
span: span!(self, start),
Expand Down Expand Up @@ -2517,10 +2450,9 @@ where
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
..self.ctx
};
let function = self.with_ctx(ctx).parse_as::<Function>()?;

Ok(SizeFeatureValue::Function(
self.with_ctx(ctx).parse_as::<Function>()?,
))
Ok(SizeFeatureValue::Function(function))
}
_ => Err(Error::new(
span,
Expand Down
3 changes: 1 addition & 2 deletions crates/swc_css_parser/src/parser/mod.rs
Expand Up @@ -51,7 +51,6 @@ pub struct ParserConfig {

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlockContentsGrammar {
NoGrammar,
StyleBlock,
DeclarationList,
RuleList,
Expand All @@ -61,7 +60,7 @@ pub enum BlockContentsGrammar {

impl Default for BlockContentsGrammar {
fn default() -> Self {
BlockContentsGrammar::NoGrammar
BlockContentsGrammar::Stylesheet
}
}

Expand Down

1 comment on commit 6cafeb2

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 6cafeb2 Previous: 1024a55 Ratio
es/full/bugs-1 390937 ns/iter (± 45171) 356067 ns/iter (± 43971) 1.10
es/full/minify/libraries/antd 2024403696 ns/iter (± 84000294) 1859111974 ns/iter (± 14784413) 1.09
es/full/minify/libraries/d3 476785546 ns/iter (± 41965702) 463355625 ns/iter (± 32126132) 1.03
es/full/minify/libraries/echarts 1792415877 ns/iter (± 195551799) 1635317767 ns/iter (± 37714106) 1.10
es/full/minify/libraries/jquery 129407773 ns/iter (± 9255158) 111886114 ns/iter (± 2504388) 1.16
es/full/minify/libraries/lodash 183424391 ns/iter (± 34477537) 151225285 ns/iter (± 8765299) 1.21
es/full/minify/libraries/moment 86003471 ns/iter (± 6876231) 72264617 ns/iter (± 5370681) 1.19
es/full/minify/libraries/react 24059463 ns/iter (± 1356149) 24802712 ns/iter (± 1502754) 0.97
es/full/minify/libraries/terser 426218413 ns/iter (± 156067983) 362972484 ns/iter (± 13027433) 1.17
es/full/minify/libraries/three 711234308 ns/iter (± 53374467) 646026264 ns/iter (± 35595182) 1.10
es/full/minify/libraries/typescript 4546603004 ns/iter (± 249963807) 3795528050 ns/iter (± 89836935) 1.20
es/full/minify/libraries/victory 1325358466 ns/iter (± 161246805) 896609770 ns/iter (± 55945265) 1.48
es/full/minify/libraries/vue 287127452 ns/iter (± 13773630) 182554178 ns/iter (± 13773073) 1.57
es/full/codegen/es3 43131 ns/iter (± 19616) 35545 ns/iter (± 2323) 1.21
es/full/codegen/es5 40857 ns/iter (± 26356) 34983 ns/iter (± 2212) 1.17
es/full/codegen/es2015 43118 ns/iter (± 7459) 34629 ns/iter (± 1635) 1.25
es/full/codegen/es2016 44904 ns/iter (± 11513) 34901 ns/iter (± 2660) 1.29
es/full/codegen/es2017 46780 ns/iter (± 8498) 35094 ns/iter (± 1802) 1.33
es/full/codegen/es2018 41574 ns/iter (± 5738) 34604 ns/iter (± 1833) 1.20
es/full/codegen/es2019 44729 ns/iter (± 8708) 34468 ns/iter (± 1173) 1.30
es/full/codegen/es2020 48745 ns/iter (± 11486) 34658 ns/iter (± 1279) 1.41
es/full/all/es3 367290261 ns/iter (± 87662089) 205519690 ns/iter (± 11365300) 1.79
es/full/all/es5 321890332 ns/iter (± 127241484) 196142869 ns/iter (± 7785188) 1.64
es/full/all/es2015 271068902 ns/iter (± 91852996) 155767285 ns/iter (± 6217327) 1.74
es/full/all/es2016 257610861 ns/iter (± 101836247) 154437144 ns/iter (± 5923843) 1.67
es/full/all/es2017 231542607 ns/iter (± 70423482) 155672702 ns/iter (± 6139698) 1.49
es/full/all/es2018 243168055 ns/iter (± 50391400) 154282845 ns/iter (± 6468276) 1.58
es/full/all/es2019 250836101 ns/iter (± 51105815) 145273363 ns/iter (± 7142148) 1.73
es/full/all/es2020 246835077 ns/iter (± 80358250) 139509565 ns/iter (± 4405715) 1.77
es/full/parser 1054014 ns/iter (± 288517) 729222 ns/iter (± 26256) 1.45
es/full/base/fixer 35289 ns/iter (± 6720) 26383 ns/iter (± 595) 1.34
es/full/base/resolver_and_hygiene 130803 ns/iter (± 25666) 92248 ns/iter (± 3414) 1.42
serialization of ast node 269 ns/iter (± 42) 216 ns/iter (± 4) 1.25
serialization of serde 264 ns/iter (± 46) 216 ns/iter (± 4) 1.22

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.