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

Allow to indent matchers of Collectors #40

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/puppet-lint/plugins/check_strict_indent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ def match(tokens)
RPAREN: :LPAREN,
HEREDOC: :HEREDOC_OPEN,
HEREDOC_POST: :HEREDOC_OPEN,
RCOLLECT: :LCOLLECT,
RRCOLLECT: :LLCOLLECT,
}
open = {
LBRACE: [],
LBRACK: [],
LPAREN: [],
HEREDOC_OPEN: [],
LCOLLECT: [],
LLCOLLECT: [],
}

matches = {}

tokens.each do |token|
if %i[LBRACE LBRACK LPAREN HEREDOC_OPEN].include?(token.type)
if %i[LBRACE LBRACK LPAREN HEREDOC_OPEN LCOLLECT LLCOLLECT].include?(token.type)
open[token.type] << token
elsif %i[RBRACE RBRACK RPAREN HEREDOC HEREDOC_POST].include?(token.type)
elsif %i[RBRACE RBRACK RPAREN HEREDOC HEREDOC_POST RCOLLECT RRCOLLECT].include?(token.type)
match = open[opening_token[token.type]].pop
unless match.nil?
matches[token] = match
Expand Down Expand Up @@ -54,7 +58,7 @@ def check
prev_token = token.prev_token
while !prev_token.nil? and prev_token.type != :NEWLINE
temp_indent += 1 if prev_token.type == :HEREDOC_OPEN
if %i[LBRACE LBRACK
if %i[LBRACE LBRACK LCOLLECT LLCOLLECT
LPAREN].include?(prev_token.type) && (matches[prev_token].nil? or matches[prev_token].line > prev_token.line)
# left braces not matched in the same line increase indent
open_groups += 1
Expand Down Expand Up @@ -97,7 +101,7 @@ def check
# unindent for closing brackets in the current line
next_token = token.next_token
while !next_token.nil? and next_token.type != :NEWLINE
if %i[RBRACE RBRACK RPAREN].include?(next_token.type)
if %i[RBRACE RBRACK RPAREN RCOLLECT RRCOLLECT].include?(next_token.type)
if !matches[next_token].nil? and matches[next_token].line < next_token.line
# right braces matched in a previous line decrease indent
indent -= 1
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/fail/collectors.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Ressource <|
$tag == 'foo'
|>
7 changes: 7 additions & 0 deletions spec/fixtures/pass/collectors.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Ressource <<|
$tag == 'foo'
|>>

Ressource <|
$tag == 'foo'
|>
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'rspec/collection_matchers'

begin
require 'simplecov'
require 'simplecov-console'
Expand Down
Loading