rubocop-twirp 0.0.1 → 0.1.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
  SHA256:
3
- metadata.gz: 5fd2c1e5539fcf54151b1a5a5fd90f83f7b841ada41ed672bc791212bc160c40
4
- data.tar.gz: 8d61149045abd2246ddf00e30dabd67620b69b49e3212e37d132af265f24c7b7
3
+ metadata.gz: 34f94e78595b7dc92e5de467fa5529b39bc3427ddf7e9e8120c794ec2fa44a0e
4
+ data.tar.gz: 2dd8f977b38beebbee179240e1ab08396258769418c2ecc079b1deeaed097d58
5
5
  SHA512:
6
- metadata.gz: ce634e64bdc75f3702aff3d39cbd60373d17c90589aa3eb241116b52eed3bbf579e7ca554d375afbb305787681a9996d93f509fdeb0ce398372315b1dd182ffb
7
- data.tar.gz: d51f7c50615aa2fa207c117cfeaf7d7234dd59ff3f6c946964e1c028ebd61bafcdbf9a17d67860ad9efa5fde8209fe06df210e1526552044e42470d30f3a50ea
6
+ metadata.gz: 823f2a6a00ad57d192e85cbd32a08342c7cc50fc09fa577a46dff75604f1ffc8fabc1f0acbdceec3ec813d0c3a838cfe0acd6448dc13c00308bca994aa00a85e
7
+ data.tar.gz: 5e70efaf01bc212da38adc36ab77f767b3a652bfa0748dc21afb6c01d3ff369f73a613eab89a0f45a51ebbc0f7d89e50456cec62c64667ca559af426c3bdeeb9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### v0.1.0 (2023-02-26)
2
+ - client response cop
3
+ - be_a cop
4
+
1
5
  ### v0.0.1 (2023-02-25)
2
6
  -
3
7
 
data/Gemfile.lock CHANGED
@@ -1,9 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-twirp (0.0.1)
4
+ rubocop-twirp (0.1.0)
5
5
  rubocop (>= 1)
6
- twirp (>= 1)
7
6
 
8
7
  GEM
9
8
  remote: https://rubygems.org/
@@ -12,11 +11,6 @@ GEM
12
11
  byebug (11.1.3)
13
12
  diff-lcs (1.5.0)
14
13
  docile (1.4.0)
15
- faraday (2.7.4)
16
- faraday-net_http (>= 2.0, < 3.1)
17
- ruby2_keywords (>= 0.0.4)
18
- faraday-net_http (3.0.2)
19
- google-protobuf (3.22.0-x86_64-darwin)
20
14
  json (2.6.3)
21
15
  parallel (1.22.1)
22
16
  parser (3.2.1.0)
@@ -51,20 +45,16 @@ GEM
51
45
  rubocop-ast (1.26.0)
52
46
  parser (>= 3.2.1.0)
53
47
  ruby-progressbar (1.11.0)
54
- ruby2_keywords (0.0.5)
55
48
  simplecov (0.22.0)
56
49
  docile (~> 1.1)
57
50
  simplecov-html (~> 0.11)
58
51
  simplecov_json_formatter (~> 0.1)
59
52
  simplecov-html (0.12.3)
60
53
  simplecov_json_formatter (0.1.4)
61
- twirp (1.10.0)
62
- faraday (< 3)
63
- google-protobuf (~> 3.0, >= 3.7.0)
64
54
  unicode-display_width (2.4.2)
65
55
 
66
56
  PLATFORMS
67
- x86_64-darwin-21
57
+ ruby
68
58
 
69
59
  DEPENDENCIES
70
60
  byebug
data/README.md CHANGED
@@ -3,29 +3,37 @@ rubocop-twirp
3
3
  ![Gem](https://img.shields.io/gem/dt/rubocop-twirp?style=plastic)
4
4
  [![codecov](https://codecov.io/gh/dpep/rubocop-twirp/branch/main/graph/badge.svg)](https://codecov.io/gh/dpep/rubocop-twirp)
5
5
 
6
- ...
6
+ Improve your Twirp code with [RuboCop](https://github.com/bbatsov/rubocop)
7
7
 
8
- [RuboCop](https://github.com/bbatsov/rubocop)
9
8
 
10
-
11
-
12
- ```ruby
9
+ ## Install
10
+ ```
13
11
  gem install rubocop-twirp
12
+ ```
14
13
 
15
- # or add to your Gemfile
14
+ Add to your Gemfile
15
+ ```ruby
16
16
  gem "rubocop-twirp", require: false
17
17
  ```
18
18
 
19
- Add this to your .rubocop.yml:
19
+ Add to .rubocop.yml
20
20
  ```
21
21
  require: rubocop-twirp
22
22
  ```
23
23
 
24
- Scan for issues
24
+ ## Use
25
+ Scan and fix issues
25
26
  ```
26
- rubocop -r rubocop-twirp --only Twirp
27
+ rubocop -r rubocop-twirp --only Twirp -A
27
28
  ```
28
29
 
30
+
31
+ Fix `Twirp::ClientResp` breaking change introduced in [Twirp v1.10](https://github.com/github/twirp-ruby/commit/4614a5fe004ca408c48ddfc813c80f3ad7bcb586). (Cop disabled by default)
32
+ ```
33
+ rubocop -r rubocop-twirp --only Twirp/DeprecatedArguments -A
34
+ ```
35
+
36
+
29
37
  ----
30
38
  ## Contributing
31
39
 
@@ -37,3 +45,9 @@ Yes please :)
37
45
  1. Commit your changes (`git commit -am 'awesome new feature'`)
38
46
  1. Push your branch (`git push origin my-feature`)
39
47
  1. Create a Pull Request
48
+
49
+
50
+ ### Resources
51
+ - https://docs.rubocop.org/rubocop/1.46/development.html
52
+ - https://github.com/rubocop/rubocop-ast/blob/master/docs/modules/ROOT/pages/node_pattern.adoc
53
+ - https://github.com/rubocop/rubocop-performance
data/config/default.yml CHANGED
@@ -1,7 +1,18 @@
1
1
  Twirp:
2
2
  Enabled: true
3
3
 
4
+ Twirp/ClientResponse:
5
+ Description: 'Use webmock-twirp to stub client responses'
6
+ Enabled: true
7
+ VersionAdded: '0.1'
8
+
4
9
  Twirp/DeprecatedArguments:
5
10
  Description: 'Use keyword args for `Twirp::ClientResp.new`'
11
+ Enabled: false
12
+ VersionAdded: '0.1'
13
+ Severity: error
14
+
15
+ Twirp/RSpecBeA:
16
+ Description: 'Use rspec-twirp to simplify specs'
6
17
  Enabled: true
7
18
  VersionAdded: '0.1'
@@ -0,0 +1,30 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Twirp
4
+ # Use webmock-twirp to stub Twirp client responses
5
+ # https://github.com/dpep/webmock-twirp
6
+ #
7
+ # @example
8
+ # # bad
9
+ # allow(MyTwirpClient).to receive(:my_rpc).and_return(Twirp::ClientResp.new(...))
10
+ #
11
+ # # good
12
+ # stub_twirp_request(MyTwirpClient, :my_rpc).to_return(...)
13
+ #
14
+ class ClientResponse < Base
15
+ RESTRICT_ON_SEND = [:new].freeze
16
+ MSG = "Avoid constructing Twirp client responses manually. Use `stub_twirp_request` from `webmock-twirp` (https://github.com/dpep/webmock-twirp)".freeze
17
+
18
+ def_node_matcher :client_response?, <<~PATTERN
19
+ (send (const (const _ :Twirp) :ClientResp) :new ...)
20
+ PATTERN
21
+
22
+ def on_send(node)
23
+ client_response?(node) do
24
+ add_offense(node.loc.expression, message: MSG)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -16,7 +16,7 @@ module RuboCop
16
16
  POSITIONAL_ARGS = [:data, :error].freeze
17
17
 
18
18
  def_node_matcher :pos_args?, <<~PATTERN
19
- (send (const (const { nil? cbase } :Twirp) :ClientResp) :new $!hash_type?+)
19
+ (send (const (const _ :Twirp) :ClientResp) :new $!hash_type?+)
20
20
  PATTERN
21
21
 
22
22
 
@@ -27,9 +27,6 @@ module RuboCop
27
27
  "`%<class_name>s.%<method_name>s(%<kwargs>s)`"
28
28
 
29
29
  def on_send(node)
30
- # kwargs were introduced in Twirp v1.10
31
- return unless Gem::Version.new(::Twirp::VERSION) >= Gem::Version.new("1.10")
32
-
33
30
  return unless pos_args?(node)
34
31
 
35
32
  kwargs = POSITIONAL_ARGS.map.with_index do |arg, index|
@@ -0,0 +1,54 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Twirp
4
+ # Use rspec-twirp to simplify specs
5
+ # https://github.com/dpep/rspec-twirp
6
+ #
7
+ # @example
8
+ # # bad
9
+ # expect(...).to be_a(Twirp::ClientResp)
10
+ # expect(...).to be_a(Twirp::Error)
11
+ #
12
+ # # good
13
+ # expect(...).to be_a_twirp_response
14
+ # expect(...).to be_a_twirp_error
15
+ #
16
+ class RSpecBeA < Base
17
+ RESTRICT_ON_SEND = [:be_a].freeze
18
+ TWIRP_RESPONSE_MSG = "Use `be_a_twirp_response` from `rspec-twirp` (https://github.com/dpep/rspec-twirp)".freeze
19
+ TWIRP_ERROR_MSG = "Use `be_a_twirp_error` from `rspec-twirp` (https://github.com/dpep/rspec-twirp)".freeze
20
+
21
+ def_node_matcher :twirp_type?, <<~PATTERN
22
+ (send _ :be_a (const (const _ :Twirp) ${:ClientResp :Error}))
23
+ PATTERN
24
+
25
+ include RangeHelp
26
+ extend AutoCorrector
27
+
28
+ def on_send(node)
29
+ twirp_type?(node) do |type|
30
+ case type
31
+ when :ClientResp
32
+ msg = TWIRP_RESPONSE_MSG
33
+ replacement = "be_a_twirp_response"
34
+ when :Error
35
+ msg = TWIRP_ERROR_MSG
36
+ replacement = "be_a_twirp_error"
37
+ else
38
+ return
39
+ end
40
+
41
+ range = range_between(
42
+ node.loc.selector.begin_pos,
43
+ node.loc.end.end_pos,
44
+ )
45
+
46
+ add_offense(range, message: msg) do |corrector|
47
+ corrector.replace(range, replacement)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module Twirp
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
data/lib/rubocop/twirp.rb CHANGED
@@ -1,6 +1,29 @@
1
1
  require "rubocop"
2
2
  require "rubocop/twirp/version"
3
- require "twirp"
3
+
4
+ module RuboCop
5
+ module Twirp
6
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
7
+ CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
8
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
9
+
10
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
11
+
12
+ # RuboCop doesn't support plugins yet, so monkey patch config
13
+ module Inject
14
+ def self.defaults!
15
+ path = CONFIG_DEFAULT.to_s
16
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
17
+ config = Config.new(hash, path).tap(&:make_excludes_absolute)
18
+ puts "configuration from #{path}" if ConfigLoader.debug?
19
+ config = ConfigLoader.merge_with_default(config, path)
20
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ RuboCop::Twirp::Inject.defaults!
4
27
 
5
28
  # load cops
6
29
  Dir["./cop/twirp/**/*.rb", base: __dir__].each { |f| require_relative f }
@@ -13,7 +13,6 @@ Gem::Specification.new do |s|
13
13
  s.version = package.const_get "VERSION"
14
14
 
15
15
  s.add_dependency "rubocop", ">= 1"
16
- s.add_dependency "twirp", ">= 1"
17
16
 
18
17
  s.add_development_dependency "byebug"
19
18
  s.add_development_dependency "rspec"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-twirp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-26 00:00:00.000000000 Z
11
+ date: 2023-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1'
27
- - !ruby/object:Gem::Dependency
28
- name: twirp
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '1'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '1'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: byebug
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -93,7 +79,9 @@ files:
93
79
  - README.md
94
80
  - config/default.yml
95
81
  - lib/rubocop-twirp.rb
82
+ - lib/rubocop/cop/twirp/client_response.rb
96
83
  - lib/rubocop/cop/twirp/deprecated_arguments.rb
84
+ - lib/rubocop/cop/twirp/rspec_be_a.rb
97
85
  - lib/rubocop/twirp.rb
98
86
  - lib/rubocop/twirp/version.rb
99
87
  - rubocop-twirp.gemspec