rubocop-vendor 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: 258d2b7bbbebb511c338bdd04579bae2c6e0bb7e08ae292b39dc45b27174d11b
4
+ data.tar.gz: 531b18092f1058d1b68c151e63dbf1e2d252601ae980b78115e72c2ad5f0464f
5
+ SHA512:
6
+ metadata.gz: 0d460a6b2433b8530ae862e1fe79436c47340a45a83739253e1e89894cba05f03924e55c80c93b3d2fd5aebc7d14edd268ba8902e46c9e2a9d7adf366e9804fe
7
+ data.tar.gz: 11c90c0c37f76378f3c2b385bbcd1d1b20d5a0283a9a9b8023f3bf6c315953d3a05784a8b7ef20765f6da82a2c495fec5f0c046b06339d2a4746c32c26fb745d
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Wealthsimple
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # RuboCop Vendor
2
+
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)
5
+
6
+ Vendor integration analysis for your projects, as an extension to [RuboCop](https://github.com/rubocop-hq/rubocop).
7
+
8
+ ## Installation
9
+
10
+ Just install the `rubocop-vendor` gem
11
+
12
+ ```sh
13
+ gem install rubocop-vendor
14
+ ```
15
+
16
+ or if you use bundler put this in your `Gemfile`
17
+
18
+ ```ruby
19
+ gem 'rubocop-vendor'
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ You need to tell RuboCop to load the Vendor extension. There are three
25
+ ways to do this:
26
+
27
+ ### RuboCop configuration file
28
+
29
+ Put this into your `.rubocop.yml`.
30
+
31
+ ```yaml
32
+ require: rubocop-vendor
33
+ ```
34
+
35
+ Now you can run `rubocop` and it will automatically load the RuboCop Vendor
36
+ cops together with the standard cops.
37
+
38
+ ### Command line
39
+
40
+ ```sh
41
+ rubocop --require rubocop-vendor
42
+ ```
43
+
44
+ ### Rake task
45
+
46
+ ```ruby
47
+ RuboCop::RakeTask.new do |task|
48
+ task.requires << 'rubocop-vendor'
49
+ end
50
+ ```
51
+
52
+ ## The Cops
53
+
54
+ All cops are located under
55
+ [`lib/rubocop/cop/vendor`](lib/rubocop/cop/vendor), and contain
56
+ examples/documentation.
57
+
58
+ In your `.rubocop.yml`, you may treat the Vendor cops just like any other
59
+ cop. For example:
60
+
61
+ ```yaml
62
+ Vednor/RollbarLogger:
63
+ Exclude:
64
+ - lib/example.rb
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ Checkout the [contribution guidelines](CONTRIBUTING.md).
70
+
71
+ ## License
72
+
73
+ `rubocop-vendor` is MIT licensed. [See the accompanying file](LICENSE) for
74
+ the full text.
@@ -0,0 +1,16 @@
1
+ # This is the default configuration file.
2
+
3
+ Vendor/RollbarInterpolation:
4
+ Description: 'Avoid interpolation to improve error grouping.'
5
+ Enabled: true
6
+ VersionAdded: '0.1.0'
7
+
8
+ Vendor/RollbarLogger:
9
+ Description: 'Use logger instead of `Rollbar.debug`, `info` or `warning` calls.'
10
+ Enabled: true
11
+ VersionAdded: '0.1.0'
12
+
13
+ Vendor/RollbarWithException:
14
+ Description: 'Always pass exception parameter when calling `Rollbar.error` or `critical`.'
15
+ Enabled: true
16
+ VersionAdded: '0.1.0'
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vendor
6
+ # This cop checks for interpolated message when calling `Rollbar.error`
7
+ # and suggests sending extra fields as hash parameter instead.
8
+ #
9
+ # The main reason for this suggestion is that Rollbar will have a harder
10
+ # time grouping messages that are dynamic.
11
+ #
12
+ # @example
13
+ # # bad
14
+ # Rollbar.error(e, "Unable to sync account #{account.id}")
15
+ #
16
+ # # good
17
+ # Rollbar.error(e, "Unable to sync account", account_id: account.id)
18
+ #
19
+ class RollbarInterpolation < Cop
20
+ MSG = 'Send extra fields as hash parameter instead of interpolated message.'
21
+
22
+ def_node_matcher :bad_method?, <<-PATTERN
23
+ (send
24
+ (const nil? :Rollbar) {:error :critical}
25
+ (...) $(dstr ...) ...)
26
+ PATTERN
27
+
28
+ def on_send(node)
29
+ interpolated_string = bad_method?(node)
30
+ return unless interpolated_string
31
+
32
+ add_offense(interpolated_string)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vendor
6
+ # This cop checks for non-error usage of Rollbar and suggests using
7
+ # `Rails.logger` instead.
8
+ #
9
+ # The main reason for this suggestion is that Rollbar has a quota that is
10
+ # often multiple times smaller than the log quota and it may become
11
+ # expensive, also an error tracker should *not* be used as a logger.
12
+ #
13
+ # @example
14
+ # # bad
15
+ # Rollbar.info("Stale message")
16
+ #
17
+ # # good
18
+ # Rails.logger.info("Stale message")
19
+ #
20
+ class RollbarLogger < Cop
21
+ MSG = 'Use `Rails.logger` for `debug`, `info` or `warning` calls.'
22
+
23
+ def_node_matcher :bad_method?, <<-PATTERN
24
+ (send $(const nil? :Rollbar) {:debug :info :warning} ...)
25
+ PATTERN
26
+
27
+ def on_send(node)
28
+ rollbar_call = bad_method?(node)
29
+ return unless rollbar_call
30
+
31
+ add_offense(rollbar_call)
32
+ end
33
+
34
+ def autocorrect(node)
35
+ lambda do |corrector|
36
+ corrector.replace(node.loc.expression, 'Rails.logger')
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vendor
6
+ # This cop checks for the exception *not* being passed when calling
7
+ # `Rollbar.error` or `Rollbar.critical` and suggests sending it as
8
+ # the first parameter.
9
+ #
10
+ # The main reason for this suggestion is that Rollbar will display the
11
+ # stack trace along with the error message which will be useful
12
+ # when debugging the issue. If the error is not needed consider using the
13
+ # logger instead.
14
+ #
15
+ # @example
16
+ # # bad
17
+ # Rollbar.error("Unable to sync account")
18
+ #
19
+ # # good
20
+ # Rollbar.error(exception, "Unable to sync account")
21
+ #
22
+ class RollbarWithException < Cop
23
+ include RangeHelp
24
+
25
+ MSG = 'Send exception as first parameter when calling `error` or `critical`.'
26
+
27
+ def_node_matcher :bad_method?, <<-PATTERN
28
+ (send
29
+ (const nil? :Rollbar) {:error :critical}
30
+ !$(lvar _)
31
+ ...)
32
+ PATTERN
33
+
34
+ def on_send(node)
35
+ first_param = bad_method?(node)
36
+ return unless first_param
37
+
38
+ begin_pos = first_param.loc.expression.begin.begin_pos
39
+ add_offense(first_param, location: range_between(begin_pos, begin_pos + 1))
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ end
5
+
6
+ require_relative 'vendor/rollbar_interpolation'
7
+ require_relative 'vendor/rollbar_logger'
8
+ require_relative 'vendor/rollbar_with_exception'
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Vendor
5
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
6
+ # bit of our configuration.
7
+ module Inject
8
+ def self.defaults!
9
+ path = CONFIG_DEFAULT.to_s
10
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
11
+ config = Config.new(hash, path)
12
+ puts "configuration from #{path}" if ConfigLoader.debug?
13
+ config = ConfigLoader.merge_with_default(config, path)
14
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Vendor
5
+ module Version
6
+ STRING = '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # RuboCop Vendor project namespace
5
+ module Vendor
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
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ require_relative 'rubocop/vendor'
6
+ require_relative 'rubocop/vendor/version'
7
+ require_relative 'rubocop/vendor/inject'
8
+
9
+ RuboCop::Vendor::Inject.defaults!
10
+
11
+ require_relative 'rubocop/cop/vendor_cops'
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-vendor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danilo Cabello
8
+ - Marco Costa
9
+ - Osman Currim
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2019-04-23 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rubocop
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 0.53.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: 0.53.0
29
+ - !ruby/object:Gem::Dependency
30
+ name: simplecov
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ description: |2
44
+ A collection of RuboCop cops to check for vendor integration
45
+ in Ruby code.
46
+ email: foss@wealthsimple.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files:
50
+ - LICENSE
51
+ - README.md
52
+ files:
53
+ - LICENSE
54
+ - README.md
55
+ - config/default.yml
56
+ - lib/rubocop-vendor.rb
57
+ - lib/rubocop/cop/vendor/rollbar_interpolation.rb
58
+ - lib/rubocop/cop/vendor/rollbar_logger.rb
59
+ - lib/rubocop/cop/vendor/rollbar_with_exception.rb
60
+ - lib/rubocop/cop/vendor_cops.rb
61
+ - lib/rubocop/vendor.rb
62
+ - lib/rubocop/vendor/inject.rb
63
+ - lib/rubocop/vendor/version.rb
64
+ homepage: https://github.com/wealthsimple/rubocop-vendor
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ homepage_uri: https://rubocop-vendor.readthedocs.io/
69
+ changelog_uri: https://github.com/wealthsimple/rubocop-vendor/blob/master/CHANGELOG.md
70
+ source_code_uri: https://github.com/wealthsimple/rubocop-vendor/
71
+ documentation_uri: https://rubocop-vendor.readthedocs.io/
72
+ bug_tracker_uri: https://github.com/wealthsimple/rubocop-vendor/issues
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '2.4'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.0.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Automatic vendor integration checking tool for Ruby code.
92
+ test_files: []