Skip to content

Commit

Permalink
[Fix #466] Make AvoidGlobalVars configurable
Browse files Browse the repository at this point in the history
The cop now has a config option called `AllowedVariables`.
It also accepts by default built-in JRuby global variables.
  • Loading branch information
Bozhidar Batsov committed Sep 10, 2013
1 parent 9fe4469 commit 2e1cdd6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* [#469](https://github.com/bbatsov/rubocop/issues/469) - Extracted useless setter call tracking part of `UselessAssignment` cop to `UselessSetterCall`.
* [#469](https://github.com/bbatsov/rubocop/issues/469) - Merged `UnusedLocalVariable` cop into `UselessAssignment`.
* [#458](https://github.com/bbatsov/rubocop/issues/458) - The merged `UselessAssignment` cop now has advanced logic that tracks not only assignment at the end of the method but also every assignment in every scope.
* [#466](https://github.com/bbatsov/rubocop/issues/466) - Allow built-in JRuby global vars in `AvoidGlobalVars`
* Added a config option `AllowedVariables` to `AvoidGlobalVars` to allow users to whitelist certain global variables

### Bugs fixed

Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ RegexpLiteral:
`MaxSlashes` '/' character.
MaxSlashes: 1

# Built-in global variables are allowed by default.
AvoidGlobalVars:
AllowedVariables: []

# Align with the style guide.
CollectionMethods:
PreferredMethods:
Expand Down
19 changes: 18 additions & 1 deletion lib/rubocop/cop/style/avoid_global_vars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module Cop
module Style
# This cops looks for uses of global variables.
# It does not report offences for built-in global variables.
# Built-in global variables are allowed by default. Additionally
# users can allow additional variables via the AllowedVariables option.
#
# Note that backreferences like $1, $2, etc are not global variables.
class AvoidGlobalVars < Cop
MSG = 'Do not introduce global variables.'

Expand Down Expand Up @@ -36,8 +40,21 @@ class AvoidGlobalVars < Cop
$stdin $stdout $stderr
$DEBUG $FILENAME $VERBOSE $SAFE
$-0 $-a $-d $-F $-i $-I $-l $-p $-v $-w
$CLASSPATH $JRUBY_VERSION $JRUBY_REVISION $ENV_JAVA
).map(&:to_sym)

def user_vars
if cop_config['AllowedVariables']
cop_config['AllowedVariables'].map(&:to_sym)
else
{}

This comment has been minimized.

Copy link
@phs

phs Sep 10, 2013

Should this be []?

This comment has been minimized.

Copy link
@bbatsov

bbatsov Sep 10, 2013

Collaborator

Absolutely! Thanks for noticing!

end
end

def allowed_var?(global_var)
BUILT_IN_VARS.include?(global_var) || user_vars.include?(global_var)
end

def on_gvar(node)
check(node)
end
Expand All @@ -49,7 +66,7 @@ def on_gvasgn(node)
def check(node)
global_var, = *node

convention(node, :name) unless BUILT_IN_VARS.include?(global_var)
convention(node, :name) unless allowed_var?(global_var)
end
end
end
Expand Down
14 changes: 12 additions & 2 deletions spec/rubocop/cop/style/avoid_global_vars_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
module Rubocop
module Cop
module Style
describe AvoidGlobalVars do
subject(:cop) { AvoidGlobalVars.new }
describe AvoidGlobalVars, :config do
cop_config = {
'AllowedVariables' => ['$allowed']
}

subject(:cop) { described_class.new(config) }
let(:cop_config) { cop_config }

it 'registers an offence for $custom' do
inspect_source(cop, ['puts $custom'])
expect(cop.offences.size).to eq(1)
end

it 'allows user whitelisted variables' do
inspect_source(cop, ['puts $allowed'])
expect(cop.offences).to be_empty
end

AvoidGlobalVars::BUILT_IN_VARS.each do |var|
it "does not register an offence for built-in variable #{var}" do
inspect_source(cop, ["puts #{var}"])
Expand Down

0 comments on commit 2e1cdd6

Please sign in to comment.