Skip to content

Commit feb4e4a

Browse files
authored
Merge pull request #708 from rodjek/chain_fix_whitespace
Chaining arrow syntax fix introduces trailing whitespaces
2 parents 813522c + 96ae610 commit feb4e4a

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

lib/puppet-lint/plugins/check_classes/arrow_on_right_operand_line.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ def check
1919

2020
def fix(problem)
2121
token = problem[:token]
22-
remove_token(token)
2322

24-
# remove any excessive whitespace on the line
25-
temp_token = token.prev_code_token
26-
while (temp_token = temp_token.next_token)
27-
remove_token(temp_token) if whitespace?(temp_token)
28-
break if temp_token.type == :NEWLINE
23+
prev_code_token = token.prev_code_token
24+
next_code_token = token.next_code_token
25+
indent_token = prev_code_token.prev_token_of(:INDENT)
26+
27+
# Delete all tokens between the two code tokens the anchor is between
28+
temp_token = prev_code_token
29+
while (temp_token = temp_token.next_token) && (temp_token != next_code_token)
30+
remove_token(temp_token) unless temp_token == token
2931
end
3032

31-
index = tokens.index(token.next_code_token)
32-
add_token(index, token)
33+
# Insert a newline and an indent before the arrow
34+
index = tokens.index(token)
35+
newline_token = PuppetLint::Lexer::Token.new(:NEWLINE, "\n", token.line, 0)
36+
add_token(index, newline_token)
37+
add_token(index + 1, indent_token) if indent_token
3338

34-
whitespace_token = PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', temp_token.line + 1, 3)
39+
# Insert a space between the arrow and the following code token
40+
index = tokens.index(token)
41+
whitespace_token = PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', token.line, 3)
3542
add_token(index + 1, whitespace_token)
3643
end
37-
38-
def whitespace?(token)
39-
Set[:INDENT, :WHITESPACE].include?(token.type)
40-
end
4144
end

spec/puppet-lint/plugins/check_classes/arrow_on_right_operand_line_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
context 'arrow on the line of left operand' do
1313
let(:code) do
14-
"Package['httpd'] #{operator}
14+
"
15+
Package['httpd'] #{operator}
1516
Service['httpd']"
1617
end
1718

@@ -27,7 +28,8 @@
2728
end
2829

2930
let(:fixed) do
30-
"Package['httpd']
31+
"
32+
Package['httpd']
3133
#{operator} Service['httpd']"
3234
end
3335

0 commit comments

Comments
 (0)