Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Each line has one grammar owner.
postcss is never one, and neither is "Prettier prints it".

Changing acceptance is additive: it only turns errors into parses, with a comment citing the owner, a test pinning the strict shape, and the conformance snapshot flip in the PR.
The one exception is a parse the owner itself rejects: turning it into an error is a bug fix, with the owner's rejection cited in the PR and a `tests/error/**` fixture pinning it.
Changing how already-accepted input is represented is a bug fix or a refactor, and the PR says which.

### CSS
Expand Down Expand Up @@ -56,6 +57,8 @@ No postcss-less leniency.
- Variable and property names are `[\w-]+` (`@-`, `@1`, `$-`), not CSS idents
- A string interpolates only on an exact `@{name}` / `${name}`; `"@{box"` and the outer `@{box-` of `"@{box-@{suffix}}"` are text
- An unquoted `url()` body runs to `)`, whitespace and newlines included
- A mixin path without parens or lookups is no call in a value (`mixin.call(inValue)`): a normal property rejects it,
a variable keeps it as text (`@v: .a .b`), flagged `value_is_raw`
- A variable declaration must reach `;` / `}`: `@page :first { ... }` is an at-rule, and the permissive `@this: () => { ... };` read is tried only when no typed value parses

### Block contents
Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_css_parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,9 @@ pub struct LessVariableDeclaration<'a> {
pub name: LessVariable<'a>,
pub colon_span: Span,
pub value: ComponentValue<'a>,
/// No typed value parsed, so `value` is the raw token run less.js `permissiveValue`
/// keeps as text: `@{name}` substitutes it as written.
pub value_is_raw: bool,
}

#[derive(Debug)]
Expand Down
24 changes: 13 additions & 11 deletions crates/oxc_css_parser/src/parser/less.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ impl<'a> Parser<'a> {
lookups,
span,
})))
} else if mixin_call.args.is_none() {
// less.js `mixin.call(inValue)`: without parens or lookups it is no call in a value.
// A variable then keeps it as text (`permissiveValue`): `@v: .a .b` is a selector for `@{v}`
Err(Error { kind: ErrorKind::UnexpectedLessMixinCall, span: mixin_call.span })
} else {
Ok(ComponentValue::LessMixinCall(self.alloc(mixin_call)))
}
Expand Down Expand Up @@ -1840,8 +1844,8 @@ impl<'a> Parse<'a> for LessVariableDeclaration<'a> {

let name = input.parse::<LessVariable>()?;
let (_, colon_span) = input.cursor.expect_colon()?;
let value = if matches!(input.cursor.peek()?.token, Token::LBrace(..)) {
ComponentValue::LessDetachedRuleset(input.parse()?)
let (value, value_is_raw) = if matches!(input.cursor.peek()?.token, Token::LBrace(..)) {
(ComponentValue::LessDetachedRuleset(input.parse()?), false)
} else {
let typed = input.try_parse(|p| {
p.with_state(ParserState {
Expand All @@ -1850,8 +1854,8 @@ impl<'a> Parse<'a> for LessVariableDeclaration<'a> {
})
.parse_maybe_less_list(/* allow_comma */ true)
});
let value = match typed {
Ok(value) => value,
let (value, value_is_raw) = match typed {
Ok(value) => (value, false),
// No typed value: only then does less.js `permissiveValue` take any
// balanced token run (`@this: () => { ... };`), and only one ending in `;`
Err(error) => {
Expand All @@ -1865,11 +1869,9 @@ impl<'a> Parse<'a> for LessVariableDeclaration<'a> {
let Some(end) = end else {
return Err(error);
};
ComponentValue::LessList(LessList {
elements: values,
comma_spans: None,
span: Span { start, end },
})
let list =
LessList { elements: values, comma_spans: None, span: Span { start, end } };
(ComponentValue::LessList(list), true)
}
};
// less.js `declaration`: a value that stops short of the statement boundary is
Expand All @@ -1882,10 +1884,10 @@ impl<'a> Parse<'a> for LessVariableDeclaration<'a> {
let span = input.cursor.peek()?.span;
return Err(Error { kind: ErrorKind::TryParseError, span });
}
value
(value, value_is_raw)
};
let span = Span { start: name.span.start, end: value.span().end };
Ok(LessVariableDeclaration { span, name, colon_span, value })
Ok(LessVariableDeclaration { span, name, colon_span, value, value_is_raw })
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a { b: .c; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/oxc_css_parser/tests/error.rs
---
error: expect token `(`, but found `{`
┌─ bare-call-in-value.less:1:3
│
1 │ a { b: .c; }
│ ^
24 changes: 24 additions & 0 deletions crates/oxc_css_parser/tests/less_ast_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,27 @@ fn percentage_is_a_selector_outside_keyframes() {
block.statements
);
}

// A mixin path without parens or lookups is no call in a value (less.js `mixin.call(inValue)`):
// a variable keeps it as text, so the gap in `.a .b` is still a descendant combinator for `@{v}`.
#[test]
fn bare_mixin_path_in_variable_value_is_raw() {
let code = "@v: .a .b;\n@p: .a:hover > .b;\n@c: .ns .m();\n@l: .ns.m[@r];";
let ss = parse_less(code);
let decl = |i: usize| {
let Statement::LessVariableDeclaration(decl) = &ss.statements[i] else {
panic!("expected a variable declaration, got {:?}", ss.statements[i]);
};
decl
};
assert!(decl(0).value_is_raw);
assert_eq!(text(code, decl(0).value.span()), ".a .b");
assert!(decl(1).value_is_raw);
assert_eq!(text(code, decl(1).value.span()), ".a:hover > .b");

// With parens or a lookup it is a call
assert!(!decl(2).value_is_raw);
assert!(matches!(decl(2).value, ComponentValue::LessMixinCall(..)));
assert!(!decl(3).value_is_raw);
assert!(matches!(decl(3).value, ComponentValue::LessNamespaceValue(..)));
}
Loading