rubocop-vendor 0.2.1 → 0.6.1

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: fe1f462290839dea864712489498d9692d77909835d10c3210f6188683f90e24
4
- data.tar.gz: d228e77bcc8c2df232f4160d98765bf2305c12d4fbed668aaaa3f535b0e1a65b
3
+ metadata.gz: 14c18d2f337b52f8134c40f8e43c92e68a2124fe963c28f51538305f10c256d0
4
+ data.tar.gz: ee0e68653af5d24dd75d29a894c8da2725030c92714e75924603db2c984e3430
5
5
  SHA512:
6
- metadata.gz: 8f231f57b14ff0aeba3c034863f4dc5eec541c15b0f2d6e035c4205101739c6b23684ab1934c8b8e071e0f33c8fe14a9677a5ea224ab4e6905bec7f88dbe23c7
7
- data.tar.gz: 0c14aad1fc61dd8010d6b01476441a43293d29807051d5c4e0625eb9cea9e9a5705644826b7e9a1e0a5c8afdaec4f76e00bc45f7ec4007e61283e21903c69b4a
6
+ metadata.gz: 96d0eb7c18afbe8711722aa88d9a75adc25284ce437c97e565d74a0218f2b9a9e473fba2de7cf417360a059a288ff7b42a21d594de1c27f1044124d1c3d2ced0
7
+ data.tar.gz: 0e7efef91d82dbf624c5726713539f35dd49a760235396eebff75dca84b17e80d778ec9c81882c4a93c59b3597162c3dee57e59d0cbe492218d8eaa5d50637e3
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
- # RuboCop Vendor
1
+ # rubocop-vendor
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/rubocop-vendor.svg)](https://badge.fury.io/rb/rubocop-vendor)
4
- [![CircleCI](https://circleci.com/gh/wealthsimple/rubocop-vendor.svg?style=svg)](https://circleci.com/gh/wealthsimple/rubocop-vendor)
4
+ [![GitHub Actions Badge](https://github.com/wealthsimple/rubocop-vendor/actions/workflows/main.yml/badge.svg)](https://github.com/wealthsimple/rubocop-vendor/actions)
5
5
 
6
6
  Vendor integration analysis for your projects, as an extension to [RuboCop](https://github.com/rubocop-hq/rubocop).
7
7
 
@@ -59,7 +59,7 @@ In your `.rubocop.yml`, you may treat the Vendor cops just like any other
59
59
  cop. For example:
60
60
 
61
61
  ```yaml
62
- Vednor/RollbarLogger:
62
+ Vendor/RollbarLogger:
63
63
  Exclude:
64
64
  - lib/example.rb
65
65
  ```
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vendor
6
+ # This cop checks for Rollbar calls outside `rescue` blocks.
7
+ #
8
+ # The main reason for this suggestion is that Rollbar should not be used
9
+ # as a logger given it has a quota that is often multiple times smaller
10
+ # than the log quota. By reporting errors outside rescue blocks
11
+ # the developer is most likely in control of the exceptional flow and
12
+ # won't need a stack trace.
13
+ #
14
+ # @example
15
+ # # bad
16
+ # Rollbar.error("Unable to sync account")
17
+ #
18
+ # # good
19
+ # begin
20
+ # 1 / 0
21
+ # rescue StandardError => exception
22
+ # Rollbar.error(exception, "Unable to sync account")
23
+ # end
24
+ #
25
+ # # good
26
+ # class ApplicationController < ActionController::Base
27
+ # rescue_from InvalidRecord do |e|
28
+ # Rollbar.error(e)
29
+ # end
30
+ # end
31
+ #
32
+ class RollbarInsideRescue < Cop
33
+ MSG = 'Only call Rollbar when handling errors inside a `rescue` block.'
34
+
35
+ # @!method rollbar?(node)
36
+ def_node_matcher :rollbar?, <<-PATTERN
37
+ (send
38
+ (const nil? :Rollbar) {:log :debug :info :warning :error :critical} ...)
39
+ PATTERN
40
+
41
+ # @!method active_support_rescuable_block?(node)
42
+ def_node_matcher :active_support_rescuable_block?, <<-PATTERN
43
+ (block
44
+ (send nil? :rescue_from ...) ...)
45
+ PATTERN
46
+
47
+ def on_send(node)
48
+ return unless rollbar?(node)
49
+ return if in_rescue_block?(node)
50
+
51
+ add_offense(node, location: node.children[0].loc.expression)
52
+ end
53
+
54
+ def in_rescue_block?(node)
55
+ current_node = node
56
+
57
+ while (current_node = current_node.parent)
58
+ return true if current_node.rescue_type?
59
+ return true if active_support_rescuable_block?(current_node)
60
+
61
+ break if current_node.def_type?
62
+ break if current_node.class_type?
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -19,6 +19,7 @@ module RuboCop
19
19
  class RollbarInterpolation < Cop
20
20
  MSG = 'Send extra fields as hash parameter instead of interpolated message.'
21
21
 
22
+ # @!method bad_method?(node)
22
23
  def_node_matcher :bad_method?, <<-PATTERN
23
24
  (send
24
25
  (const nil? :Rollbar) {:error :critical}
@@ -16,25 +16,42 @@ module RuboCop
16
16
  # Rollbar.info('Stale message')
17
17
  #
18
18
  class RollbarLog < Cop
19
+ include RangeHelp
20
+
19
21
  MSG = 'Use `Rollbar.%<method>s` instead of `Rollbar.log`.'
20
22
 
23
+ # @!method bad_method?(node)
21
24
  def_node_matcher :bad_method?, <<-PATTERN
22
25
  (send
23
26
  (const nil? :Rollbar) :log
24
- $({str sym} _) ...)
27
+ ({str sym} _) ...)
25
28
  PATTERN
26
29
 
27
30
  def on_send(node)
28
- level = bad_method?(node)
29
- return unless level
31
+ return unless bad_method?(node)
32
+
33
+ add_offense(node, location: offending_range(node))
34
+ end
30
35
 
31
- add_offense(level)
36
+ def autocorrect(node)
37
+ range = offending_range(node)
38
+ replacement = "#{node.children[2].value}#{range.source.include?('(') ? '(' : ' '}"
39
+ lambda do |corrector|
40
+ corrector.replace(range, replacement)
41
+ end
32
42
  end
33
43
 
34
44
  private
35
45
 
46
+ def offending_range(node)
47
+ range_between(
48
+ node.children[0].loc.last_column + 1,
49
+ node.children[3].loc.column
50
+ )
51
+ end
52
+
36
53
  def message(node)
37
- format(MSG, method: node.value)
54
+ format(MSG, method: node.children[2].value)
38
55
  end
39
56
  end
40
57
  end
@@ -20,20 +20,20 @@ module RuboCop
20
20
  class RollbarLogger < Cop
21
21
  MSG = 'Use `Rails.logger` for `debug`, `info` or `warning` calls.'
22
22
 
23
+ # @!method bad_method?(node)
23
24
  def_node_matcher :bad_method?, <<-PATTERN
24
- (send $(const nil? :Rollbar) {:debug :info :warning} ...)
25
+ (send (const nil? :Rollbar) {:debug :info :warning} {str hash})
25
26
  PATTERN
26
27
 
27
28
  def on_send(node)
28
- rollbar_call = bad_method?(node)
29
- return unless rollbar_call
29
+ return unless bad_method?(node)
30
30
 
31
- add_offense(rollbar_call)
31
+ add_offense(node, location: node.children[0].loc.expression)
32
32
  end
33
33
 
34
34
  def autocorrect(node)
35
35
  lambda do |corrector|
36
- corrector.replace(node.loc.expression, 'Rails.logger')
36
+ corrector.replace(node.children[0].loc.expression, 'Rails.logger')
37
37
  end
38
38
  end
39
39
  end
@@ -24,6 +24,7 @@ module RuboCop
24
24
 
25
25
  MSG = 'Send exception as first parameter when calling `error` or `critical`.'
26
26
 
27
+ # @!method bad_method?(node)
27
28
  def_node_matcher :bad_method?, <<-PATTERN
28
29
  (send
29
30
  (const nil? :Rollbar) {:error :critical}
@@ -3,6 +3,7 @@
3
3
  module RuboCop
4
4
  end
5
5
 
6
+ require_relative 'vendor/rollbar_inside_rescue'
6
7
  require_relative 'vendor/rollbar_interpolation'
7
8
  require_relative 'vendor/rollbar_log'
8
9
  require_relative 'vendor/rollbar_logger'
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Vendor
5
5
  module Version
6
- STRING = '0.2.1'
6
+ STRING = '0.6.1'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-vendor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danilo Cabello
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-04-25 00:00:00.000000000 Z
13
+ date: 2021-05-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -82,6 +82,7 @@ files:
82
82
  - README.md
83
83
  - config/default.yml
84
84
  - lib/rubocop-vendor.rb
85
+ - lib/rubocop/cop/vendor/rollbar_inside_rescue.rb
85
86
  - lib/rubocop/cop/vendor/rollbar_interpolation.rb
86
87
  - lib/rubocop/cop/vendor/rollbar_log.rb
87
88
  - lib/rubocop/cop/vendor/rollbar_logger.rb
@@ -95,7 +96,7 @@ licenses:
95
96
  - MIT
96
97
  metadata:
97
98
  homepage_uri: https://rubocop-vendor.readthedocs.io/
98
- changelog_uri: https://github.com/wealthsimple/rubocop-vendor/blob/master/CHANGELOG.md
99
+ changelog_uri: https://github.com/wealthsimple/rubocop-vendor/blob/main/CHANGELOG.md
99
100
  source_code_uri: https://github.com/wealthsimple/rubocop-vendor/
100
101
  documentation_uri: https://rubocop-vendor.readthedocs.io/
101
102
  bug_tracker_uri: https://github.com/wealthsimple/rubocop-vendor/issues
@@ -107,14 +108,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
108
  requirements:
108
109
  - - ">="
109
110
  - !ruby/object:Gem::Version
110
- version: '2.4'
111
+ version: '2.7'
111
112
  required_rubygems_version: !ruby/object:Gem::Requirement
112
113
  requirements:
113
114
  - - ">="
114
115
  - !ruby/object:Gem::Version
115
116
  version: '0'
116
117
  requirements: []
117
- rubygems_version: 3.0.3
118
+ rubygems_version: 3.1.6
118
119
  signing_key:
119
120
  specification_version: 4
120
121
  summary: Automatic vendor integration checking tool for Ruby code.