rubocop-definition_validator 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6460fb37d146570cda2e89042dabd2e660c8279b
4
- data.tar.gz: ebb16294fb3331fe3f69afe261505aa968aaa77f
3
+ metadata.gz: e64054dd1e26a1402b8b3066859dde92e333008a
4
+ data.tar.gz: e51cf35bcdc4e921b38e983dd9d0f6edf2b0681b
5
5
  SHA512:
6
- metadata.gz: 8a623aaedbf5d4896a90b425f559bff02dde539d3c661db5b3273c394f7d7dbd8a8ab164d4adfef2607f1bf6e90881899b43bd8ff81c0e9087fc0f4bde832d25
7
- data.tar.gz: 40301714be7c10e88d7a17da1d94b1e21a60b1ab35a8dcf0a738af829c37a6f5e78818d31d2c5aabbf0f2aca3884b37b09a964681a85dce55249b029b9405309
6
+ metadata.gz: b32c56023f9a88c3baf652dd3f76afdbbf3fe5bc49dce9d6177f9c5175e484728ed43d0f0b6770b40157b6dc4a7b3ae318d9c6a51274430909d77daefbb4b317
7
+ data.tar.gz: 3b73155495975c9cdb0b1dc13007d0960ad8664478cf1b327328acefcab6f204db0730dfbb154507a8e9c2b5fb9b8741f16feadc861e040d9e1ae9b50b2b97db
@@ -9,17 +9,18 @@ module RuboCop
9
9
 
10
10
  msg = nil
11
11
  Rubocop::DefinitionValidator::ChangeDetector.changed_methods.each do |m|
12
- old, new = m[:removed], m[:added]
12
+ old, new = m.removed, m.added
13
13
 
14
14
  next if old.name.size <= min
15
15
 
16
- old_callable, _ = old.callable?(name, args)
16
+ PryTestcase.pry
17
+ old_callable, *_ = old.callable?(name, args)
17
18
  next unless old_callable
18
19
 
19
- new_callable, reason = new.callable?(name, args)
20
+ new_callable, *reason = new.callable?(name, args)
20
21
  next if new_callable
21
22
 
22
- msg = reason.respond_to?(:call) ? reason.(old, new) : reason
23
+ msg = Rubocop::DefinitionValidator::Message.new(m).of(*reason)
23
24
  end
24
25
 
25
26
  return unless msg
@@ -9,7 +9,8 @@ RuboCop::DefinitionValidator::Inject.defaults!
9
9
 
10
10
  require 'rubocop/definition_validator/line'
11
11
  require 'rubocop/definition_validator/patch'
12
- require 'rubocop/definition_validator/reason'
12
+ require 'rubocop/definition_validator/message'
13
+ require 'rubocop/definition_validator/changed_method'
13
14
  require 'rubocop/definition_validator/method'
14
15
  require 'rubocop/definition_validator/change_detector'
15
16
 
@@ -15,17 +15,8 @@ module Rubocop::DefinitionValidator::ChangeDetector
15
15
  # {added: Method, removed: Method}
16
16
  # ]
17
17
  @changed_methods = patches
18
- .map{|patch| patch.changed_method_codes}
18
+ .map{|patch| patch.changed_methods}
19
19
  .flatten
20
- .map{|code|
21
- code.map{|k, v|
22
- begin
23
- [k, Rubocop::DefinitionValidator::Method.new(v.body)]
24
- rescue Rubocop::DefinitionValidator::Method::InvalidAST
25
- nil
26
- end
27
- }.compact.to_h
28
- }
29
20
  end
30
21
  end
31
22
  end
@@ -0,0 +1,11 @@
1
+ # This class has added method, removed method, line number and file name.
2
+ class Rubocop::DefinitionValidator::ChangedMethod
3
+ attr_reader :added, :removed, :line, :file_name
4
+
5
+ def initialize(added_line, removed_line, lineno, fname)
6
+ @added = Rubocop::DefinitionValidator::Method.new(added_line.body)
7
+ @removed = Rubocop::DefinitionValidator::Method.new(removed_line.body)
8
+ @line = lineno
9
+ @file_name = fname
10
+ end
11
+ end
@@ -0,0 +1,65 @@
1
+ class Rubocop::DefinitionValidator::Message
2
+ # @param [ChangedMethod] method
3
+ def initialize(method)
4
+ @method = method
5
+ end
6
+
7
+ # @param [Symbol] reason
8
+ def of(reason, *args)
9
+ __send__(reason, *args) + suffix
10
+ end
11
+
12
+
13
+ private
14
+
15
+ # @return [Proc]
16
+ def method_name
17
+ "#{@method.removed.name} is undefined. Did you mean? #{@method.added.name}"
18
+ end
19
+
20
+ # @param [Integer] given given args size.
21
+ # @param [Integer] expected expected args size.
22
+ # @return [Proc]
23
+ def not_enough_norml_arguments(given, expected)
24
+ n = expected - given
25
+ not_enough_arg_names = @method.added.normal_params.dup.pop(n).map{|x| x[1]}
26
+ "Not enough arguments. Did you forget the following arguments? [#{not_enough_arg_names.join(', ')}]"
27
+ end
28
+
29
+ # @param [Integer] given given args size.
30
+ # @param [Integer] expected expected args size.
31
+ # @return [Proc]
32
+ def not_enough_normal_after_rest_arguments(given, expected)
33
+ n = expected - given
34
+ not_enough_arg_names = @method.added.normal_params_after_rest.dup.pop(n).map{|x| x[1]}
35
+ "Not enough arguments. Did you forget the following arguments? [#{not_enough_arg_names.join(', ')}]"
36
+ end
37
+
38
+ def too_many_arguments
39
+ 'Too many arguments'
40
+ end
41
+
42
+ def kwparam_required
43
+ "Keyword params is required."
44
+ end
45
+
46
+ # @param [String] got
47
+ def kwparam_should_be_hash(got)
48
+ "Keyword params should be a Hash. But got #{got}"
49
+ end
50
+
51
+ # @param [Array<String>] not_founds
52
+ def kwparam_not_found(not_founds)
53
+ "The following keyword parameters are required. But not received. [#{not_founds.join(', ')}]"
54
+ end
55
+
56
+ def unexpected_kwparam(unexpected)
57
+ "The following keyword parameters are not expected. But received. [#{unexpected.join(', ')}]"
58
+ end
59
+
60
+
61
+
62
+ def suffix
63
+ "\nThis method is defined at #{@method.file_name} L#{@method.line}"
64
+ end
65
+ end
@@ -1,6 +1,7 @@
1
1
  require 'ripper'
2
2
 
3
3
  module Rubocop::DefinitionValidator
4
+ # a method
4
5
  class Method
5
6
  class InvalidAST < ArgumentError; end
6
7
 
@@ -47,9 +48,9 @@ module Rubocop::DefinitionValidator
47
48
 
48
49
  # @param [Array<RuboCop::Node>] args
49
50
  # @return [Boolean] callable
50
- # @return [Proc] cause
51
+ # @return [Object] cause
51
52
  def callable?(name, args)
52
- return false, Reason.method_name unless name == @name
53
+ return false, :method_name unless name == @name
53
54
 
54
55
  args = args.dup
55
56
 
@@ -57,11 +58,8 @@ module Rubocop::DefinitionValidator
57
58
  normal_params_size = (normal_params || []).size
58
59
  received_normal_params_size = args.shift(normal_params_size).size
59
60
  unless received_normal_params_size == normal_params_size
60
- return false, Reason.not_enough_arguments(
61
- received_normal_params_size,
62
- normal_params_size,
63
- :normal_params,
64
- )
61
+ return false, :not_enough_norml_arguments, received_normal_params_size, normal_params_size
62
+
65
63
  end
66
64
 
67
65
  if has_required_keyword_params?
@@ -99,11 +97,7 @@ module Rubocop::DefinitionValidator
99
97
  normal_params_after_rest_size = (normal_params_after_rest || []).size
100
98
  given_normal_params_after_rest_size = args.pop(normal_params_after_rest_size).size
101
99
  unless given_normal_params_after_rest_size == normal_params_after_rest_size
102
- return false, Reason.not_enough_arguments(
103
- given_normal_params_after_rest_size,
104
- normal_params_after_rest_size,
105
- :normal_params_after_rest
106
- )
100
+ return false, :not_enough_normal_after_rest_arguments, given_normal_params_after_rest_size, normal_params_after_rest_size
107
101
  end
108
102
 
109
103
  # rest引数があれば全て呑み込むためtrue
@@ -112,22 +106,22 @@ module Rubocop::DefinitionValidator
112
106
  if default_value_params
113
107
  return true if args.size <= default_value_params.size
114
108
  # XXX: optimize message.
115
- return false, "Too many argument"
109
+ return false, :too_many_arguments
116
110
  end
117
111
  return true if args.empty?
118
- return false, "Too many argument"
112
+ return false, :too_many_arguments
119
113
  end
120
114
 
121
115
  def decide_with_required_keyword_params(args)
122
116
  kwparam = args.pop(1)
123
- usable, reason = usable_as_keyword_param?(kwparam[0])
124
- return false, reason unless usable
117
+ usable, *reason = usable_as_keyword_param?(kwparam[0])
118
+ return false, *reason unless usable
125
119
 
126
120
  return decide_rest_args(args)
127
121
  end
128
122
 
129
123
  def decide_with_keyword_params(args)
130
- ok, _ = decide_rest_args(args.dup)
124
+ ok, *_ = decide_rest_args(args.dup)
131
125
  return true if ok
132
126
 
133
127
  return decide_with_required_keyword_params(args)
@@ -148,8 +142,8 @@ module Rubocop::DefinitionValidator
148
142
  # @param [RuboCop::Node] arg
149
143
  def usable_as_keyword_param?(arg)
150
144
  # should be hash
151
- return false, "Keyword params is required." unless arg
152
- return false, "Keyword params should be hash. But got #{arg.loc.expression.source}" unless arg.hash_type? || !arg.literal?
145
+ return false, :kwparam_required unless arg
146
+ return false, :kwparam_should_be_hash, arg.loc.expression.source unless arg.hash_type? || !arg.literal?
153
147
 
154
148
  # should have specified keyword
155
149
  return true unless arg.hash_type?
@@ -170,7 +164,7 @@ module Rubocop::DefinitionValidator
170
164
  not received_keyword_names.include?(name)
171
165
  end
172
166
  unless not_fould_requireds.empty?
173
- return false, "The following keyword parameters are required. But not received. #{not_fould_requireds.join(', ')}"
167
+ return false, :kwparam_not_found, not_fould_requireds.join(', ')
174
168
  end
175
169
 
176
170
  return true if keyword_rest_params
@@ -183,7 +177,7 @@ module Rubocop::DefinitionValidator
183
177
  not allowed_keyword_names.include?(name)
184
178
  end
185
179
  unless unexpected_keywords.empty?
186
- return false, "The following keyword parameters are not expected. But received. #{unexpected_keywords}"
180
+ return false, :unexpected_kwparam, unexpected_keywords
187
181
  end
188
182
  return true
189
183
  end
@@ -3,61 +3,64 @@
3
3
  # The original method retuns only added lines.
4
4
  # However we want added and removed lines.
5
5
  #
6
- class Rubocop::DefinitionValidator::Patch < GitDiffParser::Patch
7
- ADDED_LINE = -> (line) { line.start_with?('+') && line !~ /^\+\+\+/ }
8
- REMOVED_LINE = -> (line) { line.start_with?('-') && line !~ /^\-\-\-/ }
6
+ module Rubocop::DefinitionValidator
7
+ class Patch < GitDiffParser::Patch
8
+ ADDED_LINE = -> (line) { line.start_with?('+') && line !~ /^\+\+\+/ }
9
+ REMOVED_LINE = -> (line) { line.start_with?('-') && line !~ /^\-\-\-/ }
9
10
 
10
- def initialize(original_patch)
11
- @body = original_patch.body
12
- @file = original_patch.file
13
- @secure_hash = original_patch.secure_hash
14
- end
11
+ def initialize(original_patch)
12
+ @body = original_patch.body
13
+ @file = original_patch.file
14
+ @secure_hash = original_patch.secure_hash
15
+ end
16
+
17
+ # @return [Array<Line>] changed lines
18
+ def changed_lines
19
+ line_number = 0
20
+ removed_line_offset = 0
15
21
 
16
- # @return [Array<Line>] changed lines
17
- def changed_lines
18
- line_number = 0
19
- removed_line_offset = 0
22
+ lines.each_with_index.inject([]) do |lines, (content, patch_position)|
23
+ case content
24
+ when RANGE_INFORMATION_LINE
25
+ line_number = Regexp.last_match[:line_number].to_i
26
+ removed_line_offset = 0
27
+ when ADDED_LINE
28
+ line = Rubocop::DefinitionValidator::Line.new(
29
+ content: content,
30
+ number: line_number,
31
+ patch_position: patch_position
32
+ )
33
+ lines << line
34
+ line_number += 1
35
+ removed_line_offset = 0
36
+ when REMOVED_LINE
37
+ line = Rubocop::DefinitionValidator::Line.new(
38
+ content: content,
39
+ number: line_number + removed_line_offset,
40
+ patch_position: patch_position
41
+ )
42
+ lines << line
43
+ removed_line_offset +=1
44
+ when NOT_REMOVED_LINE
45
+ line_number += 1
46
+ removed_line_offset = 0
47
+ end
20
48
 
21
- lines.each_with_index.inject([]) do |lines, (content, patch_position)|
22
- case content
23
- when RANGE_INFORMATION_LINE
24
- line_number = Regexp.last_match[:line_number].to_i
25
- removed_line_offset = 0
26
- when ADDED_LINE
27
- line = Rubocop::DefinitionValidator::Line.new(
28
- content: content,
29
- number: line_number,
30
- patch_position: patch_position
31
- )
32
- lines << line
33
- line_number += 1
34
- removed_line_offset = 0
35
- when REMOVED_LINE
36
- line = Rubocop::DefinitionValidator::Line.new(
37
- content: content,
38
- number: line_number + removed_line_offset,
39
- patch_position: patch_position
40
- )
41
- lines << line
42
- removed_line_offset +=1
43
- when NOT_REMOVED_LINE
44
- line_number += 1
45
- removed_line_offset = 0
49
+ lines
46
50
  end
51
+ end
47
52
 
53
+ def changed_methods
54
+ lines = changed_lines
48
55
  lines
56
+ .group_by{|l| l.number}
57
+ .select{|_, v| v.size == 2}
58
+ .select{|_, v| t = v.map(&:type); t.include?('-') && t.include?('+')}
59
+ .select{|_, v| v.all?{|x| x.content =~ /def\s+\w+/}}
60
+ .map{|line, v|
61
+ sorted = v.sort_by(&:type)
62
+ ChangedMethod.new(sorted.first, sorted.last, line, @file)
63
+ }
49
64
  end
50
65
  end
51
-
52
- def changed_method_codes
53
- lines = changed_lines
54
- lines
55
- .group_by{|l| l.number}
56
- .values
57
- .select{|l| l.size == 2}
58
- .select{|l| t = l.map(&:type); t.include?('-') && t.include?('+')}
59
- .select{|l| l.all?{|x| x.content =~ /def\s+\w+/}}
60
- .map{|l| l.sort_by(&:type)}
61
- .map{|l| {added: l.first, removed: l.last}}
62
- end
63
66
  end
@@ -1,5 +1,5 @@
1
1
  module Rubocop
2
2
  module DefinitionValidator
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-definition_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masataka Kuwabara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-03 00:00:00.000000000 Z
11
+ date: 2016-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -186,11 +186,12 @@ files:
186
186
  - lib/rubocop/cop/lint/definition_validator.rb
187
187
  - lib/rubocop/definition_validator.rb
188
188
  - lib/rubocop/definition_validator/change_detector.rb
189
+ - lib/rubocop/definition_validator/changed_method.rb
189
190
  - lib/rubocop/definition_validator/inject.rb
190
191
  - lib/rubocop/definition_validator/line.rb
192
+ - lib/rubocop/definition_validator/message.rb
191
193
  - lib/rubocop/definition_validator/method.rb
192
194
  - lib/rubocop/definition_validator/patch.rb
193
- - lib/rubocop/definition_validator/reason.rb
194
195
  - lib/rubocop/definition_validator/version.rb
195
196
  - rubocop-definition_validator.gemspec
196
197
  homepage: https://github.com/actcat/rubocop-definition_validator
@@ -1,20 +0,0 @@
1
- module Rubocop::DefinitionValidator::Reason
2
- class << self
3
- # @return [Proc]
4
- def method_name
5
- -> (old, new) { "#{old.name} is undefined. Did you mean? #{new.name}" }
6
- end
7
-
8
- # @param [Integer] given given args size.
9
- # @param [Integer] expected expected args size.
10
- # @param [Symbol] kind normal_params or normal_params_after_rest
11
- # @return [Proc]
12
- def not_enough_arguments(given, expected, kind)
13
- n = expected - given
14
- -> (_old, new) {
15
- not_enough_arg_names = new.__send__(kind).dup.pop(n).map{|x| x[1]}
16
- "Not enough arguments. Did you forget the following arguments? [#{not_enough_arg_names.join(', ')}]"
17
- }
18
- end
19
- end
20
- end