Skip to content

Commit

Permalink
chore(grit): compact snapshot range (#3455)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr authored Jul 16, 2024
1 parent 0119290 commit 0d2af99
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 89 deletions.
19 changes: 15 additions & 4 deletions crates/biome_grit_patterns/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn parse_test_path(file: &Path) -> (&str, &str) {
#[derive(Debug, Default)]
struct SnapshotResult {
messages: Vec<Message>,
matched_ranges: Vec<Range>,
matched_ranges: Vec<String>,
rewritten_files: Vec<OutputFile>,
created_files: Vec<OutputFile>,
}
Expand All @@ -105,18 +105,22 @@ impl SnapshotResult {
match result {
GritQueryResult::Match(m) => {
snapshot_result.messages.extend(m.messages);
snapshot_result.matched_ranges.extend(m.ranges);
snapshot_result
.matched_ranges
.extend(m.ranges.into_iter().map(format_range));
}
GritQueryResult::Rewrite(rewrite) => {
snapshot_result.messages.extend(rewrite.original.messages);
snapshot_result
.matched_ranges
.extend(rewrite.original.ranges);
.extend(rewrite.original.ranges.into_iter().map(format_range));
snapshot_result.rewritten_files.push(rewrite.rewritten);
}
GritQueryResult::CreateFile(create_file) => {
if let Some(ranges) = create_file.ranges {
snapshot_result.matched_ranges.extend(ranges);
snapshot_result
.matched_ranges
.extend(ranges.into_iter().map(format_range));
}
snapshot_result.created_files.push(create_file.rewritten);
}
Expand All @@ -126,3 +130,10 @@ impl SnapshotResult {
snapshot_result
}
}

fn format_range(range: Range) -> String {
format!(
"{}:{}-{}:{}",
range.start.line, range.start.column, range.end.line, range.end.column
)
}
13 changes: 1 addition & 12 deletions crates/biome_grit_patterns/tests/specs/ts/arrayType.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,7 @@ expression: arrayType
SnapshotResult {
messages: [],
matched_ranges: [
Range {
start: Position {
line: 2,
column: 22,
},
end: Position {
line: 2,
column: 30,
},
start_byte: 22,
end_byte: 30,
},
"2:22-2:30",
],
rewritten_files: [],
created_files: [],
Expand Down
39 changes: 3 additions & 36 deletions crates/biome_grit_patterns/tests/specs/ts/identifier.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,9 @@ expression: identifier
SnapshotResult {
messages: [],
matched_ranges: [
Range {
start: Position {
line: 3,
column: 8,
},
end: Position {
line: 3,
column: 11,
},
start_byte: 25,
end_byte: 28,
},
Range {
start: Position {
line: 6,
column: 3,
},
end: Position {
line: 6,
column: 6,
},
start_byte: 52,
end_byte: 55,
},
Range {
start: Position {
line: 11,
column: 2,
},
end: Position {
line: 11,
column: 5,
},
start_byte: 99,
end_byte: 102,
},
"3:8-3:11",
"6:3-6:6",
"11:2-11:5",
],
rewritten_files: [],
created_files: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`"`
10 changes: 10 additions & 0 deletions crates/biome_grit_patterns/tests/specs/ts/invalidSnippet.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: crates/biome_grit_patterns/tests/spec_tests.rs
expression: invalidSnippet
---
SnapshotResult {
messages: [],
matched_ranges: [],
rewritten_files: [],
created_files: [],
}
2 changes: 2 additions & 0 deletions crates/biome_grit_patterns/tests/specs/ts/invalidSnippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

// Not going to match anything.
13 changes: 1 addition & 12 deletions crates/biome_grit_patterns/tests/specs/ts/objectLiteral.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,7 @@ expression: objectLiteral
SnapshotResult {
messages: [],
matched_ranges: [
Range {
start: Position {
line: 3,
column: 15,
},
end: Position {
line: 6,
column: 6,
},
start_byte: 42,
end_byte: 85,
},
"3:15-6:6",
],
rewritten_files: [],
created_files: [],
Expand Down
26 changes: 2 additions & 24 deletions crates/biome_grit_patterns/tests/specs/ts/strings.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,8 @@ expression: strings
SnapshotResult {
messages: [],
matched_ranges: [
Range {
start: Position {
line: 2,
column: 11,
},
end: Position {
line: 2,
column: 16,
},
start_byte: 11,
end_byte: 16,
},
Range {
start: Position {
line: 3,
column: 11,
},
end: Position {
line: 3,
column: 16,
},
start_byte: 28,
end_byte: 33,
},
"2:11-2:16",
"3:11-3:16",
],
rewritten_files: [],
created_files: [],
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_grit_patterns/tests/specs/ts/strings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

const a = 'foo';
const b = "foo";
const c = `foo`;
const c = `foo`; // Template strings do not get matched with regular ones.

0 comments on commit 0d2af99

Please sign in to comment.