rubocop-oneoff_codemod 0.0.3 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81bf98f0675cd046786de15ab60f0c0e9d9c9171dadca788e863e366b58c4327
4
- data.tar.gz: b59a62f0da18ef1bd0d8caa57d9440fb0eadc7d44d5063ccea33ecfd9b31100e
3
+ metadata.gz: 8f98a73003521d6a8f5aa5811db5db534a45dcfdb5e2ff6bfbca3f28efcb59cd
4
+ data.tar.gz: 96038200d22d10f9aba5d7ada387359b6912244a18410458d9a71791325962b3
5
5
  SHA512:
6
- metadata.gz: b1dd632033a08bc9e9fef6b3f8d8b70c2df1f97ecae979b2f2518c5a7407426ab611869ff6532cd4fbf5a61a0ca0aca710f2f7ae48f4f7376fbc2361e410312e
7
- data.tar.gz: 2c4b31f59f1f2d92d5800bd6577f95dd9e1c0e657c60fd9df242c94128541140629009b01a37f7314e83c7fc8536cd04d646aa6e9ab76b355c209e47d5697b9d
6
+ metadata.gz: f33f033ec49e89d02c79e4b2b0a7216f487ced3d3471d050b3c26da3e657f5fae5bb6d819062d376c34b50b61de90c49d2c5cb3b711ea277ceb884a654162bbd
7
+ data.tar.gz: a985602a87cf54e8b50798b1179f311c9835a086b1f7fb58c86d78d2cd3d47f22491cfcdac78086131ddd2e61578577f9835718710edc336cd1f883c7217de20
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Rubocop::OneoffCodemod
1
+ # RuboCop::OneoffCodemod
2
2
 
3
3
  A RuboCop plugin implementing comment-as-command for one-off codemod, inspired by eslint-plugin-command.
4
4
 
@@ -32,4 +32,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
32
32
 
33
33
  ## Code of Conduct
34
34
 
35
- Everyone interacting in the Rubocop::OneoffCodemod project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rubocop-oneoff_codemod/blob/master/CODE_OF_CONDUCT.md).
35
+ Everyone interacting in the RuboCop::OneoffCodemod project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rubocop-oneoff_codemod/blob/master/CODE_OF_CONDUCT.md).
@@ -8,7 +8,8 @@ module RuboCop
8
8
  extend AutoCorrector
9
9
  include IgnoredNode
10
10
 
11
- MSG = "keep-unique"
11
+ MSG = "Keep array items unique, removing duplicates."
12
+ COMMAND = "keep-unique"
12
13
 
13
14
  def initialize(*args)
14
15
  super
@@ -17,9 +18,9 @@ module RuboCop
17
18
 
18
19
  def on_new_investigation
19
20
  processed_source.comments.each do |comment|
20
- next unless comment.text == "# @keep-unique"
21
+ next unless comment.text == "# @#{COMMAND}"
21
22
 
22
- @commands[comment.location.line] = :keep_unique
23
+ @commands[comment.location.line] = COMMAND
23
24
 
24
25
  add_offense(comment.location.expression) do |corrector|
25
26
  corrector.replace(comment.location.expression, "#")
@@ -27,8 +28,12 @@ module RuboCop
27
28
  end
28
29
  end
29
30
 
31
+ def command_exists?(node)
32
+ @commands[node.location.line - 1] == COMMAND
33
+ end
34
+
30
35
  def on_array(node)
31
- return unless @commands[node.location.line - 1] == :keep_unique
36
+ return unless command_exists? node
32
37
  return unless node.loc.begin.source == "[" || node.loc.begin.source.start_with?("%w")
33
38
 
34
39
  add_offense(node) do |corrector|
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbs"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Codemod
8
+ # KeepUnique
9
+ class RbsPrototype < Base
10
+ extend AutoCorrector
11
+ include IgnoredNode
12
+
13
+ MSG = "Run rbs prototype and generates boilerplate signature declaration."
14
+ COMMAND = "rbs-prototype"
15
+
16
+ def initialize(*args)
17
+ super
18
+ @commands = {}
19
+ end
20
+
21
+ def find_node_on_line(node, target_line)
22
+ if node.loc&.line == target_line && node.is_a?(RuboCop::AST::DefNode)
23
+ node
24
+ else
25
+ node.child_nodes.find do |child_node|
26
+ find_node_on_line child_node, target_line
27
+ end
28
+ end
29
+ end
30
+
31
+ def on_new_investigation
32
+ processed_source.comments.each do |comment|
33
+ next unless comment.text == "# @#{COMMAND}"
34
+
35
+ target_node = find_node_on_line(processed_source.ast, comment.location.line + 1)
36
+ target_method = target_node.method_name
37
+
38
+ add_offense(comment.location.expression) do |corrector|
39
+ parser = RBS::Prototype::RB.new
40
+ parser.parse processed_source.raw_source
41
+ string = []
42
+ parser.decls.each do |decl|
43
+ decl.members.each do |member|
44
+ string << member.overloads.map(&:method_type).join(" | ") if target_method == member.name
45
+ end
46
+ end
47
+
48
+ corrector.replace(comment.location.expression, "# #{string.join}")
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rubocop
3
+ module RuboCop
4
4
  module OneoffCodemod
5
- VERSION = "0.0.3"
5
+ VERSION = "0.0.5"
6
6
  end
7
7
  end
@@ -6,3 +6,4 @@ require_relative "oneoff_codemod/version"
6
6
  require_relative "oneoff_codemod/plugin"
7
7
 
8
8
  require_relative "oneoff_codemod/cops/keep_unique"
9
+ require_relative "oneoff_codemod/cops/rbs_prototype"
@@ -1,4 +1,4 @@
1
- module Rubocop
1
+ module RuboCop
2
2
  module OneoffCodemod
3
3
  VERSION: String
4
4
  # See the writing guide of rbs: https://github.com/ruby/rbs#guides
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-oneoff_codemod
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koji NAKAMURA
@@ -23,6 +23,20 @@ dependencies:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
25
  version: '1.1'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rbs
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
26
40
  - !ruby/object:Gem::Dependency
27
41
  name: rubocop
28
42
  requirement: !ruby/object:Gem::Requirement
@@ -61,6 +75,7 @@ files:
61
75
  - config/default.yml
62
76
  - lib/rubocop/oneoff_codemod.rb
63
77
  - lib/rubocop/oneoff_codemod/cops/keep_unique.rb
78
+ - lib/rubocop/oneoff_codemod/cops/rbs_prototype.rb
64
79
  - lib/rubocop/oneoff_codemod/plugin.rb
65
80
  - lib/rubocop/oneoff_codemod/version.rb
66
81
  - sig/rubocop/oneoff_codemod.rbs