Skip to content

feat: validate_block/2 and line/column for every validation error - #49

Merged
shahryarjb merged 7 commits into
mainfrom
feat/declaration-diagnostics
Nov 1, 2026
Merged

shahryarjb merged 7 commits into
mainfrom
feat/declaration-diagnostics

Conversation

@shahryarjb

@shahryarjb shahryarjb commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Adds IgniterCss.validate_block/2 for one element's CSS body, and gives every validation error a line and column. Needs it to check an element's Custom CSS with a parser instead of hand-written checks.

Problem Before Now Test it
No way to check the body of one rule (what an element's CSS editor holds) Callers wrap the body in x { … } themselves, and error positions point into the wrapper validate_block/2 reads the body as a block's contents; errors are placed in the body itself IgniterCss.validate_block("padding: 4px;\n&:hover { color: red; }") → {:ok, %{errors: []}}
Errors have no position Validation has only a message errors: [%IgniterCss.Diagnostic{message, line, column, offset, text}], from Biome's own diagnostics validate_block("gap: 1px;\ncolor: red background: blue;") → line 2, column 22, text ": blue"
A } escapes the block "gap: 1px; } body { display: none; " passes when wrapped, so the rest leaks into the stylesheet Refused at the } (line 1, column 11) the example in the validate_block/2 doc
Biome 0.5.8 refuses valid CSS --x: a:b, filter: progid:…, and a;;; are errors Accepted (found in the CST, kept only while brackets close) validate("a { --x: a:b; }") → {:ok, _}
@import inside a block Biome parses it without an error @import, @charset, @namespace inside a block are errors validate_block("@import 'x.css';") → {:error, _}
At-rules below the top level can't be listed or removed get_at_rules/4 and remove_at_rule/4 see top-level at-rules only; one the parser could not place (an @import inside @font-face) is invisible list_at_rules/2 returns every at-rule in source order, nested or unplaced, none from a comment, string or url(); remove_at_rules/3 removes the picked ones wherever they are, and a second run changes nothing list_at_rules(~s|@media print { @import "a.css"; }|) → media, then import with target a.css
A body that ends unfinished gets errors about the wrapper validate_block("color crimson") → line 1, column 14, no text, "Expected a compound selector but instead found '}'."; "color crimson;" → "expected , but instead found ;" One error on what the author wrote: line 1, column 1, text color crimson, "Expected a : after the property name color". Other unfinished statements: "Expected a { after the selector &:hover", "This declaration is not finished"; a block left open: "Expected a } to close the { on line 1, column 9" at its end validate_block("gap: 1px;\ncolor crimson") → line 2, column 1, text color crimson
An @apply with no ; last in a block is refused validate_block("@apply p-4") → "This @apply rule is not finished"; validate(".a { @apply p-4 }\n.b { color: red; }") → "expected } but instead the file ends", and Biome misreads everything after the } Accepted, as CSS Syntax and Tailwind read it: the } is found in the CST, the text is parsed again with a ; before it, and later errors keep their place. What the prelude says is left to Tailwind (see the at-rule row below) validate_block("padding: 4px;\n@apply p-4") → {:ok, %{errors: []}}
A string with no closing quote in a selector is reported as a missing {, and messages carry raw line breaks validate_block("&[data-x=\"a\nb\"] { gap: 1px; }") → line 1, column 1, "Expected a { after the selector …", a line break inside the message; validate_block("&:hover\n.b") puts a line break in its message too Biome's "Missing closing quote" at line 1, column 10, text "a, wins over what Biome misreads after it. A message igniter_css writes shows a line break in the author's text as \n, as text reads when inspected validate_block("&[data-x=\"a\nb\"] { gap: 1px; }") → first error line 1, column 10, "Missing closing quote"; validate_block("&:hover\n.b") → "Expected a { after the selector &:hover\\n.b"
A string left open in a custom property passes, and a misread rule's text runs into its block's } validate_block("--x: \"abc") → {:ok, _}, since a custom property's value is exempt from Biome's errors; validate_block("&:hover { color navy }") → text color navy } --x: "abc → "Missing closing quote" at line 1, column 6 (a : in the value is still accepted); the text is color navy validate_block("--x: \"abc") → {:error, _} with "Missing closing quote"; validate_block("&:hover { color navy }") → text color navy
An at-rule ended by its block's } is refused unless its prelude is on one line with no : validate(".a { @apply p-4 gap: 1px }"), .a { @foo (min-width: 1px) } and .a { @apply foo(a: b) } → error at the end of the file Valid, as CSS Syntax reads them: a } outside the prelude's brackets ends the at-rule. @apply foo(a } still runs on, its } inside the ( validate(".a { @apply p-4 gap: 1px }") → {:ok, _}; validate_block("@apply foo(a") → {:error, _}
An @ with no name is listed as an at-rule list_at_rules(".a { @; color: red; }") → an at-rule named ; with text @; color: red;, which remove_at_rules/3 would delete An @ starts an at-rule only with an identifier or keyword against it: nothing is listed and color: red; stays list_at_rules(".a { @; color: red; }") → {:ok, []}
A line ended by \r alone is not counted validate_block("gap: 1px;\rcolor crimson") → line 1 Lines and columns are Biome's own (SourceFile::location): \n, \r\n and \r all end a line same call → line 2, column 1
Bogus nodes are told apart with a hand-copied list of 8 kinds ctx::is_bogus misses 11 of Biome's bogus kinds Biome's own SyntaxKind::is_bogus (all 19) cargo test

Checks: 399 Elixir tests (50 doctests), 479 cargo tests, clippy, fmt and credo pass. All Hex deps are already at their latest; the only new crate is biome_diagnostics 0.5.8, already in the graph through biome_css_parser.

🤖 Generated with Claude Code

shahryarjb and others added 7 commits October 31, 2026 22:27
… error carries its line and column

`IgniterCss.Validation` gains `errors`, a list of `IgniterCss.Diagnostic`
(message, 1-based line and column, byte offset, and the text it is about),
read from Biome's own parse diagnostics. Its message names the first.

`validate_block/2` reads a body as the contents of one rule, the way an
editor for a single element takes it, and places every error in that body.
A `}` that closes the block early is an error there, though what follows
it may parse.

Both now leave out errors Biome 0.5.8 raises against valid CSS: a `:` in a
custom property's value (`--x: a:b`), a legacy `progid:` filter, and a run
of stray `;`. Each is found in the CST and kept only while its brackets
close. Both report an `@import`, `@charset` or `@namespace` written inside
a block, which Biome parses without an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…th, ones the parser could not place included

`list_at_rules/2` returns every at-rule in the file as `IgniterCss.AtRule`,
in source order: nested in `@media`, `@layer`, a rule or another
at-rule's block, and where Biome could not place one, such as an
`@import` inside `@font-face` or a keyframe. Biome keeps that one as
bogus tokens, but Tailwind still imports the file it names, so it is read
from the tokens after its `@` up to the `;` or block that ends it. An `@`
counts only where a statement starts (first, or after `;`, `{` or `}`),
so one in a string, a comment or `url(@x.png)` starts nothing.

`remove_at_rules/3` removes, wherever they are, the at-rules written
exactly as ones `list_at_rules/2` returned, with the comments they own.
Matched on the text, so a second run changes nothing; one inside another
being removed goes with it, and braces that do not balance are refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… what the author wrote

No error points into the block a body is read inside or names its `}`.
A declaration with no `:`, which Biome reads as a nested rule (`color
crimson;`), is one error on that text: "Expected a `:` after the property
name `color`". Another statement the body ends in the middle of is one
error on it ("Expected a `{` after the selector `&:hover`", "This
declaration is not finished"), and a block left open is one error where
its `}` is missing, naming the `{` it lacks. Messages igniter_css writes
quote in backticks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…itten last in a block

CSS Syntax ends an at-rule with no block at the `}` of the block it is in,
and Tailwind takes `.a { @apply p-4 }`. Biome 0.5.8 reads that `}`, and
what follows up to a `;` or `{`, into the at-rule's prelude. That `}` is
found in the CST, the text is parsed again with a `;` before it, one at a
time in source order, and errors are placed back in the text as given.

Only for a prelude on one line, with its brackets closed and no `:`
followed by a space: `@apply p-4` then `color: red`, or
`@apply p-4 gap: 1px`, is another statement with its `;` missing and
stays an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… messages quote the author's text on one line

A string with no closing quote in a selector, `&[data-x="a` then `b"] { … }`,
is Biome's "Missing closing quote" at that quote again. The lexer ends the
string at the line's end and Biome reads the rest, the block included, as one
rule with no `{`; that rule is no longer reported as "Expected a `{` after the
selector …". A token the lexer could not read keeps its own error in a rule
read as a declaration with no `:` and in a statement the body ends in; one
after a misread rule's `;` keeps its error next to that one.

A message igniter_css writes quotes the author's text with a line break in it
as `\n` (or `\r`), as `text` reads when inspected, and the validation summary
does the same with the first error's message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ustom property, and a misread rule's text ends at its block's `}`

A custom property's value still takes any run of tokens, a `:` included,
but not a string the lexer could not end: `--x: "abc` is Biome's "Missing
closing quote" on `"abc`, as CSS Syntax reads it as a bad string. The string
is found as an error token in the CST that opens with a quote and spans the
error.

A declaration with no `:` written last in a block, `&:hover { color navy }`,
is named on `color navy`: the `}` Biome reads into its selector closes the
block it is in and is no longer part of the error's text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eads it, an `@` with no name starts none, and a line ends at `\r` too

* An at-rule written last in a block with no `;` ends at the block's `}`
  whenever that `}` is outside the prelude's brackets, as CSS Syntax reads
  it: `.a { @apply p-4 gap: 1px }`, `.a { @foo (min-width: 1px) }` and
  `.a { @apply foo(a: b) }` are valid, and a prelude spanning lines is too.
  What the prelude says is the at-rule's to check (Tailwind for `@apply`).
  `@apply foo(a }` still runs on, its `}` inside the `(`.
* An `@` the parser could not place starts an at-rule only when an
  identifier or a keyword Biome lexes as one is written against it.
  `.a { @; color: red; }` lists nothing, and `remove_at_rules/3` keeps
  `color: red;`.
* An error's line and column are Biome's own (`SourceFile::location`), so a
  line ends at `\n`, `\r\n` or `\r`.
* Bogus nodes are told apart with Biome's `SyntaxKind::is_bogus`, all 19 of
  its kinds, not a copy of 8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shahryarjb
shahryarjb merged commit 10734cd into main Nov 1, 2026
26 checks passed
@shahryarjb
shahryarjb deleted the feat/declaration-diagnostics branch November 1, 2026 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant