-
-
Notifications
You must be signed in to change notification settings - Fork 482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(grit): parse Grit literal snippets #3051
Conversation
CodSpeed Performance ReportMerging #3051 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left some suggestions that might help to improve the code
(Some(range), Some(source)) => source.text[..range.start().into()] | ||
.lines() | ||
.enumerate() | ||
.last() | ||
.map(|(i, line)| { | ||
let start = Position { | ||
line: (i + 1) as u32, | ||
column: line.len() as u32, | ||
}; | ||
let end = source.text[range].lines().enumerate().last().map_or( | ||
start, | ||
|(j, line)| Position { | ||
line: start.line + j as u32, | ||
column: if j == 0 { | ||
start.column + line.len() as u32 | ||
} else { | ||
line.len() as u32 | ||
}, | ||
}, | ||
); | ||
Range { | ||
start, | ||
end, | ||
start_byte: range.start().into(), | ||
end_byte: range.end().into(), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about the intention of this code, but we have a utility to extract columns and rows from the source code, I suggest to look here and see how we use it:
biome/crates/biome_diagnostics/src/display_github.rs
Lines 30 to 32 in cd6e830
let source = SourceFile::new(source_code); | |
let start = source.location(span.start())?; | |
let end = source.location(span.end())?; |
Severity::Error => 4, | ||
Severity::Fatal => 5, | ||
}), | ||
message: PrintDescription(self).to_string(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have an utility called markup_to_string
that you might want to look at:
biome/crates/biome_json_analyze/src/lib.rs
Lines 152 to 154 in cd6e830
let text = markup_to_string(markup! { | |
{PrintDiagnostic::verbose(&error)} | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately that one is only available in the biome_cli
crate. I'll see how far we get with the description and range. If necessary, I'll try to move the util.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not exactly that one, but we have it
biome/crates/biome_diagnostics/src/lib.rs
Lines 74 to 92 in 105f628
pub fn print_diagnostic_to_string(diagnostic: &Error) -> String { | |
let mut buffer = termcolor::Buffer::no_color(); | |
Formatter::new(&mut Termcolor(&mut buffer)) | |
.write_markup(markup! { | |
{PrintDiagnostic::verbose(diagnostic)} | |
}) | |
.expect("failed to emit diagnostic"); | |
let mut content = String::new(); | |
writeln!( | |
content, | |
"{}", | |
std::str::from_utf8(buffer.as_slice()).expect("non utf8 in error buffer") | |
) | |
.unwrap(); | |
content | |
} |
let token = node.value_token()?; | ||
let text = token.text_trimmed(); | ||
let range = node.syntax().text_trimmed_range().to_byte_range(); | ||
parse_snippet_content(&text[1..text.len() - 1], range, context, is_rhs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth leaving a comment explaining what we are trimming here, e.g. why there is a 1
and a -1
. For example, I don't know, even though I can guess it by looking at the PR
.map_or(None, |s| Some(DynamicPattern::Snippet(s))); | ||
Ok(Pattern::CodeSnippet(GritCodeSnippet { | ||
dynamic_snippet, | ||
source: source.to_owned(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed a lot of to_owned
in this PR. Internally, we prefer to use to_string
whenever possible unless it's a type that needs to use this function.
Summary
Grit patterns can contain snippet literals like such:
The part inside the backticks is a structural pattern that matches any function call node where the callee matches
console.log
and which has a single string argument with the contentfoo
.This PR updates the Grit crates and implements the parsing for snippet literals, so that our own Grit bindings invoke the Biome JS parser when a snippet like the above is encountered.
Test Plan
Added a quick test that helps with testing Grit patterns, although it still fails in the execution part. But at least it parses the query with a literal snippet now.