fix(css/ast): Fix type definition of @namespace (#2919)

This commit is contained in:
Alexander Akait 2021-12-01 12:29:25 +03:00 committed by GitHub
parent 563c63c225
commit 0f4ad0f8c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 48 additions and 85 deletions

View File

@ -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<Ident>,
pub uri: NamespaceUri,
}
#[ast_node("ViewportRule")]

View File

@ -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]

View File

@ -374,20 +374,15 @@ where
{
fn parse(&mut self) -> PResult<NamespaceRule> {
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,
});
}
}

View File

@ -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);

View File

@ -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,

View File

@ -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";

View File

@ -23,7 +23,7 @@
"value": "ns",
"raw": "\\6es"
},
"value": {
"uri": {
"type": "String",
"span": {
"start": 16,

View File

@ -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';

View File

@ -23,7 +23,7 @@
"value": ",s",
"raw": "\\,s"
},
"value": {
"uri": {
"type": "String",
"span": {
"start": 15,

View File

@ -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';

View File

@ -23,7 +23,7 @@
"value": "ns",
"raw": "ns"
},
"value": {
"uri": {
"type": "String",
"span": {
"start": 16,

View File

@ -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';

View File

@ -23,7 +23,7 @@
"value": ",s",
"raw": "\\2cs"
},
"value": {
"uri": {
"type": "String",
"span": {
"start": 16,

View File

@ -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';

View File

@ -23,7 +23,7 @@
"value": "ns",
"raw": "ns"
},
"value": {
"uri": {
"type": "String",
"span": {
"start": 14,

View File

@ -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';

View File

@ -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<Ident>,
pub uri: NamespaceUri,
}
pub struct ViewportRule {