Skip to content

Commit

Permalink
Drop reverse and always require Puppet 4+ style
Browse files Browse the repository at this point in the history
Puppet 3 is dead and our manifests should be written in the modern
style.
  • Loading branch information
ekohl committed Nov 28, 2019
1 parent 95ce64f commit a6eaf52
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 396 deletions.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,18 @@ gem 'puppet-lint-absolute_classname-check', :require => false

### Relative class name inclusion

Including a class by a relative name might lead to unexpected results [in Puppet 3](https://docs.puppet.com/puppet/3/lang_namespaces.html#relative-name-lookup-and-incorrect-name-resolution).
Including a class by a relative name might lead to unexpected results [in Puppet 3](https://docs.puppet.com/puppet/3/lang_namespaces.html#relative-name-lookup-and-incorrect-name-resolution). That's why a lot of manifests explicitly include by the absolute name. Since Puppet 4 names are always absolute and this is no longer needed. This lint check helps to clean up your manifests.

#### What you have done

```puppet
include foobar
include ::foobar
```

#### What you should have done

```puppet
include ::foobar
```

#### Reverse this check

This check can be reversed to check for Puppet > 4.

```ruby
PuppetLint.configuration.absolute_classname_reverse = true
include foobar
```

#### Disabling the check
Expand Down
21 changes: 5 additions & 16 deletions lib/puppet-lint/plugins/check_absolute_classname.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
PuppetLint.new_check(:relative_classname_inclusion) do
def check
message = 'class included by absolute name (::$class)'

tokens.each_with_index do |token, token_idx|
reverse = PuppetLint.configuration.absolute_classname_reverse || false
if reverse
pattern = '^(?!::)'
message = 'class included by absolute name (::$class)'
else
pattern = '^::'
message = 'class included by relative name'
end
if [:NAME,:FUNCTION_NAME].include?(token.type) && ['include','contain','require'].include?(token.value)
s = token.next_code_token
next if s.nil?
Expand All @@ -22,7 +16,7 @@ def check
in_function += 1
elsif in_function > 0 && n && n.type == :RPAREN
in_function -= 1
elsif in_function.zero? && s.value !~ /#{pattern}/
elsif in_function.zero? && s.value.start_with?('::')
notify :warning, {
message: message,
line: s.line,
Expand All @@ -36,7 +30,7 @@ def check
elsif token.type == :CLASS and token.next_code_token.type == :LBRACE
s = token.next_code_token
while s.type != :COLON
if (s.type == :NAME || s.type == :SSTRING) && s.value !~ /#{pattern}/
if (s.type == :NAME || s.type == :SSTRING) && s.value.start_with?('::')
notify :warning, {
message: message,
line: s.line,
Expand All @@ -51,11 +45,6 @@ def check
end

def fix(problem)
reverse = PuppetLint.configuration.absolute_classname_reverse || false
problem[:token].value = if reverse
problem[:token].value[2..-1]
else
'::' + problem[:token].value
end
problem[:token].value = problem[:token].value[2..-1]
end
end
Loading

0 comments on commit a6eaf52

Please sign in to comment.