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

Add option to reverse the check #18

Merged
merged 1 commit into from
Feb 6, 2019
Merged
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
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