gry 0.2.0 → 0.3.0

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: 2c1bd8a42dbd705ae82a96c17fd3a91333ee52c5
4
- data.tar.gz: 6b6b7bfbb3be43b5861a4e3dc99856e394c7348e
3
+ metadata.gz: 9605fe8fdf8e5f72df91b6e979f06dc8d136eee4
4
+ data.tar.gz: 153bbc340c13752a2f6678798b00c852db24b228
5
5
  SHA512:
6
- metadata.gz: cf28029f72d0bf2706218ea7acd46f9a57626823513b57c25b58caf5485bbb6985a0cda2be5708c44be22068e2a2483336a7f14819ea36461f911ad1eed51a94
7
- data.tar.gz: b7e0925469a617efcc20ed4c455d9f8edbee75256aed64eb3b99c012de555dbe8f20789606d5a7f9a7b8284889bc8413bdfabb3bc4354919b2adddefd8d5aebd
6
+ metadata.gz: be5344b6d408fc07cd560895720dbcafd2b1db31892273d653edd858c8f7a5a158fc3be167d96ab71e62f18f934a35864fdd6b9b60c0b1140d888b289fef8944
7
+ data.tar.gz: '068c51e65070aa65d512f8b6c38df7c62add4409b2617bb2d96a4f1f1c41331187267e4c7d294f16bdd45f19afc7c9cf4fba2535198ebf326c2e6c9df67cedec'
@@ -19,10 +19,10 @@ module Gry
19
19
  pilot_study = Gry::PilotStudy.new(cops, process: opt.process)
20
20
 
21
21
  bills = pilot_study.analyze
22
- # TODO: Specify the value from option
23
22
  congress = Congress.new(
24
23
  max_count: opt.max_count,
25
24
  min_difference: opt.min_difference,
25
+ metrics_percentile: opt.metrics_percentile,
26
26
  )
27
27
  laws = bills.map do |cop_name, bill|
28
28
  congress.discuss(cop_name, bill)
@@ -1,33 +1,78 @@
1
1
  module Gry
2
2
  class Congress
3
- def initialize(max_count:, min_difference:)
3
+ def initialize(max_count:, min_difference:, metrics_percentile:)
4
4
  @max_count = max_count
5
5
  @min_difference = min_difference
6
+ @metrics_percentile = metrics_percentile
6
7
  end
7
8
 
8
9
  # @param name [String] cop name
9
- # @param bill [Hash{Hash => Integer}] rubocop results
10
+ # @param bill [Hash{Hash => Array}] rubocop results
10
11
  # {
11
- # {config1} => 20,
12
- # {config2} => 0,
13
- # {config3} => 10,
12
+ # {config1} => [offenses],
13
+ # {config2} => [offenses],
14
+ # {config3} => [offenses],
14
15
  # }
15
16
  # @return [Law]
16
17
  def discuss(name, bill)
17
- letter = letter(bill)
18
+ letter = letter(name, bill)
18
19
  Law.new(name, bill, letter)
19
20
  end
20
21
 
21
- def letter(bill)
22
- # [[conf, count], ...]
23
- sorted = bill.sort_by{|_conf, count| count}
24
- min_count = sorted.first.last
22
+
23
+ private
24
+
25
+ def letter(name, bill)
26
+ if RubocopAdapter.metrics_cop?(name)
27
+ letter_for_metrics(name, bill)
28
+ else
29
+ letter_for_enforced_style(bill)
30
+ end
31
+ end
32
+
33
+ def letter_for_metrics(name, bill)
34
+ raise "bill.size is not 1, got #{bill.size}" unless bill.size == 1
35
+ values = bill
36
+ .values[0]
37
+ .map{|offense| offense['message']}
38
+ .map{|message| message[%r!\[([0-9.]+)/[0-9.]+\]$!, 1]}
39
+ .map(&:to_i)
40
+ .sort
41
+
42
+ max = percentile(values)
43
+ if name == 'Metrics/AbcSize'
44
+ max = max.round(2)
45
+ else
46
+ max = max.round
47
+ end
48
+
49
+ {
50
+ 'Enabled' => true,
51
+ 'Max' => max,
52
+ }
53
+ end
54
+
55
+ def letter_for_enforced_style(bill)
56
+ # [[conf, offenses], ...]
57
+ sorted = bill.sort_by{|_conf, offenses| offenses.size}
58
+ min_count = sorted[0].last.size
25
59
  return nil if min_count > @max_count
26
60
 
27
- second_count = sorted[1].last
61
+ second_count = sorted[1].last.size
28
62
  return nil if second_count - min_count < @min_difference
29
63
 
30
64
  sorted.first.first
31
65
  end
66
+
67
+ # @param values [Array<Numeric>]
68
+ # @return [Numeric]
69
+ def percentile(values)
70
+ size = values.size
71
+ index = size.to_f / 100 * @metrics_percentile - 1
72
+ v1 = values[index.floor]
73
+ v2 = values[index.ceil]
74
+
75
+ v1 + (index - index.ceil) * (v2 - v1)
76
+ end
32
77
  end
33
78
  end
@@ -17,7 +17,9 @@ module Gry
17
17
  next
18
18
  end
19
19
  end
20
- comment = to_comment(law.bill)
20
+ comment = RubocopAdapter.metrics_cop?(law.name) ?
21
+ '' :
22
+ to_comment(law.bill)
21
23
  yaml = to_yaml(letter)
22
24
  comment + yaml
23
25
  end.compact
@@ -35,7 +37,9 @@ module Gry
35
37
  end
36
38
 
37
39
  def to_comment(set_count)
38
- set_count.map do |setting, count|
40
+ set_count.map do |setting, offenses|
41
+ count = offenses.size
42
+
39
43
  x = setting
40
44
  .reject{|key, _| key == 'Enabled'}
41
45
  .map{|key, value| "#{key}: #{value}"}
@@ -1,6 +1,6 @@
1
1
  module Gry
2
2
  class Option
3
- attr_reader :args, :process, :version, :fast, :max_count, :min_difference, :display_disabled_cops
3
+ attr_reader :args, :process, :version, :fast, :max_count, :min_difference, :display_disabled_cops, :metrics_percentile
4
4
 
5
5
  def initialize(argv)
6
6
  opt = OptionParser.new
@@ -10,6 +10,7 @@ module Gry
10
10
  @max_count = 10
11
11
  @min_difference = 10
12
12
  @display_disabled_cops = false
13
+ @metrics_percentile = 95
13
14
 
14
15
  opt.banner = 'Usage: gry [options] [Cop1, Cop2, ...]'
15
16
 
@@ -19,6 +20,7 @@ module Gry
19
20
  opt.on('--[no-]fast', 'Run only fast cops. Default: true') {|v| @fast = v}
20
21
  opt.on('--max-count=10', 'Upper limit of issues.') {|v| @max_count = v.to_i}
21
22
  opt.on('--min-difference=10', 'Lower limit of issues number difference') {|v| @min_difference = v.to_i}
23
+ opt.on('--metrics_percentile=95', 'Percentile for allowed complex code') {|v| @metrics_percentile = v.to_i}
22
24
  opt.on('--display-disabled-cops', 'Display disabled cops') {|v| @display_disabled_cops = v}
23
25
 
24
26
  @args = opt.parse(argv)
@@ -40,7 +40,7 @@ module Gry
40
40
  cops, setting = *arg
41
41
  setting.each do |cop_name, s|
42
42
  res[cop_name] ||= {}
43
- res[cop_name][s] ||= 0
43
+ res[cop_name][s] ||= []
44
44
  end
45
45
 
46
46
  RubocopRunner.new(cops, setting)
@@ -58,7 +58,7 @@ module Gry
58
58
  f['offenses'].each do |offense|
59
59
  cop_name = offense['cop_name']
60
60
  next if cop_name == 'Syntax' # Syntax cop is not configurable.
61
- res[cop_name][setting[cop_name]] += 1
61
+ res[cop_name][setting[cop_name]].push(offense)
62
62
  end
63
63
  end
64
64
  end
@@ -73,10 +73,33 @@ module Gry
73
73
  # @param cop_name [String]
74
74
  # @return [Array<Hash>]
75
75
  def cop_configs(cop_name)
76
+ if RubocopAdapter.metrics_cop?(cop_name)
77
+ cop_configs_for_metrics(cop_name)
78
+ else
79
+ cop_configs_for_enforced_style(cop_name)
80
+ end
81
+ end
82
+
83
+ # @param cop_name [String]
84
+ # @return [Array<Hash>]
85
+ def cop_configs_for_metrics(cop_name)
86
+ [
87
+ {
88
+ cop_name => {
89
+ 'Enabled' => true,
90
+ 'Max' => 0,
91
+ }
92
+ }
93
+ ]
94
+ end
95
+
96
+ # @param cop_name [String]
97
+ # @return [Array<Hash>]
98
+ def cop_configs_for_enforced_style(cop_name)
76
99
  cop_config = RubocopAdapter.default_config[cop_name]
77
100
 
78
101
  # e.g. %w[EnforcedHashRocketStyle EnforcedColonStyle EnforcedLastArgumentHashStyle]
79
- enforced_style_names = RubocopAdapter.configurable_styles(cop_config)
102
+ enforced_style_names = RubocopAdapter.enforced_styles(cop_config)
80
103
 
81
104
  # e.g. [
82
105
  # %w[key separator table],
@@ -9,19 +9,26 @@ module Gry
9
9
  def configurable_cops
10
10
  conf = RuboCop::ConfigLoader.default_configuration.to_h
11
11
  conf
12
- .reject{|_key, cop_conf| configurable_styles(cop_conf).empty?}
12
+ .select{|key, cop_conf| !enforced_styles(cop_conf).empty? || metrics_cop?(key) }
13
13
  .reject{|key, _cop_conf| !rails? && key.start_with?('Rails/')}
14
14
  .select{|key, _cop_conf| conf = config_specified_by_user.for_cop(key); conf.empty? || !conf['Enabled']}
15
15
  .keys
16
16
  end
17
17
 
18
18
  # @param cop_conf [Hash]
19
- def configurable_styles(cop_conf)
19
+ def enforced_styles(cop_conf)
20
20
  cop_conf.keys.select do |key|
21
21
  key.start_with?('Enforced')
22
22
  end
23
23
  end
24
24
 
25
+ # @param cop_name [String]
26
+ # @return [Boolean]
27
+ def metrics_cop?(cop_name)
28
+ cop_name.start_with?('Metrics') &&
29
+ !%w[Metrics/ParameterLists Metrics/BlockNesting].include?(cop_name)
30
+ end
31
+
25
32
  def to_supported_styles(enforced_style)
26
33
  RuboCop::Cop::Util.to_supported_styles(enforced_style)
27
34
  end
@@ -1,5 +1,5 @@
1
1
  module Gry
2
- # Run RuboCop with spwcify cops and config
2
+ # Run RuboCop with specific cops and config
3
3
  class RubocopRunner
4
4
  # @param cops [Array<String>] cop names. e.g.) ['Style/EmptyElse']
5
5
  # @param setting [Hash] e.g.) {'Style/EmptyElse' => {'EnforcedStyle' => 'both'}}
@@ -1,3 +1,3 @@
1
1
  module Gry
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masataka Kuwabara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-23 00:00:00.000000000 Z
11
+ date: 2017-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler