Repository navigation
feat: validate_block/2 and line/column for every validation error - #49
Merged
Merged
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
IgniterCss.validate_block/2for 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.x { … }themselves, and error positions point into the wrappervalidate_block/2reads the body as a block's contents; errors are placed in the body itselfIgniterCss.validate_block("padding: 4px;\n&:hover { color: red; }")→{:ok, %{errors: []}}Validationhas only a messageerrors: [%IgniterCss.Diagnostic{message, line, column, offset, text}], from Biome's own diagnosticsvalidate_block("gap: 1px;\ncolor: red background: blue;")→ line 2, column 22, text": blue"}escapes the block"gap: 1px; } body { display: none; "passes when wrapped, so the rest leaks into the stylesheet}(line 1, column 11)validate_block/2doc--x: a:b,filter: progid:…, anda;;;are errorsvalidate("a { --x: a:b; }")→{:ok, _}@importinside a block@import,@charset,@namespaceinside a block are errorsvalidate_block("@import 'x.css';")→{:error, _}get_at_rules/4andremove_at_rule/4see top-level at-rules only; one the parser could not place (an@importinside@font-face) is invisiblelist_at_rules/2returns every at-rule in source order, nested or unplaced, none from a comment, string orurl();remove_at_rules/3removes the picked ones wherever they are, and a second run changes nothinglist_at_rules(~s|@media print { @import "a.css"; }|)→media, thenimportwith targeta.cssvalidate_block("color crimson")→ line 1, column 14, no text, "Expected a compound selector but instead found '}'.";"color crimson;"→ "expected,but instead found;"color crimson, "Expected a:after the property namecolor". 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 endvalidate_block("gap: 1px;\ncolor crimson")→ line 2, column 1, textcolor crimson@applywith no;last in a block is refusedvalidate_block("@apply p-4")→ "This@applyrule is not finished";validate(".a { @apply p-4 }\n.b { color: red; }")→ "expected}but instead the file ends", and Biome misreads everything after 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: []}}{, and messages carry raw line breaksvalidate_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"a, wins over what Biome misreads after it. A message igniter_css writes shows a line break in the author's text as\n, astextreads when inspectedvalidate_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"}validate_block("--x: \"abc")→{:ok, _}, since a custom property's value is exempt from Biome's errors;validate_block("&:hover { color navy }")→ textcolor navy }--x: "abc→ "Missing closing quote" at line 1, column 6 (a:in the value is still accepted); the text iscolor navyvalidate_block("--x: \"abc")→{:error, _}with "Missing closing quote";validate_block("&:hover { color navy }")→ textcolor navy}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}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, _}@with no name is listed as an at-rulelist_at_rules(".a { @; color: red; }")→ an at-rule named;with text@; color: red;, whichremove_at_rules/3would delete@starts an at-rule only with an identifier or keyword against it: nothing is listed andcolor: red;stayslist_at_rules(".a { @; color: red; }")→{:ok, []}\ralone is not countedvalidate_block("gap: 1px;\rcolor crimson")→ line 1SourceFile::location):\n,\r\nand\rall end a linectx::is_bogusmisses 11 of Biome's bogus kindsSyntaxKind::is_bogus(all 19)cargo testChecks: 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_diagnostics0.5.8, already in the graph throughbiome_css_parser.🤖 Generated with Claude Code