Skip to content

Commit

Permalink
feat(css/ast): Add SquareBracketBlock (#2573)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Oct 30, 2021
1 parent ef3c9a7 commit f77d6ce
Show file tree
Hide file tree
Showing 24 changed files with 831 additions and 82 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions css/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_css"
repository = "https://github.com/swc-project/swc.git"
version = "0.25.0"
version = "0.26.0"

[dependencies]
swc_css_ast = {version = "0.23.0", path = "./ast"}
swc_css_codegen = {version = "0.23.0", path = "./codegen"}
swc_css_parser = {version = "0.25.0", path = "./parser"}
swc_css_utils = {version = "0.20.0", path = "./utils/"}
swc_css_visit = {version = "0.22.0", path = "./visit"}
swc_css_ast = {version = "0.24.0", path = "./ast"}
swc_css_codegen = {version = "0.24.0", path = "./codegen"}
swc_css_parser = {version = "0.26.0", path = "./parser"}
swc_css_utils = {version = "0.21.0", path = "./utils/"}
swc_css_visit = {version = "0.23.0", path = "./visit"}
2 changes: 1 addition & 1 deletion css/ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_css_ast"
repository = "https://github.com/swc-project/swc.git"
version = "0.23.0"
version = "0.24.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
14 changes: 6 additions & 8 deletions css/ast/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use swc_common::{ast_node, EqIgnoreSpan, Span};

#[ast_node]
pub enum Value {
#[tag("SquareBracketBlock")]
SquareBracketBlock(SquareBracketBlock),

#[tag("ParenValue")]
Paren(ParenValue),

Expand Down Expand Up @@ -32,9 +35,6 @@ pub enum Value {
#[tag("BinValue")]
Bin(BinValue),

#[tag("ArrayValue")]
Array(ArrayValue),

#[tag("SpaceValues")]
Space(SpaceValues),

Expand Down Expand Up @@ -99,13 +99,11 @@ pub struct ParenValue {
pub value: Option<Box<Value>>,
}

#[ast_node("ArrayValue")]
pub struct ArrayValue {
#[ast_node("SquareBracketBlock")]
pub struct SquareBracketBlock {
/// Includes `[` and `]`.
pub span: Span,

/// Comma separated list of values.
pub values: Vec<Value>,
pub children: Option<Vec<Value>>,
}

#[ast_node("HashValue")]
Expand Down
8 changes: 4 additions & 4 deletions css/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_css_codegen"
repository = "https://github.com/swc-project/swc.git"
version = "0.23.0"
version = "0.24.0"

[dependencies]
auto_impl = "0.4.1"
bitflags = "1.3.2"
swc_atoms = {version = "0.2.7", path = "../../atoms"}
swc_common = {version = "0.14.0", path = "../../common"}
swc_css_ast = {version = "0.23.0", path = "../ast/"}
swc_css_ast = {version = "0.24.0", path = "../ast/"}
swc_css_codegen_macros = {version = "0.2.0", path = "macros/"}

[dev-dependencies]
swc_css_parser = {version = "0.25.0", path = "../parser"}
swc_css_visit = {version = "0.22.0", path = "../visit"}
swc_css_parser = {version = "0.26.0", path = "../parser"}
swc_css_visit = {version = "0.23.0", path = "../visit"}
testing = {version = "0.15.0", path = "../../testing"}
8 changes: 5 additions & 3 deletions css/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ where
Value::Str(n) => emit!(self, n),
Value::Fn(n) => emit!(self, n),
Value::Bin(n) => emit!(self, n),
Value::Array(n) => emit!(self, n),
Value::SquareBracketBlock(n) => emit!(self, n),
Value::Space(n) => emit!(self, n),
Value::Brace(n) => emit!(self, n),
Value::Lazy(n) => emit!(self, n),
Expand Down Expand Up @@ -452,10 +452,12 @@ where
}

#[emitter]
fn emit_array_value(&mut self, n: &ArrayValue) -> Result {
fn emit_square_bracket_block(&mut self, n: &SquareBracketBlock) -> Result {
punct!(self, "[");

self.emit_list(&n.values, ListFormat::CommaDelimited)?;
if let Some(values) = &n.children {
self.emit_list(&values, ListFormat::SpaceDelimited)?;
}

punct!(self, "]");
}
Expand Down
6 changes: 3 additions & 3 deletions css/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_css_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.25.2"
version = "0.26.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
Expand All @@ -17,11 +17,11 @@ bitflags = "1.2.1"
lexical = "5.2.2"
swc_atoms = {version = "0.2.7", path = "../../atoms"}
swc_common = {version = "0.14.0", path = "../../common"}
swc_css_ast = {version = "0.23.0", path = "../ast"}
swc_css_ast = {version = "0.24.0", path = "../ast"}
unicode-xid = "0.2.2"

[dev-dependencies]
serde = "1.0.127"
serde_json = "1.0.66"
swc_css_visit = {version = "0.22.0", path = "../visit"}
swc_css_visit = {version = "0.23.0", path = "../visit"}
testing = {version = "0.15.0", path = "../../testing"}
24 changes: 15 additions & 9 deletions css/parser/src/parser/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ where
let mut values = vec![];
let mut state = self.input.state();
let start_pos = self.input.cur_span()?.lo;

let mut hi = self.input.last_pos()?;

loop {
if is_one_of!(self, EOF, ";", "}", "!", ")") {
if is_one_of!(self, EOF, ";", "}", "!", ")", "]") {
self.input.reset(&state);
break;
}
Expand All @@ -47,12 +47,12 @@ where

if !eat!(self, " ") {
if self.ctx.recover_from_property_value
&& !is_one_of!(self, EOF, ";", "}", "!", ")")
&& !is_one_of!(self, EOF, ";", "}", "!", ")", "]")
{
self.input.reset(&start);

let mut tokens = vec![];
while !is_one_of!(self, EOF, ";", "}", "!", ")") {
while !is_one_of!(self, EOF, ";", "}", "!", ")", "]") {
tokens.extend(self.input.bump()?);
}

Expand Down Expand Up @@ -213,7 +213,7 @@ where

Token::Ident { .. } => return self.parse_value_ident_or_fn(),

tok!("[") => return self.parse_array_value().map(From::from),
tok!("[") => return self.parse_square_brackets_value().map(From::from),

tok!("(") => return self.parse_paren_value().map(From::from),

Expand Down Expand Up @@ -500,22 +500,28 @@ where
Ok(args)
}

fn parse_array_value(&mut self) -> PResult<ArrayValue> {
fn parse_square_brackets_value(&mut self) -> PResult<SquareBracketBlock> {
let span = self.input.cur_span()?;

expect!(self, "[");

self.input.skip_ws()?;

let ctx = Ctx {
is_in_delimited_value: true,
allow_separating_value_with_space: true,
..self.ctx
};
let values = self.with_ctx(ctx).parse_comma_separated_value()?;

let children = Some(self.with_ctx(ctx).parse_property_values()?.0);

self.input.skip_ws()?;

expect!(self, "]");

Ok(ArrayValue {
Ok(SquareBracketBlock {
span: span!(self, span.lo),
values,
children,
})
}

Expand Down
2 changes: 1 addition & 1 deletion css/parser/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ macro_rules! mtd {
}

impl Visit for SpanVisualizer<'_> {
mtd!(ArrayValue, visit_array_value);
mtd!(SquareBracketBlock, visit_square_bracket_block);
mtd!(AtRule, visit_at_rule);
mtd!(AtSelector, visit_at_selector);
mtd!(AtTextValue, visit_at_text_value);
Expand Down
4 changes: 2 additions & 2 deletions css/parser/tests/fixture/rome/custom-properties/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,13 @@
},
"value": [
{
"type": "ArrayValue",
"type": "SquareBracketBlock",
"span": {
"start": 249,
"end": 252,
"ctxt": 0
},
"values": []
"children": []
}
],
"important": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ error: Value
14 | --brackets: [ ];
| ^^^

error: ArrayValue
error: SquareBracketBlock
--> $DIR/tests/fixture/rome/custom-properties/input.css:14:14
|
14 | --brackets: [ ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@
"raw": "4"
},
{
"type": "ArrayValue",
"type": "SquareBracketBlock",
"span": {
"start": 43,
"end": 54,
"ctxt": 0
},
"values": [
"children": [
{
"type": "Text",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ error: Value
2 | grid-template-columns: repeat(4, [col-start]);
| ^^^^^^^^^^^

error: ArrayValue
error: SquareBracketBlock
--> $DIR/tests/fixture/rome/grid/repeat/line-name/input.css:2:35
|
2 | grid-template-columns: repeat(4, [col-start]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@
},
"values": [
{
"type": "ArrayValue",
"type": "SquareBracketBlock",
"span": {
"start": 43,
"end": 54,
"ctxt": 0
},
"values": [
"children": [
{
"type": "Text",
"span": {
Expand All @@ -149,13 +149,13 @@
"raw": "min-content"
},
{
"type": "ArrayValue",
"type": "SquareBracketBlock",
"span": {
"start": 67,
"end": 79,
"ctxt": 0
},
"values": [
"children": [
{
"type": "Text",
"span": {
Expand All @@ -179,13 +179,13 @@
"raw": "max-content"
},
{
"type": "ArrayValue",
"type": "SquareBracketBlock",
"span": {
"start": 92,
"end": 101,
"ctxt": 0
},
"values": [
"children": [
{
"type": "Text",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ error: Value
2 | grid-template-columns: repeat(4, [col-start] min-content [col-middle] max-content [col-end]);
| ^^^^^^^^^^^

error: ArrayValue
error: SquareBracketBlock
--> $DIR/tests/fixture/rome/grid/repeat/multi-values/input.css:2:35
|
2 | grid-template-columns: repeat(4, [col-start] min-content [col-middle] max-content [col-end]);
Expand Down Expand Up @@ -157,7 +157,7 @@ error: Value
2 | grid-template-columns: repeat(4, [col-start] min-content [col-middle] max-content [col-end]);
| ^^^^^^^^^^^^

error: ArrayValue
error: SquareBracketBlock
--> $DIR/tests/fixture/rome/grid/repeat/multi-values/input.css:2:59
|
2 | grid-template-columns: repeat(4, [col-start] min-content [col-middle] max-content [col-end]);
Expand Down Expand Up @@ -193,7 +193,7 @@ error: Value
2 | grid-template-columns: repeat(4, [col-start] min-content [col-middle] max-content [col-end]);
| ^^^^^^^^^

error: ArrayValue
error: SquareBracketBlock
--> $DIR/tests/fixture/rome/grid/repeat/multi-values/input.css:2:84
|
2 | grid-template-columns: repeat(4, [col-start] min-content [col-middle] max-content [col-end]);
Expand Down
Loading

1 comment on commit f77d6ce

@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: f77d6ce Previous: ef3c9a7 Ratio
base_tr_fixer 21911 ns/iter (± 3622) 23713 ns/iter (± 335) 0.92
base_tr_resolver_and_hygiene 138920 ns/iter (± 31749) 130448 ns/iter (± 25374) 1.06
codegen_es2015 52175 ns/iter (± 9871) 52752 ns/iter (± 323) 0.99
codegen_es2016 59520 ns/iter (± 14623) 52835 ns/iter (± 428) 1.13
codegen_es2017 61435 ns/iter (± 420) 52634 ns/iter (± 346) 1.17
codegen_es2018 61833 ns/iter (± 774) 52850 ns/iter (± 389) 1.17
codegen_es2019 61998 ns/iter (± 697) 52499 ns/iter (± 358) 1.18
codegen_es2020 62359 ns/iter (± 632) 52352 ns/iter (± 353) 1.19
codegen_es3 62376 ns/iter (± 451) 52388 ns/iter (± 442) 1.19
codegen_es5 62252 ns/iter (± 485) 52530 ns/iter (± 451) 1.19
config_for_file 15631 ns/iter (± 3702) 12890 ns/iter (± 6641) 1.21
full_es2015 196566474 ns/iter (± 79118342) 184441365 ns/iter (± 9283345) 1.07
full_es2016 148120814 ns/iter (± 20268415) 142544610 ns/iter (± 8230328) 1.04
full_es2017 154293734 ns/iter (± 21526729) 151972537 ns/iter (± 12718639) 1.02
full_es2018 175382387 ns/iter (± 40424971) 152307953 ns/iter (± 13484415) 1.15
full_es2019 172920657 ns/iter (± 35237645) 156149824 ns/iter (± 13248108) 1.11
full_es2020 184616274 ns/iter (± 18588870) 154608685 ns/iter (± 7428277) 1.19
full_es3 275247942 ns/iter (± 131803302) 228217254 ns/iter (± 20184653) 1.21
full_es5 244599849 ns/iter (± 59022397) 212440948 ns/iter (± 13785688) 1.15
parser 764936 ns/iter (± 17284) 671007 ns/iter (± 16509) 1.14
transforms_es2015 1229895 ns/iter (± 369975) 1009846 ns/iter (± 73651) 1.22
transforms_es2016 662399 ns/iter (± 135899) 604011 ns/iter (± 32069) 1.10
transforms_es2017 658870 ns/iter (± 655080) 588579 ns/iter (± 16127) 1.12
transforms_es2018 626743 ns/iter (± 77898) 577334 ns/iter (± 28787) 1.09
transforms_es2019 634882 ns/iter (± 267705) 561449 ns/iter (± 29177) 1.13
transforms_es2020 615713 ns/iter (± 88633) 561729 ns/iter (± 27815) 1.10
transforms_es3 1992967 ns/iter (± 816329) 1137184 ns/iter (± 40006) 1.75
transforms_es5 1808507 ns/iter (± 590210) 994672 ns/iter (± 68846) 1.82
ser_ast_node 158 ns/iter (± 25) 147 ns/iter (± 3) 1.07
ser_serde 171 ns/iter (± 24) 160 ns/iter (± 5) 1.07
emit_colors 14384926 ns/iter (± 22202437) 16215835 ns/iter (± 22244834) 0.89
emit_large 93731593 ns/iter (± 124085107) 101316579 ns/iter (± 156503773) 0.93
base_clone 2374073 ns/iter (± 252199) 2406698 ns/iter (± 221137) 0.99
fold_span 3877815 ns/iter (± 608664) 3857643 ns/iter (± 182529) 1.01
fold_span_panic 4004882 ns/iter (± 493093) 4083577 ns/iter (± 285213) 0.98
visit_mut_span 3416899 ns/iter (± 1176204) 2894836 ns/iter (± 328591) 1.18
visit_mut_span_panic 3923091 ns/iter (± 566131) 2945175 ns/iter (± 237369) 1.33
boxing_boxed 169 ns/iter (± 11) 142 ns/iter (± 0) 1.19
boxing_boxed_clone 81 ns/iter (± 0) 68 ns/iter (± 0) 1.19
boxing_unboxed 155 ns/iter (± 0) 161 ns/iter (± 1) 0.96
boxing_unboxed_clone 76 ns/iter (± 0) 64 ns/iter (± 0) 1.19

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

Please sign in to comment.