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
22 changes: 22 additions & 0 deletions crates/biome_css_factory/src/generated/node_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions crates/biome_css_factory/src/generated/syntax_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/biome_css_formatter/src/css/any/at_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl FormatRule<AnyCssAtRule> for FormatAnyCssAtRule {
AnyCssAtRule::ScssDebugAtRule(node) => node.format().fmt(f),
AnyCssAtRule::ScssEachAtRule(node) => node.format().fmt(f),
AnyCssAtRule::ScssErrorAtRule(node) => node.format().fmt(f),
AnyCssAtRule::ScssForAtRule(node) => node.format().fmt(f),
AnyCssAtRule::ScssIfAtRule(node) => node.format().fmt(f),
AnyCssAtRule::ScssWarnAtRule(node) => node.format().fmt(f),
AnyCssAtRule::ScssWhileAtRule(node) => node.format().fmt(f),
Expand Down
38 changes: 38 additions & 0 deletions crates/biome_css_formatter/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7381,6 +7381,44 @@ impl IntoFormat<CssFormatContext> for biome_css_syntax::ScssExpression {
)
}
}
impl FormatRule<biome_css_syntax::ScssForAtRule>
for crate::scss::statements::for_at_rule::FormatScssForAtRule
{
type Context = CssFormatContext;
#[inline(always)]
fn fmt(
&self,
node: &biome_css_syntax::ScssForAtRule,
f: &mut CssFormatter,
) -> FormatResult<()> {
FormatNodeRule::<biome_css_syntax::ScssForAtRule>::fmt(self, node, f)
}
}
impl AsFormat<CssFormatContext> for biome_css_syntax::ScssForAtRule {
type Format<'a> = FormatRefWithRule<
'a,
biome_css_syntax::ScssForAtRule,
crate::scss::statements::for_at_rule::FormatScssForAtRule,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::scss::statements::for_at_rule::FormatScssForAtRule::default(),
)
}
}
impl IntoFormat<CssFormatContext> for biome_css_syntax::ScssForAtRule {
type Format = FormatOwnedWithRule<
biome_css_syntax::ScssForAtRule,
crate::scss::statements::for_at_rule::FormatScssForAtRule,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::scss::statements::for_at_rule::FormatScssForAtRule::default(),
)
}
}
impl FormatRule<biome_css_syntax::ScssIdentifier>
for crate::scss::value::identifier::FormatScssIdentifier
{
Expand Down
39 changes: 39 additions & 0 deletions crates/biome_css_formatter/src/scss/statements/for_at_rule.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::prelude::*;
use biome_css_syntax::{ScssForAtRule, ScssForAtRuleFields};
use biome_formatter::write;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatScssForAtRule;

impl FormatNodeRule<ScssForAtRule> for FormatScssForAtRule {
fn fmt_fields(&self, node: &ScssForAtRule, f: &mut CssFormatter) -> FormatResult<()> {
let ScssForAtRuleFields {
for_token,
variable,
from_token,
lower_bound,
operator,
upper_bound,
block,
} = node.as_fields();

write!(
f,
[
for_token.format(),
space(),
variable.format(),
space(),
from_token.format(),
space(),
lower_bound.format(),
space(),
operator.format(),
space(),
upper_bound.format(),
space(),
block.format()
Comment on lines +10 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Avoid a panic on recovered @for nodes.

Line 23 through Line 35 format SyntaxResult<_> fields directly. If the parser recovers a malformed or incomplete @for, this path can panic instead of returning a normal formatting error. Please resolve the fields with ? before write!.

🩹 Suggested fix
         let ScssForAtRuleFields {
             for_token,
             variable,
             from_token,
             lower_bound,
             operator,
             upper_bound,
             block,
         } = node.as_fields();
+        let for_token = for_token?;
+        let variable = variable?;
+        let from_token = from_token?;
+        let lower_bound = lower_bound?;
+        let operator = operator?;
+        let upper_bound = upper_bound?;
+        let block = block?;
 
         write!(
             f,
             [
                 for_token.format(),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let ScssForAtRuleFields {
for_token,
variable,
from_token,
lower_bound,
operator,
upper_bound,
block,
} = node.as_fields();
write!(
f,
[
for_token.format(),
space(),
variable.format(),
space(),
from_token.format(),
space(),
lower_bound.format(),
space(),
operator.format(),
space(),
upper_bound.format(),
space(),
block.format()
let ScssForAtRuleFields {
for_token,
variable,
from_token,
lower_bound,
operator,
upper_bound,
block,
} = node.as_fields();
let for_token = for_token?;
let variable = variable?;
let from_token = from_token?;
let lower_bound = lower_bound?;
let operator = operator?;
let upper_bound = upper_bound?;
let block = block?;
write!(
f,
[
for_token.format(),
space(),
variable.format(),
space(),
from_token.format(),
space(),
lower_bound.format(),
space(),
operator.format(),
space(),
upper_bound.format(),
space(),
block.format()
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/biome_css_formatter/src/scss/statements/for_at_rule.rs` around lines
10 - 35, The code is formatting SyntaxResult<_> fields directly and can panic on
recovered/malformed `@for` nodes; after calling node.as_fields()
(ScssForAtRuleFields), `?`-resolve each SyntaxResult field (e.g., lower_bound,
upper_bound, operator, block, variable, from_token, for_token) into concrete
values before calling write! so any parse errors return early instead of causing
a panic; update the code to bind resolved values (let lower_bound =
lower_bound?; etc.) and then pass those resolved values into the write! call
that formats the for_at_rule.

]
)
}
}
1 change: 1 addition & 0 deletions crates/biome_css_formatter/src/scss/statements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub(crate) mod debug_at_rule;
pub(crate) mod each_at_rule;
pub(crate) mod error_at_rule;
pub(crate) mod for_at_rule;
pub(crate) mod if_at_rule;
pub(crate) mod warn_at_rule;
pub(crate) mod while_at_rule;
17 changes: 17 additions & 0 deletions crates/biome_css_formatter/tests/specs/css/scss/at-rule/for.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.a{
@for $i from 1 through 3{
width:$i * 1px;
}
}

.b{
@for
$step
from
$start+1
to
$end - 1
{
margin:$step;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: css/scss/at-rule/for.scss
---

# Input

```scss
.a{
@for $i from 1 through 3{
width:$i * 1px;
}
}

.b{
@for
$step
from
$start+1
to
$end - 1
{
margin:$step;
}
}

```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Quote style: Double Quotes
Trailing newline: true
-----

```scss
.a {
@for $i from 1 through 3 {
width: $i * 1px;
}
}

.b {
@for $step from $start + 1 to $end - 1 {
margin: $step;
}
}

```
2 changes: 2 additions & 0 deletions crates/biome_css_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ impl<'src> CssLexer<'src> {
b"important" => IMPORTANT_KW,
b"from" => FROM_KW,
b"to" => TO_KW,
b"through" => THROUGH_KW,
b"var" => VAR_KW,
b"highlight" => HIGHLIGHT_KW,
b"part" => PART_KW,
Expand Down Expand Up @@ -838,6 +839,7 @@ impl<'src> CssLexer<'src> {
b"debug" => DEBUG_KW,
b"warn" => WARN_KW,
b"error" => ERROR_KW,
b"for" => FOR_KW,
b"while" => WHILE_KW,
b"sass" => SASS_KW,
b"style" => STYLE_KW,
Expand Down
8 changes: 6 additions & 2 deletions crates/biome_css_parser/src/lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ fn cdo_and_cdc() {
#[test]
fn keywords() {
assert_lex! {
"media keyframes important from sass each debug warn error",
"media keyframes important from through sass each debug warn error for",
MEDIA_KW:5,
WHITESPACE:1,
KEYFRAMES_KW:9,
Expand All @@ -318,6 +318,8 @@ fn keywords() {
WHITESPACE:1,
FROM_KW:4,
WHITESPACE:1,
THROUGH_KW:7,
WHITESPACE:1,
SASS_KW:4,
WHITESPACE:1,
EACH_KW:4,
Expand All @@ -326,7 +328,9 @@ fn keywords() {
WHITESPACE:1,
WARN_KW:4,
WHITESPACE:1,
ERROR_KW:5
ERROR_KW:5,
WHITESPACE:1,
FOR_KW:3
}
}

Expand Down
7 changes: 5 additions & 2 deletions crates/biome_css_parser/src/syntax/at_rule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ use crate::syntax::CssSyntaxFeatures;
use crate::syntax::parse_error::{expected_any_at_rule, tailwind_disabled};
use crate::syntax::scss::{
parse_bogus_scss_else_at_rule, parse_scss_debug_at_rule, parse_scss_each_at_rule,
parse_scss_error_at_rule, parse_scss_if_at_rule, parse_scss_warn_at_rule,
parse_scss_while_at_rule,
parse_scss_error_at_rule, parse_scss_for_at_rule, parse_scss_if_at_rule,
parse_scss_warn_at_rule, parse_scss_while_at_rule,
};
use biome_css_syntax::CssSyntaxKind::*;
use biome_css_syntax::T;
Expand Down Expand Up @@ -142,6 +142,9 @@ pub(crate) fn parse_any_at_rule(p: &mut CssParser) -> ParsedSyntax {
T![each] => CssSyntaxFeatures::Scss
.parse_supported_syntax(p, parse_scss_each_at_rule)
.or_else(|| parse_unknown_at_rule(p)),
T![for] => CssSyntaxFeatures::Scss
.parse_supported_syntax(p, parse_scss_for_at_rule)
.or_else(|| parse_unknown_at_rule(p)),
T![if] => CssSyntaxFeatures::Scss
.parse_supported_syntax(p, parse_scss_if_at_rule)
.or_else(|| parse_unknown_at_rule(p)),
Expand Down
Loading
Loading