rubocop 0.3.0 → 0.4.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.
- data/.rubocop.yml +40 -0
- data/.travis.yml +1 -0
- data/Gemfile +7 -10
- data/Gemfile.lock +21 -19
- data/README.md +62 -5
- data/VERSION +1 -1
- data/bin/rubocop +1 -1
- data/lib/rubocop/cli.rb +35 -15
- data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -0
- data/lib/rubocop/cop/collection_methods.rb +29 -0
- data/lib/rubocop/cop/cop.rb +1 -1
- data/lib/rubocop/cop/def_parentheses.rb +4 -1
- data/lib/rubocop/cop/encoding.rb +6 -4
- data/lib/rubocop/cop/end_of_line.rb +1 -3
- data/lib/rubocop/cop/favor_modifier.rb +82 -0
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
- data/lib/rubocop/cop/grammar.rb +3 -2
- data/lib/rubocop/cop/indentation.rb +1 -1
- data/lib/rubocop/cop/line_length.rb +7 -3
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +31 -0
- data/lib/rubocop/cop/new_lambda_literal.rb +17 -0
- data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -47
- data/lib/rubocop/cop/syntax.rb +19 -0
- data/lib/rubocop/cop/tab.rb +1 -3
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/report/report.rb +5 -2
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +14 -0
- data/rubocop.gemspec +38 -22
- data/spec/rubocop/cli_spec.rb +120 -40
- data/spec/rubocop/cops/align_parameters_spec.rb +29 -29
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
- data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +34 -0
- data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
- data/spec/rubocop/cops/cop_spec.rb +5 -5
- data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
- data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
- data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
- data/spec/rubocop/cops/encoding_spec.rb +17 -11
- data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
- data/spec/rubocop/cops/favor_modifier_spec.rb +112 -0
- data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +62 -0
- data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +45 -0
- data/spec/rubocop/cops/grammar_spec.rb +14 -9
- data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
- data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/indentation_spec.rb +7 -7
- data/spec/rubocop/cops/line_length_spec.rb +4 -4
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +47 -0
- data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +23 -0
- data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
- data/spec/rubocop/cops/offence_spec.rb +4 -4
- data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
- data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
- data/spec/rubocop/cops/parentheses_around_condition_spec.rb +64 -0
- data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
- data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +22 -0
- data/spec/rubocop/cops/space_around_operators_spec.rb +50 -25
- data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
- data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
- data/spec/rubocop/cops/string_literals_spec.rb +5 -5
- data/spec/rubocop/cops/syntax_spec.rb +18 -0
- data/spec/rubocop/cops/tab_spec.rb +3 -3
- data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
- data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
- data/spec/rubocop/cops/unless_else_spec.rb +4 -4
- data/spec/rubocop/cops/when_then_spec.rb +5 -5
- data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
- data/spec/rubocop/reports/report_spec.rb +5 -5
- data/spec/spec_helper.rb +6 -0
- metadata +57 -28
- data/.rbenv-version +0 -1
- data/.rvmrc +0 -1
- data/features/rubocop.feature +0 -9
- data/features/step_definitions/rubocop_steps.rb +0 -1
- data/features/support/env.rb +0 -15
data/.rubocop.yml
CHANGED
|
@@ -6,6 +6,7 @@ Encoding:
|
|
|
6
6
|
|
|
7
7
|
LineLength:
|
|
8
8
|
Enabled: true
|
|
9
|
+
Max: 79
|
|
9
10
|
|
|
10
11
|
Tab:
|
|
11
12
|
Enabled: true
|
|
@@ -87,3 +88,42 @@ NestedTernaryOperator:
|
|
|
87
88
|
|
|
88
89
|
UnlessElse:
|
|
89
90
|
Enabled: true
|
|
91
|
+
|
|
92
|
+
AmpersandsPipesVsAndOr:
|
|
93
|
+
Enabled: true
|
|
94
|
+
|
|
95
|
+
WhenThen:
|
|
96
|
+
Enabled: true
|
|
97
|
+
|
|
98
|
+
IfUnlessModifier:
|
|
99
|
+
Enabled: true
|
|
100
|
+
|
|
101
|
+
WhileUntilModifier:
|
|
102
|
+
Enabled: true
|
|
103
|
+
|
|
104
|
+
FavorUnlessOverNegatedIf:
|
|
105
|
+
Enabled: true
|
|
106
|
+
|
|
107
|
+
FavorUntilOverNegatedWhile:
|
|
108
|
+
Enabled: true
|
|
109
|
+
|
|
110
|
+
SpaceAroundEqualsInParameterDefault:
|
|
111
|
+
Enabled: true
|
|
112
|
+
|
|
113
|
+
NewLambdaLiteral:
|
|
114
|
+
Enabled: true
|
|
115
|
+
|
|
116
|
+
ParenthesesAroundCondition:
|
|
117
|
+
Enabled: true
|
|
118
|
+
|
|
119
|
+
MethodAndVariableSnakeCase:
|
|
120
|
+
Enabled: true
|
|
121
|
+
|
|
122
|
+
ClassAndModuleCamelCase:
|
|
123
|
+
Enabled: true
|
|
124
|
+
|
|
125
|
+
Syntax:
|
|
126
|
+
Enabled: true
|
|
127
|
+
|
|
128
|
+
CollectionMethods:
|
|
129
|
+
Enabled: true
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
source 'http://rubygems.org'
|
|
2
|
-
# Add dependencies required to use your gem here.
|
|
3
|
-
# Example:
|
|
4
|
-
# gem "activesupport", ">= 2.3.5"
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
gem 'term-ansicolor'
|
|
4
|
+
|
|
8
5
|
group :development do
|
|
9
|
-
gem 'rake'
|
|
10
|
-
gem 'rspec'
|
|
11
|
-
gem 'yard'
|
|
12
|
-
gem 'bundler'
|
|
13
|
-
gem 'jeweler'
|
|
6
|
+
gem 'rake'
|
|
7
|
+
gem 'rspec'
|
|
8
|
+
gem 'yard'
|
|
9
|
+
gem 'bundler'
|
|
10
|
+
gem 'jeweler'
|
|
14
11
|
gem 'simplecov'
|
|
15
12
|
end
|
data/Gemfile.lock
CHANGED
|
@@ -1,39 +1,41 @@
|
|
|
1
1
|
GEM
|
|
2
2
|
remote: http://rubygems.org/
|
|
3
3
|
specs:
|
|
4
|
-
diff-lcs (1.
|
|
4
|
+
diff-lcs (1.2.2)
|
|
5
5
|
git (1.2.5)
|
|
6
6
|
jeweler (1.8.4)
|
|
7
7
|
bundler (~> 1.0)
|
|
8
8
|
git (>= 1.2.5)
|
|
9
9
|
rake
|
|
10
10
|
rdoc
|
|
11
|
-
json (1.7.
|
|
12
|
-
multi_json (1.
|
|
13
|
-
rake (10.0.
|
|
14
|
-
rdoc (
|
|
11
|
+
json (1.7.7)
|
|
12
|
+
multi_json (1.7.2)
|
|
13
|
+
rake (10.0.4)
|
|
14
|
+
rdoc (4.0.1)
|
|
15
15
|
json (~> 1.4)
|
|
16
|
-
rspec (2.
|
|
17
|
-
rspec-core (~> 2.
|
|
18
|
-
rspec-expectations (~> 2.
|
|
19
|
-
rspec-mocks (~> 2.
|
|
20
|
-
rspec-core (2.
|
|
21
|
-
rspec-expectations (2.
|
|
22
|
-
diff-lcs (
|
|
23
|
-
rspec-mocks (2.
|
|
16
|
+
rspec (2.13.0)
|
|
17
|
+
rspec-core (~> 2.13.0)
|
|
18
|
+
rspec-expectations (~> 2.13.0)
|
|
19
|
+
rspec-mocks (~> 2.13.0)
|
|
20
|
+
rspec-core (2.13.1)
|
|
21
|
+
rspec-expectations (2.13.0)
|
|
22
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
|
23
|
+
rspec-mocks (2.13.0)
|
|
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
30
|
|
|
30
31
|
PLATFORMS
|
|
31
32
|
ruby
|
|
32
33
|
|
|
33
34
|
DEPENDENCIES
|
|
34
|
-
bundler
|
|
35
|
-
jeweler
|
|
36
|
-
rake
|
|
37
|
-
rspec
|
|
35
|
+
bundler
|
|
36
|
+
jeweler
|
|
37
|
+
rake
|
|
38
|
+
rspec
|
|
38
39
|
simplecov
|
|
39
|
-
|
|
40
|
+
term-ansicolor
|
|
41
|
+
yard
|
data/README.md
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
|
+
[](http://badge.fury.io/rb/rubocop)
|
|
1
2
|
[](https://travis-ci.org/bbatsov/rubocop)
|
|
2
3
|
|
|
3
|
-
#
|
|
4
|
+
# RuboCop
|
|
4
5
|
|
|
5
|
-
Ruby code style checker based on the
|
|
6
|
+
**RuboCop** is a Ruby code style checker based on the
|
|
7
|
+
[Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide).
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
**RuboCop**'s installation is pretty standard:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
$ gem install rubocop
|
|
15
|
+
```
|
|
6
16
|
|
|
7
17
|
## Basic Usage
|
|
8
18
|
|
|
9
|
-
Running `rubocop` with no arguments will check all Ruby source files
|
|
19
|
+
Running `rubocop` with no arguments will check all Ruby source files
|
|
20
|
+
in the current folder:
|
|
10
21
|
|
|
11
22
|
```bash
|
|
12
23
|
$ rubocop
|
|
@@ -18,8 +29,54 @@ Alternatively you can `rubocop` a list of files and folders to check:
|
|
|
18
29
|
$ rubocop app spec lib/something.rb
|
|
19
30
|
```
|
|
20
31
|
|
|
32
|
+
For more details check the available command-line options:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
$ rubocop -h
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Command flag | Description
|
|
39
|
+
-------------------|------------------------------------------------------------
|
|
40
|
+
`-v/--version` | Displays the current version and exits
|
|
41
|
+
`-d/--debug` | Displays some extra debug output
|
|
42
|
+
`-e/--emacs` | Output the results in Emacs format
|
|
43
|
+
`-c/--config` | Run with specified config file
|
|
44
|
+
`-s/-silent` | Suppress the final summary
|
|
45
|
+
|
|
46
|
+
## Configuration
|
|
47
|
+
|
|
48
|
+
The behavior of RuboCop can be controlled via the
|
|
49
|
+
[.rubocop.yml](https://github.com/bbatsov/rubocop/blob/master/.rubocop.yml)
|
|
50
|
+
configuration file. The file can be placed either in your home folder
|
|
51
|
+
or in some project folder.
|
|
52
|
+
|
|
53
|
+
RuboCop will start looking for the configuration file in the directory
|
|
54
|
+
it was started in and continue its way up to the home folder.
|
|
55
|
+
|
|
56
|
+
The file has the following format:
|
|
57
|
+
|
|
58
|
+
```yml
|
|
59
|
+
Encoding:
|
|
60
|
+
Enabled: true
|
|
61
|
+
|
|
62
|
+
LineLength:
|
|
63
|
+
Enabled: true
|
|
64
|
+
Max: 79
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
It allows to enable/disable certain cops (checks) and to alter their
|
|
68
|
+
behavior if they accept any parameters.
|
|
69
|
+
|
|
70
|
+
## Compatibility
|
|
71
|
+
|
|
72
|
+
Unfortunately every major Ruby implementation has its own code
|
|
73
|
+
analysis tooling, which makes the development of a portable code
|
|
74
|
+
analyzer a daunting task.
|
|
75
|
+
|
|
76
|
+
RuboCop currently supports MRI 1.9 and MRI 2.0. Support for JRuby and
|
|
77
|
+
Rubinius is not planned at this point.
|
|
78
|
+
|
|
21
79
|
## Copyright
|
|
22
80
|
|
|
23
|
-
Copyright (c) 2012-2013 Bozhidar Batsov. See LICENSE.txt for
|
|
81
|
+
Copyright (c) 2012-2013 Bozhidar Batsov. See [LICENSE.txt](LICENSE.txt) for
|
|
24
82
|
further details.
|
|
25
|
-
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.4.0
|
data/bin/rubocop
CHANGED
data/lib/rubocop/cli.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'optparse'
|
|
4
4
|
require 'yaml'
|
|
5
|
+
require_relative 'cop/grammar'
|
|
5
6
|
|
|
6
7
|
module Rubocop
|
|
7
8
|
# The CLI is a class responsible of handling all the command line interface
|
|
@@ -17,8 +18,8 @@ module Rubocop
|
|
|
17
18
|
OptionParser.new do |opts|
|
|
18
19
|
opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
|
|
19
20
|
|
|
20
|
-
opts.on('-
|
|
21
|
-
$options[:
|
|
21
|
+
opts.on('-d', '--[no-]debug', 'Display debug info') do |d|
|
|
22
|
+
$options[:debug] = d
|
|
22
23
|
end
|
|
23
24
|
opts.on('-e', '--emacs', 'Emacs style output') do
|
|
24
25
|
$options[:mode] = :emacs_style
|
|
@@ -29,13 +30,21 @@ module Rubocop
|
|
|
29
30
|
opts.on('-s', '--silent', 'Silence summary') do |s|
|
|
30
31
|
$options[:silent] = s
|
|
31
32
|
end
|
|
33
|
+
opts.on('-v', '--version', 'Display version') do
|
|
34
|
+
puts Rubocop::VERSION
|
|
35
|
+
exit(0)
|
|
36
|
+
end
|
|
32
37
|
end.parse!(args)
|
|
33
38
|
|
|
34
|
-
cops = Cop::Cop.all
|
|
35
|
-
show_cops_on_duty(cops) if $options[:verbose]
|
|
36
39
|
total_offences = 0
|
|
37
40
|
@configs = {}
|
|
38
41
|
|
|
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
|
+
|
|
39
48
|
target_files(args).each do |file|
|
|
40
49
|
report = Report.create(file, $options[:mode])
|
|
41
50
|
source = File.readlines(file).map do |line|
|
|
@@ -46,15 +55,13 @@ module Rubocop
|
|
|
46
55
|
tokens, sexp, correlations = CLI.rip_source(source)
|
|
47
56
|
|
|
48
57
|
cops.each do |cop_klass|
|
|
49
|
-
config = $options[:config] || config_from_dotfile(File.dirname(file))
|
|
50
58
|
cop_config = config[cop_klass.name.split('::').last] if config
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
end
|
|
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?
|
|
58
65
|
end
|
|
59
66
|
|
|
60
67
|
report.display unless report.empty?
|
|
@@ -63,6 +70,7 @@ module Rubocop
|
|
|
63
70
|
unless $options[:silent]
|
|
64
71
|
print "\n#{target_files(args).count} files inspected, "
|
|
65
72
|
puts "#{total_offences} offences detected"
|
|
73
|
+
.send(total_offences.zero? ? :green : :red)
|
|
66
74
|
end
|
|
67
75
|
|
|
68
76
|
return total_offences == 0 ? 0 : 1
|
|
@@ -89,6 +97,7 @@ module Rubocop
|
|
|
89
97
|
# directory where the inspected file is. If no .rubocop.yml is
|
|
90
98
|
# found there, the user's home directory is checked.
|
|
91
99
|
def config_from_dotfile(target_file_dir)
|
|
100
|
+
return unless target_file_dir
|
|
92
101
|
# @configs is a cache that maps directories to
|
|
93
102
|
# configurations. We search for .rubocop.yml only if we haven't
|
|
94
103
|
# already found it for the given directory.
|
|
@@ -108,10 +117,21 @@ module Rubocop
|
|
|
108
117
|
@configs[target_file_dir]
|
|
109
118
|
end
|
|
110
119
|
|
|
120
|
+
def cops_on_duty(config)
|
|
121
|
+
cops_on_duty = []
|
|
122
|
+
|
|
123
|
+
Cop::Cop.all.each do |cop_klass|
|
|
124
|
+
cop_config = config[cop_klass.name.split('::').last] if config
|
|
125
|
+
cops_on_duty << cop_klass if cop_config.nil? || cop_config['Enabled']
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
cops_on_duty
|
|
129
|
+
end
|
|
130
|
+
|
|
111
131
|
def show_cops_on_duty(cops)
|
|
112
|
-
puts 'Reporting for duty
|
|
113
|
-
cops.each { |c| puts c }
|
|
114
|
-
puts '
|
|
132
|
+
puts '== Reporting for duty =='
|
|
133
|
+
cops.each { |c| puts ' * '.yellow + c.to_s.green }
|
|
134
|
+
puts '========================'
|
|
115
135
|
end
|
|
116
136
|
|
|
117
137
|
# Generate a list of target files by expanding globing patterns
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class ClassAndModuleCamelCase < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Use CamelCase for classes and modules.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
[:class, :module].each do |keyword|
|
|
10
|
+
each(keyword, sexp) do |s|
|
|
11
|
+
if s[1][0] == :const_ref && s[1][1][0] == :@const &&
|
|
12
|
+
s[1][1][1] =~ /_/
|
|
13
|
+
add_offence(:convention, s[1][1][2].lineno, ERROR_MESSAGE)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class CollectionMethods < Cop
|
|
6
|
+
PREFERRED_METHODS = {
|
|
7
|
+
'collect' => 'map',
|
|
8
|
+
'inject' => 'reduce',
|
|
9
|
+
'detect' => 'find',
|
|
10
|
+
'find_all' => 'select',
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
def inspect(file, source, tokens, sexp)
|
|
14
|
+
each(:call, sexp) do |s|
|
|
15
|
+
s.drop(2).each_slice(2) do |m|
|
|
16
|
+
method_name = m[1][1]
|
|
17
|
+
if PREFERRED_METHODS[method_name]
|
|
18
|
+
add_offence(
|
|
19
|
+
:convention,
|
|
20
|
+
m[1][2].lineno,
|
|
21
|
+
"Prefer #{PREFERRED_METHODS[method_name]} over #{method_name}."
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/rubocop/cop/cop.rb
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
module Rubocop
|
|
4
4
|
module Cop
|
|
5
5
|
module DefParentheses
|
|
6
|
-
EMPTY_PARAMS = [:params, nil, nil, nil,
|
|
6
|
+
EMPTY_PARAMS = [:params, nil, nil, nil,
|
|
7
|
+
nil, nil, nil, nil] if RUBY_VERSION >= '2.0.0'
|
|
8
|
+
EMPTY_PARAMS = [:params, nil, nil, nil,
|
|
9
|
+
nil, nil] if RUBY_VERSION < '2.0.0'
|
|
7
10
|
|
|
8
11
|
def inspect(file, source, tokens, sexp)
|
|
9
12
|
each(:def, sexp) { |def_sexp| check(tokens, def_sexp) }
|
data/lib/rubocop/cop/encoding.rb
CHANGED
|
@@ -6,10 +6,12 @@ module Rubocop
|
|
|
6
6
|
ERROR_MESSAGE = 'Missing encoding comment.'
|
|
7
7
|
|
|
8
8
|
def inspect(file, source, tokens, sexp)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
unless RUBY_VERSION >= '2.0.0'
|
|
10
|
+
expected_line = 0
|
|
11
|
+
expected_line += 1 if source[expected_line] =~ /^#!/
|
|
12
|
+
unless source[expected_line] =~ /#.*coding: (UTF|utf)-8/
|
|
13
|
+
add_offence(:convention, 1, ERROR_MESSAGE)
|
|
14
|
+
end
|
|
13
15
|
end
|
|
14
16
|
end
|
|
15
17
|
end
|
|
@@ -7,9 +7,7 @@ module Rubocop
|
|
|
7
7
|
|
|
8
8
|
def inspect(file, source, tokens, sexp)
|
|
9
9
|
source.each_with_index do |line, index|
|
|
10
|
-
if line =~ /\r$/
|
|
11
|
-
add_offence(:convention, index + 1, ERROR_MESSAGE)
|
|
12
|
-
end
|
|
10
|
+
add_offence(:convention, index + 1, ERROR_MESSAGE) if line =~ /\r$/
|
|
13
11
|
end
|
|
14
12
|
end
|
|
15
13
|
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
module FavorModifier
|
|
6
|
+
def check(kind, tokens, sexp)
|
|
7
|
+
token_positions = tokens.map(&:pos)
|
|
8
|
+
token_texts = tokens.map(&:text)
|
|
9
|
+
each(kind, sexp) do |s|
|
|
10
|
+
# If it contains an else, it can't be written as a modifier.
|
|
11
|
+
next if s[3] && s[3][0] == :else
|
|
12
|
+
|
|
13
|
+
sexp_positions = all_positions(s)
|
|
14
|
+
ix = token_positions.index(sexp_positions.first)
|
|
15
|
+
if_ix = token_texts[0..ix].rindex(kind.to_s) # index of if/unless/...
|
|
16
|
+
ix = token_positions.index(sexp_positions.last)
|
|
17
|
+
end_ix = ix + token_texts[ix..-1].index('end')
|
|
18
|
+
|
|
19
|
+
# If there's a comment anywhere between
|
|
20
|
+
# if/unless/while/until and end, we don't report. It's
|
|
21
|
+
# possible that the comment will be less clear if put above
|
|
22
|
+
# a one liner rather than inside.
|
|
23
|
+
next if tokens[if_ix...end_ix].map(&:type).include?(:on_comment)
|
|
24
|
+
|
|
25
|
+
if token_positions[end_ix].lineno - token_positions[if_ix].lineno > 2
|
|
26
|
+
next # not a single-line body
|
|
27
|
+
end
|
|
28
|
+
# The start ix is the index of the leftmost token on the
|
|
29
|
+
# line of the if/unless, i.e. the index of if/unless itself,
|
|
30
|
+
# or of the indentation space.
|
|
31
|
+
start_ix = if_ix.downto(0).find do |block_ix|
|
|
32
|
+
block_ix == 0 || tokens[block_ix - 1].text =~ /\n/
|
|
33
|
+
end
|
|
34
|
+
# The stop index is the index of the token just before
|
|
35
|
+
# 'end', not counting whitespace tokens.
|
|
36
|
+
stop_ix = (end_ix - 1).downto(0).find do |block_ix|
|
|
37
|
+
tokens[block_ix].text !~ /\s/
|
|
38
|
+
end
|
|
39
|
+
if length(tokens, start_ix, stop_ix) <= LineLength.max
|
|
40
|
+
add_offence(:convention, token_positions[if_ix].lineno,
|
|
41
|
+
error_message)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def length(tokens, start_ix, stop_ix)
|
|
47
|
+
(start_ix..stop_ix).reduce(0) do |acc, ix|
|
|
48
|
+
acc + if ix > start_ix && tokens[ix - 1].text =~ /\n/
|
|
49
|
+
0
|
|
50
|
+
else
|
|
51
|
+
tokens[ix].text.length
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class IfUnlessModifier < Cop
|
|
58
|
+
include FavorModifier
|
|
59
|
+
|
|
60
|
+
def error_message
|
|
61
|
+
'Favor modifier if/unless usage when you have a single-line body. ' +
|
|
62
|
+
'Another good alternative is the usage of control flow and/or.'
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def inspect(file, source, tokens, sexp)
|
|
66
|
+
[:if, :unless].each { |kind| check(kind, tokens, sexp) }
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
class WhileUntilModifier < Cop
|
|
71
|
+
include FavorModifier
|
|
72
|
+
|
|
73
|
+
def error_message
|
|
74
|
+
'Favor modifier while/until usage when you have a single-line body.'
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def inspect(file, source, tokens, sexp)
|
|
78
|
+
[:while, :until].each { |kind| check(kind, tokens, sexp) }
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
module FavorOtherKeywordOverNegation
|
|
6
|
+
private
|
|
7
|
+
def check(grammar_part, sexp)
|
|
8
|
+
each(grammar_part, sexp) do |s|
|
|
9
|
+
# Don't complain about negative if/else. We don't want unless/else.
|
|
10
|
+
next if s[3] && [:else, :elsif].include?(s[3][0])
|
|
11
|
+
|
|
12
|
+
condition = s[1]
|
|
13
|
+
condition = condition[1][0] while condition[0] == :paren
|
|
14
|
+
|
|
15
|
+
if condition[0] == :unary && [:!, :not].include?(condition[1])
|
|
16
|
+
add_offence(:convention, all_positions(s).first.lineno,
|
|
17
|
+
error_message)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class FavorUnlessOverNegatedIf < Cop
|
|
24
|
+
include FavorOtherKeywordOverNegation
|
|
25
|
+
|
|
26
|
+
def error_message
|
|
27
|
+
'Favor unless (or control flow or) over if for negative conditions.'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def inspect(file, source, tokens, sexp)
|
|
31
|
+
[:if, :if_mod].each { |grammar_part| check(grammar_part, sexp) }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
class FavorUntilOverNegatedWhile < Cop
|
|
36
|
+
include FavorOtherKeywordOverNegation
|
|
37
|
+
|
|
38
|
+
def error_message
|
|
39
|
+
'Favor until over while for negative conditions.'
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def inspect(file, source, tokens, sexp)
|
|
43
|
+
[:while, :while_mod].each { |grammar_part| check(grammar_part, sexp) }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/rubocop/cop/grammar.rb
CHANGED
|
@@ -90,8 +90,9 @@ module Rubocop
|
|
|
90
90
|
find(path, sexp, [:on_op, '|'])
|
|
91
91
|
end
|
|
92
92
|
path += [sexp[0]] if Symbol === sexp[0]
|
|
93
|
-
# Compensate for reverse order of if modifier
|
|
94
|
-
|
|
93
|
+
# Compensate for reverse order of if/unless/while/until modifier.
|
|
94
|
+
modifiers = [:if_mod, :unless_mod, :while_mod, :until_mod]
|
|
95
|
+
children = modifiers.include?(sexp[0]) ? sexp.reverse : sexp
|
|
95
96
|
|
|
96
97
|
children.each do |elem|
|
|
97
98
|
case elem
|
|
@@ -4,16 +4,20 @@ module Rubocop
|
|
|
4
4
|
module Cop
|
|
5
5
|
class LineLength < Cop
|
|
6
6
|
ERROR_MESSAGE = 'Line is too long. [%d/%d]'
|
|
7
|
-
MAX_LINE_LENGTH = 79
|
|
8
7
|
|
|
9
8
|
def inspect(file, source, tokens, sexp)
|
|
10
9
|
source.each_with_index do |line, index|
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
max = LineLength.max
|
|
11
|
+
if line.length > max
|
|
12
|
+
message = sprintf(ERROR_MESSAGE, line.length, max)
|
|
13
13
|
add_offence(:convention, index + 1, message)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
|
+
|
|
18
|
+
def self.max
|
|
19
|
+
LineLength.config ? LineLength.config['Max'] || 79 : 79
|
|
20
|
+
end
|
|
17
21
|
end
|
|
18
22
|
end
|
|
19
23
|
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class MethodAndVariableSnakeCase < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Use snake_case for methods and variables.'
|
|
7
|
+
SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
|
|
8
|
+
|
|
9
|
+
def inspect(file, source, tokens, sexp)
|
|
10
|
+
each(:def, sexp) { |s| check(s[1]) }
|
|
11
|
+
|
|
12
|
+
each(:assign, sexp) do |s|
|
|
13
|
+
case s[1][0]
|
|
14
|
+
when :var_field
|
|
15
|
+
check(s[1][1])
|
|
16
|
+
when :field
|
|
17
|
+
if s[1][1][0] == :var_ref && s[1][1][1][0..1] == [:@kw, 'self']
|
|
18
|
+
check(s[1][3])
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def check(sexp)
|
|
25
|
+
if [:@ivar, :@ident].include?(sexp[0]) && sexp[1] !~ SNAKE_CASE
|
|
26
|
+
add_offence(:convention, sexp[2].lineno, ERROR_MESSAGE)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class NewLambdaLiteral < Cop
|
|
6
|
+
ERROR_MESSAGE = 'The new lambda literal syntax is preferred in Ruby 1.9.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
each(:fcall, sexp) do |s|
|
|
10
|
+
if s[1][0..1] == [:@ident, 'lambda']
|
|
11
|
+
add_offence(:convention, s[1][-1].lineno, ERROR_MESSAGE)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|