Skip to content

Commit 84b68b8

Browse files
Fix block ignore not applied when multiple comments present (#422)
* Prevent early exiting ignore comments in case there are more later * Add test * Update changelog
1 parent 30c713b commit 84b68b8

File tree

3 files changed

+71
-22
lines changed

3 files changed

+71
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
### Fixed
1818
- Fixed issue through static linking where Windows binary would not execute due to missing `VCRUNTIME140.dll`. ([#413](https://github.com/JohnnyMorganz/StyLua/issues/413))
1919
- Fixed assignment with comment sometimes not hanging leading to malformed syntax. ([#416](https://github.com/JohnnyMorganz/StyLua/issues/416))
20+
- Fixed block ignores not applied when multiple leading block ignore comments are present at once. ([#421](https://github.com/JohnnyMorganz/StyLua/issues/421))
2021

2122
## [0.12.5] - 2022-03-08
2223
### Fixed

src/context.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,36 @@ impl Context {
4444
// To preserve immutability of Context, we return a new Context with the `formatting_disabled` field toggled or left the same
4545
// where necessary. Context is cheap so this is reasonable to do.
4646
pub fn check_toggle_formatting(&self, node: &impl Node) -> Self {
47-
// Check comments
47+
// Load all the leading comments from the token
4848
let leading_trivia = node.surrounding_trivia().0;
49-
for trivia in leading_trivia {
50-
let comment_lines = match trivia.token_type() {
51-
TokenType::SingleLineComment { comment } => comment,
52-
TokenType::MultiLineComment { comment, .. } => comment,
53-
_ => continue,
54-
}
55-
.lines()
56-
.map(|line| line.trim());
57-
58-
for line in comment_lines {
59-
if line == "stylua: ignore start" && !self.formatting_disabled {
60-
return Self {
61-
formatting_disabled: true,
62-
..*self
63-
};
64-
} else if line == "stylua: ignore end" && self.formatting_disabled {
65-
return Self {
66-
formatting_disabled: false,
67-
..*self
68-
};
49+
let comment_lines = leading_trivia
50+
.iter()
51+
.filter_map(|trivia| {
52+
match trivia.token_type() {
53+
TokenType::SingleLineComment { comment } => Some(comment),
54+
TokenType::MultiLineComment { comment, .. } => Some(comment),
55+
_ => None,
6956
}
57+
.map(|comment| comment.lines().map(|line| line.trim()))
58+
})
59+
.flatten();
60+
61+
// Load the current formatting disabled state
62+
let mut formatting_disabled = self.formatting_disabled;
63+
64+
// Work through all the lines and update the state as necessary
65+
for line in comment_lines {
66+
if line == "stylua: ignore start" {
67+
formatting_disabled = true;
68+
} else if line == "stylua: ignore end" {
69+
formatting_disabled = false;
7070
}
7171
}
7272

73-
*self
73+
Self {
74+
formatting_disabled,
75+
..*self
76+
}
7477
}
7578

7679
/// Checks whether we should format the given node.

tests/test_ignore.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,48 @@ local bar = baz
211211
local bar = baz
212212
"###);
213213
}
214+
215+
#[test]
216+
fn test_multiline_block_ignore_multiple_comments_in_leading_trivia() {
217+
insta::assert_snapshot!(
218+
format(
219+
r###"--stylua: ignore start
220+
local a = 1
221+
--stylua: ignore end
222+
223+
--stylua: ignore start
224+
local b = 2
225+
--stylua: ignore end
226+
227+
--stylua: ignore start
228+
local c = 3
229+
--stylua: ignore end
230+
231+
-- Some very large comment
232+
233+
--stylua: ignore start
234+
local d = 4
235+
--stylua: ignore end
236+
"###
237+
),
238+
@r###"
239+
--stylua: ignore start
240+
local a = 1
241+
--stylua: ignore end
242+
243+
--stylua: ignore start
244+
local b = 2
245+
--stylua: ignore end
246+
247+
--stylua: ignore start
248+
local c = 3
249+
--stylua: ignore end
250+
251+
-- Some very large comment
252+
253+
--stylua: ignore start
254+
local d = 4
255+
--stylua: ignore end
256+
"###
257+
)
258+
}

0 commit comments

Comments
 (0)