rubocop-petal 0.6.0 → 0.8.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: c7d73cfbe6b4f194f1c60d25235331cf7a08b04b516411efeabeafce9e6e7886
4
- data.tar.gz: 193dd79a6efd61cdd2edbb5bfae7158497ca7cc2bb52da38b02dfecd776eadfa
3
+ metadata.gz: 424a55f53bbcb88794cf8a0122a376217db95d890fc789d0288a76434cdcb023
4
+ data.tar.gz: 838d1178021d47035f10b7fb139e3fb50955b823ec4f94f80bad0f504d042017
5
5
  SHA512:
6
- metadata.gz: 15ef827ea4cf56872e2c85178463e2d3a05a6a47411832439c81cb4dd9a9ff9221efb61603d2a9e6b38d5859fe0aa2745499bd1d98ef516169e1947065bf1ebb
7
- data.tar.gz: 0c14a8c68e7af71afbd81cd827fdd76dfbcc8780e06ab7f1f2f662cc7409fe505b2afc05fa6260f18c6d3a1dd8df05176d51c965c71b68c4f75501abe4a9aa96
6
+ metadata.gz: d1d19b3bb4c94b070a0acb1d06442541aa333ef8a2170a4d2074ab828051909811d90c8e37ad4d10b5f76ba7fde2ba493bb2f1dcbbc9f5e5e2e930561be23412
7
+ data.tar.gz: a188cc40b0da58690ef31c33698ba33c6043e0d0a52d474b4ee07be5049991357373c97666b5c42996be7efedb56a8e540a4bbbd9d81ac8761bf6144c121ae73
data/config/default.yml CHANGED
@@ -77,3 +77,21 @@ Rails/TableName:
77
77
  Chewy/ResetOnType:
78
78
  Description: 'Prevent using reset! methods on Chewy type class'
79
79
  Enabled: true
80
+
81
+ RSpec/JsonParseResponseBody:
82
+ Description: 'Prevent JSON.parse(response.body) in favor of response.parsed_body'
83
+ Enabled: true
84
+ SafeAutoCorrect: true
85
+ Include:
86
+ - spec/**/*
87
+
88
+ RSpec/JsonResponse:
89
+ Description: 'Prevent json_response in favor of response.parsed_body'
90
+ Enabled: true
91
+ SafeAutoCorrect: true
92
+ Include:
93
+ - spec/**/*
94
+
95
+ Performance/Snif:
96
+ Description: 'Prevent snif in favor of detect'
97
+ Enabled: true
@@ -6,17 +6,17 @@ module RuboCop
6
6
  # Detect unnecessary usage of Grape namespace.
7
7
  #
8
8
  # # bad
9
- # namespace :my_namespace
9
+ # namespace :some_path do
10
10
  # get {}
11
11
  # end
12
12
  #
13
13
  # # good
14
- # get :my_namespace {}
14
+ # get :some_path {}
15
15
  #
16
16
  class UnnecessaryNamespace < Base
17
17
  MSG = 'Unnecessary usage of Grape namespace. '\
18
- 'Specify endpoint name with a argument: `get :my_namespace`.'
19
- HTTP_ACTIONS = Set.new(%i[get put post patch delete])
18
+ 'Specify endpoint name with an argument: `get :some_path`.'
19
+ HTTP_ACTIONS = Set.new(%i[get head put post patch delete])
20
20
  GRAPE_NAMESPACE_ALIAS = Set.new(%i[namespace resource resources])
21
21
  METHOD_JUSTIFY_NAMESPACE = Set.new(%i[route_param namespaces resource resources version])
22
22
 
@@ -28,8 +28,12 @@ module RuboCop
28
28
  (block (send nil? METHOD_JUSTIFY_NAMESPACE ...) ...)
29
29
  PATTERN
30
30
 
31
- def_node_matcher :http_action?, <<~PATTERN
32
- (block (send nil? HTTP_ACTIONS) ...)
31
+ def_node_matcher :http_action_with_path?, <<~PATTERN
32
+ (block (send nil? HTTP_ACTIONS ({sym | str} $_)? ...) ...)
33
+ PATTERN
34
+
35
+ def_node_matcher :http_action_with_options?, <<~PATTERN
36
+ (block (send nil? HTTP_ACTIONS ...) ...)
33
37
  PATTERN
34
38
 
35
39
  def on_send(node)
@@ -42,13 +46,21 @@ module RuboCop
42
46
  end
43
47
 
44
48
  http_action_node = select_http_action_block_node(node_to_select_http_action)
45
- add_offense(node) if http_action_node.size == 1
49
+
50
+ return if http_action_node.size != 1
51
+
52
+ paths = paths_added_with_http_action(http_action_node.first)
53
+ add_offense(node) if paths.size.zero?
46
54
  end
47
55
 
48
56
  private
49
57
 
58
+ def paths_added_with_http_action(node)
59
+ http_action_with_path?(node).yield_self.to_a.flatten.compact
60
+ end
61
+
50
62
  def select_http_action_block_node(nodes)
51
- nodes.select { |node| http_action?(node) }
63
+ nodes.select { |node| http_action_with_path?(node) || http_action_with_options?(node) }
52
64
  end
53
65
 
54
66
  def namespace_node(node)
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Performance
6
+ # Prevent using `snif`.
7
+ # Consider using `detect`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # period.equity_packs.snif(:code, 'ONCALL')
12
+ #
13
+ # # good
14
+ # period.equity_packs.detect { |equity_pack| equity_pack.code == 'ONCALL' }
15
+ class Snif < Base
16
+ MSG = 'Use `detect` instead.'
17
+
18
+ def_node_matcher :snif?, <<~PATTERN
19
+ (send _ :snif _ _)
20
+ PATTERN
21
+
22
+ def on_send(node)
23
+ return unless snif?(node)
24
+
25
+ add_offense(node)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # Prevent using `JSON.parse(response.body)` in spec.
7
+ # Consider using `response.parsed_body`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # JSON.parse(response.body) do; end
12
+ #
13
+ # # good
14
+ # response.parsed_body
15
+ #
16
+ class JsonParseResponseBody < Base
17
+ extend AutoCorrector
18
+
19
+ MSG = 'Use `response.parsed_body` instead.'
20
+
21
+ def_node_matcher :json_parse_response_body?, <<~PATTERN
22
+ (send (const _ :JSON) :parse (send (send _ :response) :body))
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ return unless json_parse_response_body?(node)
27
+
28
+ add_offense(node) do |corrector|
29
+ corrector.replace(node, 'response.parsed_body')
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RSpec
6
+ # Prevent using `json_response` in spec.
7
+ # Consider using `response.parsed_body`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # expect(json_response)
12
+ #
13
+ # # good
14
+ # expect(response.parsed_body)
15
+ #
16
+ class JsonResponse < Base
17
+ extend AutoCorrector
18
+
19
+ MSG = 'Use `response.parsed_body` instead.'
20
+
21
+ def_node_matcher :json_response?, <<~PATTERN
22
+ (send _ :json_response)
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ return unless json_response?(node)
27
+
28
+ add_offense(node) do |corrector|
29
+ corrector.replace(node, 'response.parsed_body')
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Petal
5
- VERSION = '0.6.0'
5
+ VERSION = '0.8.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-petal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Francis Bastien
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-24 00:00:00.000000000 Z
11
+ date: 2023-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -49,20 +49,12 @@ email:
49
49
  - jfbastien@petalmd.com
50
50
  executables: []
51
51
  extensions: []
52
- extra_rdoc_files: []
52
+ extra_rdoc_files:
53
+ - LICENSE.txt
54
+ - README.md
53
55
  files:
54
- - ".github/workflows/build.yml"
55
- - ".gitignore"
56
- - ".rspec"
57
- - ".rubocop.yml"
58
- - CHANGELOG.md
59
- - Gemfile
60
- - Gemfile.lock
61
56
  - LICENSE.txt
62
57
  - README.md
63
- - Rakefile
64
- - bin/console
65
- - bin/setup
66
58
  - config/default.yml
67
59
  - lib/rubocop-petal.rb
68
60
  - lib/rubocop/cop/chewy/reset_on_type.rb
@@ -71,24 +63,28 @@ files:
71
63
  - lib/rubocop/cop/grape/unnecessary_namespace.rb
72
64
  - lib/rubocop/cop/migration/foreign_key_option.rb
73
65
  - lib/rubocop/cop/migration/schema_statements_methods.rb
66
+ - lib/rubocop/cop/performance/snif.rb
74
67
  - lib/rubocop/cop/petal_cops.rb
75
68
  - lib/rubocop/cop/rails/enum_prefix.rb
76
69
  - lib/rubocop/cop/rails/risky_activerecord_invocation.rb
77
70
  - lib/rubocop/cop/rails/table_name.rb
78
71
  - lib/rubocop/cop/rspec/authenticated_as.rb
79
72
  - lib/rubocop/cop/rspec/create_list_max.rb
73
+ - lib/rubocop/cop/rspec/json_parse_response_body.rb
74
+ - lib/rubocop/cop/rspec/json_response.rb
80
75
  - lib/rubocop/cop/rspec/sidekiq_inline.rb
81
76
  - lib/rubocop/cop/rspec/stub_products.rb
82
77
  - lib/rubocop/petal.rb
83
78
  - lib/rubocop/petal/inject.rb
84
79
  - lib/rubocop/petal/version.rb
85
- - rubocop-petal.gemspec
86
80
  homepage: https://github.com/petalmd/rubocop-petal
87
81
  licenses:
88
82
  - MIT
89
83
  metadata:
90
84
  homepage_uri: https://github.com/petalmd/rubocop-petal
85
+ changelog_uri: https://github.com/petalmd/rubocop-petal/blob/main/CHANGELOG.md
91
86
  source_code_uri: https://github.com/petalmd/rubocop-petal
87
+ bug_tracker_uri: https://github.com/petalmd/rubocop-petal/issues
92
88
  rubygems_mfa_required: 'true'
93
89
  post_install_message:
94
90
  rdoc_options: []
@@ -1,18 +0,0 @@
1
- name: Build
2
- on:
3
- pull_request:
4
- push:
5
- branches:
6
- - main
7
- jobs:
8
- build:
9
- runs-on: ubuntu-latest
10
- steps:
11
- - uses: actions/checkout@v2
12
- - name: Set up Ruby
13
- uses: ruby/setup-ruby@v1
14
- with:
15
- ruby-version: 2.6
16
- bundler-cache: true
17
- - name: Default task, RSpec and Rubocop
18
- run: bundle exec rake
data/.gitignore DELETED
@@ -1,12 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
- /.idea/
10
-
11
- # rspec failure tracking
12
- .rspec_status
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,18 +0,0 @@
1
- require:
2
- - rubocop-rspec
3
-
4
- AllCops:
5
- NewCops: enable
6
- TargetRubyVersion: 2.6
7
- SuggestExtensions: false
8
-
9
- Naming/FileName:
10
- Exclude:
11
- - lib/rubocop-petal.rb
12
-
13
- Metrics/BlockLength:
14
- Exclude:
15
- - spec/**/*_spec.rb
16
-
17
- RSpec/ExampleLength:
18
- Enabled: false
data/CHANGELOG.md DELETED
@@ -1,48 +0,0 @@
1
- # Changelog
2
-
3
- # main
4
-
5
- # v0.6.0
6
-
7
- * Added cop `RSpec::SidekiqInline` ([#24](https://github.com/petalmd/rubocop-petal/pull/24))
8
- * Remove cop `Rails/ValidateUniquenessCase` ([#21](https://github.com/petalmd/rubocop-petal/pull/21))
9
-
10
- # v0.5.0
11
-
12
- * Added cop `Rails/ValidateUniquenessCase` ([#20](https://github.com/petalmd/rubocop-petal/pull/20))
13
-
14
- # v0.4.1
15
-
16
- * Fix typo default config SafeAutoCorrect RSpec/StubProducts ([#19](https://github.com/petalmd/rubocop-petal/pull/19))
17
-
18
- # v0.4.0
19
-
20
- * Added cop `RSpec/StubProducts` ([#18](https://github.com/petalmd/rubocop-petal/pull/18))
21
-
22
- # v0.3.1
23
-
24
- * Correct cop name `Migration/SchemaStatementsMethods` in config ([#17](https://github.com/petalmd/rubocop-petal/pull/17))
25
-
26
- # v0.3.0
27
-
28
- * Added cop `Added Migration/ForeignKeyOption` ([#11](https://github.com/petalmd/rubocop-petal/pull/11))
29
- * Added cop `Added Grape/PreferNamespace` ([#6](https://github.com/petalmd/rubocop-petal/pull/6))
30
- * Added cop `Added Migration/SchemaStatementsMethods` ([#14](https://github.com/petalmd/rubocop-petal/pull/14))
31
- * Remove cop `Added Migration/UseChangeTableBulk` ([#15](https://github.com/petalmd/rubocop-petal/pull/15))
32
- * Update cop `Grape/PreferNamespace` ([#16](https://github.com/petalmd/rubocop-petal/pull/16))
33
-
34
- # v0.2.1
35
-
36
- * Update lock dependencies `rubocop-rails` ([#9](https://github.com/petalmd/rubocop-petal/pull/9))
37
-
38
- # v0.2.0
39
-
40
- * Added cop `RSpec/AuthenticatedAs` ([#3](https://github.com/petalmd/rubocop-petal/pull/3))
41
- * Added cop `Grape/UnnecessaryNamespace` ([#2](https://github.com/petalmd/rubocop-petal/pull/2))
42
- * Added cop `RSpec/CreateListMax` ([#4](https://github.com/petalmd/rubocop-petal/pull/4))
43
- * Added Cop `Migration/UseChangeTableBulk` ([#7](https://github.com/petalmd/rubocop-petal/pull/7))
44
- * Added cop `Grape/HelpersIncludeModule` ([#1](https://github.com/petalmd/rubocop-petal/pull/1))
45
-
46
- # v0.1.0
47
-
48
- * First version
data/Gemfile DELETED
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- # Specify your gem's dependencies in rubocop-petal.gemspec
6
- gemspec
7
-
8
- gem 'rake', '~> 13.0'
9
-
10
- gem 'rspec', '~> 3.0'
11
-
12
- gem 'rubocop', '~> 1.0'
13
- gem 'rubocop-rspec', require: false
14
-
15
- gem 'activesupport', '~> 5.2.0'
data/Gemfile.lock DELETED
@@ -1,78 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- rubocop-petal (0.6.0)
5
- rubocop (>= 1.7.0, < 2.0)
6
- rubocop-rails (~> 2.10)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- activesupport (5.2.6)
12
- concurrent-ruby (~> 1.0, >= 1.0.2)
13
- i18n (>= 0.7, < 2)
14
- minitest (~> 5.1)
15
- tzinfo (~> 1.1)
16
- ast (2.4.2)
17
- concurrent-ruby (1.1.9)
18
- diff-lcs (1.4.4)
19
- i18n (1.8.11)
20
- concurrent-ruby (~> 1.0)
21
- minitest (5.15.0)
22
- parallel (1.21.0)
23
- parser (3.0.3.2)
24
- ast (~> 2.4.1)
25
- rack (3.0.0)
26
- rainbow (3.0.0)
27
- rake (13.0.6)
28
- regexp_parser (2.2.0)
29
- rexml (3.2.5)
30
- rspec (3.10.0)
31
- rspec-core (~> 3.10.0)
32
- rspec-expectations (~> 3.10.0)
33
- rspec-mocks (~> 3.10.0)
34
- rspec-core (3.10.1)
35
- rspec-support (~> 3.10.0)
36
- rspec-expectations (3.10.1)
37
- diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.10.0)
39
- rspec-mocks (3.10.2)
40
- diff-lcs (>= 1.2.0, < 2.0)
41
- rspec-support (~> 3.10.0)
42
- rspec-support (3.10.3)
43
- rubocop (1.23.0)
44
- parallel (~> 1.10)
45
- parser (>= 3.0.0.0)
46
- rainbow (>= 2.2.2, < 4.0)
47
- regexp_parser (>= 1.8, < 3.0)
48
- rexml
49
- rubocop-ast (>= 1.12.0, < 2.0)
50
- ruby-progressbar (~> 1.7)
51
- unicode-display_width (>= 1.4.0, < 3.0)
52
- rubocop-ast (1.15.0)
53
- parser (>= 3.0.1.1)
54
- rubocop-rails (2.15.2)
55
- activesupport (>= 4.2.0)
56
- rack (>= 1.1)
57
- rubocop (>= 1.7.0, < 2.0)
58
- rubocop-rspec (2.6.0)
59
- rubocop (~> 1.19)
60
- ruby-progressbar (1.11.0)
61
- thread_safe (0.3.6)
62
- tzinfo (1.2.9)
63
- thread_safe (~> 0.1)
64
- unicode-display_width (2.1.0)
65
-
66
- PLATFORMS
67
- ruby
68
-
69
- DEPENDENCIES
70
- activesupport (~> 5.2.0)
71
- rake (~> 13.0)
72
- rspec (~> 3.0)
73
- rubocop (~> 1.0)
74
- rubocop-petal!
75
- rubocop-rspec
76
-
77
- BUNDLED WITH
78
- 2.2.3
data/Rakefile DELETED
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
- require 'rubocop/rake_task'
6
-
7
- RSpec::Core::RakeTask.new(:spec)
8
-
9
- RuboCop::RakeTask.new
10
-
11
- task default: %i[spec rubocop]
12
-
13
- RSpec::Core::RakeTask.new(:spec) do |spec|
14
- spec.pattern = FileList['spec/**/*_spec.rb']
15
- end
16
-
17
- desc 'Generate a new cop with a template'
18
- task :new_cop, [:cop] do |_task, args|
19
- require 'rubocop'
20
-
21
- cop_name = args.fetch(:cop) do
22
- warn 'usage: bundle exec rake new_cop[Department/Name]'
23
- exit!
24
- end
25
-
26
- generator = RuboCop::Cop::Generator.new(cop_name)
27
-
28
- generator.write_source
29
- generator.write_spec
30
- generator.inject_require(root_file_path: 'lib/rubocop/cop/petal_cops.rb')
31
- generator.inject_config(config_file_path: 'config/default.yml')
32
-
33
- puts generator.todo
34
- end
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'rubocop/petal'
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require 'irb'
15
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'lib/rubocop/petal/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'rubocop-petal'
7
- spec.version = RuboCop::Petal::VERSION
8
- spec.authors = ['Jean-Francis Bastien']
9
- spec.email = ['jfbastien@petalmd.com']
10
-
11
- spec.summary = 'Petal custom cops'
12
- spec.homepage = 'https://github.com/petalmd/rubocop-petal'
13
- spec.license = 'MIT'
14
- spec.required_ruby_version = Gem::Requirement.new('>= 2.6')
15
-
16
- spec.metadata['homepage_uri'] = spec.homepage
17
- spec.metadata['source_code_uri'] = 'https://github.com/petalmd/rubocop-petal'
18
- spec.metadata['rubygems_mfa_required'] = 'true'
19
- # Specify which files should be added to the gem when it is released.
20
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
- end
24
- spec.bindir = 'exe'
25
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
- spec.require_paths = ['lib']
27
-
28
- # Uncomment to register a new dependency of your gem
29
- # spec.add_dependency "example-gem", "~> 1.0"
30
-
31
- # For more information and examples about making a new gem, checkout our
32
- # guide at: https://bundler.io/guides/creating_gem.html
33
-
34
- spec.add_runtime_dependency 'rubocop', '>= 1.7.0', '< 2.0'
35
- spec.add_dependency 'rubocop-rails', '~> 2.10'
36
- end