debtective 0.2.1 → 0.2.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
  SHA256:
3
- metadata.gz: 3a9c75632f4f3a8785af1c96209c22b68772f8bde253cbd76e2bdb774a3812a8
4
- data.tar.gz: d4b50399a0806ad8e4a6fb20305429882847ef79dd42bd176d17be3f5f1df786
3
+ metadata.gz: fa54bf140a87eeb794cba1f8823d00a9ab5407b77f7d8b18c39414857e301edc
4
+ data.tar.gz: 2169b279c88a1527c3e1107921aaacd7b81905442361804ac4eaf3731856357e
5
5
  SHA512:
6
- metadata.gz: 59b68258f51e5f7a83992c8d24d0bc028fcb7dcdea2f379d0af011a8ec9c5dba645d6ffef4f94652d3ce7f0662185597934b96a6c8d2fa74ccc392b027b207fe
7
- data.tar.gz: a5556a654c194fc442727c67f6c06686c6df851fe4008540670603d5aac8203e67e96a62d498b0eb57b91bdae76a2c967d789b29709659b84d3178632e7b893f
6
+ metadata.gz: e3c5d0c77005f8b6d410339b9e649ba774d4a677ccc60cb7ee96c4761b24bc740880d648871cc65309b542935008a13df8fcd30cd89b7c968243231046fdd728
7
+ data.tar.gz: 8a9e4f628f36eb6d21eef51464d068e24fd696c8e8335127a2d95cc683bbca0c59bc91272e0450c8daac662f8f7f5014ff060bc692e7699925778c2f64725d9c
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "parser/current"
3
+ require "debtective/stderr_helper"
4
4
 
5
5
  module Debtective
6
6
  # Find the index of the line ending a statement
@@ -18,6 +18,8 @@ module Debtective
18
18
  # => 3
19
19
  #
20
20
  class EndOfStatement
21
+ include StderrHelper
22
+
21
23
  # @param lines [Array<String>] lines of code
22
24
  # @param index [Integer] index of the statement first line
23
25
  def initialize(lines, first_line_index)
@@ -27,24 +29,24 @@ module Debtective
27
29
 
28
30
  # index of the line ending the statement
29
31
  # @return [Integer]
32
+ # @note use suppress_stderr to prevent error outputs
33
+ # from RubyVM::InstructionSequence.compile
30
34
  def call
31
- suppress_stderr do
32
- last_line_index || @first_line_index
33
- end
35
+ suppress_stderr { last_line_index }
34
36
  end
35
37
 
36
38
  private
37
39
 
38
- # index of the line ending the statement
40
+ # recursively find index of the line ending the statement
41
+ # @param index [Integer] used for recursion
39
42
  # @return[Integer, void]
40
43
  # @note it is possible that no line ends the statement
41
44
  # especially if first line is not the start of a statement
42
- def last_line_index
43
- @lines.index.with_index do |_line, index|
44
- index >= @first_line_index &&
45
- statement?(index) &&
46
- !chained?(index)
47
- end
45
+ def last_line_index(index = @first_line_index)
46
+ return @first_line_index if index >= @lines.size
47
+ return index if !chained?(index) && statement?(index)
48
+
49
+ last_line_index(index + 1)
48
50
  end
49
51
 
50
52
  # check if the code from first index to given index is a statement
@@ -53,9 +55,8 @@ module Debtective
53
55
  # @return boolean
54
56
  def statement?(index)
55
57
  code = @lines[@first_line_index..index].join("\n")
56
- ::Parser::CurrentRuby.parse(code)
57
- true
58
- rescue Parser::SyntaxError
58
+ RubyVM::InstructionSequence.compile(code)
59
+ rescue SyntaxError
59
60
  false
60
61
  end
61
62
 
@@ -66,16 +67,5 @@ module Debtective
66
67
  def chained?(index)
67
68
  @lines[index + 1]&.match?(/^(\s*)\./)
68
69
  end
69
-
70
- # silence the $stderr
71
- # to avoid logs from the parser
72
- # @return void
73
- def suppress_stderr
74
- original_stderr = $stderr.clone
75
- $stderr.reopen(File.new("/dev/null", "w"))
76
- yield
77
- ensure
78
- $stderr.reopen(original_stderr)
79
- end
80
70
  end
81
71
  end
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "debtective/todo"
3
4
  require "debtective/end_of_statement"
4
5
 
5
6
  module Debtective
6
7
  # Find the todos comments and their boundaries
7
8
  class FileTodos
8
- Result = Struct.new(:pathname, :todo_index, :boundaries)
9
-
10
9
  BEFORE_LINE_TODO_REGEX = /^\s*#\sTODO:\s/
11
10
  INLINE_TODO_REGEX = /\s*#\sTODO:\s/
12
11
  COMMENT_REGEX = /\s*#/
@@ -22,7 +21,7 @@ module Debtective
22
21
  boundaries = boundaries(line, index)
23
22
  next if boundaries.nil?
24
23
 
25
- Result.new(@pathname, index, boundaries)
24
+ Todo.new(@pathname, index, boundaries)
26
25
  end
27
26
  end
28
27
 
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Debtective
4
+ # Silence the $stderr
5
+ module StderrHelper
6
+ # @return void
7
+ def suppress_stderr
8
+ original_stderr = $stderr.clone
9
+ $stderr.reopen(File.new("/dev/null", "w"))
10
+ yield
11
+ ensure
12
+ $stderr.reopen(original_stderr)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "debtective/end_of_statement"
4
+
5
+ module Debtective
6
+ # Hold todo information
7
+ class Todo
8
+ attr_accessor :pathname, :todo_index, :boundaries
9
+
10
+ # @param pathname [Pathname]
11
+ # @param todo_index [Integer] first line of todo comment
12
+ # @param boundaries [Range] first and last index of the concerned code
13
+ def initialize(pathname, todo_index, boundaries)
14
+ @pathname = pathname
15
+ @todo_index = todo_index
16
+ @boundaries = boundaries
17
+ end
18
+
19
+ # location in the codebase
20
+ # @return [String]
21
+ def location
22
+ "#{pathname}:#{todo_index + 1}"
23
+ end
24
+
25
+ # size of the todo code
26
+ # @return [Integer]
27
+ def size
28
+ boundaries.size
29
+ end
30
+
31
+ # line numbers (like displayed in an IDE)
32
+ # @return [Range]
33
+ def line_numbers
34
+ (boundaries.min + 1)..(boundaries.max + 1)
35
+ end
36
+ end
37
+ end
@@ -7,17 +7,10 @@ module Debtective
7
7
  class TodoList
8
8
  DEFAULT_PATHS = ["./**/*"].freeze
9
9
 
10
- REPORTS = %i[
11
- combined_count
12
- extended_count
13
- todos
14
- ].freeze
15
-
16
- Result = Struct.new(*REPORTS)
17
-
18
- # @return [TodoList::Result]
10
+ # @return [Array<FileTodos::Result>]
19
11
  def call
20
- Result.new(*REPORTS.map { __send__(_1) })
12
+ ruby_pathnames
13
+ .flat_map { FileTodos.new(_1).call }
21
14
  end
22
15
 
23
16
  private
@@ -35,14 +28,6 @@ module Debtective
35
28
  .select { _1.file? && _1.extname == ".rb" }
36
29
  end
37
30
 
38
- # @return [Array<FileTodos::Result>]
39
- def todos
40
- @todos ||=
41
- ruby_pathnames
42
- .flat_map { FileTodos.new(_1).call }
43
- .select(&:any?)
44
- end
45
-
46
31
  # @return [Integer]
47
32
  def extended_count
48
33
  todos
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Debtective
4
+ # Apply calculations on a todo list
5
+ class TodoListCounts
6
+ # @param todo_list [Array<Debtective::Todo>]
7
+ def initialize(todos)
8
+ @todos = todos
9
+ end
10
+
11
+ # @return [Integer]
12
+ def extended_count
13
+ @todos.sum(&:size)
14
+ end
15
+
16
+ # @return [Integer]
17
+ def combined_count
18
+ @todos
19
+ .group_by(&:pathname)
20
+ .values
21
+ .sum do |pathname_todos|
22
+ pathname_todos
23
+ .map { _1.boundaries.to_a }
24
+ .reduce(:|)
25
+ .length
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Debtective
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "debtective/todo_list"
4
+ require "debtective/todo_list_counts"
4
5
 
5
6
  begin
6
7
  require "#{Rails.root}/config/initializers/debtective" if defined?(Rails)
@@ -11,51 +12,39 @@ end
11
12
  namespace :debtective do
12
13
  desc "Todo List"
13
14
  task :todo_list do
14
- todo_list = Debtective::TodoList.new.call
15
- todos = todo_list.todos.sort_by { _1.boundaries.size }.reverse
15
+ array_row =
16
+ lambda do |value1, value2, value3|
17
+ [
18
+ value1.to_s.ljust(120),
19
+ value2.to_s.rjust(12),
20
+ value3.to_s.rjust(12)
21
+ ].join(" | ").prepend("| ").concat(" |")
22
+ end
16
23
 
17
- separator = Array.new(120 + 3 + 12 + 3 + 12) { "-" }.join
24
+ separator = Array.new(array_row.call(nil, nil, nil).size) { "-" }.join
18
25
 
19
- puts separator
26
+ todo_list = Debtective::TodoList.new.call
27
+ todos = todo_list.sort_by { _1.boundaries.size }.reverse
28
+ todo_list_counts = Debtective::TodoListCounts.new(todos)
20
29
 
30
+ puts separator
21
31
  puts "todo list"
22
-
23
32
  puts separator
24
-
25
- puts(
26
- [
27
- "pathname".ljust(120),
28
- "lines".rjust(12)
29
- ].join(" | ")
30
- )
31
-
33
+ puts array_row.call("pathname", "line numbers", "lines count")
32
34
  puts separator
33
-
34
- todos.each do |todo|
35
- location = "#{todo.pathname}:#{todo.todo_index + 1}".ljust(120)
36
- puts(
37
- [
38
- location,
39
- [todo.boundaries.first + 1, todo.boundaries.last + 1].join(":").rjust(12),
40
- todo.boundaries.size.to_s.rjust(12)
41
- ].join(" | ")
42
- )
43
- end
35
+ todos.each { puts array_row.call(_1.location, _1.line_numbers, _1.size) }
44
36
 
45
37
  puts separator
46
-
47
38
  puts "todos count"
48
39
  puts todos.count
49
40
 
50
41
  puts separator
51
-
52
42
  puts "combined lines count"
53
- puts todo_list.combined_count
43
+ puts todo_list_counts.combined_count
54
44
 
55
45
  puts separator
56
-
57
46
  puts "extended lines count"
58
- puts todo_list.extended_count
47
+ puts todo_list_counts.extended_count
59
48
 
60
49
  puts separator
61
50
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debtective
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edouard Piron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-09 00:00:00.000000000 Z
11
+ date: 2023-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: parser
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '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'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -123,7 +109,10 @@ files:
123
109
  - lib/debtective/end_of_statement.rb
124
110
  - lib/debtective/file_todos.rb
125
111
  - lib/debtective/railtie.rb
112
+ - lib/debtective/stderr_helper.rb
113
+ - lib/debtective/todo.rb
126
114
  - lib/debtective/todo_list.rb
115
+ - lib/debtective/todo_list_counts.rb
127
116
  - lib/debtective/version.rb
128
117
  - lib/tasks/debtective/todo_list.rake
129
118
  homepage: https://github.com/BigBigDoudou/debtective