Skip to content

Commit

Permalink
--ignore-parser-error option.
Browse files Browse the repository at this point in the history
- we may tolerate some rewrites that produce parse errors
  • Loading branch information
dvmarcilio committed Nov 18, 2022
1 parent 027e6ca commit 1db699d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions polyglot/piranha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ piranha_summary = run_piranha_cli(path_to_codebase,
* `rules.toml`: *piranha rules* expresses the specific AST patterns to match and __replacement patterns__ for these matches (in-place). These rules can also specify the pre-built language specific cleanups to trigger.
* `edges.toml` (_optional_): expresses the flow between the rules
- `dry_run` : Disables in-place rewriting of code
- `ignore_parse_error` : Does not throw an error on syntactically incorrect source code produced by a rewrite.

<h5> Returns </h5>

Expand Down
3 changes: 3 additions & 0 deletions polyglot/piranha/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub(crate) struct CommandLineArguments {
/// Disables in-place rewriting of code
#[clap(short = 'd', long, parse(try_from_str), default_value_t = false)]
pub(crate) dry_run: bool,
/// Doesn't throw error on syntactically incorrect source code
#[clap(short = 'i', long, parse(try_from_str), default_value_t = false)]
pub(crate) ignore_parse_error: bool,
}

fn read_language_specific_rules(language_name: &str) -> Rules {
Expand Down
4 changes: 3 additions & 1 deletion polyglot/piranha/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ use tree_sitter::Node;
/// * path_to_codebase: Path to the root of the code base that Piranha will update
/// * path_to_configuration: Path to the directory that contains - `piranha_arguments.toml`, `rules.toml` and optionally `edges.toml`
/// * dry_run: determines if Piranha should actually update the code.
/// * ignore_parse_error: Piranha won't panic if a rewrite raises a parse error from tree-sitter.
///
/// Returns Piranha Output Summary for each file touched or analyzed by Piranha.
/// For each file, it reports its content after the rewrite, the list of matches and the list of rewrites.
#[pyfunction]
pub fn run_piranha_cli(
path_to_codebase: String, path_to_configurations: String, dry_run: bool,
path_to_codebase: String, path_to_configurations: String, dry_run: bool, ignore_parse_error: bool,
) -> Vec<PiranhaOutputSummary> {
let configuration = PiranhaArguments::new(CommandLineArguments {
path_to_codebase,
path_to_configurations,
path_to_output_summary: None,
dry_run,
ignore_parse_error,
});
execute_piranha(&configuration)
}
Expand Down
7 changes: 6 additions & 1 deletion polyglot/piranha/src/models/piranha_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub struct PiranhaArguments {
/// Disables in-place rewriting of code
#[getset(get = "pub")]
dry_run: bool,
/// Won't panic if a rewrite raises a tree-sitter parse error
#[getset(get = "pub")]
ignore_parse_error: bool,
}

impl PiranhaArguments {
Expand Down Expand Up @@ -102,7 +105,8 @@ impl PiranhaArguments {
.path_to_output_summaries(args.path_to_output_summary)
.language_name(piranha_args_from_config.language())
.language(piranha_args_from_config.language().get_language())
.dry_run(args.dry_run);
.dry_run(args.dry_run)
.ignore_parse_error(args.ignore_parse_error);

if let Some(v) = piranha_args_from_config.delete_file_if_empty() {
args_builder.delete_file_if_empty(v);
Expand Down Expand Up @@ -146,6 +150,7 @@ impl Default for PiranhaArguments {
cleanup_comments_buffer: 2,
cleanup_comments: false,
dry_run: false,
ignore_parse_error: false,
}
}
}
22 changes: 13 additions & 9 deletions polyglot/piranha/src/models/source_code_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,29 @@ impl SourceCodeUnit {
self.ast.edit(&ts_edit);
self._replace_file_contents_and_re_parse(&new_source_code, parser, true);
if self.ast.root_node().has_error() {
let msg = format!(
"Produced syntactically incorrect source code {}",
self.code()
);
error!("{}", msg);
panic!("{}", msg);
if self.piranha_arguments.ignore_parse_error().clone() {
debug!("Parse error ocurred, but ignore_parse_error == true");
} else {
let msg = format!(
"Produced syntactically incorrect source code {}",
self.code()
);
error!("{}", msg);
panic!("{}", msg);
}
}
ts_edit
}

/// Deletes the trailing comma after the {deleted_range}
/// # Arguments
/// * `deleted_range` - the range of the deleted code
///
///
/// # Returns
/// code range of the closest node
///
/// Algorithm:
/// Get the node after the {deleted_range}'s end byte (heuristic 5 characters)
/// Algorithm:
/// Get the node after the {deleted_range}'s end byte (heuristic 5 characters)
/// Traverse this node and get the node closest to the range {deleted_range}'s end byte
/// IF this closest node is a comma, extend the {new_delete_range} to include the comma.
fn delete_trailing_comma(&mut self, deleted_range: Range) -> Range {
Expand Down
2 changes: 2 additions & 0 deletions polyglot/piranha/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn run_match_test(relative_path_to_tests: &str, number_of_matches: usize) {
path_to_configurations: format!("{path_to_test_ff}/configurations/"),
path_to_output_summary: None,
dry_run: true,
ignore_parse_error: false,
});
let output_summaries = execute_piranha(&args);

Expand All @@ -77,6 +78,7 @@ fn run_rewrite_test(relative_path_to_tests: &str, n_files_changed: usize) {
path_to_configurations: format!("{path_to_test_ff}/configurations/"),
path_to_output_summary: None,
dry_run: true,
ignore_parse_error: false,
});
let output_summaries = execute_piranha(&args);
// Checks if there are any rewrites performed for the file
Expand Down
4 changes: 2 additions & 2 deletions polyglot/piranha/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@


def test_piranha_rewrite():
output_summary = run_piranha_cli('test-resources/java/feature_flag_system_1/treated/input', 'test-resources/java/feature_flag_system_1/treated/configurations', False)
output_summary = run_piranha_cli('test-resources/java/feature_flag_system_1/treated/input', 'test-resources/java/feature_flag_system_1/treated/configurations', False, False)
assert is_as_expected('test-resources/java/feature_flag_system_1/treated', output_summary)

def test_piranha_match_only():
output_summary = run_piranha_cli('test-resources/java/structural_find/input', 'test-resources/java/structural_find/configurations', False)
output_summary = run_piranha_cli('test-resources/java/structural_find/input', 'test-resources/java/structural_find/configurations', False, False)
assert len(output_summary[0].matches) == 20

def is_as_expected(path_to_scenario, output_summary):
Expand Down

0 comments on commit 1db699d

Please sign in to comment.