rubocop 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. data/.rubocop.yml +78 -0
  2. data/CHANGELOG.md +22 -0
  3. data/CONTRIBUTING.md +13 -6
  4. data/Gemfile.lock +3 -3
  5. data/README.md +42 -1
  6. data/VERSION +1 -1
  7. data/bin/rubocop +1 -0
  8. data/lib/rubocop/cli.rb +47 -18
  9. data/lib/rubocop/cop/alias.rb +21 -0
  10. data/lib/rubocop/cop/align_parameters.rb +5 -4
  11. data/lib/rubocop/cop/avoid_class_vars.rb +20 -0
  12. data/lib/rubocop/cop/avoid_for.rb +17 -0
  13. data/lib/rubocop/cop/avoid_perl_backrefs.rb +20 -0
  14. data/lib/rubocop/cop/avoid_perlisms.rb +55 -0
  15. data/lib/rubocop/cop/blocks.rb +1 -1
  16. data/lib/rubocop/cop/cop.rb +1 -1
  17. data/lib/rubocop/cop/favor_sprintf.rb +45 -0
  18. data/lib/rubocop/cop/grammar.rb +3 -3
  19. data/lib/rubocop/cop/offence.rb +3 -1
  20. data/lib/rubocop/cop/semicolon.rb +18 -0
  21. data/lib/rubocop/cop/space_after_comma_etc.rb +2 -1
  22. data/lib/rubocop/cop/string_literals.rb +12 -0
  23. data/lib/rubocop/cop/symbol_snake_case.rb +27 -0
  24. data/lib/rubocop/cop/syntax.rb +14 -2
  25. data/lib/rubocop/cop/variable_interpolation.rb +20 -0
  26. data/lib/rubocop/cop/when_then.rb +3 -0
  27. data/lib/rubocop/report/emacs_style.rb +2 -2
  28. data/lib/rubocop/version.rb +1 -1
  29. data/lib/rubocop.rb +9 -0
  30. data/rubocop.gemspec +21 -2
  31. data/spec/rubocop/cli_spec.rb +68 -1
  32. data/spec/rubocop/cops/alias_spec.rb +27 -0
  33. data/spec/rubocop/cops/align_parameters_spec.rb +14 -5
  34. data/spec/rubocop/cops/avoid_class_vars_spec.rb +20 -0
  35. data/spec/rubocop/cops/avoid_for_spec.rb +22 -0
  36. data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
  37. data/spec/rubocop/cops/avoid_perlisms_spec.rb +32 -0
  38. data/spec/rubocop/cops/favor_sprintf_spec.rb +50 -0
  39. data/spec/rubocop/cops/grammar_spec.rb +3 -7
  40. data/spec/rubocop/cops/offence_spec.rb +6 -0
  41. data/spec/rubocop/cops/semicolon_spec.rb +29 -0
  42. data/spec/rubocop/cops/string_literals_spec.rb +4 -1
  43. data/spec/rubocop/cops/symbol_snake_case_spec.rb +62 -0
  44. data/spec/rubocop/cops/syntax_spec.rb +16 -0
  45. data/spec/rubocop/cops/variable_interpolation_spec.rb +45 -0
  46. data/spec/rubocop/cops/when_then_spec.rb +7 -0
  47. metadata +22 -3
data/.rubocop.yml CHANGED
@@ -1,129 +1,207 @@
1
1
  # This is the default configuration file with all checking enabled. It is also
2
2
  # the configuration used to check the rubocop source code.
3
3
 
4
+ # Use UTF-8 as the source file encoding.
4
5
  Encoding:
5
6
  Enabled: true
6
7
 
8
+ # Limit lines to 80 characters.
7
9
  LineLength:
8
10
  Enabled: true
9
11
  Max: 79
10
12
 
13
+ # No hard tabs.
11
14
  Tab:
12
15
  Enabled: true
13
16
 
17
+ # Avoid trailing whitespace.
14
18
  TrailingWhitespace:
15
19
  Enabled: true
16
20
 
21
+ # Indent when as deep as case.
17
22
  Indentation:
18
23
  Enabled: true
19
24
 
25
+ # Use empty lines between defs.
20
26
  EmptyLines:
21
27
  Enabled: true
22
28
 
29
+ # Use spaces around operators.
23
30
  SpaceAroundOperators:
24
31
  Enabled: true
25
32
 
33
+ # Use spaces around { and before }.
26
34
  SpaceAroundBraces:
27
35
  Enabled: true
28
36
 
37
+ # No spaces after ( or before ).
29
38
  SpaceInsideParens:
30
39
  Enabled: true
31
40
 
41
+ # No spaces after [ or before ].
32
42
  SpaceInsideBrackets:
33
43
  Enabled: true
34
44
 
45
+ # Use spaces after commas.
35
46
  SpaceAfterComma:
36
47
  Enabled: true
37
48
 
49
+ # Use spaces after semicolons.
38
50
  SpaceAfterSemicolon:
39
51
  Enabled: true
40
52
 
53
+ # Use spaces after colons.
41
54
  SpaceAfterColon:
42
55
  Enabled: true
43
56
 
57
+ # Prefer symbols instead of strings as hash keys.
44
58
  HashSyntax:
45
59
  Enabled: true
46
60
 
61
+ # Use Unix-style line endings.
47
62
  EndOfLine:
48
63
  Enabled: true
49
64
 
65
+ # Add underscores to large numeric literals to improve their readability.
50
66
  NumericLiterals:
51
67
  Enabled: true
52
68
 
69
+ # Align the parameters of a method call if they span more than one line.
53
70
  AlignParameters:
54
71
  Enabled: true
55
72
 
73
+ # Use def with parentheses when there are arguments.
56
74
  DefWithParentheses:
57
75
  Enabled: true
58
76
 
77
+ # Omit the parentheses when the method doesn't accept any arguments.
59
78
  DefWithoutParentheses:
60
79
  Enabled: true
61
80
 
81
+ # Never use if x; .... Use the ternary operator instead.
62
82
  IfWithSemicolon:
63
83
  Enabled: true
64
84
 
85
+ # Never use then for multi-line if/unless.
65
86
  MultilineIfThen:
66
87
  Enabled: true
67
88
 
89
+ # Favor the ternary operator(?:) over if/then/else/end constructs.
68
90
  OneLineConditional:
69
91
  Enabled: true
70
92
 
93
+ # Avoid using {...} for multi-line blocks (multiline chaining is always ugly).
71
94
  MultilineBlocks:
72
95
  Enabled: true
73
96
 
97
+ # Prefer {...} over do...end for single-line blocks.
74
98
  SingleLineBlocks:
75
99
  Enabled: true
76
100
 
101
+ # Avoid parameter lists longer than three or four parameters.
77
102
  ParameterLists:
78
103
  Enabled: true
79
104
 
105
+ # Prefer ' strings when you don't need string interpolation or special symbols.
80
106
  StringLiterals:
81
107
  Enabled: true
82
108
 
109
+ # Avoid multi-line ?: (the ternary operator); use if/unless instead.
83
110
  MultilineTernaryOperator:
84
111
  Enabled: true
85
112
 
113
+ # Use one expression per branch in a ternary operator.
86
114
  NestedTernaryOperator:
87
115
  Enabled: true
88
116
 
117
+ # Never use unless with else. Rewrite these with the positive case first.
89
118
  UnlessElse:
90
119
  Enabled: true
91
120
 
121
+ # Use &&/|| for boolean expressions, and/or for control flow.
92
122
  AmpersandsPipesVsAndOr:
93
123
  Enabled: true
94
124
 
125
+ # Use when x then ... for one-line cases.
95
126
  WhenThen:
96
127
  Enabled: true
97
128
 
129
+ # Favor modifier if/unless usage when you have a single-line body.
98
130
  IfUnlessModifier:
99
131
  Enabled: true
100
132
 
133
+ # Favor modifier while/until usage when you have a single-line body.
101
134
  WhileUntilModifier:
102
135
  Enabled: true
103
136
 
137
+ # Favor unless over if for negative conditions (or control flow or).
104
138
  FavorUnlessOverNegatedIf:
105
139
  Enabled: true
106
140
 
141
+ # Favor until over while for negative conditions.
107
142
  FavorUntilOverNegatedWhile:
108
143
  Enabled: true
109
144
 
145
+ # Use spaces around the = operator when assigning default values in def params.
110
146
  SpaceAroundEqualsInParameterDefault:
111
147
  Enabled: true
112
148
 
149
+ # Use the new lambda literal syntax.
113
150
  NewLambdaLiteral:
114
151
  Enabled: true
115
152
 
153
+ # Don't use parentheses around the condition of an if/unless/while.
116
154
  ParenthesesAroundCondition:
117
155
  Enabled: true
118
156
 
157
+ # Use snake_case for symbols, methods and variables.
119
158
  MethodAndVariableSnakeCase:
120
159
  Enabled: true
121
160
 
161
+ # Use CamelCase for classes and modules.
122
162
  ClassAndModuleCamelCase:
123
163
  Enabled: true
124
164
 
165
+ # Causes Ruby to check the syntax of the script and exit without executing.
125
166
  Syntax:
126
167
  Enabled: true
127
168
 
169
+ # Preferred collection methods.
128
170
  CollectionMethods:
129
171
  Enabled: true
172
+
173
+ # Prefer each over for.
174
+ AvoidFor:
175
+ Enabled: true
176
+
177
+ # Avoid Perl-style global variables.
178
+ AvoidPerlisms:
179
+ Enabled: true
180
+
181
+ # Avoid Perl-style regex back references.
182
+ AvoidPerlBackrefs:
183
+ Enabled: true
184
+
185
+ # Avoid the use of class variables.
186
+ AvoidClassVars:
187
+ Enabled: true
188
+
189
+ # Symbol literals should use snake_case.
190
+ SymbolSnakeCase:
191
+ Enabled: true
192
+
193
+ # Don't interpolate global, instance and class variables directly in strings.
194
+ VariableInterpolation:
195
+ Enabled: true
196
+
197
+ # Don't use semicolons to terminate expressions.
198
+ Semicolon:
199
+ Enabled: true
200
+
201
+ # Use sprintf instead of %
202
+ FavorSprintf:
203
+ Enabled: true
204
+
205
+ # Use alias_method instead of alias.
206
+ Alias:
207
+ Enabled: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ ## master (unreleased)
4
+
5
+ ### New features
6
+
7
+ ### Bugs fixed
8
+
9
+ ## 0.5.0 (04/17/2013)
10
+
11
+ ### New features
12
+
13
+ * New cop `FavorSprintf` that checks for usages of `String#%`
14
+ * New cop `Semicolon` that checks for usages of `;` as expression separator
15
+ * New cop `VariableInterpolation` that checks for variable interpolation in double quoted strings
16
+ * New cop `Alias` that checks for uses of the keyword `alias`
17
+ * Automatically detect extensionless Ruby files with shebangs when search for Ruby source files in a directory
18
+
19
+ ### Bugs fixed
20
+
21
+ * [#59](https://github.com/bbatsov/rubocop/issues/59) - Interpolated variables not enclosed in braces are not noticed
22
+ * [#42](https://github.com/bbatsov/rubocop/issues/42) - Received malformed format string ArgumentError from rubocop
data/CONTRIBUTING.md CHANGED
@@ -1,9 +1,16 @@
1
- # Contributing to Rubocop
2
-
3
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
4
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
1
+ # Contributing to RuboCop
2
+
3
+ * Check out the latest master to make sure the feature hasn't been
4
+ implemented or the bug hasn't been fixed yet.
5
+ * Check out the issue tracker to make sure someone already hasn't
6
+ requested it and/or contributed it.
5
7
  * Fork the project.
6
8
  * Start a feature/bugfix branch.
7
9
  * Commit and push until you are happy with your contribution.
8
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
9
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
10
+ * Make sure to add tests for it. This is important so I don't break it
11
+ in a future version unintentionally.
12
+ * Update the [Changelog](CHANGELOG.md) accordingly.
13
+ * Please try not to mess with the Rakefile, version, or history. If
14
+ you want to have your own version, or is otherwise necessary, that
15
+ is fine, but please isolate to its own commit so I can cherry-pick
16
+ around it.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- diff-lcs (1.2.2)
4
+ diff-lcs (1.2.3)
5
5
  git (1.2.5)
6
6
  jeweler (1.8.4)
7
7
  bundler (~> 1.0)
@@ -20,13 +20,13 @@ GEM
20
20
  rspec-core (2.13.1)
21
21
  rspec-expectations (2.13.0)
22
22
  diff-lcs (>= 1.1.3, < 2.0)
23
- rspec-mocks (2.13.0)
23
+ rspec-mocks (2.13.1)
24
24
  simplecov (0.7.1)
25
25
  multi_json (~> 1.0)
26
26
  simplecov-html (~> 0.7.1)
27
27
  simplecov-html (0.7.1)
28
28
  term-ansicolor (1.1.4)
29
- yard (0.8.5.2)
29
+ yard (0.8.6)
30
30
 
31
31
  PLATFORMS
32
32
  ruby
data/README.md CHANGED
@@ -41,7 +41,7 @@ Command flag | Description
41
41
  `-d/--debug` | Displays some extra debug output
42
42
  `-e/--emacs` | Output the results in Emacs format
43
43
  `-c/--config` | Run with specified config file
44
- `-s/-silent` | Suppress the final summary
44
+ `-s/--silent` | Suppress the final summary
45
45
 
46
46
  ## Configuration
47
47
 
@@ -76,6 +76,47 @@ analyzer a daunting task.
76
76
  RuboCop currently supports MRI 1.9 and MRI 2.0. Support for JRuby and
77
77
  Rubinius is not planned at this point.
78
78
 
79
+ ## Editor integration
80
+
81
+ ### Emacs
82
+
83
+ [rubocop.el](https://github.com/bbatsov/rubocop-emacs) is a simple
84
+ Emacs interface for RuboCop. It allows you to run RuboCop inside Emacs
85
+ and quickly jump between problems in your code.
86
+
87
+ [flycheck](https://github.com/lunaryorn/flycheck) > 0.9 also supports
88
+ RuboCop and uses it by default when available.
89
+
90
+ ### Other Editors
91
+
92
+ Here's one great opportunity to contribute to RuboCop - implement
93
+ RuboCop integration for your favorite editor.
94
+
95
+ ## Contributors
96
+
97
+ Here's a [list](https://github.com/bbatsov/rubocop/contributors) of
98
+ all the people who have contributed to the development of RuboCop.
99
+
100
+ I'm extremely grateful to each and every one of them!
101
+
102
+ I'd like to single out [Jonas Arvidsson](https://github.com/jonas054)
103
+ for his many excellent code contributions as well as valuable feedback
104
+ and ideas!
105
+
106
+ If you'd like to contribute to RuboCop, please take the time to go
107
+ through our short
108
+ [contribution guidelines](CONTRIBUTING.md).
109
+
110
+ Converting more of the Ruby Style Guide into RuboCop cops is our top
111
+ priority right now. Writing a new cop is a great way to dive into RuboCop!
112
+
113
+ Of course, bug reports and suggestions for improvements are always
114
+ welcome. GitHub pull requests are even better! :-)
115
+
116
+ ## Changelog
117
+
118
+ RuboCop's changelog is available [here](CHANGELOG.md).
119
+
79
120
  ## Copyright
80
121
 
81
122
  Copyright (c) 2012-2013 Bozhidar Batsov. See [LICENSE.txt](LICENSE.txt) for
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
data/bin/rubocop CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
2
3
 
3
4
  $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib')
4
5
 
data/lib/rubocop/cli.rb CHANGED
@@ -36,15 +36,11 @@ module Rubocop
36
36
  end
37
37
  end.parse!(args)
38
38
 
39
+ cops = Cop::Cop.all
40
+ show_cops_on_duty(cops) if $options[:debug]
39
41
  total_offences = 0
40
42
  @configs = {}
41
43
 
42
- target_files = target_files(args)
43
- config = $options[:config] || config_from_dotfile(target_files[0])
44
-
45
- cops = cops_on_duty(config)
46
- show_cops_on_duty(cops) if $options[:debug]
47
-
48
44
  target_files(args).each do |file|
49
45
  report = Report.create(file, $options[:mode])
50
46
  source = File.readlines(file).map do |line|
@@ -52,16 +48,29 @@ module Rubocop
52
48
  line.chomp
53
49
  end
54
50
 
55
- tokens, sexp, correlations = CLI.rip_source(source)
51
+ syntax_cop = Rubocop::Cop::Syntax.new
52
+ syntax_cop.inspect(file, source, nil, nil)
56
53
 
57
- cops.each do |cop_klass|
58
- cop_config = config[cop_klass.name.split('::').last] if config
59
- cop_klass.config = cop_config
60
- cop = cop_klass.new
61
- cop.correlations = correlations
62
- cop.inspect(file, source, tokens, sexp)
63
- total_offences += cop.offences.count
64
- report << cop if cop.has_report?
54
+ if syntax_cop.offences.map(&:severity).include?(:error)
55
+ # In case of a syntax error we just report that error and do
56
+ # no more checking in the file.
57
+ report << syntax_cop
58
+ total_offences += syntax_cop.offences.count
59
+ else
60
+ tokens, sexp, correlations = CLI.rip_source(source)
61
+ config = $options[:config] || config_from_dotfile(File.dirname(file))
62
+
63
+ cops.each do |cop_klass|
64
+ cop_config = config[cop_klass.name.split('::').last] if config
65
+ if cop_config.nil? || cop_config['Enabled']
66
+ cop_klass.config = cop_config
67
+ cop = cop_klass.new
68
+ cop.correlations = correlations
69
+ cop.inspect(file, source, tokens, sexp)
70
+ total_offences += cop.offences.count
71
+ report << cop if cop.has_report?
72
+ end
73
+ end
65
74
  end
66
75
 
67
76
  report.display unless report.empty?
@@ -136,16 +145,16 @@ module Rubocop
136
145
 
137
146
  # Generate a list of target files by expanding globing patterns
138
147
  # (if any). If args is empty recursively finds all Ruby source
139
- # files in the current directory
148
+ # files under the current directory
140
149
  # @return [Array] array of filenames
141
150
  def target_files(args)
142
- return Dir['**/*.rb'] if args.empty?
151
+ return ruby_files if args.empty?
143
152
 
144
153
  files = []
145
154
 
146
155
  args.each do |target|
147
156
  if File.directory?(target)
148
- files << Dir["#{target}/**/*.rb"]
157
+ files << ruby_files(target)
149
158
  elsif target =~ /\*/
150
159
  files << Dir[target]
151
160
  else
@@ -155,5 +164,25 @@ module Rubocop
155
164
 
156
165
  files.flatten
157
166
  end
167
+
168
+ # Finds all Ruby source files under the current or other supplied
169
+ # directory. A Ruby source file is defined as a file with the `.rb`
170
+ # extension or a file with no extension that has a ruby shebang line
171
+ # as its first line.
172
+ # @param root Root directory under which to search for ruby source files
173
+ # @return [Array] Array of filenames
174
+ def ruby_files(root = Dir.pwd)
175
+ files = Dir["#{root}/**/*"].reject { |file| FileTest.directory? file }
176
+
177
+ rb = []
178
+
179
+ rb << files.select { |file| File.extname(file) == '.rb' }
180
+ rb << files.select do |file|
181
+ File.extname(file) == '' &&
182
+ File.open(file) { |f| f.readline } =~ /#!.*ruby/
183
+ end
184
+
185
+ rb.flatten
186
+ end
158
187
  end
159
188
  end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class Alias < Cop
6
+ ERROR_MESSAGE = 'Use alias_method instead of alias.'
7
+
8
+ def inspect(file, source, tokens, sexp)
9
+ each(:alias, sexp) do |s|
10
+ lineno = s[1][1][1][2].lineno
11
+
12
+ add_offence(
13
+ :convention,
14
+ lineno,
15
+ ERROR_MESSAGE
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -41,11 +41,12 @@ module Rubocop
41
41
  # means there's only one parameter.
42
42
  return nil if [:command, :command_call].include?(arg_paren[1][0][0])
43
43
 
44
- args_add_block = arg_paren[1]
45
- unless args_add_block[0] == :args_add_block
46
- fail "\n#{@file}: #{method_add_arg}"
44
+ if arg_paren[1][0] == :args_add_block
45
+ args_add_block = arg_paren[1]
46
+ args_add_block[1].empty? ? [args_add_block[2]] : args_add_block[1]
47
+ else
48
+ arg_paren[1]
47
49
  end
48
- args_add_block[1].empty? ? [args_add_block[2]] : args_add_block[1]
49
50
  end
50
51
 
51
52
  def divide_args(args)
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AvoidClassVars < Cop
6
+ def inspect(file, source, tokens, sexp)
7
+ each(:@cvar, sexp) do |s|
8
+ class_var = s[1]
9
+ lineno = s[2].lineno
10
+
11
+ add_offence(
12
+ :convention,
13
+ lineno,
14
+ "Replace class var #{class_var} with a class instance var."
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AvoidFor < Cop
6
+ ERROR_MESSAGE = 'Prefer *each* over *for*.'
7
+
8
+ def inspect(file, source, tokens, sexp)
9
+ each(:for, sexp) do |s|
10
+ add_offence(:convention,
11
+ s[1][1][2].lineno,
12
+ ERROR_MESSAGE)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AvoidPerlBackrefs < Cop
6
+ def inspect(file, source, tokens, sexp)
7
+ each(:@backref, sexp) do |s|
8
+ backref = s[1]
9
+ lineno = s[2].lineno
10
+
11
+ add_offence(
12
+ :convention,
13
+ lineno,
14
+ "Prefer the use of MatchData over #{backref}."
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AvoidPerlisms < Cop
6
+ PREFERRED_VARS = {
7
+ '$:' => '$LOAD_PATH',
8
+ '$"' => '$LOADED_FEATURES',
9
+ '$0' => '$PROGRAM_NAME',
10
+ '$1' => 'MatchData',
11
+ '$2' => 'MatchData',
12
+ '$3' => 'MatchData',
13
+ '$4' => 'MatchData',
14
+ '$5' => 'MatchData',
15
+ '$6' => 'MatchData',
16
+ '$7' => 'MatchData',
17
+ '$8' => 'MatchData',
18
+ '$9' => 'MatchData',
19
+ '$!' => '$ERROR_INFO',
20
+ '$@' => '$ERROR_POSITION',
21
+ '$;' => '$FIELD_SEPARATOR',
22
+ '$,' => '$OUTPUT_FIELD_SEPARATOR',
23
+ '$/' => '$INPUT_RECORD_SEPARATOR',
24
+ '$\\' => 'OUTPUT_RECORD_SEPARATOR',
25
+ '$.' => '$INPUT_LINE_NUMBER',
26
+ '$_' => '$LAST_READ_LINE',
27
+ '$>' => '$DEFAULT_OUTPUT',
28
+ '$<' => '$DEFAULT_INPUT',
29
+ '$$' => '$PROCESS_ID',
30
+ '$?' => '$CHILD_STATUS',
31
+ '$~' => '$LAST_MATCH_INFO',
32
+ '$=' => '$IGNORECASE',
33
+ '$*' => '$ARGV',
34
+ '$&' => '$MATCH',
35
+ '$`' => '$PREMATCH',
36
+ '$\'' => 'POSTMATCH',
37
+ '$+' => '$LAST_PAREN_MATCH'
38
+ }
39
+
40
+ def inspect(file, source, tokens, sexp)
41
+ each(:@gvar, sexp) do |s|
42
+ global_var = s[1]
43
+
44
+ if PREFERRED_VARS[global_var]
45
+ add_offence(
46
+ :convention,
47
+ s[2].lineno,
48
+ "Prefer #{PREFERRED_VARS[global_var]} over #{global_var}."
49
+ )
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -29,7 +29,7 @@ module Rubocop
29
29
  if path.last == :brace_block
30
30
  rbrace_ix = @reverse_correlations[path.object_id] - [ix]
31
31
  if rbrace_ix.empty?
32
- fail "\n#@file:#{t.pos.lineno}:#{t.pos.column}: " +
32
+ fail "\n#{@file}:#{t.pos.lineno}:#{t.pos.column}: " +
33
33
  'Matching brace not found'
34
34
  end
35
35
  if tokens[*rbrace_ix].pos.lineno > t.pos.lineno
@@ -26,7 +26,7 @@ module Rubocop
26
26
  end
27
27
 
28
28
  def to_s
29
- "[[#{@pos.lineno}, #{@pos.column}], #@type, #{@text.inspect}]"
29
+ "[[#{@pos.lineno}, #{@pos.column}], #{@type}, #{@text.inspect}]"
30
30
  end
31
31
  end
32
32
 
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class FavorSprintf < Cop
6
+ ERROR_MESSAGE = 'Favor sprintf over String#%.'
7
+
8
+ def inspect(file, source, tokens, sexp)
9
+ each(:binary, sexp) do |s|
10
+ op1 = s[1]
11
+ operator = s[2]
12
+ op2 = s[3]
13
+
14
+ # we care only about the % operator
15
+ next unless matching?(operator, op1, op2)
16
+
17
+ # FIXME implement reliable lineno extraction
18
+ # we either have some string literal
19
+ if op1[0] == :string_literal
20
+ lineno_struct = s[1][1][1][2]
21
+ lineno = lineno_struct.lineno if lineno_struct.respond_to?(:lineno)
22
+ else
23
+ # or some identifier
24
+ lineno_struct = s[1][1][2]
25
+ lineno = lineno_struct.lineno if lineno_struct.respond_to?(:lineno)
26
+ end
27
+
28
+ add_offence(
29
+ :convention,
30
+ lineno,
31
+ ERROR_MESSAGE
32
+ ) if lineno
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def matching?(operator, op1, op2)
39
+ return false unless operator == :%
40
+
41
+ op1[0] == :string_literal or op2[0] == :array
42
+ end
43
+ end
44
+ end
45
+ end