rubocop-petal 1.0.0 → 1.1.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: 1d59a2d5781acf6465d3b1f5fef4575af1db1da8fd4af2d4ec3b0af863139cb2
4
- data.tar.gz: aa4221534a285b746c8dd195347ab93c27954762bdd26f93242c712c8cbed20f
3
+ metadata.gz: 8569e249e175a6a16fbfd1b097654b83327dde914826987a0faaae4f418688c9
4
+ data.tar.gz: 91eb5f11e7e5bfe69b9395f8d79674d5aeaa986cf69ac8d684ccbdd1c954b3a7
5
5
  SHA512:
6
- metadata.gz: 81707e3d93a3e10e36dcdd299c63f804b8b820a8406b267715c084ed8fd5b0180633ea2e2a335623df414e3251959813332526d1e796fe86771241af85c09920
7
- data.tar.gz: '048bde3d8f343cb218b2bd25dbbedbd30fc8ff7579028e3a6f3d805800c7212720b7fc48e4b8757dd9948c5775730f27287490b18dc701d15e893800a7c73562'
6
+ metadata.gz: 2b7d7797b3774dc52bb4643ed871ed08e570b19298c32c733f0265d7bd257709ff425c632f9889078a4f3cd9b1f71a3cb6479cb5b26197915490640e5ed3b9b7
7
+ data.tar.gz: 633e71440f3ba8513d388da6aed6c9a63986fdf262d990e4434054a0b56b836338cb6ddbd9fa075165dc04d6e4ee9cc041b944e7940bb8c1cf7d9c3395f04f66
data/README.md CHANGED
@@ -54,7 +54,7 @@ bundle exec rake 'new_cop[Rails/MyNewCop]'
54
54
  ```
55
55
 
56
56
  Have a look to [RuboCop documentation](https://docs.rubocop.org/rubocop/1.23/development.html) to get started with
57
- _node pattern_.
57
+ _node pattern_. [Rubocop AST](https://github.com/rubocop/rubocop-ast/blob/master/docs/modules/ROOT/pages/node_pattern.adoc)
58
58
 
59
59
  ## Release
60
60
 
data/config/base.yml CHANGED
@@ -38,13 +38,13 @@ Metrics/BlockNesting:
38
38
  Metrics/ClassLength:
39
39
  Enabled: false
40
40
  Metrics/CyclomaticComplexity:
41
- Max: 10
41
+ Max: 9
42
42
  Metrics/MethodLength:
43
43
  Enabled: false
44
44
  Metrics/ModuleLength:
45
45
  Enabled: false
46
46
  Metrics/PerceivedComplexity:
47
- Max: 12
47
+ Max: 10
48
48
  RSpec:
49
49
  StyleGuideBaseURL: https://rspec.rubystyle.guide
50
50
  RSpec/ContextWording:
@@ -92,6 +92,8 @@ Rails/UnknownEnv:
92
92
  - staging
93
93
  Rails/WhereExists:
94
94
  Enabled: false
95
+ Rails/TableNameAssignment:
96
+ Enabled: true
95
97
  Style/CollectionMethods:
96
98
  PreferredMethods:
97
99
  detect: detect
data/config/default.yml CHANGED
@@ -5,6 +5,18 @@ Grape/PreferNamespace:
5
5
  Include:
6
6
  - app/api/**/*
7
7
 
8
+ Migration/AlwaysBulkChangeTable:
9
+ Description: 'Suggest to always use `bulk: true` when using `change_table`.'
10
+ Enabled: true
11
+ Include:
12
+ - db/migrate/**
13
+
14
+ Migration/ChangeTableReferences:
15
+ Description: 'Prevent using `t.references` or `t.belongs_to` in a change_table.'
16
+ Enabled: true
17
+ Include:
18
+ - db/migrate/**
19
+
8
20
  Migration/ForeignKeyOption:
9
21
  Description: 'Specify the foreign key option to create the constraint.'
10
22
  Enabled: true
@@ -67,13 +79,6 @@ Rails/RiskyActiverecordInvocation:
67
79
  Description: 'Interpolation, use hash or parameterized syntax.'
68
80
  Enabled: true
69
81
 
70
- Rails/TableName:
71
- Description: 'Prevent usage of table_name= to in favor of convention.'
72
- Enabled: true
73
- StyleGuide: "https://rails.rubystyle.guide/#keep-ar-defaults"
74
- Include:
75
- - app/models/**/*
76
-
77
82
  Chewy/ResetOnType:
78
83
  Description: 'Prevent using reset! methods on Chewy type class'
79
84
  Enabled: true
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Migration
6
+ # Using `bulk: true` with `change_table` is recommended.
7
+ # Without `bulk: true`, `change_table` generates multiple
8
+ # `ALTER TABLE` statements and the migration process will
9
+ # be duplicated.
10
+ #
11
+ # @bad
12
+ # change_table :users do |t|
13
+ # t.string :first_name
14
+ # t.string :last_name
15
+ # end
16
+ #
17
+ # @good
18
+ # change_table :users, bulk: true do |t|
19
+ # t.string :first_name
20
+ # t.string :last_name
21
+ # end
22
+ #
23
+ class AlwaysBulkChangeTable < Rails::BulkChangeTable
24
+ MSG = 'Add `bulk: true` when using change_table.'
25
+ RESTRICT_ON_SEND = %i[change_table].freeze
26
+
27
+ def on_send(node)
28
+ return unless node.command?(:change_table)
29
+
30
+ unless include_bulk_options?(node)
31
+ add_offense(node)
32
+ return
33
+ end
34
+
35
+ add_offense(node) unless bulk_options_true?(node)
36
+ end
37
+
38
+ def bulk_options_true?(node)
39
+ options = node.arguments[1]
40
+ options.each_pair do |key, value|
41
+ return true if key.value == :bulk && value.true_type?
42
+ end
43
+ false
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Migration
6
+ # Prevent using `t.references` or `t.belongs_to` in a change_table.
7
+ # Internally, `t.references` use multiple `ALTER TABLE` statements.
8
+ # Since Rails cannot transform automatically `t.references` inside
9
+ # a `change_table bulk: true` we can manually create the equivalent
10
+ # `ALTER TABLE` statement using `t.bigint`, `t.index` and `t.foreign_key`.
11
+ #
12
+ # #bad
13
+ # change_table :subscriptions, bulk: true do |t|
14
+ # t.references :user, null: false, foreign_key: true
15
+ # end
16
+ #
17
+ # #good
18
+ # change_table :subscriptions, bulk: true do |t|
19
+ # t.bigint :user_id, null: false
20
+ # t.index :user_id
21
+ # t.foreign_key :users, column: :user_id
22
+ # end
23
+ class ChangeTableReferences < Base
24
+ MSG = 'Use a combination of `t.bigint`, `t.index` and `t.foreign_key` in a change_table.'
25
+
26
+ # @!method add_references_in_block?(node)
27
+ def_node_search :add_references_in_block?, <<~PATTERN
28
+ (send lvar /references|belongs_to/ ...)
29
+ PATTERN
30
+
31
+ # @!method change_table?(node)
32
+ def_node_search :change_table?, <<~PATTERN
33
+ (send nil? :change_table ...)
34
+ PATTERN
35
+
36
+ def on_block(node)
37
+ return unless change_table?(node)
38
+
39
+ references_node = node.children.detect { |n| add_references_in_block?(n) }
40
+ return unless references_node
41
+
42
+ arguments = references_node.child_nodes[1]
43
+ references_methods_range = references_node.source_range.with(end_pos: arguments.source_range.begin_pos - 1)
44
+ add_offense(references_methods_range)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Petal
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.1'
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: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Francis Bastien
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-15 00:00:00.000000000 Z
11
+ date: 2023-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.50.0
19
+ version: '1.50'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.50.0
26
+ version: '1.50'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rubocop-performance
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.17.0
33
+ version: '1.17'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.17.0
40
+ version: '1.17'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rubocop-rails
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 2.17.0
61
+ version: '2.17'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 2.17.0
68
+ version: '2.17'
69
69
  description:
70
70
  email:
71
71
  - jfbastien@petalmd.com
@@ -84,13 +84,14 @@ files:
84
84
  - lib/rubocop/cop/grape/helpers_include_module.rb
85
85
  - lib/rubocop/cop/grape/prefer_namespace.rb
86
86
  - lib/rubocop/cop/grape/unnecessary_namespace.rb
87
+ - lib/rubocop/cop/migration/always_bulk_change_table.rb
88
+ - lib/rubocop/cop/migration/change_table_references.rb
87
89
  - lib/rubocop/cop/migration/foreign_key_option.rb
88
90
  - lib/rubocop/cop/migration/schema_statements_methods.rb
89
91
  - lib/rubocop/cop/performance/snif.rb
90
92
  - lib/rubocop/cop/petal_cops.rb
91
93
  - lib/rubocop/cop/rails/enum_prefix.rb
92
94
  - lib/rubocop/cop/rails/risky_activerecord_invocation.rb
93
- - lib/rubocop/cop/rails/table_name.rb
94
95
  - lib/rubocop/cop/rspec/authenticated_as.rb
95
96
  - lib/rubocop/cop/rspec/create_list_max.rb
96
97
  - lib/rubocop/cop/rspec/json_response.rb
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Rails
6
- # This cop enforces the absence of explicit table name definitions.
7
- #
8
- # # bad
9
- # self.table_name = 'some_table_name'
10
- # self.table_name = :some_other_name
11
- class TableName < Base
12
- include ActiveRecordHelper
13
-
14
- MSG = %(
15
- Avoid using `self.table_name=` if at all possible. When ActiveRecord's defaults are used, it may be omitted.
16
- If you are working on a new model, you should name the table and model to fit the defaults
17
- If you absolutely must use it, disable the cop with `# rubocop:disable Rails/TableName`
18
- )
19
-
20
- def on_send(node)
21
- add_offense(node) if find_set_table_name(node).to_a.any?
22
- end
23
- end
24
- end
25
- end
26
- end