Skip to content

Commit

Permalink
feat(css/parser): normalize and improve function name
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 15, 2022
1 parent fc6ed6b commit 0245890
Show file tree
Hide file tree
Showing 32 changed files with 873 additions and 532 deletions.
Empty file modified crates/swc_atoms/scripts/add-from-cargo-check.sh 100644 → 100755
Empty file.
263 changes: 263 additions & 0 deletions crates/swc_atoms/words.txt

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion crates/swc_css_ast/src/base.rs
Expand Up @@ -100,12 +100,21 @@ impl Take for SimpleBlock {
}
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum FunctionName {
#[tag("Ident")]
Ident(Ident),
#[tag("DashedIdent")]
DashedIdent(DashedIdent),
}

#[ast_node("Function")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct Function {
/// Span starting from the `lo` of identifier and to the end of `)`.
pub span: Span,
pub name: Ident,
pub name: FunctionName,
pub value: Vec<ComponentValue>,
}

Expand Down
39 changes: 24 additions & 15 deletions crates/swc_css_parser/src/parser/input.rs
@@ -1,8 +1,8 @@
use std::{borrow::Cow, fmt::Debug, mem::take};

use swc_atoms::{Atom, JsWord};
use swc_atoms::Atom;
use swc_common::{BytePos, Span, Spanned, SyntaxContext};
use swc_css_ast::{ComponentValue, ListOfComponentValues, Token, TokenAndSpan};
use swc_css_ast::{ComponentValue, FunctionName, ListOfComponentValues, Token, TokenAndSpan};

use super::PResult;
use crate::error::{Error, ErrorKind};
Expand Down Expand Up @@ -224,7 +224,7 @@ type SpanLike = (BytePos, BytePos);
#[derive(Debug)]
enum TokenOrBlock<'a> {
Token(&'a TokenAndSpan),
Function(Box<(Span, JsWord, Atom)>),
Function(Box<(Span, FunctionName)>),
LBracket(SpanLike),
LParen(SpanLike),
LBrace(SpanLike),
Expand Down Expand Up @@ -279,11 +279,7 @@ impl<'a> Input<'a> {
function.name.span_hi() + BytePos(1),
Default::default(),
),
function.name.value.clone(),
match &function.name.raw {
Some(raw) => raw.clone(),
_ => Atom::from(function.name.value.clone()),
},
function.name.clone(),
))));
}

Expand Down Expand Up @@ -381,13 +377,26 @@ impl<'a> Input<'a> {
TokenOrBlock::Token(token_and_span) => {
return Ok(Cow::Borrowed(token_and_span))
}
TokenOrBlock::Function(function) => TokenAndSpan {
span: function.0,
token: Token::Function {
value: function.1,
raw: function.2,
},
},
TokenOrBlock::Function(function) => {
let name = match function.1 {
FunctionName::Ident(name) => match name.raw {
Some(raw) => (name.value, raw),
_ => (name.value.clone(), Atom::from(name.value)),
},
FunctionName::DashedIdent(name) => match name.raw {
Some(raw) => (name.value, raw),
_ => (name.value.clone(), Atom::from(name.value)),
},
};

TokenAndSpan {
span: function.0,
token: Token::Function {
value: name.0,
raw: name.1,
},
}
}
TokenOrBlock::LBracket(span) => TokenAndSpan {
span: Span::new(span.0, span.1, Default::default()),
token: Token::LBracket,
Expand Down
41 changes: 28 additions & 13 deletions crates/swc_css_parser/src/parser/syntax/mod.rs
Expand Up @@ -148,7 +148,8 @@ where
unreachable!()
}
};
let name = if at_keyword_name.0.starts_with("--") {
let is_dashed_ident = at_keyword_name.0.starts_with("--");
let name = if is_dashed_ident {
AtRuleName::DashedIdent(DashedIdent {
span: Span::new(span.lo + BytePos(1), span.hi, Default::default()),
value: at_keyword_name.0,
Expand Down Expand Up @@ -195,7 +196,7 @@ where
at_rule.span = span!(self, span.lo);

// Canonicalization against a grammar
if self.ctx.need_canonicalize {
if !is_dashed_ident && self.ctx.need_canonicalize {
at_rule = self.canonicalize_at_rule_prelude(at_rule)?;
}

Expand All @@ -213,7 +214,7 @@ where
at_rule.span = span!(self, span.lo);

// Canonicalization against a grammar
if self.ctx.need_canonicalize {
if !is_dashed_ident && self.ctx.need_canonicalize {
at_rule = self.canonicalize_at_rule_prelude(at_rule)?;
at_rule = self.canonicalize_at_rule_block(at_rule)?;
}
Expand Down Expand Up @@ -604,16 +605,21 @@ where
//
// Return nothing.
let span = self.input.cur_span();
let is_dashed_ident = match cur!(self) {
Token::Ident { value, .. } => value.starts_with("--"),
let declaration_name = match cur!(self) {
Token::Ident { value, .. } => value,
_ => {
return Err(Error::new(span, ErrorKind::Expected("ident")));
}
};
let is_dashed_ident = declaration_name.starts_with("--");
let name = if is_dashed_ident {
DeclarationName::DashedIdent(self.parse()?)
let ident = self.parse()?;

DeclarationName::DashedIdent(ident)
} else {
DeclarationName::Ident(self.parse()?)
let ident = self.parse()?;

DeclarationName::Ident(ident)
};
let mut declaration = Declaration {
span: Default::default(),
Expand Down Expand Up @@ -918,16 +924,25 @@ where
// Create a function with its name equal to the value of the current input token
// and with its value initially set to an empty list.
let span = self.input.cur_span();
let ident = match bump!(self) {
let function_name = match bump!(self) {
Token::Function { value, raw } => (value, raw),
_ => {
unreachable!()
}
};
let name = Ident {
span: Span::new(span.lo, span.hi - BytePos(1), Default::default()),
value: ident.0,
raw: Some(ident.1),
let is_dashed_ident = function_name.0.starts_with("--");
let name = if is_dashed_ident {
FunctionName::DashedIdent(DashedIdent {
span: Span::new(span.lo, span.hi - BytePos(1), Default::default()),
value: function_name.0,
raw: Some(function_name.1),
})
} else {
FunctionName::Ident(Ident {
span: Span::new(span.lo, span.hi - BytePos(1), Default::default()),
value: function_name.0.to_ascii_lowercase(),
raw: Some(function_name.1),
})
};
let mut function = Function {
span: Default::default(),
Expand Down Expand Up @@ -970,7 +985,7 @@ where
function.span = span!(self, span.lo);

// Canonicalization against a grammar
if self.ctx.need_canonicalize {
if !is_dashed_ident && self.ctx.need_canonicalize {
function = self.canonicalize_function_value(function)?;
}

Expand Down
3 changes: 1 addition & 2 deletions crates/swc_css_parser/src/parser/util.rs
Expand Up @@ -267,11 +267,10 @@ where
&mut self,
mut function: Function,
) -> PResult<Function> {
let function_name = function.name.value.to_ascii_lowercase();
let locv = self.create_locv(function.value);

function.value = match self.parse_according_to_grammar(&locv, |parser| {
parser.parse_function_values(&function_name)
parser.parse_function_values(&function.name)
}) {
Ok(values) => values,
Err(err) => {
Expand Down

0 comments on commit 0245890

Please sign in to comment.