rubocop 0.1.0 → 0.2.1

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 (62) hide show
  1. data/.rbenv-version +1 -0
  2. data/.travis.yml +4 -0
  3. data/CONTRIBUTING.md +9 -0
  4. data/Gemfile +7 -8
  5. data/Gemfile.lock +21 -33
  6. data/README.md +17 -11
  7. data/Rakefile +0 -3
  8. data/VERSION +1 -1
  9. data/bin/rubocop +1 -1
  10. data/features/step_definitions/rubocop_steps.rb +1 -0
  11. data/features/support/env.rb +3 -1
  12. data/lib/rubocop/cli.rb +50 -24
  13. data/lib/rubocop/cop/align_parameters.rb +112 -0
  14. data/lib/rubocop/cop/blocks.rb +48 -0
  15. data/lib/rubocop/cop/cop.rb +44 -21
  16. data/lib/rubocop/cop/def_parentheses.rb +38 -0
  17. data/lib/rubocop/cop/empty_lines.rb +7 -6
  18. data/lib/rubocop/cop/encoding.rb +1 -1
  19. data/lib/rubocop/cop/end_of_line.rb +17 -0
  20. data/lib/rubocop/cop/grammar.rb +69 -9
  21. data/lib/rubocop/cop/hash_syntax.rb +26 -0
  22. data/lib/rubocop/cop/if_then_else.rb +49 -0
  23. data/lib/rubocop/cop/indentation.rb +16 -27
  24. data/lib/rubocop/cop/line_length.rb +2 -2
  25. data/lib/rubocop/cop/numeric_literals.rb +19 -0
  26. data/lib/rubocop/cop/offence.rb +2 -3
  27. data/lib/rubocop/cop/parameter_lists.rb +17 -0
  28. data/lib/rubocop/cop/space_after_comma_etc.rb +10 -9
  29. data/lib/rubocop/cop/string_literals.rb +36 -0
  30. data/lib/rubocop/cop/surrounding_space.rb +66 -17
  31. data/lib/rubocop/cop/tab.rb +2 -2
  32. data/lib/rubocop/cop/ternary_operator.rb +24 -0
  33. data/lib/rubocop/cop/trailing_whitespace.rb +2 -2
  34. data/lib/rubocop/report/emacs_style.rb +4 -3
  35. data/lib/rubocop/version.rb +1 -1
  36. data/lib/rubocop.rb +10 -0
  37. data/rubocop.gemspec +37 -17
  38. data/spec/rubocop/cli_spec.rb +32 -5
  39. data/spec/rubocop/cops/align_parameters_spec.rb +201 -0
  40. data/spec/rubocop/cops/blocks_spec.rb +35 -0
  41. data/spec/rubocop/cops/cop_spec.rb +4 -2
  42. data/spec/rubocop/cops/def_parentheses_spec.rb +48 -0
  43. data/spec/rubocop/cops/empty_lines_spec.rb +9 -8
  44. data/spec/rubocop/cops/end_of_line_spec.rb +17 -0
  45. data/spec/rubocop/cops/grammar_spec.rb +54 -12
  46. data/spec/rubocop/cops/hash_syntax_spec.rb +44 -0
  47. data/spec/rubocop/cops/if_then_else_spec.rb +74 -0
  48. data/spec/rubocop/cops/indentation_spec.rb +29 -4
  49. data/spec/rubocop/cops/line_length_spec.rb +4 -2
  50. data/spec/rubocop/cops/numeric_literals_spec.rb +49 -0
  51. data/spec/rubocop/cops/offence_spec.rb +4 -3
  52. data/spec/rubocop/cops/parameter_lists_spec.rb +24 -0
  53. data/spec/rubocop/cops/space_after_comma_etc_spec.rb +7 -5
  54. data/spec/rubocop/cops/string_literals_spec.rb +32 -0
  55. data/spec/rubocop/cops/surrounding_space_spec.rb +89 -26
  56. data/spec/rubocop/cops/tab_spec.rb +4 -2
  57. data/spec/rubocop/cops/ternary_operator_spec.rb +24 -0
  58. data/spec/rubocop/cops/trailing_whitespace_spec.rb +5 -3
  59. data/spec/rubocop/reports/emacs_style_spec.rb +5 -3
  60. data/spec/rubocop/reports/report_spec.rb +3 -1
  61. data/spec/spec_helper.rb +11 -2
  62. metadata +39 -32
data/.rbenv-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p327
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ script: bundle exec rspec
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,9 @@
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.
5
+ * Fork the project.
6
+ * Start a feature/bugfix branch.
7
+ * 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.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source 'http://rubygems.org'
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
@@ -6,11 +6,10 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "rspec", "~> 2.8.0"
10
- gem "yard", "~> 0.7"
11
- gem "redcarpet"
12
- gem "cucumber", ">= 0"
13
- gem "bundler", "~> 1.1.0"
14
- gem "jeweler", "~> 1.8.3"
15
- gem "simplecov"
9
+ gem 'rake', '~> 10.0.0'
10
+ gem 'rspec', '~> 2.12.0'
11
+ gem 'yard', '~> 0.8.0'
12
+ gem 'bundler', '~> 1.2.0'
13
+ gem 'jeweler', '~> 1.8.3'
14
+ gem 'simplecov'
16
15
  end
data/Gemfile.lock CHANGED
@@ -1,51 +1,39 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- builder (3.0.0)
5
- cucumber (1.1.9)
6
- builder (>= 2.1.2)
7
- diff-lcs (>= 1.1.2)
8
- gherkin (~> 2.9.0)
9
- json (>= 1.4.6)
10
- term-ansicolor (>= 1.0.6)
11
4
  diff-lcs (1.1.3)
12
- gherkin (2.9.3)
13
- json (>= 1.4.6)
14
5
  git (1.2.5)
15
- jeweler (1.8.3)
6
+ jeweler (1.8.4)
16
7
  bundler (~> 1.0)
17
8
  git (>= 1.2.5)
18
9
  rake
19
10
  rdoc
20
- json (1.6.6)
21
- multi_json (1.3.2)
22
- rake (0.9.2.2)
11
+ json (1.7.6)
12
+ multi_json (1.5.0)
13
+ rake (10.0.3)
23
14
  rdoc (3.12)
24
15
  json (~> 1.4)
25
- redcarpet (2.1.1)
26
- rspec (2.8.0)
27
- rspec-core (~> 2.8.0)
28
- rspec-expectations (~> 2.8.0)
29
- rspec-mocks (~> 2.8.0)
30
- rspec-core (2.8.0)
31
- rspec-expectations (2.8.0)
32
- diff-lcs (~> 1.1.2)
33
- rspec-mocks (2.8.0)
34
- simplecov (0.6.2)
35
- multi_json (~> 1.3)
36
- simplecov-html (~> 0.5.3)
37
- simplecov-html (0.5.3)
38
- term-ansicolor (1.0.7)
39
- yard (0.7.5)
16
+ rspec (2.12.0)
17
+ rspec-core (~> 2.12.0)
18
+ rspec-expectations (~> 2.12.0)
19
+ rspec-mocks (~> 2.12.0)
20
+ rspec-core (2.12.2)
21
+ rspec-expectations (2.12.1)
22
+ diff-lcs (~> 1.1.3)
23
+ rspec-mocks (2.12.1)
24
+ simplecov (0.7.1)
25
+ multi_json (~> 1.0)
26
+ simplecov-html (~> 0.7.1)
27
+ simplecov-html (0.7.1)
28
+ yard (0.8.3)
40
29
 
41
30
  PLATFORMS
42
31
  ruby
43
32
 
44
33
  DEPENDENCIES
45
- bundler (~> 1.1.0)
46
- cucumber
34
+ bundler (~> 1.2.0)
47
35
  jeweler (~> 1.8.3)
48
- redcarpet
49
- rspec (~> 2.8.0)
36
+ rake (~> 10.0.0)
37
+ rspec (~> 2.12.0)
50
38
  simplecov
51
- yard (~> 0.7)
39
+ yard (~> 0.8.0)
data/README.md CHANGED
@@ -1,19 +1,25 @@
1
+ [![Build Status](https://travis-ci.org/bbatsov/rubocop.png?branch=master)](https://travis-ci.org/bbatsov/rubocop)
2
+
1
3
  # rubocop
2
4
 
3
- Ruby code style checker.
5
+ Ruby code style checker based on the [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide).
6
+
7
+ ## Basic Usage
8
+
9
+ Running `rubocop` with no arguments will check all Ruby source files in the current folder:
10
+
11
+ ```bash
12
+ $ rubocop
13
+ ```
14
+
15
+ Alternatively you can `rubocop` a list of files and folders to check:
4
16
 
5
- ## Contributing to Rubocop
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
- * Fork the project.
10
- * Start a feature/bugfix branch.
11
- * Commit and push until you are happy with your contribution.
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * 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.
17
+ ```bash
18
+ $ rubocop app spec lib/something.rb
19
+ ```
14
20
 
15
21
  ## Copyright
16
22
 
17
- Copyright (c) 2012 Bozhidar Batsov. See LICENSE.txt for
23
+ Copyright (c) 2012-2013 Bozhidar Batsov. See LICENSE.txt for
18
24
  further details.
19
25
 
data/Rakefile CHANGED
@@ -36,9 +36,6 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
36
  spec.rcov = true
37
37
  end
38
38
 
39
- require 'cucumber/rake/task'
40
- Cucumber::Rake::Task.new(:features)
41
-
42
39
  task :default => :spec
43
40
 
44
41
  require 'yard'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.1
data/bin/rubocop CHANGED
@@ -12,5 +12,5 @@ time = Benchmark.realtime do
12
12
  result = cli.run
13
13
  end
14
14
 
15
- puts "Finished in #{time} seconds"
15
+ puts "Finished in #{time} seconds" if $options[:verbose]
16
16
  exit result
@@ -0,0 +1 @@
1
+ # encoding: utf-8
@@ -1,9 +1,11 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'bundler'
2
4
  begin
3
5
  Bundler.setup(:default, :development)
4
6
  rescue Bundler::BundlerError => e
5
7
  $stderr.puts e.message
6
- $stderr.puts "Run `bundle install` to install missing gems"
8
+ $stderr.puts 'Run `bundle install` to install missing gems'
7
9
  exit e.status_code
8
10
  end
9
11
 
data/lib/rubocop/cli.rb CHANGED
@@ -11,36 +11,36 @@ module Rubocop
11
11
  # the target files
12
12
  # @return [Fixnum] UNIX exit code
13
13
  def run(args = ARGV)
14
- options = { :mode => :default }
14
+ $options = { mode: :default }
15
15
 
16
16
  OptionParser.new do |opts|
17
- opts.banner = "Usage: rubocop [options] [file1, file2, ...]"
17
+ opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
18
18
 
19
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
20
- options[:verbose] = v
19
+ opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
20
+ $options[:verbose] = v
21
21
  end
22
- opts.on("-e", "--emacs", "Emacs style output") do
23
- options[:mode] = :emacs_style
22
+ opts.on('-e', '--emacs', 'Emacs style output') do
23
+ $options[:mode] = :emacs_style
24
24
  end
25
25
  end.parse!(args)
26
26
 
27
27
  cops = Cop::Cop.all
28
+ show_cops_on_duty(cops) if $options[:verbose]
28
29
  total_offences = 0
29
30
 
30
- target_files(args).reject { |f| File.directory?(f) }.each do |file|
31
- report = Report.create(file, options[:mode])
32
- source = File.readlines(file).map { |line|
33
- enc = line.encoding.name
34
- # Get rid of invalid byte sequences
35
- line.encode!('UTF-16', enc, :invalid => :replace, :replace => '')
36
- line.encode!(enc, 'UTF-16')
37
-
31
+ target_files(args).each do |file|
32
+ report = Report.create(file, $options[:mode])
33
+ source = File.readlines(file).map do |line|
34
+ get_rid_of_invalid_byte_sequences(line)
38
35
  line.chomp
39
- }
36
+ end
37
+
38
+ tokens, sexp, correlations = CLI.rip_source(source)
40
39
 
41
40
  cops.each do |cop_klass|
42
41
  cop = cop_klass.new
43
- cop.inspect_source(file, source)
42
+ cop.correlations = correlations
43
+ cop.inspect(file, source, tokens, sexp)
44
44
  total_offences += cop.offences.count
45
45
  report << cop if cop.has_report?
46
46
  end
@@ -54,22 +54,48 @@ module Rubocop
54
54
  return total_offences == 0 ? 0 : 1
55
55
  end
56
56
 
57
+ def get_rid_of_invalid_byte_sequences(line)
58
+ enc = line.encoding.name
59
+ # UTF-16 works better in this algorithm but is not supported in 1.9.2.
60
+ temporary_encoding = (RUBY_VERSION == '1.9.2') ? 'UTF-8' : 'UTF-16'
61
+ line.encode!(temporary_encoding, enc, invalid: :replace, replace: '')
62
+ line.encode!(enc, temporary_encoding)
63
+ end
64
+
65
+ def self.rip_source(source)
66
+ tokens = Ripper.lex(source.join("\n")).map { |t| Cop::Token.new(*t) }
67
+ sexp = Ripper.sexp(source.join("\n"))
68
+ Cop::Position.make_position_objects(sexp)
69
+ correlations = Cop::Grammar.new(tokens).correlate(sexp)
70
+ [tokens, sexp, correlations]
71
+ end
72
+
73
+ def show_cops_on_duty(cops)
74
+ puts 'Reporting for duty:'
75
+ cops.each { |c| puts c }
76
+ puts '*******************'
77
+ end
78
+
57
79
  # Generate a list of target files by expanding globing patterns
58
80
  # (if any). If args is empty recursively finds all Ruby source
59
81
  # files in the current directory
60
82
  # @return [Array] array of filenames
61
83
  def target_files(args)
62
- raw_target_files(args).reject { |name| name =~ /_flymake/ }
63
- end
64
-
65
- def raw_target_files(args)
66
84
  return Dir['**/*.rb'] if args.empty?
67
85
 
68
- if glob = args.detect { |arg| arg =~ /\*/ }
69
- Dir[glob]
70
- else
71
- args
86
+ files = []
87
+
88
+ args.each do |target|
89
+ if File.directory?(target)
90
+ files << Dir["#{target}/**/*.rb"]
91
+ elsif target =~ /\*/
92
+ files << Dir[target]
93
+ else
94
+ files << target
95
+ end
72
96
  end
97
+
98
+ files.flatten
73
99
  end
74
100
  end
75
101
  end
@@ -0,0 +1,112 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AlignParameters < Cop
6
+ ERROR_MESSAGE = 'Align the parameters of a method call if they span ' +
7
+ 'more than one line.'
8
+
9
+ def inspect(file, source, tokens, sexp)
10
+ @file = file
11
+ @tokens = tokens
12
+ @token_indexes = {}
13
+ @tokens.each_with_index { |t, ix| @token_indexes[t.pos] = ix }
14
+
15
+ each(:method_add_arg, sexp) do |method_add_arg|
16
+ args = get_args(method_add_arg) or next
17
+ first_arg, rest_of_args = divide_args(args)
18
+ @first_lparen_ix = get_lparen_ix(method_add_arg)
19
+ pos_of_1st_arg = position_of(first_arg) or next # Give up.
20
+ rest_of_args.each do |arg|
21
+ pos = position_of(arg) or next # Give up if no position found.
22
+ if pos.lineno != pos_of_1st_arg.lineno
23
+ if pos.column != pos_of_1st_arg.column
24
+ add_offence(:convention, pos.lineno, ERROR_MESSAGE)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def get_args(method_add_arg)
34
+ fcall = method_add_arg[1]
35
+ return nil if fcall[0] != :fcall
36
+ return nil if fcall[1][0..1] == [:@ident, 'lambda']
37
+ arg_paren = method_add_arg[2..-1][0]
38
+ return nil if arg_paren[0] != :arg_paren || arg_paren[1].nil?
39
+
40
+ # A command (call wihtout parentheses) as first parameter
41
+ # means there's only one parameter.
42
+ return nil if [:command, :command_call].include?(arg_paren[1][0][0])
43
+
44
+ args_add_block = arg_paren[1]
45
+ unless args_add_block[0] == :args_add_block
46
+ fail "\n#{@file}: #{method_add_arg}"
47
+ end
48
+ args_add_block[1].empty? ? [args_add_block[2]] : args_add_block[1]
49
+ end
50
+
51
+ def divide_args(args)
52
+ if args[0] == :args_add_star
53
+ first_arg = args[1]
54
+ rest_of_args = args[2..-1]
55
+ else
56
+ first_arg = args[0]
57
+ rest_of_args = args[1..-1]
58
+ end
59
+ [first_arg, rest_of_args]
60
+ end
61
+
62
+ def get_lparen_ix(method_add_arg)
63
+ method_name_pos = method_add_arg[1][1][-1]
64
+ method_name_ix = @token_indexes[method_name_pos]
65
+ method_name_ix +
66
+ @tokens[method_name_ix..-1].map(&:type).index(:on_lparen)
67
+ end
68
+
69
+ def position_of(sexp)
70
+ # Indentation inside a string literal is irrelevant.
71
+ return nil if sexp[0] == :string_literal
72
+
73
+ pos = find_pos_in_sexp(sexp) or return nil # Nil means not found.
74
+ ix = find_first_non_whitespace_token(pos) or return nil
75
+ @tokens[ix].pos
76
+ end
77
+
78
+ def find_pos_in_sexp(sexp)
79
+ return sexp[2] if Position === sexp[2]
80
+ sexp.grep(Array).each do |s|
81
+ pos = find_pos_in_sexp(s) and return pos
82
+ end
83
+ nil
84
+ end
85
+
86
+ def find_first_non_whitespace_token(pos)
87
+ ix = @token_indexes[pos]
88
+ newline_found = false
89
+ start_ix = ix.downto(0) do |i|
90
+ case @tokens[i].text
91
+ when '('
92
+ break i + 1 if i == @first_lparen_ix
93
+ when "\n"
94
+ newline_found = true
95
+ when /\t/
96
+ # Bail out if tabs are used. Too difficult to calculate column.
97
+ return nil
98
+ when ','
99
+ if newline_found
100
+ break i + 1
101
+ else
102
+ # Bail out if there's a preceding comma on the same line.
103
+ return nil
104
+ end
105
+ end
106
+ end
107
+ offset = @tokens[start_ix..-1].index { |t| not whitespace?(t) }
108
+ start_ix + offset
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class Blocks < Cop
6
+ ERROR_MESSAGE = ['Avoid using {...} for multi-line blocks.',
7
+ 'Prefer {...} over do...end for single-line blocks.']
8
+
9
+ def inspect(file, source, tokens, sexp)
10
+ @file = file
11
+
12
+ # The reverse_correlations maps grammar path object ids to
13
+ # token indexes, so we can use it to find the corresponding }
14
+ # for each {.
15
+ reverse_correlations = Hash.new([])
16
+ @correlations.each do |ix, path|
17
+ reverse_correlations[path.object_id] += [ix]
18
+ end
19
+ tokens.each_with_index do |t, ix|
20
+ case [t.type, t.text]
21
+ when [:on_lbrace, '{']
22
+ path = @correlations[ix] or next
23
+ if path.last == :brace_block
24
+ rbrace_ix = reverse_correlations[path.object_id] - [ix]
25
+ if rbrace_ix.empty?
26
+ fail "\n#@file:#{t.pos.lineno}:#{t.pos.column}: " +
27
+ 'Matching brace not found'
28
+ end
29
+ if tokens[*rbrace_ix].pos.lineno > t.pos.lineno
30
+ add_offence(:convention, t.pos.lineno, ERROR_MESSAGE[0])
31
+ end
32
+ end
33
+ when [:on_kw, 'do']
34
+ end_offset = tokens[ix..-1].index { |t2| t2.text == 'end' }
35
+ unless end_offset
36
+ fail "\n#@file:#{t.pos.lineno}:#{t.pos.column}: " +
37
+ 'Matching end not found'
38
+ end
39
+ end_token_ix = ix + end_offset
40
+ if tokens[end_token_ix].pos.lineno == t.pos.lineno
41
+ add_offence(:convention, t.pos.lineno, ERROR_MESSAGE[1])
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -2,8 +2,37 @@
2
2
 
3
3
  module Rubocop
4
4
  module Cop
5
+ class Position < Struct.new :lineno, :column
6
+ # Does a recursive search and replaces each [lineno, column] array
7
+ # in the sexp with a Position object.
8
+ def self.make_position_objects(sexp)
9
+ if sexp[0] =~ /^@/
10
+ sexp[2] = Position.new(*sexp[2])
11
+ else
12
+ sexp.grep(Array).each { |s| make_position_objects(s) }
13
+ end
14
+ end
15
+
16
+ # The point of this class is to provide named attribute access.
17
+ # So we don't want backwards compatibility with array indexing.
18
+ undef_method :[]
19
+ end
20
+
21
+ class Token
22
+ attr_reader :pos, :type, :text
23
+
24
+ def initialize(pos, type, text)
25
+ @pos, @type, @text = Position.new(*pos), type, text
26
+ end
27
+
28
+ def to_s
29
+ "[[#{@pos.lineno}, #{@pos.column}], #@type, #{@text.inspect}]"
30
+ end
31
+ end
32
+
5
33
  class Cop
6
34
  attr_accessor :offences
35
+ attr_writer :correlations
7
36
 
8
37
  @all = []
9
38
  @enabled = []
@@ -16,7 +45,6 @@ module Rubocop
16
45
  end
17
46
 
18
47
  def self.inherited(subclass)
19
- puts "Registering cop #{subclass}"
20
48
  all << subclass
21
49
  end
22
50
 
@@ -36,42 +64,37 @@ module Rubocop
36
64
  !@offences.empty?
37
65
  end
38
66
 
39
- def inspect_source(file, source)
40
- case method(:inspect).arity
41
- when 2
42
- inspect(file, source)
43
- else
44
- tokens = Ripper.lex(source.join("\n"))
45
- sexp = Ripper.sexp(source.join("\n"))
46
- inspect(file, source, tokens, sexp)
47
- end
48
- end
49
-
50
- def add_offence(file, line_number, line, message)
51
- @offences << Offence.new(file, line_number, line, message)
67
+ def add_offence(file, line_number, message)
68
+ @offences << Offence.new(file, line_number, message)
52
69
  end
53
70
 
54
71
  private
55
72
 
56
73
  def each_parent_of(sym, sexp)
57
74
  parents = []
58
- sexp.each { |elem|
75
+ sexp.each do |elem|
59
76
  if Array === elem
60
77
  if elem[0] == sym
61
- parents << sexp
78
+ parents << sexp unless parents.include?(sexp)
62
79
  elem = elem[1..-1]
63
80
  end
64
- each_parent_of(sym, elem) { |parent| parents << parent }
81
+ each_parent_of(sym, elem) do |parent|
82
+ parents << parent unless parents.include?(parent)
83
+ end
65
84
  end
66
- }
67
- parents.uniq.each { |parent| yield parent }
85
+ end
86
+ parents.each { |parent| yield parent }
68
87
  end
69
88
 
70
89
  def each(sym, sexp)
71
90
  yield sexp if sexp[0] == sym
72
- sexp.each { |elem|
91
+ sexp.each do |elem|
73
92
  each(sym, elem) { |s| yield s } if Array === elem
74
- }
93
+ end
94
+ end
95
+
96
+ def whitespace?(token)
97
+ [:on_sp, :on_ignored_nl, :on_nl].include?(token.type)
75
98
  end
76
99
  end
77
100
  end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class DefParentheses < Cop
6
+ ERROR_MESSAGE = ['Use def with parentheses when there are arguments.',
7
+ 'Omit the parentheses in defs when the method ' +
8
+ "doesn't accept any arguments."]
9
+ EMPTY_PARAMS = [:params, nil, nil, nil, nil, nil]
10
+
11
+ def inspect(file, source, tokens, sexp)
12
+ each(:def, sexp) do |def_sexp|
13
+ pos = def_sexp[1][-1]
14
+ case def_sexp[2][0]
15
+ when :params
16
+ if def_sexp[2] != EMPTY_PARAMS
17
+ add_offence(:convention, pos.lineno, ERROR_MESSAGE[0])
18
+ end
19
+ when :paren
20
+ if def_sexp[2][1] == EMPTY_PARAMS
21
+ method_name_ix = tokens.index { |t| t.pos == pos }
22
+ start = method_name_ix + 1
23
+ rparen_ix = start + tokens[start..-1].index { |t| t.text == ')' }
24
+ first_body_token = tokens[(rparen_ix + 1)..-1].find do |t|
25
+ not whitespace?(t)
26
+ end
27
+ if first_body_token.pos.lineno > pos.lineno
28
+ # Only report offence if there's a line break after
29
+ # the empty parens.
30
+ add_offence(:convention, pos.lineno, ERROR_MESSAGE[1])
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Rubocop
2
4
  module Cop
3
5
  class EmptyLines < Cop
@@ -7,17 +9,16 @@ module Rubocop
7
9
  each_parent_of(:def, sexp) do |parent|
8
10
  defs = parent.select { |child| child[0] == :def }
9
11
  identifier_of_first_def = defs[0][1]
10
- current_row_ix = identifier_of_first_def[-1][0] - 1
12
+ current_row_ix = identifier_of_first_def[-1].lineno - 1
11
13
  # The first def doesn't need to have an empty line above it,
12
14
  # so we iterate starting at index 1.
13
- defs[1..-1].each { |child|
14
- next_row_ix = child[1][-1][0] - 1
15
+ defs[1..-1].each do |child|
16
+ next_row_ix = child[1][-1].lineno - 1
15
17
  if source[current_row_ix..next_row_ix].grep(/^[ \t]*$/).empty?
16
- add_offence(:convention, next_row_ix, source[next_row_ix],
17
- ERROR_MESSAGE)
18
+ add_offence(:convention, next_row_ix + 1, ERROR_MESSAGE)
18
19
  end
19
20
  current_row_ix = next_row_ix
20
- }
21
+ end
21
22
  end
22
23
  end
23
24
  end
@@ -9,7 +9,7 @@ module Rubocop
9
9
  def inspect(file, source, tokens, sexp)
10
10
  unless source[0] =~ /#.*coding: (UTF|utf)-8/
11
11
  message = sprintf(ERROR_MESSAGE)
12
- add_offence(:convention, 0, 0, message)
12
+ add_offence(:convention, 1, message)
13
13
  end
14
14
  end
15
15
  end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class EndOfLine < Cop
6
+ ERROR_MESSAGE = 'Carriage return character detected.'
7
+
8
+ def inspect(file, source, tokens, sexp)
9
+ source.each_with_index do |line, index|
10
+ if line =~ /\r$/
11
+ add_offence(:convention, index + 1, ERROR_MESSAGE)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end