rubocop-petal 0.2.1 → 0.3.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: 23c16c29cc61affe8674156a8c36e121d07cbfda0f5db9a9bcc697822d52d263
4
- data.tar.gz: e6b2d9dbc87c12675d08fd838d58710a0b47dd1084f39a5c6cff52f1a93517b2
3
+ metadata.gz: c9c488168be9965605cec779d3496901a44c339bfab481d46b6bfd5a766584aa
4
+ data.tar.gz: 4aa0a079fc001b2f42f8b7920a43353ae217dbc5c0dc6154c020604c3e9928ee
5
5
  SHA512:
6
- metadata.gz: 7ac33dda0ec0a66d1819149a05a079ad152f666983eb4c5044f8dec987896d5fabee35e173a16b57cfb0eaa55275343eab672cf05532ccd46120ae14f6acb905
7
- data.tar.gz: b06f412a68de52822e646e79fa707b222d57900ca9291350f9dc3b66ad76c75d4c653dddb23102cedea7d8c8022934abdb14576c4fe758715d0e3e3438b038e0
6
+ metadata.gz: bcbdc1f68ee6438939a848094d048e44da32aec786622dfcee20579e961c85eaf0aea1d88782eee2d85f8c1cc2304977e497e7be0a67e59e5e62d9d12ad0707b
7
+ data.tar.gz: c512bcd9560851dfaa04dab391b6fb2e760247af631ab6cc73d5cb411c166c4cfb2bda90f83bb93c32c485357ba3f132ffbaba76806621accea024ae8b845cbb
data/CHANGELOG.md CHANGED
@@ -2,9 +2,17 @@
2
2
 
3
3
  # main
4
4
 
5
+ # v0.3.0
6
+
7
+ * Added cop `Added Migration/ForeignKeyOption` ([#11](https://github.com/petalmd/rubocop-petal/pull/11))
8
+ * Added cop `Added Grape/PreferNamespace` ([#6](https://github.com/petalmd/rubocop-petal/pull/6))
9
+ * Added cop `Added Migration/SchemaStatementsMethods` ([#14](https://github.com/petalmd/rubocop-petal/pull/14))
10
+ * Remove cop `Added Migration/UseChangeTableBulk` ([#15](https://github.com/petalmd/rubocop-petal/pull/15))
11
+ * Update cop `Grape/PreferNamespace` ([#16](https://github.com/petalmd/rubocop-petal/pull/16))
12
+
5
13
  # v0.2.1
6
14
 
7
- Update lock dependencies `rubocop-rails` ([#9](https://github.com/petalmd/rubocop-petal/pull/9))
15
+ * Update lock dependencies `rubocop-rails` ([#9](https://github.com/petalmd/rubocop-petal/pull/9))
8
16
 
9
17
  # v0.2.0
10
18
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-petal (0.2.1)
4
+ rubocop-petal (0.3.0)
5
5
  rubocop (>= 1.7.0, < 2.0)
6
6
  rubocop-rails (~> 2.10)
7
7
 
@@ -51,7 +51,7 @@ GEM
51
51
  unicode-display_width (>= 1.4.0, < 3.0)
52
52
  rubocop-ast (1.15.0)
53
53
  parser (>= 3.0.1.1)
54
- rubocop-rails (2.12.4)
54
+ rubocop-rails (2.13.2)
55
55
  activesupport (>= 4.2.0)
56
56
  rack (>= 1.1)
57
57
  rubocop (>= 1.7.0, < 2.0)
data/config/default.yml CHANGED
@@ -1,3 +1,22 @@
1
+ Grape/PreferNamespace:
2
+ Description: 'Prevent usage of namespace aliases.'
3
+ Enabled: true
4
+ StyleGuide: https://github.com/ruby-grape/grape#namespace-validation-and-coercion
5
+ Include:
6
+ - app/api/**/*
7
+
8
+ Migration/ForeignKeyOption:
9
+ Description: 'Specify the foreign key option to create the constraint.'
10
+ Enabled: true
11
+ Include:
12
+ - db/migrate/**
13
+
14
+ Migration/SchemaStatementsConnection:
15
+ Description: 'Suggest to use SchemaStatements methods already defined in a migration class.'
16
+ Enabled: true
17
+ Include:
18
+ - db/migrate/**
19
+
1
20
  RSpec/AuthenticatedAs:
2
21
  Description: 'Suggest to use authenticated_as instead of legacy api_key.'
3
22
  Enabled: true
@@ -11,12 +30,6 @@ RSpec/CreateListMax:
11
30
  Include:
12
31
  - spec/**/*
13
32
 
14
- Migration/UseChangeTableBulk:
15
- Description: 'Enforces the use of option `bulk: true` with `change_table`'
16
- Enabled: true
17
- Include:
18
- - db/migrate/**
19
-
20
33
  Grape/HelpersIncludeModule:
21
34
  Description: 'Prevent using helpers with block to include module'
22
35
  Enabled: true
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Grape
6
+ # Prevent usage of `namespace` aliases
7
+ #
8
+ # # bad:
9
+ #
10
+ # group :my_group do ... end
11
+ # resource :my_resource do ... end
12
+ # resources :my_resources do ... end
13
+ # segment :my_segment do ... end
14
+ #
15
+ # # good:
16
+ #
17
+ # namespace :my_namespace do ... end
18
+ #
19
+ class PreferNamespace < Base
20
+ extend AutoCorrector
21
+
22
+ MSG = 'Prefer using `namespace` over its aliases.'
23
+
24
+ NAMESPACE_ALIASES = %i[resource resources group segment].freeze
25
+ RESTRICT_ON_SEND = NAMESPACE_ALIASES
26
+
27
+ def_node_matcher :using_alias_on_api?, <<~PATTERN
28
+ (send nil? ...)
29
+ PATTERN
30
+
31
+ def_node_matcher :namespace_alias, <<~PATTERN
32
+ (send nil? $_ ...)
33
+ PATTERN
34
+
35
+ def on_send(node)
36
+ return unless using_alias_on_api? node
37
+ # Check if use block
38
+ return unless node.block_node&.children&.last
39
+
40
+ add_offense(node) do |corrector|
41
+ corrector.replace(node, node.source.gsub(/^#{namespace_alias(node)}/, 'namespace'))
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Migration
6
+ # Specify the foreign key option to create the constraint.
7
+ #
8
+ # # bad
9
+ # add_reference(:products, :user)
10
+ # add_reference(:products, :user, foreign_key: false)
11
+ # add_belongs_to(:products, :user)
12
+ # t.references(:user)
13
+ #
14
+ # # good
15
+ # add_reference(:products, :user, foreign_key: true)
16
+ # add_reference(:products, :user, foreign_key: { to_table: users })
17
+ # add_belongs_to(:products, :user, foreign_key: true)
18
+ # t.references(:user, foreign_key: true)
19
+ #
20
+ class ForeignKeyOption < Base
21
+ MSG = 'Add `foreign_key: true` or `foreign_key: { to_table: :some_table }`'
22
+
23
+ ADDING_REFERENCES_METHODS = Set.new(%i[add_reference add_belongs_to references belongs_to]).freeze
24
+
25
+ def_node_matcher :adding_reference?, <<~PATTERN
26
+ (send _ ADDING_REFERENCES_METHODS ...)
27
+ PATTERN
28
+
29
+ def_node_matcher :foreign_key_option?, <<~PATTERN
30
+ (send _ ADDING_REFERENCES_METHODS ... (hash <(pair (sym :foreign_key) {true hash}) ...>))
31
+ PATTERN
32
+
33
+ def_node_matcher :polymorphic_reference?, <<~PATTERN
34
+ (... (hash <(pair (sym :polymorphic) true) ... >))
35
+ PATTERN
36
+
37
+ def on_send(node)
38
+ return unless adding_reference?(node)
39
+ return if polymorphic_reference?(node)
40
+
41
+ add_offense(node) unless foreign_key_option?(node)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Migration
6
+ # Use SchemaStatements methods already defined in a migration class.
7
+ # Using already defined method is sorter and will be log during migration.
8
+ #
9
+ # # bad
10
+ # ActiveRecord::Base.connection.table_exists? 'users'
11
+ #
12
+ # # bad
13
+ # ActiveRecord::Base.connection.column_exists? 'users', 'first_name'
14
+ #
15
+ # # bad
16
+ # ApplicationRecord.connection.execute('SELECT COUNT(*) FROM `users`')
17
+ #
18
+ # # good
19
+ # table_exists? 'users'
20
+ #
21
+ # # good
22
+ # column_exists? 'users', 'first_name'
23
+ #
24
+ # # good
25
+ # execute('SELECT COUNT(*) FROM `users`')
26
+ #
27
+ class SchemaStatementsMethods < Base
28
+ extend AutoCorrector
29
+
30
+ MSG = 'Use already defined methods. Remove `ActiveRecord::Base.connection`.'
31
+
32
+ def_node_matcher :use_connection, <<~PATTERN
33
+ (:send (... (:const ...) :connection) ...)
34
+ PATTERN
35
+
36
+ def on_send(node)
37
+ return unless use_connection(node)
38
+
39
+ add_offense(node.children[0]) do |corrector|
40
+ corrector.replace(node, node.source.gsub("#{node.children[0].source}.", ''))
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Petal
5
- VERSION = '0.2.1'
5
+ VERSION = '0.3.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.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Francis Bastien
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-13 00:00:00.000000000 Z
11
+ date: 2022-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -67,8 +67,10 @@ files:
67
67
  - lib/rubocop-petal.rb
68
68
  - lib/rubocop/cop/chewy/reset_on_type.rb
69
69
  - lib/rubocop/cop/grape/helpers_include_module.rb
70
+ - lib/rubocop/cop/grape/prefer_namespace.rb
70
71
  - lib/rubocop/cop/grape/unnecessary_namespace.rb
71
- - lib/rubocop/cop/migration/use_change_table_bulk.rb
72
+ - lib/rubocop/cop/migration/foreign_key_option.rb
73
+ - lib/rubocop/cop/migration/schema_statements_methods.rb
72
74
  - lib/rubocop/cop/petal_cops.rb
73
75
  - lib/rubocop/cop/rails/enum_prefix.rb
74
76
  - lib/rubocop/cop/rails/risky_activerecord_invocation.rb
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Migration
6
- # Use `bulk: true` with `change_table`.
7
- # # bad
8
- #
9
- # change_table :my_table
10
- #
11
- # # good
12
- #
13
- # change_table :my_table, bulk: true
14
- class UseChangeTableBulk < Base
15
- MSG = 'Use `change_table` with `bulk: true`.'
16
- RESTRICT_ON_SEND = %i[change_table].freeze
17
-
18
- def_node_matcher :use_bulk?, <<~PATTERN
19
- (send nil? :change_table _ (hash <(pair (sym :bulk) true) ...>) ...)
20
- PATTERN
21
-
22
- def on_send(node)
23
- return if use_bulk?(node)
24
-
25
- add_offense(node)
26
- end
27
- end
28
- end
29
- end
30
- end