-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grit): parse Grit literal snippets (#3051)
- Loading branch information
Showing
18 changed files
with
463 additions
and
33 deletions.
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.