diff --git a/crates/biome_grit_formatter/src/grit/auxiliary/sequential.rs b/crates/biome_grit_formatter/src/grit/auxiliary/sequential.rs index fd7152a66fb8..fc945446b178 100644 --- a/crates/biome_grit_formatter/src/grit/auxiliary/sequential.rs +++ b/crates/biome_grit_formatter/src/grit/auxiliary/sequential.rs @@ -1,10 +1,29 @@ use crate::prelude::*; use biome_grit_syntax::GritSequential; -use biome_rowan::AstNode; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritSequential; impl FormatNodeRule for FormatGritSequential { fn fmt_fields(&self, node: &GritSequential, f: &mut GritFormatter) -> FormatResult<()> { format_verbatim_node(node.syntax()).fmt(f) + + // TODO: investigate the verbatim panic when this code runs + // let GritSequentialFields { + // l_curly_token, + // sequential_token, + // sequential, + // r_curly_token, + // } = node.as_fields(); + // + // write!( + // f, + // [ + // sequential_token.format(), + // space(), + // l_curly_token.format(), + // sequential.format(), + // r_curly_token.format() + // ] + // ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_and.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_and.rs index eb40ca5bc45f..2efef88247d8 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_and.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_and.rs @@ -1,10 +1,28 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternAnd; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternAnd, GritPatternAndFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternAnd; impl FormatNodeRule for FormatGritPatternAnd { fn fmt_fields(&self, node: &GritPatternAnd, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternAndFields { + patterns, + and_token, + l_curly_token, + r_curly_token, + } = node.as_fields(); + + write!( + f, + [ + and_token.format(), + space(), + l_curly_token.format(), + space(), + patterns.format(), + space(), + r_curly_token.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/patterns/pattern_contains.rs b/crates/biome_grit_formatter/src/grit/patterns/pattern_contains.rs index a497b6712ae3..21b7a8470ff4 100644 --- a/crates/biome_grit_formatter/src/grit/patterns/pattern_contains.rs +++ b/crates/biome_grit_formatter/src/grit/patterns/pattern_contains.rs @@ -1,10 +1,23 @@ use crate::prelude::*; -use biome_grit_syntax::GritPatternContains; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPatternContains, GritPatternContainsFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPatternContains; impl FormatNodeRule for FormatGritPatternContains { fn fmt_fields(&self, node: &GritPatternContains, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPatternContainsFields { + contains, + contains_token, + until_clause, + } = node.as_fields(); + + write!(f, [contains_token.format(), space(), contains.format()])?; + + if let Some(until_clause) = until_clause { + write!(f, [space(), until_clause.format()])?; + } + + Ok(()) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/curly_predicate_list.rs b/crates/biome_grit_formatter/src/grit/predicates/curly_predicate_list.rs index 87720797fd56..0770972a6e12 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/curly_predicate_list.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/curly_predicate_list.rs @@ -1,10 +1,24 @@ use crate::prelude::*; -use biome_grit_syntax::GritCurlyPredicateList; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritCurlyPredicateList, GritCurlyPredicateListFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritCurlyPredicateList; impl FormatNodeRule for FormatGritCurlyPredicateList { fn fmt_fields(&self, node: &GritCurlyPredicateList, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritCurlyPredicateListFields { + predicates, + r_curly_token, + l_curly_token, + } = node.as_fields(); + + write!( + f, + [ + l_curly_token.format(), + predicates.format(), + r_curly_token.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_any.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_any.rs index fe958f141454..e6f8f55ac9a9 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_any.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_any.rs @@ -1,10 +1,28 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateAny; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateAny, GritPredicateAnyFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateAny; impl FormatNodeRule for FormatGritPredicateAny { fn fmt_fields(&self, node: &GritPredicateAny, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateAnyFields { + any_token, + l_curly_token, + predicates, + r_curly_token, + } = node.as_fields(); + write!( + f, + [ + l_curly_token.format(), + hard_line_break(), + any_token.format(), + hard_line_break(), + soft_block_indent(&predicates.format()), + hard_line_break(), + r_curly_token.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/predicates/predicate_call.rs b/crates/biome_grit_formatter/src/grit/predicates/predicate_call.rs index ab68963aa429..ec3c99c95d2a 100644 --- a/crates/biome_grit_formatter/src/grit/predicates/predicate_call.rs +++ b/crates/biome_grit_formatter/src/grit/predicates/predicate_call.rs @@ -1,10 +1,25 @@ use crate::prelude::*; -use biome_grit_syntax::GritPredicateCall; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritPredicateCall, GritPredicateCallFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritPredicateCall; impl FormatNodeRule for FormatGritPredicateCall { fn fmt_fields(&self, node: &GritPredicateCall, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritPredicateCallFields { + name, + l_paren_token, + named_args, + r_paren_token, + } = node.as_fields(); + write!( + f, + [ + name.format(), + l_paren_token.format(), + named_args.format(), + r_paren_token.format() + ] + ) } } diff --git a/crates/biome_grit_formatter/src/grit/value/backtick_snippet_literal.rs b/crates/biome_grit_formatter/src/grit/value/backtick_snippet_literal.rs index bdb8caca1b63..2a4f65a5ddc2 100644 --- a/crates/biome_grit_formatter/src/grit/value/backtick_snippet_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/backtick_snippet_literal.rs @@ -1,6 +1,7 @@ use crate::prelude::*; -use biome_grit_syntax::GritBacktickSnippetLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritBacktickSnippetLiteral, GritBacktickSnippetLiteralFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritBacktickSnippetLiteral; impl FormatNodeRule for FormatGritBacktickSnippetLiteral { @@ -9,6 +10,7 @@ impl FormatNodeRule for FormatGritBacktickSnippetLit node: &GritBacktickSnippetLiteral, f: &mut GritFormatter, ) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritBacktickSnippetLiteralFields { value_token } = node.as_fields(); + write!(f, [value_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/boolean_literal.rs b/crates/biome_grit_formatter/src/grit/value/boolean_literal.rs index e17bd51ddf24..e367841f05f8 100644 --- a/crates/biome_grit_formatter/src/grit/value/boolean_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/boolean_literal.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritBooleanLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritBooleanLiteral, GritBooleanLiteralFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritBooleanLiteral; impl FormatNodeRule for FormatGritBooleanLiteral { fn fmt_fields(&self, node: &GritBooleanLiteral, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritBooleanLiteralFields { value } = node.as_fields(); + write!(f, [value.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/code_snippet.rs b/crates/biome_grit_formatter/src/grit/value/code_snippet.rs index 6dc62de3c21a..cd10db272c74 100644 --- a/crates/biome_grit_formatter/src/grit/value/code_snippet.rs +++ b/crates/biome_grit_formatter/src/grit/value/code_snippet.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritCodeSnippet; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritCodeSnippet, GritCodeSnippetFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritCodeSnippet; impl FormatNodeRule for FormatGritCodeSnippet { fn fmt_fields(&self, node: &GritCodeSnippet, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritCodeSnippetFields { source } = node.as_fields(); + write!(f, [source.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/double_literal.rs b/crates/biome_grit_formatter/src/grit/value/double_literal.rs index c8ea2905b229..155c068c25bf 100644 --- a/crates/biome_grit_formatter/src/grit/value/double_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/double_literal.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritDoubleLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritDoubleLiteral, GritDoubleLiteralFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritDoubleLiteral; impl FormatNodeRule for FormatGritDoubleLiteral { fn fmt_fields(&self, node: &GritDoubleLiteral, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritDoubleLiteralFields { value_token } = node.as_fields(); + write!(f, [value_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/int_literal.rs b/crates/biome_grit_formatter/src/grit/value/int_literal.rs index 523db1268d0b..1b7652cef518 100644 --- a/crates/biome_grit_formatter/src/grit/value/int_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/int_literal.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritIntLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritIntLiteral, GritIntLiteralFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritIntLiteral; impl FormatNodeRule for FormatGritIntLiteral { fn fmt_fields(&self, node: &GritIntLiteral, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritIntLiteralFields { value_token } = node.as_fields(); + write!(f, [value_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/negative_int_literal.rs b/crates/biome_grit_formatter/src/grit/value/negative_int_literal.rs index 82e92cd7e85b..61196bd85c25 100644 --- a/crates/biome_grit_formatter/src/grit/value/negative_int_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/negative_int_literal.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritNegativeIntLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritNegativeIntLiteral, GritNegativeIntLiteralFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritNegativeIntLiteral; impl FormatNodeRule for FormatGritNegativeIntLiteral { fn fmt_fields(&self, node: &GritNegativeIntLiteral, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritNegativeIntLiteralFields { value_token } = node.as_fields(); + write!(f, [value_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/raw_backtick_snippet_literal.rs b/crates/biome_grit_formatter/src/grit/value/raw_backtick_snippet_literal.rs index e762178cd21b..3389b56f2295 100644 --- a/crates/biome_grit_formatter/src/grit/value/raw_backtick_snippet_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/raw_backtick_snippet_literal.rs @@ -1,6 +1,7 @@ use crate::prelude::*; -use biome_grit_syntax::GritRawBacktickSnippetLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritRawBacktickSnippetLiteral, GritRawBacktickSnippetLiteralFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritRawBacktickSnippetLiteral; impl FormatNodeRule for FormatGritRawBacktickSnippetLiteral { @@ -9,6 +10,7 @@ impl FormatNodeRule for FormatGritRawBacktickSnip node: &GritRawBacktickSnippetLiteral, f: &mut GritFormatter, ) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritRawBacktickSnippetLiteralFields { value_token } = node.as_fields(); + write!(f, [value_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/regex_literal.rs b/crates/biome_grit_formatter/src/grit/value/regex_literal.rs index c0948163aff1..f3e4eeaf4b20 100644 --- a/crates/biome_grit_formatter/src/grit/value/regex_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/regex_literal.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritRegexLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritRegexLiteral, GritRegexLiteralFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritRegexLiteral; impl FormatNodeRule for FormatGritRegexLiteral { fn fmt_fields(&self, node: &GritRegexLiteral, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritRegexLiteralFields { value_token } = node.as_fields(); + write!(f, [value_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/snippet_regex_literal.rs b/crates/biome_grit_formatter/src/grit/value/snippet_regex_literal.rs index 1079ea8541ae..8be36cec3a37 100644 --- a/crates/biome_grit_formatter/src/grit/value/snippet_regex_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/snippet_regex_literal.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use biome_grit_syntax::GritSnippetRegexLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritSnippetRegexLiteral, GritSnippetRegexLiteralFields}; #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritSnippetRegexLiteral; impl FormatNodeRule for FormatGritSnippetRegexLiteral { @@ -9,6 +9,7 @@ impl FormatNodeRule for FormatGritSnippetRegexLiteral { node: &GritSnippetRegexLiteral, f: &mut GritFormatter, ) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritSnippetRegexLiteralFields { value_token } = node.as_fields(); + write!(f, [value_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/string_literal.rs b/crates/biome_grit_formatter/src/grit/value/string_literal.rs index 36cbe6bc9065..4583dac3061d 100644 --- a/crates/biome_grit_formatter/src/grit/value/string_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/string_literal.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritStringLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritStringLiteral, GritStringLiteralFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritStringLiteral; impl FormatNodeRule for FormatGritStringLiteral { fn fmt_fields(&self, node: &GritStringLiteral, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritStringLiteralFields { value_token } = node.as_fields(); + write!(f, [value_token.format()]) } } diff --git a/crates/biome_grit_formatter/src/grit/value/undefined_literal.rs b/crates/biome_grit_formatter/src/grit/value/undefined_literal.rs index e324434db215..0ec8e0a55a7c 100644 --- a/crates/biome_grit_formatter/src/grit/value/undefined_literal.rs +++ b/crates/biome_grit_formatter/src/grit/value/undefined_literal.rs @@ -1,10 +1,12 @@ use crate::prelude::*; -use biome_grit_syntax::GritUndefinedLiteral; -use biome_rowan::AstNode; +use biome_formatter::write; +use biome_grit_syntax::{GritUndefinedLiteral, GritUndefinedLiteralFields}; + #[derive(Debug, Clone, Default)] pub(crate) struct FormatGritUndefinedLiteral; impl FormatNodeRule for FormatGritUndefinedLiteral { fn fmt_fields(&self, node: &GritUndefinedLiteral, f: &mut GritFormatter) -> FormatResult<()> { - format_verbatim_node(node.syntax()).fmt(f) + let GritUndefinedLiteralFields { token_token } = node.as_fields(); + write!(f, [token_token.format()]) } } diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit b/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit new file mode 100644 index 000000000000..d1c485bad485 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit @@ -0,0 +1,3 @@ +`function ($args) { $body }` where { $args <: contains `x` } + +`console.$_($content)` where { $content <: contains `secret` until `sanitized($_)` } diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit.snap new file mode 100644 index 000000000000..7a0e01f7cf23 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/contains.grit.snap @@ -0,0 +1,45 @@ +--- +source: crates/biome_formatter_test/src/snapshot_builder.rs +info: grit/patterns/contains.grit +--- +# Input + +```grit +`function ($args) { $body }` where { $args <: contains `x` } + +`console.$_($content)` where { $content <: contains `secret` until `sanitized($_)` } + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Indent width: 2 +Line ending: LF +Line width: 80 +Attribute Position: Auto +----- + +```grit +`function ($args) { $body }` where { + $args <: contains `x` +} + +`console.$_($content)` where { + $content <: contains `secret` until `sanitized($_)` +} +``` + + + +## Unimplemented nodes/tokens + +"\t$args" => 37..43 +"\t$content" => 94..103 +" until `sanitized($_)`" => 124..146 diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/create_new_files.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/create_new_files.grit.snap index c77607c91516..a8e933827f37 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/patterns/create_new_files.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/create_new_files.grit.snap @@ -42,6 +42,5 @@ Attribute Position: Auto "\t$" => 77..79 " " => 83..84 "\t$new_file_nam" => 87..101 -" `$functionName.test.js" => 104..127 "\t$new_file" => 130..140 " file(name = $new_file_name, body = $f" => 144..182 diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap index 4acafd780f6e..75aaf5077990 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/list_pattern.grit.snap @@ -46,32 +46,5 @@ Attribute Position: Auto "`var $x = [$numbers]`=>`var firstPrimes = [$numbers]`" => 0..53 "\t$number" => 62..70 -"`2`" => 76..79 -" `3" => 80..83 -" `5" => 85..88 "`var $x = [$numbers]`=>`var numbersLong = [$numbers]` wh" => 94..150 "\t$number" => 156..164 -"\t\t\t" => 171..174 -" `3" => 178..181 -" `5" => 183..186 -" `6" => 188..191 -" `7" => 193..196 -" `8" => 198..201 -" `2" => 203..206 -" `3" => 208..211 -" `5" => 213..216 -" `6" => 218..221 -" `7" => 223..226 -" `8" => 228..231 -" `2" => 233..236 -" `3" => 238..241 -" `5" => 243..246 -"\t\t\t" => 249..252 -" `7" => 256..259 -" `8" => 261..264 -" `2" => 266..269 -" `3" => 271..274 -" `5" => 276..279 -" `6" => 281..284 -" `7" => 286..289 -" `8" => 291..294 diff --git a/crates/biome_grit_formatter/tests/specs/grit/patterns/where_pattern.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/patterns/where_pattern.grit.snap index e1098ad62218..b8bc031b396c 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/patterns/where_pattern.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/patterns/where_pattern.grit.snap @@ -30,9 +30,3 @@ Attribute Position: Auto `$method('$message')` where { } ``` - - - -## Unimplemented nodes/tokens - -"`$method('$message')`" => 0..21 diff --git a/crates/biome_grit_formatter/tests/specs/grit/predicates/assignment.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/predicates/assignment.grit.snap index 92bef0737f41..20e64f197d5e 100644 --- a/crates/biome_grit_formatter/tests/specs/grit/predicates/assignment.grit.snap +++ b/crates/biome_grit_formatter/tests/specs/grit/predicates/assignment.grit.snap @@ -36,4 +36,3 @@ Attribute Position: Auto "`console.log($message)` as $log " => 0..32 "\t$new_log_call" => 40..54 -" `logger.log($message)" => 56..78 diff --git a/crates/biome_grit_formatter/tests/specs/grit/sequential.grit b/crates/biome_grit_formatter/tests/specs/grit/sequential.grit new file mode 100644 index 000000000000..d91e7140af79 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/sequential.grit @@ -0,0 +1,4 @@ +sequential { + bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, + bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` +} diff --git a/crates/biome_grit_formatter/tests/specs/grit/sequential.grit.snap b/crates/biome_grit_formatter/tests/specs/grit/sequential.grit.snap new file mode 100644 index 000000000000..06e860fbe631 --- /dev/null +++ b/crates/biome_grit_formatter/tests/specs/grit/sequential.grit.snap @@ -0,0 +1,46 @@ +--- +source: crates/biome_formatter_test/src/snapshot_builder.rs +info: grit/sequential.grit +--- +# Input + +```grit +sequential { + bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, + bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` +} + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Indent width: 2 +Line ending: LF +Line width: 80 +Attribute Position: Auto +----- + +```grit +sequential { + bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, + bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` +} +``` + + + +## Unimplemented nodes/tokens + +"sequential {\n bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`,\n bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)`\n}" => 0..214 +# Lines exceeding max width of 80 characters +``` + 2: bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, + 3: bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` +``` diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_after.grit b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_after.grit new file mode 100644 index 000000000000..e04891655b8f --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_after.grit @@ -0,0 +1,3 @@ +`console.warn($_)` as $warn where { + $warn <: after `console.log($_)` +} diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_after.grit.snap b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_after.grit.snap new file mode 100644 index 000000000000..531043cacfab --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_after.grit.snap @@ -0,0 +1,94 @@ +--- +source: crates/biome_grit_parser/tests/spec_test.rs +expression: snapshot +--- +## Input +```grit +`console.warn($_)` as $warn where { + $warn <: after `console.log($_)` +} + +``` + +## AST + +``` +GritRoot { + bom_token: missing (optional), + version: missing (optional), + language: missing (optional), + definitions: GritDefinitionList [ + GritPatternWhere { + pattern: GritPatternAs { + pattern: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@0..19 "`console.warn($_)`" [] [Whitespace(" ")], + }, + }, + as_token: AS_KW@19..22 "as" [] [Whitespace(" ")], + variable: GritVariable { + value_token: GRIT_VARIABLE@22..28 "$warn" [] [Whitespace(" ")], + }, + }, + where_token: WHERE_KW@28..34 "where" [] [Whitespace(" ")], + side_condition: GritPredicateAnd { + and_token: missing (optional), + l_curly_token: L_CURLY@34..35 "{" [] [], + predicates: GritPredicateList [ + GritPredicateMatch { + left: GritVariable { + value_token: GRIT_VARIABLE@35..44 "$warn" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")], + }, + match_token: MATCH@44..47 "<:" [] [Whitespace(" ")], + right: GritPatternAfter { + after_token: AFTER_KW@47..53 "after" [] [Whitespace(" ")], + pattern: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@53..70 "`console.log($_)`" [] [], + }, + }, + }, + }, + ], + r_curly_token: R_CURLY@70..72 "}" [Newline("\n")] [], + }, + }, + ], + eof_token: EOF@72..73 "" [Newline("\n")] [], +} +``` + +## CST + +``` +0: GRIT_ROOT@0..73 + 0: (empty) + 1: (empty) + 2: (empty) + 3: GRIT_DEFINITION_LIST@0..72 + 0: GRIT_PATTERN_WHERE@0..72 + 0: GRIT_PATTERN_AS@0..28 + 0: GRIT_CODE_SNIPPET@0..19 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@0..19 + 0: GRIT_BACKTICK_SNIPPET@0..19 "`console.warn($_)`" [] [Whitespace(" ")] + 1: AS_KW@19..22 "as" [] [Whitespace(" ")] + 2: GRIT_VARIABLE@22..28 + 0: GRIT_VARIABLE@22..28 "$warn" [] [Whitespace(" ")] + 1: WHERE_KW@28..34 "where" [] [Whitespace(" ")] + 2: GRIT_PREDICATE_AND@34..72 + 0: (empty) + 1: L_CURLY@34..35 "{" [] [] + 2: GRIT_PREDICATE_LIST@35..70 + 0: GRIT_PREDICATE_MATCH@35..70 + 0: GRIT_VARIABLE@35..44 + 0: GRIT_VARIABLE@35..44 "$warn" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")] + 1: MATCH@44..47 "<:" [] [Whitespace(" ")] + 2: GRIT_PATTERN_AFTER@47..70 + 0: AFTER_KW@47..53 "after" [] [Whitespace(" ")] + 1: GRIT_CODE_SNIPPET@53..70 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@53..70 + 0: GRIT_BACKTICK_SNIPPET@53..70 "`console.log($_)`" [] [] + 3: R_CURLY@70..72 "}" [Newline("\n")] [] + 4: EOF@72..73 "" [Newline("\n")] [] + +``` diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_any.grit b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_any.grit new file mode 100644 index 000000000000..4dbfe97e0da5 --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_any.grit @@ -0,0 +1,3 @@ +`var $x = [$names]` => `var coolPeople = [$names]` where { + $names <: every any {`"andrew"`, `"alex"`} +} diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_any.grit.snap b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_any.grit.snap new file mode 100644 index 000000000000..0a9640d8cf87 --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_any.grit.snap @@ -0,0 +1,121 @@ +--- +source: crates/biome_grit_parser/tests/spec_test.rs +expression: snapshot +--- +## Input +```grit +`var $x = [$names]` => `var coolPeople = [$names]` where { + $names <: every any {`"andrew"`, `"alex"`} +} + +``` + +## AST + +``` +GritRoot { + bom_token: missing (optional), + version: missing (optional), + language: missing (optional), + definitions: GritDefinitionList [ + GritPatternWhere { + pattern: GritRewrite { + left: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@0..20 "`var $x = [$names]`" [] [Whitespace(" ")], + }, + }, + annotation: missing (optional), + fat_arrow_token: FAT_ARROW@20..23 "=>" [] [Whitespace(" ")], + right: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@23..51 "`var coolPeople = [$names]`" [] [Whitespace(" ")], + }, + }, + }, + where_token: WHERE_KW@51..57 "where" [] [Whitespace(" ")], + side_condition: GritPredicateAnd { + and_token: missing (optional), + l_curly_token: L_CURLY@57..58 "{" [] [], + predicates: GritPredicateList [ + GritPredicateMatch { + left: GritVariable { + value_token: GRIT_VARIABLE@58..68 "$names" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")], + }, + match_token: MATCH@68..71 "<:" [] [Whitespace(" ")], + right: GritEvery { + every_token: EVERY_KW@71..77 "every" [] [Whitespace(" ")], + pattern: GritPatternAny { + any_token: ANY_KW@77..81 "any" [] [Whitespace(" ")], + l_curly_token: L_CURLY@81..82 "{" [] [], + patterns: GritPatternList [ + GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@82..92 "`\"andrew\"`" [] [], + }, + }, + COMMA@92..94 "," [] [Whitespace(" ")], + GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@94..102 "`\"alex\"`" [] [], + }, + }, + ], + r_curly_token: R_CURLY@102..103 "}" [] [], + }, + }, + }, + ], + r_curly_token: R_CURLY@103..105 "}" [Newline("\n")] [], + }, + }, + ], + eof_token: EOF@105..106 "" [Newline("\n")] [], +} +``` + +## CST + +``` +0: GRIT_ROOT@0..106 + 0: (empty) + 1: (empty) + 2: (empty) + 3: GRIT_DEFINITION_LIST@0..105 + 0: GRIT_PATTERN_WHERE@0..105 + 0: GRIT_REWRITE@0..51 + 0: GRIT_CODE_SNIPPET@0..20 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@0..20 + 0: GRIT_BACKTICK_SNIPPET@0..20 "`var $x = [$names]`" [] [Whitespace(" ")] + 1: (empty) + 2: FAT_ARROW@20..23 "=>" [] [Whitespace(" ")] + 3: GRIT_CODE_SNIPPET@23..51 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@23..51 + 0: GRIT_BACKTICK_SNIPPET@23..51 "`var coolPeople = [$names]`" [] [Whitespace(" ")] + 1: WHERE_KW@51..57 "where" [] [Whitespace(" ")] + 2: GRIT_PREDICATE_AND@57..105 + 0: (empty) + 1: L_CURLY@57..58 "{" [] [] + 2: GRIT_PREDICATE_LIST@58..103 + 0: GRIT_PREDICATE_MATCH@58..103 + 0: GRIT_VARIABLE@58..68 + 0: GRIT_VARIABLE@58..68 "$names" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")] + 1: MATCH@68..71 "<:" [] [Whitespace(" ")] + 2: GRIT_EVERY@71..103 + 0: EVERY_KW@71..77 "every" [] [Whitespace(" ")] + 1: GRIT_PATTERN_ANY@77..103 + 0: ANY_KW@77..81 "any" [] [Whitespace(" ")] + 1: L_CURLY@81..82 "{" [] [] + 2: GRIT_PATTERN_LIST@82..102 + 0: GRIT_CODE_SNIPPET@82..92 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@82..92 + 0: GRIT_BACKTICK_SNIPPET@82..92 "`\"andrew\"`" [] [] + 1: COMMA@92..94 "," [] [Whitespace(" ")] + 2: GRIT_CODE_SNIPPET@94..102 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@94..102 + 0: GRIT_BACKTICK_SNIPPET@94..102 "`\"alex\"`" [] [] + 3: R_CURLY@102..103 "}" [] [] + 3: R_CURLY@103..105 "}" [Newline("\n")] [] + 4: EOF@105..106 "" [Newline("\n")] [] + +``` diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_end.grit b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_end.grit new file mode 100644 index 000000000000..485d7ba47120 --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_end.grit @@ -0,0 +1,3 @@ +`var $x = [$names]` => `var coolPeople = [$names]` where { + $names <: every and {`"andrew"`, `"alex"`} +} diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_end.grit.snap b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_end.grit.snap new file mode 100644 index 000000000000..4102713b910b --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_end.grit.snap @@ -0,0 +1,121 @@ +--- +source: crates/biome_grit_parser/tests/spec_test.rs +expression: snapshot +--- +## Input +```grit +`var $x = [$names]` => `var coolPeople = [$names]` where { + $names <: every and {`"andrew"`, `"alex"`} +} + +``` + +## AST + +``` +GritRoot { + bom_token: missing (optional), + version: missing (optional), + language: missing (optional), + definitions: GritDefinitionList [ + GritPatternWhere { + pattern: GritRewrite { + left: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@0..20 "`var $x = [$names]`" [] [Whitespace(" ")], + }, + }, + annotation: missing (optional), + fat_arrow_token: FAT_ARROW@20..23 "=>" [] [Whitespace(" ")], + right: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@23..51 "`var coolPeople = [$names]`" [] [Whitespace(" ")], + }, + }, + }, + where_token: WHERE_KW@51..57 "where" [] [Whitespace(" ")], + side_condition: GritPredicateAnd { + and_token: missing (optional), + l_curly_token: L_CURLY@57..58 "{" [] [], + predicates: GritPredicateList [ + GritPredicateMatch { + left: GritVariable { + value_token: GRIT_VARIABLE@58..68 "$names" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")], + }, + match_token: MATCH@68..71 "<:" [] [Whitespace(" ")], + right: GritEvery { + every_token: EVERY_KW@71..77 "every" [] [Whitespace(" ")], + pattern: GritPatternAnd { + and_token: AND_KW@77..81 "and" [] [Whitespace(" ")], + l_curly_token: L_CURLY@81..82 "{" [] [], + patterns: GritPatternList [ + GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@82..92 "`\"andrew\"`" [] [], + }, + }, + COMMA@92..94 "," [] [Whitespace(" ")], + GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@94..102 "`\"alex\"`" [] [], + }, + }, + ], + r_curly_token: R_CURLY@102..103 "}" [] [], + }, + }, + }, + ], + r_curly_token: R_CURLY@103..105 "}" [Newline("\n")] [], + }, + }, + ], + eof_token: EOF@105..106 "" [Newline("\n")] [], +} +``` + +## CST + +``` +0: GRIT_ROOT@0..106 + 0: (empty) + 1: (empty) + 2: (empty) + 3: GRIT_DEFINITION_LIST@0..105 + 0: GRIT_PATTERN_WHERE@0..105 + 0: GRIT_REWRITE@0..51 + 0: GRIT_CODE_SNIPPET@0..20 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@0..20 + 0: GRIT_BACKTICK_SNIPPET@0..20 "`var $x = [$names]`" [] [Whitespace(" ")] + 1: (empty) + 2: FAT_ARROW@20..23 "=>" [] [Whitespace(" ")] + 3: GRIT_CODE_SNIPPET@23..51 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@23..51 + 0: GRIT_BACKTICK_SNIPPET@23..51 "`var coolPeople = [$names]`" [] [Whitespace(" ")] + 1: WHERE_KW@51..57 "where" [] [Whitespace(" ")] + 2: GRIT_PREDICATE_AND@57..105 + 0: (empty) + 1: L_CURLY@57..58 "{" [] [] + 2: GRIT_PREDICATE_LIST@58..103 + 0: GRIT_PREDICATE_MATCH@58..103 + 0: GRIT_VARIABLE@58..68 + 0: GRIT_VARIABLE@58..68 "$names" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")] + 1: MATCH@68..71 "<:" [] [Whitespace(" ")] + 2: GRIT_EVERY@71..103 + 0: EVERY_KW@71..77 "every" [] [Whitespace(" ")] + 1: GRIT_PATTERN_AND@77..103 + 0: AND_KW@77..81 "and" [] [Whitespace(" ")] + 1: L_CURLY@81..82 "{" [] [] + 2: GRIT_PATTERN_LIST@82..102 + 0: GRIT_CODE_SNIPPET@82..92 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@82..92 + 0: GRIT_BACKTICK_SNIPPET@82..92 "`\"andrew\"`" [] [] + 1: COMMA@92..94 "," [] [Whitespace(" ")] + 2: GRIT_CODE_SNIPPET@94..102 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@94..102 + 0: GRIT_BACKTICK_SNIPPET@94..102 "`\"alex\"`" [] [] + 3: R_CURLY@102..103 "}" [] [] + 3: R_CURLY@103..105 "}" [Newline("\n")] [] + 4: EOF@105..106 "" [Newline("\n")] [] + +``` diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_maybe.grit b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_maybe.grit new file mode 100644 index 000000000000..af64e89b9b73 --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_maybe.grit @@ -0,0 +1,3 @@ +`throw new Error($err)` as $thrown => `throw new CustomError($err);` where { + $err <: maybe string(fragment=$fun) => `{ message: $err }` +} diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_maybe.grit.snap b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_maybe.grit.snap new file mode 100644 index 000000000000..799610cb391b --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/pattern_maybe.grit.snap @@ -0,0 +1,146 @@ +--- +source: crates/biome_grit_parser/tests/spec_test.rs +expression: snapshot +--- +## Input +```grit +`throw new Error($err)` as $thrown => `throw new CustomError($err);` where { + $err <: maybe string(fragment=$fun) => `{ message: $err }` +} + +``` + +## AST + +``` +GritRoot { + bom_token: missing (optional), + version: missing (optional), + language: missing (optional), + definitions: GritDefinitionList [ + GritPatternWhere { + pattern: GritRewrite { + left: GritPatternAs { + pattern: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@0..24 "`throw new Error($err)`" [] [Whitespace(" ")], + }, + }, + as_token: AS_KW@24..27 "as" [] [Whitespace(" ")], + variable: GritVariable { + value_token: GRIT_VARIABLE@27..35 "$thrown" [] [Whitespace(" ")], + }, + }, + annotation: missing (optional), + fat_arrow_token: FAT_ARROW@35..38 "=>" [] [Whitespace(" ")], + right: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@38..69 "`throw new CustomError($err);`" [] [Whitespace(" ")], + }, + }, + }, + where_token: WHERE_KW@69..75 "where" [] [Whitespace(" ")], + side_condition: GritPredicateAnd { + and_token: missing (optional), + l_curly_token: L_CURLY@75..76 "{" [] [], + predicates: GritPredicateList [ + GritPredicateMatch { + left: GritVariable { + value_token: GRIT_VARIABLE@76..84 "$err" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")], + }, + match_token: MATCH@84..87 "<:" [] [Whitespace(" ")], + right: GritPatternMaybe { + maybe_token: MAYBE_KW@87..93 "maybe" [] [Whitespace(" ")], + pattern: GritRewrite { + left: GritNodeLike { + name: GritName { + value_token: GRIT_NAME@93..99 "string" [] [], + }, + l_paren_token: L_PAREN@99..100 "(" [] [], + named_args: GritNamedArgList [ + GritNamedArg { + name: GritName { + value_token: GRIT_NAME@100..108 "fragment" [] [], + }, + eq_token: EQ@108..109 "=" [] [], + pattern: GritVariable { + value_token: GRIT_VARIABLE@109..113 "$fun" [] [], + }, + }, + ], + r_paren_token: R_PAREN@113..115 ")" [] [Whitespace(" ")], + }, + annotation: missing (optional), + fat_arrow_token: FAT_ARROW@115..118 "=>" [] [Whitespace(" ")], + right: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@118..137 "`{ message: $err }`" [] [], + }, + }, + }, + }, + }, + ], + r_curly_token: R_CURLY@137..139 "}" [Newline("\n")] [], + }, + }, + ], + eof_token: EOF@139..140 "" [Newline("\n")] [], +} +``` + +## CST + +``` +0: GRIT_ROOT@0..140 + 0: (empty) + 1: (empty) + 2: (empty) + 3: GRIT_DEFINITION_LIST@0..139 + 0: GRIT_PATTERN_WHERE@0..139 + 0: GRIT_REWRITE@0..69 + 0: GRIT_PATTERN_AS@0..35 + 0: GRIT_CODE_SNIPPET@0..24 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@0..24 + 0: GRIT_BACKTICK_SNIPPET@0..24 "`throw new Error($err)`" [] [Whitespace(" ")] + 1: AS_KW@24..27 "as" [] [Whitespace(" ")] + 2: GRIT_VARIABLE@27..35 + 0: GRIT_VARIABLE@27..35 "$thrown" [] [Whitespace(" ")] + 1: (empty) + 2: FAT_ARROW@35..38 "=>" [] [Whitespace(" ")] + 3: GRIT_CODE_SNIPPET@38..69 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@38..69 + 0: GRIT_BACKTICK_SNIPPET@38..69 "`throw new CustomError($err);`" [] [Whitespace(" ")] + 1: WHERE_KW@69..75 "where" [] [Whitespace(" ")] + 2: GRIT_PREDICATE_AND@75..139 + 0: (empty) + 1: L_CURLY@75..76 "{" [] [] + 2: GRIT_PREDICATE_LIST@76..137 + 0: GRIT_PREDICATE_MATCH@76..137 + 0: GRIT_VARIABLE@76..84 + 0: GRIT_VARIABLE@76..84 "$err" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")] + 1: MATCH@84..87 "<:" [] [Whitespace(" ")] + 2: GRIT_PATTERN_MAYBE@87..137 + 0: MAYBE_KW@87..93 "maybe" [] [Whitespace(" ")] + 1: GRIT_REWRITE@93..137 + 0: GRIT_NODE_LIKE@93..115 + 0: GRIT_NAME@93..99 + 0: GRIT_NAME@93..99 "string" [] [] + 1: L_PAREN@99..100 "(" [] [] + 2: GRIT_NAMED_ARG_LIST@100..113 + 0: GRIT_NAMED_ARG@100..113 + 0: GRIT_NAME@100..108 + 0: GRIT_NAME@100..108 "fragment" [] [] + 1: EQ@108..109 "=" [] [] + 2: GRIT_VARIABLE@109..113 + 0: GRIT_VARIABLE@109..113 "$fun" [] [] + 3: R_PAREN@113..115 ")" [] [Whitespace(" ")] + 1: (empty) + 2: FAT_ARROW@115..118 "=>" [] [Whitespace(" ")] + 3: GRIT_CODE_SNIPPET@118..137 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@118..137 + 0: GRIT_BACKTICK_SNIPPET@118..137 "`{ message: $err }`" [] [] + 3: R_CURLY@137..139 "}" [Newline("\n")] [] + 4: EOF@139..140 "" [Newline("\n")] [] + +``` diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/sequential.grit b/crates/biome_grit_parser/tests/grit_test_suite/ok/sequential.grit new file mode 100644 index 000000000000..d91e7140af79 --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/sequential.grit @@ -0,0 +1,4 @@ +sequential { + bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, + bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` +} diff --git a/crates/biome_grit_parser/tests/grit_test_suite/ok/sequential.grit.snap b/crates/biome_grit_parser/tests/grit_test_suite/ok/sequential.grit.snap new file mode 100644 index 000000000000..3e019884f79d --- /dev/null +++ b/crates/biome_grit_parser/tests/grit_test_suite/ok/sequential.grit.snap @@ -0,0 +1,195 @@ +--- +source: crates/biome_grit_parser/tests/spec_test.rs +expression: snapshot +--- +## Input +```grit +sequential { + bubble file($body) where $body <: contains `console.log($message)` => `console.warn($message)`, + bubble file($body) where $body <: contains `console.warn($message)` => `console.info($message)` +} + +``` + +## AST + +``` +GritRoot { + bom_token: missing (optional), + version: missing (optional), + language: missing (optional), + definitions: GritDefinitionList [ + GritSequential { + sequential_token: SEQUENTIAL_KW@0..11 "sequential" [] [Whitespace(" ")], + l_curly_token: L_CURLY@11..12 "{" [] [], + sequential: GritPatternList [ + GritBubble { + bubble_token: BUBBLE_KW@12..24 "bubble" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")], + variables: missing (optional), + pattern: GritPatternWhere { + pattern: GritNodeLike { + name: GritName { + value_token: GRIT_NAME@24..28 "file" [] [], + }, + l_paren_token: L_PAREN@28..29 "(" [] [], + named_args: GritNamedArgList [ + GritVariable { + value_token: GRIT_VARIABLE@29..34 "$body" [] [], + }, + ], + r_paren_token: R_PAREN@34..36 ")" [] [Whitespace(" ")], + }, + where_token: WHERE_KW@36..42 "where" [] [Whitespace(" ")], + side_condition: GritPredicateMatch { + left: GritVariable { + value_token: GRIT_VARIABLE@42..48 "$body" [] [Whitespace(" ")], + }, + match_token: MATCH@48..51 "<:" [] [Whitespace(" ")], + right: GritPatternContains { + contains_token: CONTAINS_KW@51..60 "contains" [] [Whitespace(" ")], + contains: GritRewrite { + left: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@60..84 "`console.log($message)`" [] [Whitespace(" ")], + }, + }, + annotation: missing (optional), + fat_arrow_token: FAT_ARROW@84..87 "=>" [] [Whitespace(" ")], + right: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@87..111 "`console.warn($message)`" [] [], + }, + }, + }, + until_clause: missing (optional), + }, + }, + }, + }, + COMMA@111..112 "," [] [], + GritBubble { + bubble_token: BUBBLE_KW@112..124 "bubble" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")], + variables: missing (optional), + pattern: GritPatternWhere { + pattern: GritNodeLike { + name: GritName { + value_token: GRIT_NAME@124..128 "file" [] [], + }, + l_paren_token: L_PAREN@128..129 "(" [] [], + named_args: GritNamedArgList [ + GritVariable { + value_token: GRIT_VARIABLE@129..134 "$body" [] [], + }, + ], + r_paren_token: R_PAREN@134..136 ")" [] [Whitespace(" ")], + }, + where_token: WHERE_KW@136..142 "where" [] [Whitespace(" ")], + side_condition: GritPredicateMatch { + left: GritVariable { + value_token: GRIT_VARIABLE@142..148 "$body" [] [Whitespace(" ")], + }, + match_token: MATCH@148..151 "<:" [] [Whitespace(" ")], + right: GritPatternContains { + contains_token: CONTAINS_KW@151..160 "contains" [] [Whitespace(" ")], + contains: GritRewrite { + left: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@160..185 "`console.warn($message)`" [] [Whitespace(" ")], + }, + }, + annotation: missing (optional), + fat_arrow_token: FAT_ARROW@185..188 "=>" [] [Whitespace(" ")], + right: GritCodeSnippet { + source: GritBacktickSnippetLiteral { + value_token: GRIT_BACKTICK_SNIPPET@188..212 "`console.info($message)`" [] [], + }, + }, + }, + until_clause: missing (optional), + }, + }, + }, + }, + ], + r_curly_token: R_CURLY@212..214 "}" [Newline("\n")] [], + }, + ], + eof_token: EOF@214..215 "" [Newline("\n")] [], +} +``` + +## CST + +``` +0: GRIT_ROOT@0..215 + 0: (empty) + 1: (empty) + 2: (empty) + 3: GRIT_DEFINITION_LIST@0..214 + 0: GRIT_SEQUENTIAL@0..214 + 0: SEQUENTIAL_KW@0..11 "sequential" [] [Whitespace(" ")] + 1: L_CURLY@11..12 "{" [] [] + 2: GRIT_PATTERN_LIST@12..212 + 0: GRIT_BUBBLE@12..111 + 0: BUBBLE_KW@12..24 "bubble" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")] + 1: (empty) + 2: GRIT_PATTERN_WHERE@24..111 + 0: GRIT_NODE_LIKE@24..36 + 0: GRIT_NAME@24..28 + 0: GRIT_NAME@24..28 "file" [] [] + 1: L_PAREN@28..29 "(" [] [] + 2: GRIT_NAMED_ARG_LIST@29..34 + 0: GRIT_VARIABLE@29..34 + 0: GRIT_VARIABLE@29..34 "$body" [] [] + 3: R_PAREN@34..36 ")" [] [Whitespace(" ")] + 1: WHERE_KW@36..42 "where" [] [Whitespace(" ")] + 2: GRIT_PREDICATE_MATCH@42..111 + 0: GRIT_VARIABLE@42..48 + 0: GRIT_VARIABLE@42..48 "$body" [] [Whitespace(" ")] + 1: MATCH@48..51 "<:" [] [Whitespace(" ")] + 2: GRIT_PATTERN_CONTAINS@51..111 + 0: CONTAINS_KW@51..60 "contains" [] [Whitespace(" ")] + 1: GRIT_REWRITE@60..111 + 0: GRIT_CODE_SNIPPET@60..84 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@60..84 + 0: GRIT_BACKTICK_SNIPPET@60..84 "`console.log($message)`" [] [Whitespace(" ")] + 1: (empty) + 2: FAT_ARROW@84..87 "=>" [] [Whitespace(" ")] + 3: GRIT_CODE_SNIPPET@87..111 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@87..111 + 0: GRIT_BACKTICK_SNIPPET@87..111 "`console.warn($message)`" [] [] + 2: (empty) + 1: COMMA@111..112 "," [] [] + 2: GRIT_BUBBLE@112..212 + 0: BUBBLE_KW@112..124 "bubble" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")] + 1: (empty) + 2: GRIT_PATTERN_WHERE@124..212 + 0: GRIT_NODE_LIKE@124..136 + 0: GRIT_NAME@124..128 + 0: GRIT_NAME@124..128 "file" [] [] + 1: L_PAREN@128..129 "(" [] [] + 2: GRIT_NAMED_ARG_LIST@129..134 + 0: GRIT_VARIABLE@129..134 + 0: GRIT_VARIABLE@129..134 "$body" [] [] + 3: R_PAREN@134..136 ")" [] [Whitespace(" ")] + 1: WHERE_KW@136..142 "where" [] [Whitespace(" ")] + 2: GRIT_PREDICATE_MATCH@142..212 + 0: GRIT_VARIABLE@142..148 + 0: GRIT_VARIABLE@142..148 "$body" [] [Whitespace(" ")] + 1: MATCH@148..151 "<:" [] [Whitespace(" ")] + 2: GRIT_PATTERN_CONTAINS@151..212 + 0: CONTAINS_KW@151..160 "contains" [] [Whitespace(" ")] + 1: GRIT_REWRITE@160..212 + 0: GRIT_CODE_SNIPPET@160..185 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@160..185 + 0: GRIT_BACKTICK_SNIPPET@160..185 "`console.warn($message)`" [] [Whitespace(" ")] + 1: (empty) + 2: FAT_ARROW@185..188 "=>" [] [Whitespace(" ")] + 3: GRIT_CODE_SNIPPET@188..212 + 0: GRIT_BACKTICK_SNIPPET_LITERAL@188..212 + 0: GRIT_BACKTICK_SNIPPET@188..212 "`console.info($message)`" [] [] + 2: (empty) + 3: R_CURLY@212..214 "}" [Newline("\n")] [] + 4: EOF@214..215 "" [Newline("\n")] [] + +``` diff --git a/crates/biome_service/src/file_handlers/grit.rs b/crates/biome_service/src/file_handlers/grit.rs index 95cd975926b7..71db123767c7 100644 --- a/crates/biome_service/src/file_handlers/grit.rs +++ b/crates/biome_service/src/file_handlers/grit.rs @@ -1,3 +1,8 @@ +use super::{ + AnalyzerCapabilities, Capabilities, DebugCapabilities, DocumentFileSource, ExtensionHandler, + FormatterCapabilities, ParseResult, ParserCapabilities, SearchCapabilities, +}; +use crate::workspace::GetSyntaxTreeResult; use crate::{ settings::{ServiceLanguage, Settings, WorkspaceSettingsHandle}, WorkspaceError, @@ -7,15 +12,10 @@ use biome_formatter::{IndentStyle, IndentWidth, LineEnding, LineWidth, Printed}; use biome_fs::BiomePath; use biome_grit_formatter::{context::GritFormatOptions, format_node}; use biome_grit_parser::parse_grit_with_cache; -use biome_grit_syntax::GritLanguage; +use biome_grit_syntax::{GritLanguage, GritRoot, GritSyntaxNode}; use biome_parser::AnyParse; use biome_rowan::NodeCache; -use super::{ - AnalyzerCapabilities, Capabilities, DebugCapabilities, DocumentFileSource, ExtensionHandler, - FormatterCapabilities, ParseResult, ParserCapabilities, SearchCapabilities, -}; - #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct GritFormatterSettings { @@ -113,9 +113,9 @@ impl ExtensionHandler for GritFileHandler { Capabilities { parser: ParserCapabilities { parse: Some(parse) }, debug: DebugCapabilities { - debug_syntax_tree: None, + debug_syntax_tree: Some(debug_syntax_tree), debug_control_flow: None, - debug_formatter_ir: None, + debug_formatter_ir: Some(debug_formatter_ir), }, analyzer: AnalyzerCapabilities { lint: None, @@ -149,6 +149,30 @@ fn parse( } } +fn debug_syntax_tree(_rome_path: &BiomePath, parse: AnyParse) -> GetSyntaxTreeResult { + let syntax: GritSyntaxNode = parse.syntax(); + let tree: GritRoot = parse.tree(); + GetSyntaxTreeResult { + cst: format!("{syntax:#?}"), + ast: format!("{tree:#?}"), + } +} + +fn debug_formatter_ir( + biome_path: &BiomePath, + document_file_source: &DocumentFileSource, + parse: AnyParse, + settings: WorkspaceSettingsHandle, +) -> Result { + let options = settings.format_options::(biome_path, document_file_source); + + let tree = parse.syntax(); + let formatted = format_node(options, &tree)?; + + let root_element = formatted.into_document(); + Ok(root_element.to_string()) +} + #[tracing::instrument(level = "debug", skip(parse, settings))] fn format( biome_path: &BiomePath,