Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove blank lines instead of emptying it's contents #667

Closed
wants to merge 1 commit into from

Conversation

Bennet-Sunder
Copy link
Contributor

Fixes #664

@@ -51,6 +51,10 @@ pub struct Match {
#[get_mut]
#[serde(skip)]
pub(crate) associated_comments: Vec<Range>,
#[get]
#[get_mut]
#[serde(skip)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not just empty lines right? All leading white spaes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only empty lines, we match leading white spaces that starts with a new line character.
https://github.com/uber/piranha/pull/667/files#diff-b157ce35dcfb24fa5ff9bfb7323302e655455ad2a78bb484fdd2e855d7e740f4R143

@Bennet-Sunder Bennet-Sunder force-pushed the remove_blank_lines branch 2 times, most recently from 0af76d8 to a6c5f6b Compare June 2, 2024 16:37
@Bennet-Sunder
Copy link
Contributor Author

@dvmarcilio Could you pls have a look at this PR when you have bandwidth.

@stefanheule
Copy link
Collaborator

@Bennet-Sunder Thanks a lot for the contribution. We are still in the process of testing this internally to make sure it doesn't break any existing use-cases, but will try to merge as soon as possible.

end_point: Point { row: 1, column: 8 }
}
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Bennet-Sunder ,

Thank you for your contribution! I tested this internally and noticed that the change might break some rewrites. The issue is that in some grammars (particularly Swift), whitespace and newlines are part of the node. If you delete them, you might accidentally delete parts of a sibling node unrelated to the node you're deleting.

Please accept this test suggestion to see what I mean. The test should fail with your change (make sure to add Swift at the top). My proposed solution is to delete only whitespace or newlines until you find the end_byte of the previous sibling node. Let me know if you need help with this.

Suggested change
}
}
#[test]
fn test_switch_entry_blank_lines() {
let inline_cleanup_rule = piranha_rule! {
name= "inline_cleanup_rule",
query= "
(
(switch_entry
(switch_pattern) @p1
(switch_pattern) @p2
(switch_pattern) @p3
(switch_pattern) @p4
(switch_pattern) @p5
(switch_pattern) @p6
) @custom_entry
(#eq? @p1 \".case_zeta_first\")
)
",
replace_node = "custom_entry",
replace = ""
};
let inline_rule = InstantiatedRule::new(&inline_cleanup_rule, &HashMap::new());
let source_code = r#"
public var namespace: ParameterNamespace {
switch self {
case .case_random_word_alpha,
.case_random_word_beta,
.case_random_word_gamma,
.case_random_word_delta:
return .namespace_group_alpha
case .case_zeta_first,
.case_zeta_second,
.case_zeta_third,
.case_zeta_fourth,
.case_zeta_fifth,
.case_zeta_sixth:
return .namespace_group_beta
case .case_random_word_omega:
return .namespace_group_gamma
}
}
"#
.trim();
let piranha_arguments = PiranhaArgumentsBuilder::default()
.paths_to_codebase(vec![UNUSED_CODE_PATH.to_string()])
.language(PiranhaLanguage::from(SWIFT))
.build();
let mut rule_store = RuleStore::new(&piranha_arguments);
let mut parser = piranha_arguments.language().parser();
let mut source_code_unit = SourceCodeUnit::new(
&mut parser,
source_code.to_string(),
&HashMap::new(),
PathBuf::new().as_path(),
&piranha_arguments,
);
source_code_unit.apply_rule(inline_rule, &mut rule_store, &mut parser, &None);
let transformed_code = source_code_unit.code();
let expected_code = r#"
public var namespace: ParameterNamespace {
switch self {
case .case_random_word_alpha,
.case_random_word_beta,
.case_random_word_gamma,
.case_random_word_delta:
return .namespace_group_alpha
case .case_random_word_omega:
return .namespace_group_gamma
}
}
"#
.trim();
assert_eq!(transformed_code, expected_code);
}

@@ -16,7 +16,7 @@ use tree_sitter::Parser;
use crate::{
filter,
models::{
default_configs::{JAVA, UNUSED_CODE_PATH},
default_configs::{JAVA, RUBY, UNUSED_CODE_PATH},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default_configs::{JAVA, RUBY, UNUSED_CODE_PATH},
default_configs::{JAVA, RUBY, SWIFT, UNUSED_CODE_PATH},

@Bennet-Sunder
Copy link
Contributor Author

@stefanheule @danieltrt Thanks for your review of the PR. I can see that a new PR is raised with the fixes. I'll close this PR in favour of the newer one.

stefanheule pushed a commit that referenced this pull request Sep 2, 2024
Fixes the PR #667 for feature request #664

---------

Co-authored-by: Bennet Sunder <bennet.sunder@freshworks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove emptied lines instead of emptying only the contents of the line
4 participants