Skip to content

Commit

Permalink
Fix block ignore not applied when multiple comments present (#422)
Browse files Browse the repository at this point in the history
* Prevent early exiting ignore comments in case there are more later

* Add test

* Update changelog
  • Loading branch information
JohnnyMorganz committed Mar 27, 2022
1 parent 30c713b commit 84b68b8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fixed issue through static linking where Windows binary would not execute due to missing `VCRUNTIME140.dll`. ([#413](https://github.com/JohnnyMorganz/StyLua/issues/413))
- Fixed assignment with comment sometimes not hanging leading to malformed syntax. ([#416](https://github.com/JohnnyMorganz/StyLua/issues/416))
- Fixed block ignores not applied when multiple leading block ignore comments are present at once. ([#421](https://github.com/JohnnyMorganz/StyLua/issues/421))

## [0.12.5] - 2022-03-08
### Fixed
Expand Down
47 changes: 25 additions & 22 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,36 @@ impl Context {
// To preserve immutability of Context, we return a new Context with the `formatting_disabled` field toggled or left the same
// where necessary. Context is cheap so this is reasonable to do.
pub fn check_toggle_formatting(&self, node: &impl Node) -> Self {
// Check comments
// Load all the leading comments from the token
let leading_trivia = node.surrounding_trivia().0;
for trivia in leading_trivia {
let comment_lines = match trivia.token_type() {
TokenType::SingleLineComment { comment } => comment,
TokenType::MultiLineComment { comment, .. } => comment,
_ => continue,
}
.lines()
.map(|line| line.trim());

for line in comment_lines {
if line == "stylua: ignore start" && !self.formatting_disabled {
return Self {
formatting_disabled: true,
..*self
};
} else if line == "stylua: ignore end" && self.formatting_disabled {
return Self {
formatting_disabled: false,
..*self
};
let comment_lines = leading_trivia
.iter()
.filter_map(|trivia| {
match trivia.token_type() {
TokenType::SingleLineComment { comment } => Some(comment),
TokenType::MultiLineComment { comment, .. } => Some(comment),
_ => None,
}
.map(|comment| comment.lines().map(|line| line.trim()))
})
.flatten();

// Load the current formatting disabled state
let mut formatting_disabled = self.formatting_disabled;

// Work through all the lines and update the state as necessary
for line in comment_lines {
if line == "stylua: ignore start" {
formatting_disabled = true;
} else if line == "stylua: ignore end" {
formatting_disabled = false;
}
}

*self
Self {
formatting_disabled,
..*self
}
}

/// Checks whether we should format the given node.
Expand Down
45 changes: 45 additions & 0 deletions tests/test_ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,48 @@ local bar = baz
local bar = baz
"###);
}

#[test]
fn test_multiline_block_ignore_multiple_comments_in_leading_trivia() {
insta::assert_snapshot!(
format(
r###"--stylua: ignore start
local a = 1
--stylua: ignore end
--stylua: ignore start
local b = 2
--stylua: ignore end
--stylua: ignore start
local c = 3
--stylua: ignore end
-- Some very large comment
--stylua: ignore start
local d = 4
--stylua: ignore end
"###
),
@r###"
--stylua: ignore start
local a = 1
--stylua: ignore end
--stylua: ignore start
local b = 2
--stylua: ignore end
--stylua: ignore start
local c = 3
--stylua: ignore end
-- Some very large comment
--stylua: ignore start
local d = 4
--stylua: ignore end
"###
)
}

0 comments on commit 84b68b8

Please sign in to comment.