Skip to content

Commit

Permalink
feat(grit): syntax support for raw snippets in Grit queries (#3815)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr authored Sep 6, 2024
1 parent 6e5cf86 commit d3dacc9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions crates/biome_grit_patterns/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub enum CompileError {
/// A metavariable was expected at the given range.
InvalidMetavariableRange(ByteRange),

/// Raw snippets are only allowed on the right-hand side of a rule.
InvalidRawSnippetPosition,

/// Regular expressions are not allowed on the right-hand side of a rule.
InvalidRegexPosition,

Expand Down Expand Up @@ -92,6 +95,9 @@ impl Diagnostic for CompileError {
CompileError::InvalidMetavariableRange(_) => {
fmt.write_markup(markup! { "Invalid range for metavariable" })
}
CompileError::InvalidRawSnippetPosition => {
fmt.write_markup(markup! { "Invalid range for metavariable" })
}
CompileError::InvalidRegexPosition => fmt.write_markup(
markup! { "Regular expressions are not allowed on the right-hand side of a rule" },
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ impl LiteralCompiler {
debug_assert!(source.len() >= 2, "Literals must have quotes");
parse_snippet_content(&source[1..source.len() - 1], range, context, is_rhs)
}
AnyGritCodeSnippetSource::GritRawBacktickSnippetLiteral(_) => todo!(),
AnyGritCodeSnippetSource::GritRawBacktickSnippetLiteral(node) => {
if !is_rhs {
return Err(CompileError::InvalidRawSnippetPosition);
}

let token = node.value_token()?;
let source = token.text_trimmed();
let range = token.text_trimmed_range().to_byte_range();
debug_assert!(source.starts_with("raw`") && source.ends_with('`'));
parse_snippet_content(&source[4..source.len() - 1], range, context, is_rhs)
}
},
AnyGritLiteral::GritDoubleLiteral(node) => Ok(Pattern::FloatConstant(
FloatConstant::new(node.value_token()?.text_trimmed().parse().map_err(|err| {
Expand Down
1 change: 1 addition & 0 deletions crates/biome_grit_patterns/tests/specs/ts/rawSnippet.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`console.log($message)` => raw`if(' // I like broken code"`
12 changes: 12 additions & 0 deletions crates/biome_grit_patterns/tests/specs/ts/rawSnippet.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/biome_grit_patterns/tests/spec_tests.rs
expression: rawSnippet
---
SnapshotResult {
messages: [],
matched_ranges: [
"1:1-1:29",
],
rewritten_files: [],
created_files: [],
}
1 change: 1 addition & 0 deletions crates/biome_grit_patterns/tests/specs/ts/rawSnippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello, world!');

0 comments on commit d3dacc9

Please sign in to comment.