swc-project/swc · error · Error
Expected 'n' character in Ident
Error message
Expected 'n' character in Ident
What it means
In the An+B microsyntax of :nth-* selectors, the ident at the `n` position must begin with (an optional `-` followed by) `n` or `N`; otherwise the parser rejects it with this error at the token's span.
Source
Thrown at crates/swc_css_parser/src/parser/selectors/mod.rs:1271
let a;
let a_raw;
let n_value;
match cur!(self) {
Token::Ident { .. } => {
let ident_value = match bump!(self) {
Token::Ident { value, .. } => value,
_ => {
unreachable!();
}
};
let has_minus_sign = ident_value.starts_with('-');
let n_char = if has_minus_sign { ident_value.chars().nth(1) } else { ident_value.chars().next() };
if n_char != Some('n') && n_char != Some('N') {
return Err(Error::new(
span,
ErrorKind::Expected("'n' character in Ident"),
));
}
a = Some(if has_minus_sign { -1 } else {1 });
a_raw = Some(self.input.atom(if has_plus_sign { "+" } else if has_minus_sign { "-" } else { "" }));
n_value = if has_minus_sign { ident_value[1..].to_string() } else { ident_value.to_string() };
}
tok!("dimension") => {
let dimension = match bump!(self) {
Token::Dimension { dimension } => {
let DimensionToken { value, raw_value, unit, .. } = *dimension;
(value, raw_value, unit)
}
_ => {View on GitHub (pinned to 5176682b65)
Solutions
- Use valid An+B forms like 2n, -n+3, odd, even
- Collect parser errors for diagnostics
- Fix the nth- expression
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/swc_css_parser/src/parser/selectors/mod.rs:1271 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/369d6aea65e0536f.
Report an issue: GitHub.