Skip to content

Commit

Permalink
Add option to reverse the check
Browse files Browse the repository at this point in the history
Meaning: Warn if there are :: in front of a class definition.

Fixes voxpupuli#17.
  • Loading branch information
baurmatt committed Feb 6, 2019
1 parent c3611c8 commit 7974217
Show file tree
Hide file tree
Showing 3 changed files with 398 additions and 150 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ 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). This plugin is **not** recommended for use with Puppet code that has dropped support for Puppet 3 (EOL 20161231).
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).

#### What you have done

Expand All @@ -42,6 +42,14 @@ include foobar
include ::foobar
```

#### Reverse this check

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

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

#### Disabling the check

To disable this check, you can add `--no-relative_classname_inclusion-check` to your puppet-lint command line.
Expand Down
38 changes: 26 additions & 12 deletions lib/puppet-lint/plugins/check_absolute_classname.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
PuppetLint.new_check(:relative_classname_inclusion) do
def check
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?
next if s.type == :FARROW

in_function = 0
while s.type != :NEWLINE
n = s.next_code_token
Expand All @@ -13,12 +22,12 @@ def check
in_function += 1
elsif in_function > 0 && n && n.type == :RPAREN
in_function -= 1
elsif in_function == 0 && s.value !~ /^::/
elsif in_function.zero? && s.value !~ /#{pattern}/
notify :warning, {
:message => 'class included by relative name',
:line => s.line,
:column => s.column,
:token => s,
message: message,
line: s.line,
column: s.column,
token: s
}
end
end
Expand All @@ -27,21 +36,26 @@ 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 !~ /^::/
if (s.type == :NAME || s.type == :SSTRING) && s.value !~ /#{pattern}/
notify :warning, {
:message => 'class included by relative name',
:line => s.line,
:column => s.column,
:token => s,
message: message,
line: s.line,
column: s.column,
token: s
}
end
s = s.next_token
end
end
end
end
end

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

0 comments on commit 7974217

Please sign in to comment.