abc_size_visualizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f52f861e9648b636dfcc86181cc365b79ba093a4a09c4c45353ccd1e4dacb7f2
4
+ data.tar.gz: 95876ace082fd00b1b82db979a9472e0561e560dcca9191e76b581bb924da6e0
5
+ SHA512:
6
+ metadata.gz: 57e4bd8f098bf144f50f8e89f11ca10c95c9362b5bdfa94b0475d993e092bfaba206f06946fccf0f26903761155bb7a261c9f3b59893cf7b6c284f46021224f3
7
+ data.tar.gz: 4dc54f448f0d9ed9f78ef7453917ed3fcb5e676e5f16a44b9d0a9df7708beed623720447bb1ae4ebf29b5c091c3011e148558bdc3811d01ac0a630daa279e464
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 3.1
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in abc_size_visualizer.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "standard", "~> 1.3"
13
+
14
+ gem "debug", "~> 1.6"
data/Gemfile.lock ADDED
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ abc_size_visualizer (0.1.0)
5
+ colorize (~> 0.8.0)
6
+ rouge (~> 4.0.0)
7
+ rubocop (~> 1.39.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.2)
13
+ colorize (0.8.1)
14
+ debug (1.6.3)
15
+ irb (>= 1.3.6)
16
+ reline (>= 0.3.1)
17
+ diff-lcs (1.5.0)
18
+ io-console (0.5.11)
19
+ irb (1.5.0)
20
+ reline (>= 0.3.0)
21
+ json (2.6.2)
22
+ language_server-protocol (3.17.0.1)
23
+ parallel (1.22.1)
24
+ parser (3.1.3.0)
25
+ ast (~> 2.4.1)
26
+ rainbow (3.1.1)
27
+ rake (13.0.6)
28
+ regexp_parser (2.6.1)
29
+ reline (0.3.1)
30
+ io-console (~> 0.5)
31
+ rexml (3.2.5)
32
+ rouge (4.0.0)
33
+ rspec (3.12.0)
34
+ rspec-core (~> 3.12.0)
35
+ rspec-expectations (~> 3.12.0)
36
+ rspec-mocks (~> 3.12.0)
37
+ rspec-core (3.12.0)
38
+ rspec-support (~> 3.12.0)
39
+ rspec-expectations (3.12.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.12.0)
42
+ rspec-mocks (3.12.0)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.12.0)
45
+ rspec-support (3.12.0)
46
+ rubocop (1.39.0)
47
+ json (~> 2.3)
48
+ parallel (~> 1.10)
49
+ parser (>= 3.1.2.1)
50
+ rainbow (>= 2.2.2, < 4.0)
51
+ regexp_parser (>= 1.8, < 3.0)
52
+ rexml (>= 3.2.5, < 4.0)
53
+ rubocop-ast (>= 1.23.0, < 2.0)
54
+ ruby-progressbar (~> 1.7)
55
+ unicode-display_width (>= 1.4.0, < 3.0)
56
+ rubocop-ast (1.24.0)
57
+ parser (>= 3.1.1.0)
58
+ rubocop-performance (1.15.1)
59
+ rubocop (>= 1.7.0, < 2.0)
60
+ rubocop-ast (>= 0.4.0)
61
+ ruby-progressbar (1.11.0)
62
+ standard (1.19.0)
63
+ language_server-protocol (= 3.17.0.1)
64
+ rubocop (= 1.39.0)
65
+ rubocop-performance (= 1.15.1)
66
+ unicode-display_width (2.3.0)
67
+
68
+ PLATFORMS
69
+ arm64-darwin-21
70
+
71
+ DEPENDENCIES
72
+ abc_size_visualizer!
73
+ debug (~> 1.6)
74
+ rake (~> 13.0)
75
+ rspec (~> 3.0)
76
+ standard (~> 1.3)
77
+
78
+ BUNDLED WITH
79
+ 2.3.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Yusuke Sangenya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # AbcSizeVisualizer
2
+
3
+ ```bash
4
+ $ gem install abc_size_visualizer
5
+ $ abc_size_visualizer some_code.rb
6
+ # prints size of 1)assignment 2)branch 3)condition with code.
7
+ # red means assignment, yellow means branch, blue means condition
8
+ ```
9
+
10
+ ![screenshot](./docs/screenshot.png)
11
+
12
+ ## Contributing
13
+
14
+ Bug reports and pull requests are welcome on GitHub at https://github.com/genya0407/abc_size_visualizer.
15
+
16
+ ## License
17
+
18
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift("#{__dir__}/lib")
5
+
6
+ require "abc_size_visualizer"
7
+
8
+ module AbcSizeVisualizer
9
+ class VisualizeAbcSize
10
+ COLOR_BY_TYPE = {
11
+ assignment: :red,
12
+ branch: :yellow,
13
+ condition: :blue
14
+ }
15
+ MAX_BAR_SIZE = 20
16
+
17
+ def initialize(source_file_name:)
18
+ @abc_size_result = []
19
+ @source = File.read(source_file_name)
20
+ buffer = Parser::Source::Buffer.new(source_file_name, source: @source)
21
+ parser = Parser::CurrentRuby.new(RuboCop::AST::Builder.new)
22
+ @node = parser.parse(buffer)
23
+ @highlighted_source_by_line_num = begin
24
+ formatter = Rouge::Formatters::TerminalTruecolor.new
25
+ lexer = Rouge::Lexers::Ruby.new
26
+ formatter.format(lexer.lex(@source)).lines(chomp: true).zip(1..).map do |line, line_num|
27
+ [line_num, line]
28
+ end.to_h
29
+ end
30
+ end
31
+
32
+ def execute
33
+ calculators = analyze(@node)
34
+
35
+ @source.lines(chomp: true).zip(1..).each do |line, line_num|
36
+ calculator = calculators.find { |calculator| calculator.abc_by_line_num[line_num] }
37
+ bar = generate_bar(calculator:, line_num:).ljust(MAX_BAR_SIZE + 3)
38
+ highlighted_line = @highlighted_source_by_line_num[line_num]
39
+ annotation = generate_annotation(calculator:, line_num:)
40
+ puts [bar, highlighted_line, annotation].join("")
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def analyze(node)
47
+ case node.type
48
+ when :def, :defs
49
+ calculator = ::AbcSizeVisualizer::AbcSizeCalculator.new(node)
50
+ calculator.calculate
51
+ [calculator]
52
+ when :begin, :module, :class
53
+ node.each_child_node.flat_map do |child_node|
54
+ analyze(child_node)
55
+ end
56
+ else
57
+ []
58
+ end
59
+ end
60
+
61
+ def generate_bar(calculator:, line_num:)
62
+ if calculator
63
+ abc_size_by_type = calculator.abc_by_line_num[line_num]
64
+ max_abc = calculator.abc_by_line_num.values.map { |abc| abc.values.sum }.max
65
+ types = %i[assignment branch condition]
66
+ sizes = types.map do |type|
67
+ (MAX_BAR_SIZE * (abc_size_by_type[type].to_f / max_abc)).ceil
68
+ end
69
+ bar = sizes.zip(types).map do |size, type|
70
+ ColorizedString[" " * size].colorize(background: COLOR_BY_TYPE[type])
71
+ end.join("")
72
+ ljust_count = [(MAX_BAR_SIZE + 3) - sizes.sum, 0].max
73
+ bar + (" " * ljust_count)
74
+ else
75
+ " " * (MAX_BAR_SIZE + 3)
76
+ end
77
+ end
78
+
79
+ def generate_annotation(calculator:, line_num:)
80
+ return "" unless calculator
81
+
82
+ abc_size_by_type = calculator.abc_by_line_num[line_num]
83
+ return "" if abc_size_by_type.values.sum <= 0
84
+
85
+ " # <" + abc_size_by_type.values_at(:assignment, :branch, :condition).join(", ") + ">"
86
+ end
87
+ end
88
+ end
89
+
90
+ AbcSizeVisualizer::VisualizeAbcSize.new(source_file_name: ARGV[0]).execute
@@ -0,0 +1,151 @@
1
+ # Based on https://github.com/rubocop/rubocop/blob/master/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb
2
+
3
+ module AbcSizeVisualizer
4
+ # > ABC is .. a software size metric .. computed by counting the number
5
+ # > of assignments, branches and conditions for a section of code.
6
+ # > http://c2.com/cgi/wiki?AbcMetric
7
+ #
8
+ # We separate the *calculator* from the *cop* so that the calculation,
9
+ # the formula itself, is easier to test.
10
+ class AbcSizeCalculator
11
+ include RuboCop::Cop::Metrics::Utils::IteratingBlock
12
+ include RuboCop::Cop::Metrics::Utils::RepeatedCsendDiscount
13
+ prepend RuboCop::Cop::Metrics::Utils::RepeatedAttributeDiscount
14
+
15
+ # > Branch -- an explicit forward program branch out of scope -- a
16
+ # > function call, class method call ..
17
+ # > http://c2.com/cgi/wiki?AbcMetric
18
+ BRANCH_NODES = %i[send csend yield].freeze
19
+
20
+ # > Condition -- a logical/Boolean test, == != <= >= < > else case
21
+ # > default try catch ? and unary conditionals.
22
+ # > http://c2.com/cgi/wiki?AbcMetric
23
+ CONDITION_NODES = RuboCop::Cop::Metrics::CyclomaticComplexity::COUNTED_NODES.freeze
24
+
25
+ def self.calculate(node, discount_repeated_attributes: false)
26
+ new(node, discount_repeated_attributes:).calculate
27
+ end
28
+
29
+ # TODO: move to rubocop-ast
30
+ ARGUMENT_TYPES = %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg].freeze
31
+
32
+ private_constant :BRANCH_NODES, :CONDITION_NODES, :ARGUMENT_TYPES
33
+
34
+ attr_reader :assignment, :branch, :condition
35
+
36
+ def initialize(node)
37
+ @assignment = IntegerOnLocation.new
38
+ @branch = IntegerOnLocation.new
39
+ @condition = IntegerOnLocation.new
40
+ @node = node
41
+ reset_repeated_csend
42
+ end
43
+
44
+ def abc_by_line_num
45
+ @abc_by_line_num ||= [@assignment, @branch, @condition].map(&:line_nums).flatten.map do |line_num|
46
+ [
47
+ line_num,
48
+ {assignment: @assignment, branch: @branch, condition: @condition}.transform_values do
49
+ _1.value_at(line_num:) || 0
50
+ end
51
+ ]
52
+ end.to_h
53
+ end
54
+
55
+ def calculate
56
+ visit_depth_last(@node) { |child| calculate_node(child) }
57
+
58
+ [
59
+ Math.sqrt((@assignment.to_i**2) + (@branch.to_i**2) + (@condition.to_i**2)).round(2),
60
+ "<#{@assignment.to_i}, #{@branch.to_i}, #{@condition.to_i}>"
61
+ ]
62
+ end
63
+
64
+ def evaluate_branch_nodes(node)
65
+ if node.comparison_method?
66
+ @condition.add(node, 1)
67
+ else
68
+ @branch.add(node, 1)
69
+ @condition.add(node, 1) if node.csend_type? && !discount_for_repeated_csend?(node)
70
+ end
71
+ end
72
+
73
+ def evaluate_condition_node(node)
74
+ @condition.add(node, 1) if else_branch?(node)
75
+ @condition.add(node, 1)
76
+ end
77
+
78
+ def else_branch?(node)
79
+ %i[case if].include?(node.type) && node.else? && node.loc.else.is?("else")
80
+ end
81
+
82
+ private
83
+
84
+ def visit_depth_last(node, &block)
85
+ node.each_child_node { |child| visit_depth_last(child, &block) }
86
+ yield node
87
+ end
88
+
89
+ def calculate_node(node)
90
+ @assignment.add(node, 1) if assignment?(node)
91
+
92
+ if branch?(node)
93
+ evaluate_branch_nodes(node)
94
+ elsif condition?(node)
95
+ evaluate_condition_node(node)
96
+ end
97
+ end
98
+
99
+ def assignment?(node)
100
+ return compound_assignment(node) if node.masgn_type? || node.shorthand_asgn?
101
+
102
+ node.for_type? ||
103
+ (node.respond_to?(:setter_method?) && node.setter_method?) ||
104
+ simple_assignment?(node) ||
105
+ argument?(node)
106
+ end
107
+
108
+ def compound_assignment(node)
109
+ # Methods setter cannot be detected for multiple assignments
110
+ # and shorthand assigns, so we'll count them here instead
111
+ children = node.masgn_type? ? node.children[0].children : node.children
112
+
113
+ will_be_miscounted = children.count do |child|
114
+ child.respond_to?(:setter_method?) && !child.setter_method?
115
+ end
116
+ @assignment.add(node, will_be_miscounted)
117
+
118
+ false
119
+ end
120
+
121
+ def simple_assignment?(node)
122
+ if !node.equals_asgn?
123
+ false
124
+ elsif node.lvasgn_type?
125
+ reset_on_lvasgn(node)
126
+ capturing_variable?(node.children.first)
127
+ else
128
+ true
129
+ end
130
+ end
131
+
132
+ def capturing_variable?(name)
133
+ # TODO: Remove `Symbol#to_s` after supporting only Ruby >= 2.7.
134
+ name && !name.to_s.start_with?("_")
135
+ end
136
+
137
+ def branch?(node)
138
+ BRANCH_NODES.include?(node.type)
139
+ end
140
+
141
+ def argument?(node)
142
+ ARGUMENT_TYPES.include?(node.type) && capturing_variable?(node.children.first)
143
+ end
144
+
145
+ def condition?(node)
146
+ return false if iterating_block?(node) == false
147
+
148
+ CONDITION_NODES.include?(node.type)
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,27 @@
1
+ module AbcSizeVisualizer
2
+ class IntegerOnLocation
3
+ attr_reader :value_by_line_num
4
+
5
+ def initialize
6
+ @value_by_line_num = {}
7
+ end
8
+
9
+ def add(node, value)
10
+ line_num = node.loc.expression.line
11
+ @value_by_line_num[line_num] ||= 0
12
+ @value_by_line_num[line_num] += value
13
+ end
14
+
15
+ def value_at(line_num:)
16
+ @value_by_line_num[line_num]
17
+ end
18
+
19
+ def to_i
20
+ @value_by_line_num.values.sum || 0
21
+ end
22
+
23
+ def line_nums
24
+ @value_by_line_num.keys
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AbcSizeVisualizer
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+ require "parser/current"
5
+ require "colorized_string"
6
+ require "rouge"
7
+
8
+ require_relative "abc_size_visualizer/abc_size_calculator"
9
+ require_relative "abc_size_visualizer/integer_on_location"
10
+ require_relative "abc_size_visualizer/version"
11
+
12
+ module AbcSizeVisualizer
13
+ class Error < StandardError; end
14
+ # Your code goes here...
15
+ end
@@ -0,0 +1,4 @@
1
+ module AbcSizeVisualizer
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abc_size_visualizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke Sangenya
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.39.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.39.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rouge
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.0.0
55
+ description: Visualize ABC size
56
+ email:
57
+ - longinus.eva@gmail.com
58
+ executables:
59
+ - visualize_abc_size
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".rspec"
64
+ - ".standard.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - exe/visualize_abc_size
71
+ - lib/abc_size_visualizer.rb
72
+ - lib/abc_size_visualizer/abc_size_calculator.rb
73
+ - lib/abc_size_visualizer/integer_on_location.rb
74
+ - lib/abc_size_visualizer/version.rb
75
+ - sig/abc_size_visualizer.rbs
76
+ homepage: https://github.com/genya0407/abc_size_visualizer
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/genya0407/abc_size_visualizer
81
+ source_code_uri: https://github.com/genya0407/abc_size_visualizer
82
+ changelog_uri: https://github.com/genya0407/abc_size_visualizer
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 3.1.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.3.5
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Visualize ABC size.
102
+ test_files: []