Skip to content

Commit

Permalink
fix: removal of trailing comma lines (#416)
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Kumar Gupta <[email protected]>
  • Loading branch information
abhishek818 committed Jul 13, 2024
1 parent 9587e9e commit ad1ce31
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
18 changes: 17 additions & 1 deletion crates/core/src/inline_snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,28 @@ pub(crate) fn inline_sorted_snippets_with_offset(
}
}

let mut trailing_comma_lines = vec![];

for (range, snippet) in replacements {
let range = adjust_range(&range.effective_range(), offset, &code)?;
if range.start > code.len() || range.end > code.len() {
bail!("Range {:?} is out of bounds for code:\n{}\n", range, code);
}
code.replace_range(range, snippet);
code.replace_range(range.clone(), snippet);

// Check if the replacement results in any lines containing just a single comma
let line_start = code[..range.start].rfind('\n').map_or(0, |pos| pos + 1);
let line_end = code[range.start..].find('\n').map_or(code.len(), |pos| range.start + pos);

let line_content = code[line_start..line_end].trim();
if line_content == "," {
trailing_comma_lines.push(line_start..line_end+1);
}
}

// Remove lines with a trailing comma
for range in trailing_comma_lines {
code.replace_range(range, "");
}

Ok((code, output_ranges, original_ranges))
Expand Down
98 changes: 98 additions & 0 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12014,6 +12014,104 @@ fn trailing_comma_import_from_python_with_alias() {
.unwrap();
}

// refer https://github.com/getgrit/gritql/issues/416
#[test]
fn trailing_comma_after_argument_removal() {
run_test_expected({
TestArgExpected {
pattern: r#"
language python
`TaskMetadata($args)` where {
$args <: any {
contains `n_samples=$_` as $ns_kwarg where {
$ns_kwarg <: `n_samples = $ns_val` => .
},
contains `avg_character_length=$_` as $avg_kwarg where {
$avg_kwarg <: `avg_character_length = $avg_val` => `stats=GeneralDescriptiveStats(n_samples=$ns_val, avg_character_length=$avg_val)`
},
},
}
"#.to_owned(),
source: r#"
from pydantic import BaseModel
class TaskMetadata(BaseModel):
n_samples: dict[str, int]
avg_character_length: dict[str, float]
if __name__ == "__main__":
TaskMetadata(
name="TbilisiCityHallBitextMining",
dataset={
"path": "jupyterjazz/tbilisi-city-hall-titles",
"revision": "798bb599140565cca2dab8473035fa167e5ee602",
},
description="Parallel news titles from the Tbilisi City Hall website (https://tbilisi.gov.ge/).",
type="BitextMining",
category="s2s",
eval_splits=[_EVAL_SPLIT],
eval_langs=_EVAL_LANGS,
main_score="f1",
domains=["News"],
text_creation="created",
n_samples={_EVAL_SPLIT: 1820},
reference="https://huggingface.co/datasets/jupyterjazz/tbilisi-city-hall-titles",
date=("2024-05-02", "2024-05-03"),
form=["written"],
task_subtypes=[],
license="Not specified",
socioeconomic_status="mixed",
annotations_creators="derived",
dialect=[],
bibtex_citation="",
avg_character_length={_EVAL_SPLIT: 78},
)
"#
.to_owned(),
expected: r#"
from pydantic import BaseModel
class TaskMetadata(BaseModel):
n_samples: dict[str, int]
avg_character_length: dict[str, float]
if __name__ == "__main__":
TaskMetadata(
name="TbilisiCityHallBitextMining",
dataset={
"path": "jupyterjazz/tbilisi-city-hall-titles",
"revision": "798bb599140565cca2dab8473035fa167e5ee602",
},
description="Parallel news titles from the Tbilisi City Hall website (https://tbilisi.gov.ge/).",
type="BitextMining",
category="s2s",
eval_splits=[_EVAL_SPLIT],
eval_langs=_EVAL_LANGS,
main_score="f1",
domains=["News"],
text_creation="created",
reference="https://huggingface.co/datasets/jupyterjazz/tbilisi-city-hall-titles",
date=("2024-05-02", "2024-05-03"),
form=["written"],
task_subtypes=[],
license="Not specified",
socioeconomic_status="mixed",
annotations_creators="derived",
dialect=[],
bibtex_citation="",
stats=GeneralDescriptiveStats(n_samples={_EVAL_SPLIT: 1820}, avg_character_length={_EVAL_SPLIT: 78}),
)
"#
.to_owned(),
}
})
.unwrap();
}

#[test]
fn python_orphaned_from_imports() {
run_test_expected({
Expand Down

0 comments on commit ad1ce31

Please sign in to comment.