-
-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grit): implement disregarded snippet nodes (#4084)
- Loading branch information
Showing
19 changed files
with
272 additions
and
20 deletions.
There are no files selected for viewing
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
51 changes: 51 additions & 0 deletions
51
crates/biome_grit_patterns/src/grit_target_language/js_target_language/constants.rs
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
1 change: 1 addition & 0 deletions
1
crates/biome_grit_patterns/tests/specs/ts/asyncFunctionDoesntMatchNonAsync.grit
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 @@ | ||
`async function foo() {}` |
12 changes: 12 additions & 0 deletions
12
crates/biome_grit_patterns/tests/specs/ts/asyncFunctionDoesntMatchNonAsync.snap
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,12 @@ | ||
--- | ||
source: crates/biome_grit_patterns/tests/spec_tests.rs | ||
expression: asyncFunctionDoesntMatchNonAsync | ||
--- | ||
SnapshotResult { | ||
messages: [], | ||
matched_ranges: [ | ||
"3:1-3:23", | ||
], | ||
rewritten_files: [], | ||
created_files: [], | ||
} |
3 changes: 3 additions & 0 deletions
3
crates/biome_grit_patterns/tests/specs/ts/asyncFunctionDoesntMatchNonAsync.ts
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,3 @@ | ||
function foo() {} | ||
function bar(){} | ||
async function foo(){} |
1 change: 1 addition & 0 deletions
1
crates/biome_grit_patterns/tests/specs/ts/functionMatchesAsync.grit
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 @@ | ||
`function foo() {}` |
13 changes: 13 additions & 0 deletions
13
crates/biome_grit_patterns/tests/specs/ts/functionMatchesAsync.snap
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,13 @@ | ||
--- | ||
source: crates/biome_grit_patterns/tests/spec_tests.rs | ||
expression: functionMatchesAsync | ||
--- | ||
SnapshotResult { | ||
messages: [], | ||
matched_ranges: [ | ||
"1:1-1:18", | ||
"3:1-3:23", | ||
], | ||
rewritten_files: [], | ||
created_files: [], | ||
} |
3 changes: 3 additions & 0 deletions
3
crates/biome_grit_patterns/tests/specs/ts/functionMatchesAsync.ts
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,3 @@ | ||
function foo() {} | ||
function bar(){} | ||
async function foo(){} |
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 @@ | ||
`type $T = $U` |
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,13 @@ | ||
--- | ||
source: crates/biome_grit_patterns/tests/spec_tests.rs | ||
expression: typeAlias | ||
--- | ||
SnapshotResult { | ||
messages: [], | ||
matched_ranges: [ | ||
"1:1-1:19", | ||
"2:1-2:19", | ||
], | ||
rewritten_files: [], | ||
created_files: [], | ||
} |
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,2 @@ | ||
type Foo = string; | ||
type Bar = number; |
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,49 @@ | ||
use crate::{ | ||
js_kinds_src::{AstSrc, Field}, | ||
language_kind::LanguageKind, | ||
}; | ||
use biome_string_case::Case; | ||
use xtask::Result; | ||
|
||
pub fn generate_target_language_constants( | ||
ast: &AstSrc, | ||
_language_kind: LanguageKind, | ||
) -> Result<String> { | ||
let disregarded_slots: Vec<String> = ast | ||
.nodes | ||
.iter() | ||
.flat_map(|node| { | ||
let node_kind = Case::Constant.convert(node.name.as_str()); | ||
node.fields | ||
.iter() | ||
.enumerate() | ||
.filter_map(|(index, field)| match field { | ||
Field::Token { name, optional, .. } => Some((index, name, optional)), | ||
Field::Node { .. } => None, | ||
}) | ||
// TODO: We might want to move this to `js_kinds_src.rs` when we | ||
// start supporting other languages with Grit. | ||
.filter_map(|(index, name, optional)| match (name.as_str(), optional) { | ||
("async", true) => Some(format!("({node_kind}, {index}, OnlyIf(&[\"\"])),")), | ||
(";", true) => Some(format!("({node_kind}, {index}, Always),")), | ||
_ => None, | ||
}) | ||
.collect::<Vec<_>>() | ||
}) | ||
.collect(); | ||
let disregarded_slots = disregarded_slots.join("\n "); | ||
|
||
let result = format!( | ||
"use crate::grit_target_language::DisregardedSlotCondition::{{self, *}}; | ||
use biome_js_syntax::JsSyntaxKind::{{self, *}}; | ||
pub(crate) const DISREGARDED_SNIPPET_SLOTS: &[(JsSyntaxKind, u32, DisregardedSlotCondition)] = &[ | ||
{disregarded_slots} | ||
]; | ||
" | ||
); | ||
|
||
let pretty = xtask::reformat(result)?; | ||
|
||
Ok(pretty) | ||
} |
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.