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

refactor(css/parser): new approach #6224

Merged
merged 20 commits into from Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from 19 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
@@ -0,0 +1,7 @@
a {};

b {};

c { }

c {color: red; }
@@ -0,0 +1,10 @@
a {}
;

b {}
;

c {}
c {
color: red;
}
@@ -0,0 +1,5 @@
a{};

b {};

c {}c{color:red}
Expand Up @@ -17,8 +17,6 @@ $bad foo { color: red }

$bad: rule;

/* TODO: FIX ME */
/* a {}; b {}; */

a { div.major { color: blue } color: red }
a { div:hover { color: blue } color: red }
Expand Down
Expand Up @@ -18,9 +18,10 @@ $bad foo {
color: red;
}
$bad: rule;


a {
div.major { color: blue }
color: red }
div.major { color: blue } color: red }
a {
div: hover {color:blue} color:red;
}
Expand Down
@@ -1,2 +1,4 @@
{}{color:red}#broken# {color:red}$bad{color:red}$bad {color:red}$bad foo{color:red}$bad foo {color:red}$bad: rule;a{div.major { color: blue }color: red }
a {div:hover{color:blue}color:red}a{div:hover{color:blue};color:red}
{}{color:red}#broken# {color:red}$bad{color:red}$bad {color:red}$bad foo{color:red}$bad foo {color:red}$bad: rule;


a {div.major { color: blue } color: red }a{div:hover{color:blue}color:red}a{div:hover{color:blue};color:red}
Expand Up @@ -31,18 +31,30 @@

@layer {
@layer foo {
color: red;
.class {
color: red;
}
}
@layer foo {
color: red;
.class {
color: red;
}
}
}

@layer {
@layer foo { color: red; }
@layer foo {
.class {
color: red;
}
}
}
@layer {
@layer foo { color: red; }
@layer foo {
.class {
color: red;
}
}
}

@layer framework {
Expand Down

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

37 changes: 20 additions & 17 deletions crates/swc_css_parser/src/parser/at_rules/mod.rs
Expand Up @@ -171,7 +171,7 @@ where
parser.input.skip_ws();

let prelude = if is!(parser, Ident) {
let mut name_list = vec![];
let mut name_list: Vec<LayerName> = vec![];

while is!(parser, Ident) {
name_list.push(parser.parse()?);
Expand All @@ -185,23 +185,26 @@ where
}
}

match name_list.len() == 1 {
// Block
true => Some(AtRulePrelude::LayerPrelude(LayerPrelude::Name(
name_list.remove(0),
))),
// Statement
false => {
let first = name_list[0].span;
let last = name_list[name_list.len() - 1].span;

Some(AtRulePrelude::LayerPrelude(LayerPrelude::NameList(
LayerNameList {
name_list,
span: Span::new(first.lo, last.hi, Default::default()),
},
)))
if is!(parser, ";") {
let first = name_list[0].span;
let last = name_list[name_list.len() - 1].span;

Some(AtRulePrelude::LayerPrelude(LayerPrelude::NameList(
LayerNameList {
name_list,
span: Span::new(first.lo, last.hi, Default::default()),
},
)))
} else {
if name_list.len() > 1 {
let span = parser.input.cur_span();

return Err(Error::new(span, ErrorKind::Expected("';' token")));
}

Some(AtRulePrelude::LayerPrelude(LayerPrelude::Name(
name_list.remove(0),
)))
}
} else {
None
Expand Down