-
-
Notifications
You must be signed in to change notification settings - Fork 442
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use biome_diagnostics::{display::SourceFile, Diagnostic, PrintDescription, Severity}; | ||
use grit_util::AnalysisLog; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use crate::source_location_ext::SourceFileExt; | ||
|
||
pub trait GritAnalysisExt { | ||
fn to_log(&self, path: Option<&Path>) -> AnalysisLog; | ||
} | ||
|
||
impl<T> GritAnalysisExt for T | ||
where | ||
T: Diagnostic, | ||
{ | ||
fn to_log(&self, path: Option<&Path>) -> AnalysisLog { | ||
let location = self.location(); | ||
let source = location.source_code.map(SourceFile::new); | ||
|
||
let range = match (location.span, source) { | ||
(Some(range), Some(source)) => source.to_grit_range(range), | ||
_ => None, | ||
}; | ||
|
||
AnalysisLog { | ||
engine_id: Some("biome".to_owned()), | ||
file: path.map(Path::to_path_buf).or_else(|| { | ||
location | ||
.resource | ||
.and_then(|r| r.as_file().map(PathBuf::from)) | ||
}), | ||
level: Some(match self.severity() { | ||
Severity::Hint => 1, | ||
Severity::Information => 2, | ||
Severity::Warning => 3, | ||
Severity::Error => 4, | ||
Severity::Fatal => 5, | ||
}), | ||
message: PrintDescription(self).to_string(), | ||
position: range.as_ref().map(|r| r.start), | ||
range, | ||
syntax_tree: None, | ||
source: location.source_code.map(|s| s.text.to_string()), | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::{grit_analysis_ext::GritAnalysisExt, grit_tree::GritTree}; | ||
use biome_js_parser::{parse, JsParserOptions}; | ||
use biome_js_syntax::JsFileSource; | ||
use grit_util::{AnalysisLogs, FileOrigin, Parser, SnippetTree}; | ||
use std::path::Path; | ||
|
||
pub struct GritJsParser; | ||
|
||
impl Parser for GritJsParser { | ||
type Tree = GritTree; | ||
|
||
fn parse_file( | ||
&mut self, | ||
body: &str, | ||
path: Option<&Path>, | ||
logs: &mut AnalysisLogs, | ||
_old_tree: FileOrigin<'_, GritTree>, | ||
) -> Option<GritTree> { | ||
let parse_result = parse(body, JsFileSource::tsx(), JsParserOptions::default()); | ||
|
||
for diagnostic in parse_result.diagnostics() { | ||
logs.push(diagnostic.to_log(path)); | ||
} | ||
|
||
Some(GritTree::new(parse_result.syntax().into())) | ||
} | ||
|
||
fn parse_snippet( | ||
&mut self, | ||
prefix: &'static str, | ||
source: &str, | ||
postfix: &'static str, | ||
) -> SnippetTree<GritTree> { | ||
let context = format!("{prefix}{source}{postfix}"); | ||
|
||
let len = if cfg!(target_arch = "wasm32") { | ||
|src: &str| src.chars().count() as u32 | ||
} else { | ||
|src: &str| src.len() as u32 | ||
}; | ||
|
||
let parse_result = parse(&context, JsFileSource::tsx(), JsParserOptions::default()); | ||
|
||
SnippetTree { | ||
tree: GritTree::new(parse_result.syntax().into()), | ||
source: source.to_owned(), | ||
prefix, | ||
postfix, | ||
snippet_start: (len(prefix) + len(source) - len(source.trim_start())), | ||
snippet_end: (len(prefix) + len(source.trim_end())), | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
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
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