diff --git a/crates/swc_css_ast/src/at_rule/mod.rs b/crates/swc_css_ast/src/at_rule/mod.rs index e913a901911..f72b8f1e021 100644 --- a/crates/swc_css_ast/src/at_rule/mod.rs +++ b/crates/swc_css_ast/src/at_rule/mod.rs @@ -55,7 +55,7 @@ pub struct FontFaceRule { } #[ast_node] -pub enum NamespaceValue { +pub enum NamespaceUri { #[tag("UrlValue")] Url(UrlValue), @@ -66,8 +66,8 @@ pub enum NamespaceValue { #[ast_node("NamespaceRule")] pub struct NamespaceRule { pub span: Span, - pub prefix: Ident, - pub value: NamespaceValue, + pub prefix: Option, + pub uri: NamespaceUri, } #[ast_node("ViewportRule")] diff --git a/crates/swc_css_codegen/src/lib.rs b/crates/swc_css_codegen/src/lib.rs index 2d2be64c3ce..e1a6a88f1b2 100644 --- a/crates/swc_css_codegen/src/lib.rs +++ b/crates/swc_css_codegen/src/lib.rs @@ -224,10 +224,10 @@ where } } #[emitter] - fn emit_namespace_value(&mut self, n: &NamespaceValue) -> Result { + fn emit_namespace_uri(&mut self, n: &NamespaceUri) -> Result { match n { - NamespaceValue::Url(n) => emit!(self, n), - NamespaceValue::Str(n) => emit!(self, n), + NamespaceUri::Url(n) => emit!(self, n), + NamespaceUri::Str(n) => emit!(self, n), } } @@ -237,11 +237,12 @@ where keyword!(self, "namespace"); space!(self); - emit!(self, n.prefix); + if n.prefix.is_some() { + emit!(self, n.prefix); + space!(self); + } - space!(self); - - emit!(self, n.value); + emit!(self, n.uri); } #[emitter] diff --git a/crates/swc_css_parser/src/parser/at_rule.rs b/crates/swc_css_parser/src/parser/at_rule.rs index f1e0c13e542..737481a73ef 100644 --- a/crates/swc_css_parser/src/parser/at_rule.rs +++ b/crates/swc_css_parser/src/parser/at_rule.rs @@ -374,20 +374,15 @@ where { fn parse(&mut self) -> PResult { let span = self.input.cur_span()?; - // TODO: make optional - let mut prefix = Ident { - span: DUMMY_SP, - value: js_word!(""), - raw: js_word!(""), - }; + let mut prefix = None; if is!(self, Ident) { prefix = match bump!(self) { - Token::Ident { value, raw } => Ident { + Token::Ident { value, raw } => Some(Ident { span: span!(self, span.lo), value, raw, - }, + }), _ => { unreachable!() } @@ -398,22 +393,18 @@ where let start_value_span = self.input.cur_span()?; - let value = match bump!(self) { - Token::Str { value, raw } => NamespaceValue::Str(Str { + let uri = match bump!(self) { + Token::Str { value, raw } => NamespaceUri::Str(Str { span: span!(self, start_value_span.lo), value, raw, }), - Token::Url { value, raw } => NamespaceValue::Url(UrlValue { + Token::Url { value, raw } => NamespaceUri::Url(UrlValue { span: span!(self, start_value_span.lo), url: value, raw, }), - _ => NamespaceValue::Str(Str { - span: span!(self, start_value_span.lo), - value: js_word!(""), - raw: js_word!(""), - }), + _ => return Err(Error::new(span, ErrorKind::Expected("Str or Url"))), }; eat!(self, ";"); @@ -421,7 +412,7 @@ where return Ok(NamespaceRule { span: span!(self, span.lo), prefix, - value, + uri, }); } } diff --git a/crates/swc_css_parser/tests/fixture.rs b/crates/swc_css_parser/tests/fixture.rs index bbf4adb132a..0a23cecfccf 100644 --- a/crates/swc_css_parser/tests/fixture.rs +++ b/crates/swc_css_parser/tests/fixture.rs @@ -327,7 +327,7 @@ impl Visit for SpanVisualizer<'_> { mtd!(KeyframesRule, visit_keyframes_rule); mtd!(MediaQuery, visit_media_query); mtd!(MediaRule, visit_media_rule); - mtd!(NamespaceValue, visit_namespace_value); + mtd!(NamespaceUri, visit_namespace_uri); mtd!(NamespaceRule, visit_namespace_rule); mtd!(NestedPageRule, visit_nested_page_rule); mtd!(NotMediaQuery, visit_not_media_query); diff --git a/crates/swc_css_parser/tests/fixture/at-rule/namespace/output.json b/crates/swc_css_parser/tests/fixture/at-rule/namespace/output.json index cb9342e9ec4..79569016639 100644 --- a/crates/swc_css_parser/tests/fixture/at-rule/namespace/output.json +++ b/crates/swc_css_parser/tests/fixture/at-rule/namespace/output.json @@ -23,7 +23,7 @@ "value": "empty", "raw": "empty" }, - "value": { + "uri": { "type": "String", "span": { "start": 17, @@ -41,17 +41,8 @@ "end": 35, "ctxt": 0 }, - "prefix": { - "type": "Identifier", - "span": { - "start": 0, - "end": 0, - "ctxt": 0 - }, - "value": "", - "raw": "" - }, - "value": { + "prefix": null, + "uri": { "type": "String", "span": { "start": 32, @@ -69,17 +60,8 @@ "end": 81, "ctxt": 0 }, - "prefix": { - "type": "Identifier", - "span": { - "start": 0, - "end": 0, - "ctxt": 0 - }, - "value": "", - "raw": "" - }, - "value": { + "prefix": null, + "uri": { "type": "UrlValue", "span": { "start": 47, @@ -107,7 +89,7 @@ "value": "svg", "raw": "svg" }, - "value": { + "uri": { "type": "UrlValue", "span": { "start": 97, @@ -125,17 +107,8 @@ "end": 172, "ctxt": 0 }, - "prefix": { - "type": "Identifier", - "span": { - "start": 0, - "end": 0, - "ctxt": 0 - }, - "value": "", - "raw": "" - }, - "value": { + "prefix": null, + "uri": { "type": "String", "span": { "start": 141, @@ -163,7 +136,7 @@ "value": "svg", "raw": "svg" }, - "value": { + "uri": { "type": "String", "span": { "start": 188, diff --git a/crates/swc_css_parser/tests/fixture/at-rule/namespace/span.rust-debug b/crates/swc_css_parser/tests/fixture/at-rule/namespace/span.rust-debug index 9e8074c419e..6263f537c32 100644 --- a/crates/swc_css_parser/tests/fixture/at-rule/namespace/span.rust-debug +++ b/crates/swc_css_parser/tests/fixture/at-rule/namespace/span.rust-debug @@ -33,7 +33,7 @@ error: Ident 1 | @namespace empty ""; | ^^^^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/at-rule/namespace/input.css:1:18 | 1 | @namespace empty ""; @@ -63,9 +63,7 @@ error: NamespaceRule 2 | @namespace ""; | ^^^^^^^^^^^^^^ -error: Ident - -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/at-rule/namespace/input.css:2:12 | 2 | @namespace ""; @@ -95,7 +93,7 @@ error: NamespaceRule 3 | @namespace url(http://www.w3.org/1999/xhtml); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/at-rule/namespace/input.css:3:12 | 3 | @namespace url(http://www.w3.org/1999/xhtml); @@ -131,7 +129,7 @@ error: Ident 4 | @namespace svg url(http://www.w3.org/2000/svg); | ^^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/at-rule/namespace/input.css:4:16 | 4 | @namespace svg url(http://www.w3.org/2000/svg); @@ -161,7 +159,7 @@ error: NamespaceRule 5 | @namespace "http://www.w3.org/1999/xhtml"; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/at-rule/namespace/input.css:5:12 | 5 | @namespace "http://www.w3.org/1999/xhtml"; @@ -197,7 +195,7 @@ error: Ident 6 | @namespace svg "http://www.w3.org/2000/svg"; | ^^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/at-rule/namespace/input.css:6:16 | 6 | @namespace svg "http://www.w3.org/2000/svg"; diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/IX2tz8hkGmrHq2cazP46_A/output.json b/crates/swc_css_parser/tests/fixture/esbuild/misc/IX2tz8hkGmrHq2cazP46_A/output.json index 9cfc53cc2b1..cde1dbd14e8 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/IX2tz8hkGmrHq2cazP46_A/output.json +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/IX2tz8hkGmrHq2cazP46_A/output.json @@ -23,7 +23,7 @@ "value": "ns", "raw": "\\6es" }, - "value": { + "uri": { "type": "String", "span": { "start": 16, diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/IX2tz8hkGmrHq2cazP46_A/span.rust-debug b/crates/swc_css_parser/tests/fixture/esbuild/misc/IX2tz8hkGmrHq2cazP46_A/span.rust-debug index 53312cd23ec..fd1e349a978 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/IX2tz8hkGmrHq2cazP46_A/span.rust-debug +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/IX2tz8hkGmrHq2cazP46_A/span.rust-debug @@ -28,7 +28,7 @@ error: Ident 1 | @namespace \6es 'path'; | ^^^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/esbuild/misc/IX2tz8hkGmrHq2cazP46_A/input.css:1:17 | 1 | @namespace \6es 'path'; diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/JygAjyd3aaFifbGpuMgJOA/output.json b/crates/swc_css_parser/tests/fixture/esbuild/misc/JygAjyd3aaFifbGpuMgJOA/output.json index 22d47f93ddd..0c8095c61e3 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/JygAjyd3aaFifbGpuMgJOA/output.json +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/JygAjyd3aaFifbGpuMgJOA/output.json @@ -23,7 +23,7 @@ "value": ",s", "raw": "\\,s" }, - "value": { + "uri": { "type": "String", "span": { "start": 15, diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/JygAjyd3aaFifbGpuMgJOA/span.rust-debug b/crates/swc_css_parser/tests/fixture/esbuild/misc/JygAjyd3aaFifbGpuMgJOA/span.rust-debug index 3fabe7b93c1..234dc99dd30 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/JygAjyd3aaFifbGpuMgJOA/span.rust-debug +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/JygAjyd3aaFifbGpuMgJOA/span.rust-debug @@ -28,7 +28,7 @@ error: Ident 1 | @namespace \,s 'p\61th'; | ^^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/esbuild/misc/JygAjyd3aaFifbGpuMgJOA/input.css:1:16 | 1 | @namespace \,s 'p\61th'; diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/Zb1jcH156xr4eFiUgmg-jg/output.json b/crates/swc_css_parser/tests/fixture/esbuild/misc/Zb1jcH156xr4eFiUgmg-jg/output.json index cdd1ada1b9e..c52508c1ccf 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/Zb1jcH156xr4eFiUgmg-jg/output.json +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/Zb1jcH156xr4eFiUgmg-jg/output.json @@ -23,7 +23,7 @@ "value": "ns", "raw": "ns" }, - "value": { + "uri": { "type": "String", "span": { "start": 16, diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/Zb1jcH156xr4eFiUgmg-jg/span.rust-debug b/crates/swc_css_parser/tests/fixture/esbuild/misc/Zb1jcH156xr4eFiUgmg-jg/span.rust-debug index bbd31f3aec4..6b34c8d5b1b 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/Zb1jcH156xr4eFiUgmg-jg/span.rust-debug +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/Zb1jcH156xr4eFiUgmg-jg/span.rust-debug @@ -28,7 +28,7 @@ error: Ident 1 | @n\61mespace ns 'path'; | ^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/esbuild/misc/Zb1jcH156xr4eFiUgmg-jg/input.css:1:17 | 1 | @n\61mespace ns 'path'; diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/a-wik1bC7k04EzrSSB0gcw/output.json b/crates/swc_css_parser/tests/fixture/esbuild/misc/a-wik1bC7k04EzrSSB0gcw/output.json index 2fcf6f5d219..7238ef0f591 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/a-wik1bC7k04EzrSSB0gcw/output.json +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/a-wik1bC7k04EzrSSB0gcw/output.json @@ -23,7 +23,7 @@ "value": ",s", "raw": "\\2cs" }, - "value": { + "uri": { "type": "String", "span": { "start": 16, diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/a-wik1bC7k04EzrSSB0gcw/span.rust-debug b/crates/swc_css_parser/tests/fixture/esbuild/misc/a-wik1bC7k04EzrSSB0gcw/span.rust-debug index 89ce5273ed5..b50390bd193 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/a-wik1bC7k04EzrSSB0gcw/span.rust-debug +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/a-wik1bC7k04EzrSSB0gcw/span.rust-debug @@ -28,7 +28,7 @@ error: Ident 1 | @namespace \2cs 'p\61th'; | ^^^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/esbuild/misc/a-wik1bC7k04EzrSSB0gcw/input.css:1:17 | 1 | @namespace \2cs 'p\61th'; diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/um4N0M2i5kY73ExgnkIV9g/output.json b/crates/swc_css_parser/tests/fixture/esbuild/misc/um4N0M2i5kY73ExgnkIV9g/output.json index 0e66843e7b0..4fbcce097a8 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/um4N0M2i5kY73ExgnkIV9g/output.json +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/um4N0M2i5kY73ExgnkIV9g/output.json @@ -23,7 +23,7 @@ "value": "ns", "raw": "ns" }, - "value": { + "uri": { "type": "String", "span": { "start": 14, diff --git a/crates/swc_css_parser/tests/fixture/esbuild/misc/um4N0M2i5kY73ExgnkIV9g/span.rust-debug b/crates/swc_css_parser/tests/fixture/esbuild/misc/um4N0M2i5kY73ExgnkIV9g/span.rust-debug index 5115072bbad..3aede2c2faa 100644 --- a/crates/swc_css_parser/tests/fixture/esbuild/misc/um4N0M2i5kY73ExgnkIV9g/span.rust-debug +++ b/crates/swc_css_parser/tests/fixture/esbuild/misc/um4N0M2i5kY73ExgnkIV9g/span.rust-debug @@ -28,7 +28,7 @@ error: Ident 1 | @namespace ns 'p\61th'; | ^^ -error: NamespaceValue +error: NamespaceUri --> $DIR/tests/fixture/esbuild/misc/um4N0M2i5kY73ExgnkIV9g/input.css:1:15 | 1 | @namespace ns 'p\61th'; diff --git a/crates/swc_css_visit/src/lib.rs b/crates/swc_css_visit/src/lib.rs index 6825ffcfbb2..9391aedc606 100644 --- a/crates/swc_css_visit/src/lib.rs +++ b/crates/swc_css_visit/src/lib.rs @@ -347,15 +347,15 @@ define!({ pub block: Block, } - pub enum NamespaceValue { + pub enum NamespaceUri { Url(UrlValue), Str(Str), } pub struct NamespaceRule { pub span: Span, - pub prefix: Ident, - pub value: NamespaceValue, + pub prefix: Option, + pub uri: NamespaceUri, } pub struct ViewportRule {