sevencop 0.22.0 → 0.23.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +0 -16
  4. data/config/default.yml +0 -143
  5. data/lib/sevencop/cop_concerns.rb +0 -3
  6. data/lib/sevencop/version.rb +1 -1
  7. data/lib/sevencop.rb +0 -16
  8. metadata +2 -21
  9. data/lib/rubocop/cop/sevencop/rails_migration_add_check_constraint.rb +0 -111
  10. data/lib/rubocop/cop/sevencop/rails_migration_add_column_with_default_value.rb +0 -229
  11. data/lib/rubocop/cop/sevencop/rails_migration_add_foreign_key.rb +0 -166
  12. data/lib/rubocop/cop/sevencop/rails_migration_add_index_concurrently.rb +0 -164
  13. data/lib/rubocop/cop/sevencop/rails_migration_batch_in_batches.rb +0 -95
  14. data/lib/rubocop/cop/sevencop/rails_migration_batch_in_transaction.rb +0 -83
  15. data/lib/rubocop/cop/sevencop/rails_migration_batch_with_throttling.rb +0 -108
  16. data/lib/rubocop/cop/sevencop/rails_migration_change_column.rb +0 -113
  17. data/lib/rubocop/cop/sevencop/rails_migration_change_column_null.rb +0 -128
  18. data/lib/rubocop/cop/sevencop/rails_migration_create_table_force.rb +0 -89
  19. data/lib/rubocop/cop/sevencop/rails_migration_jsonb.rb +0 -131
  20. data/lib/rubocop/cop/sevencop/rails_migration_remove_column.rb +0 -258
  21. data/lib/rubocop/cop/sevencop/rails_migration_rename_column.rb +0 -81
  22. data/lib/rubocop/cop/sevencop/rails_migration_rename_table.rb +0 -79
  23. data/lib/rubocop/cop/sevencop/rails_migration_reserved_word_mysql.rb +0 -232
  24. data/lib/rubocop/cop/sevencop/rails_migration_unique_index_columns_count.rb +0 -92
  25. data/lib/sevencop/cop_concerns/batch_processing.rb +0 -32
  26. data/lib/sevencop/cop_concerns/column_type_method.rb +0 -26
  27. data/lib/sevencop/cop_concerns/disable_ddl_transaction.rb +0 -49
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2aff4e0e776ef3fa5b3f71ece7b7c83013caadaf829fc86827ab246f5f80382
4
- data.tar.gz: 1889785ce282e4d530c7eef4a54afaa948181b1557e788425e9b30e831935b77
3
+ metadata.gz: 0f1682050c56b200417e16dfd5e9f90898838834b29609ef01548b40ab4d4700
4
+ data.tar.gz: f44e1c9e38ee48b6719e7df278d7858d9f6dc846c3469a96d11975b5a0d572b9
5
5
  SHA512:
6
- metadata.gz: 386a275378516b205cbd1dc4d50226776d106dc98eb8c534973ff7379c39b59f0e49ae4985058f79254d2256a09ac459b6357e1c5d0f03a8e2d780ec8b1fc990
7
- data.tar.gz: 91adea811d280655577fd88cdb4b861b3a9e04f4c89d9542a4ce38281f325cfe0be590cb45ce8937375b4d13e67970518e6381531f46087689e58f3b8d0c5156
6
+ metadata.gz: 5c3fe3df83f0a4aef3503cfe39d8d82262c1dfd3bdd5b8745922a2649991d56c208d9a6ccc8a6b2f87757e7cdb1423e33f97869a39f4172984c15b4941797df9
7
+ data.tar.gz: e9dc4ceaac5717391ba294a3995572e9cce89f3316104d2e2139af30fc994d60a9e13c0ee2eb864566b27b2e6d04ff634f0b714d1d93c0ce1f4d969294bf2da9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.22.0)
4
+ sevencop (0.23.0)
5
5
  activesupport
6
6
  rubocop
7
7
 
data/README.md CHANGED
@@ -38,22 +38,6 @@ Note that all cops are `Enabled: false` by default.
38
38
  - [Sevencop/MethodDefinitionOrdered](lib/rubocop/cop/sevencop/method_definition_ordered.rb)
39
39
  - [Sevencop/RailsBelongsToOptional](lib/rubocop/cop/sevencop/rails_belongs_to_optional.rb)
40
40
  - [Sevencop/RailsInferredSpecType](lib/rubocop/cop/sevencop/rails_inferred_spec_type.rb)
41
- - [Sevencop/RailsMigrationAddCheckConstraint](lib/rubocop/cop/sevencop/rails_migration_add_check_constraint.rb)
42
- - [Sevencop/RailsMigrationAddColumnWithDefaultValue](lib/rubocop/cop/sevencop/rails_migration_add_column_with_default_value.rb)
43
- - [Sevencop/RailsMigrationAddForeignKey](lib/rubocop/cop/sevencop/rails_migration_add_foreign_key.rb)
44
- - [Sevencop/RailsMigrationAddIndexConcurrently](lib/rubocop/cop/sevencop/rails_migration_add_index_concurrently.rb)
45
- - [Sevencop/RailsMigrationBatchInBatches](lib/rubocop/cop/sevencop/rails_migration_batch_in_batches.rb)
46
- - [Sevencop/RailsMigrationBatchInTransaction](lib/rubocop/cop/sevencop/rails_migration_batch_in_transaction.rb)
47
- - [Sevencop/RailsMigrationBatchWithThrottling](lib/rubocop/cop/sevencop/rails_migration_batch_with_throttling.rb)
48
- - [Sevencop/RailsMigrationChangeColumn](lib/rubocop/cop/sevencop/rails_migration_change_column.rb)
49
- - [Sevencop/RailsMigrationChangeColumnNull](lib/rubocop/cop/sevencop/rails_migration_change_column_null.rb)
50
- - [Sevencop/RailsMigrationCreateTableForce](lib/rubocop/cop/sevencop/rails_migration_create_table_force.rb)
51
- - [Sevencop/RailsMigrationJsonb](lib/rubocop/cop/sevencop/rails_migration_jsonb.rb)
52
- - [Sevencop/RailsMigrationRemoveColumn](lib/rubocop/cop/sevencop/rails_migration_remove_column.rb)
53
- - [Sevencop/RailsMigrationRenameColumn](lib/rubocop/cop/sevencop/rails_migration_rename_column.rb)
54
- - [Sevencop/RailsMigrationRenameTable](lib/rubocop/cop/sevencop/rails_migration_rename_table.rb)
55
- - [Sevencop/RailsMigrationReservedWordMysql](lib/rubocop/cop/sevencop/rails_migration_reserved_word_mysql.rb)
56
- - [Sevencop/RailsMigrationUniqueIndexColumnsCount](lib/rubocop/cop/sevencop/rails_migration_unique_index_columns_count.rb)
57
41
  - [Sevencop/RailsOrderField](lib/rubocop/cop/sevencop/rails_order_field.rb)
58
42
  - [Sevencop/RailsUniquenessValidatorExplicitCaseSensitivity](lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb)
59
43
  - [Sevencop/RailsWhereNot](lib/rubocop/cop/sevencop/rails_where_not.rb)
data/config/default.yml CHANGED
@@ -82,149 +82,6 @@ Sevencop/RailsInferredSpecType:
82
82
  Safe: false
83
83
  VersionAdded: '0.9'
84
84
 
85
- Sevencop/RailsMigrationAddCheckConstraint:
86
- Description: |
87
- Activate a check constraint in a separate migration in PostgreSQL.
88
- Enabled: false
89
- Safe: false
90
- VersionAdded: '0.22'
91
- Include:
92
- - db/migrate/**/*.rb
93
-
94
- Sevencop/RailsMigrationAddColumnWithDefaultValue:
95
- Description: |
96
- Add the column without a default value then change the default.
97
- Enabled: false
98
- Safe: false
99
- VersionAdded: '0.22'
100
- Include:
101
- - db/migrate/**/*.rb
102
-
103
- Sevencop/RailsMigrationAddForeignKey:
104
- Description: |
105
- Activate a foreign key validation in a separate migration in PostgreSQL.
106
- Enabled: false
107
- Safe: false
108
- VersionAdded: '0.22'
109
- Include:
110
- - db/migrate/**/*.rb
111
-
112
- Sevencop/RailsMigrationAddIndexConcurrently:
113
- Description: |
114
- Use `algorithm: :concurrently` on adding indexes to existing tables in PostgreSQL.
115
- Enabled: false
116
- Safe: false
117
- VersionAdded: '0.22'
118
- Include:
119
- - db/migrate/**/*.rb
120
-
121
- Sevencop/RailsMigrationBatchInBatches:
122
- Description: |
123
- Use `in_batches` in batch processing.
124
- Enabled: false
125
- Safe: false
126
- VersionAdded: '0.22'
127
- Include:
128
- - db/migrate/**/*.rb
129
-
130
- Sevencop/RailsMigrationBatchInTransaction:
131
- Description: |
132
- Disable transaction in batch processing.
133
- Enabled: false
134
- Safe: false
135
- VersionAdded: '0.22'
136
- Include:
137
- - db/migrate/**/*.rb
138
-
139
- Sevencop/RailsMigrationBatchWithThrottling:
140
- Description: |
141
- Use throttling in batch processing.
142
- Enabled: false
143
- Safe: false
144
- VersionAdded: '0.22'
145
- Include:
146
- - db/migrate/**/*.rb
147
-
148
- Sevencop/RailsMigrationCreateTableForce:
149
- Description: |
150
- Create tables without `force: true` option.
151
- Enabled: false
152
- VersionAdded: '0.22'
153
- Include:
154
- - db/migrate/**/*.rb
155
-
156
- Sevencop/RailsMigrationJsonb:
157
- Description: |
158
- Prefer `jsonb` to `json`.
159
- Enabled: false
160
- Safe: false
161
- VersionAdded: '0.22'
162
- Include:
163
- - db/migrate/**/*.rb
164
-
165
- Sevencop/RailsMigrationChangeColumn:
166
- Description: |
167
- Avoid changing column type that is in use.
168
- Enabled: false
169
- Safe: false
170
- VersionAdded: '0.22'
171
- Include:
172
- - db/migrate/**/*.rb
173
-
174
- Sevencop/RailsMigrationChangeColumnNull:
175
- Description: |
176
- Avoid simply setting `NOT NULL` constraint on an existing column in PostgreSQL.
177
- Enabled: false
178
- Safe: false
179
- VersionAdded: '0.22'
180
- Include:
181
- - db/migrate/**/*.rb
182
-
183
- Sevencop/RailsMigrationRemoveColumn:
184
- Description: |
185
- Add to `ignored_columns` and then remove the column.
186
- Enabled: false
187
- Safe: false
188
- VersionAdded: '0.22'
189
- Include:
190
- - db/migrate/**/*.rb
191
-
192
- Sevencop/RailsMigrationRenameColumn:
193
- Description: |
194
- Avoid renaming columns that are in use.
195
- Enabled: false
196
- Safe: false
197
- VersionAdded: '0.22'
198
- Include:
199
- - db/migrate/**/*.rb
200
-
201
- Sevencop/RailsMigrationRenameTable:
202
- Description: |
203
- Avoid removing tables that are in use.
204
- Enabled: false
205
- Safe: false
206
- VersionAdded: '0.22'
207
- Include:
208
- - db/migrate/**/*.rb
209
-
210
- Sevencop/RailsMigrationReservedWordMysql:
211
- Description: |
212
- Avoid using MySQL reserved words as identifiers.
213
- Enabled: false
214
- Safe: false
215
- VersionAdded: '0.20'
216
- Include:
217
- - db/migrate/**/*.rb
218
-
219
- Sevencop/RailsMigrationUniqueIndexColumnsCount:
220
- Description: |
221
- Keep unique index columns count less than a specified number.
222
- Enabled: false
223
- VersionAdded: '0.22'
224
- MaxColumnsCount: 3
225
- Include:
226
- - db/migrate/**/*.rb
227
-
228
85
  Sevencop/RailsOrderField:
229
86
  Description: |
230
87
  Wrap safe SQL String by `Arel.sql`.
@@ -2,9 +2,6 @@
2
2
 
3
3
  module Sevencop
4
4
  module CopConcerns
5
- autoload :BatchProcessing, 'sevencop/cop_concerns/batch_processing'
6
- autoload :ColumnTypeMethod, 'sevencop/cop_concerns/column_type_method'
7
- autoload :DisableDdlTransaction, 'sevencop/cop_concerns/disable_ddl_transaction'
8
5
  autoload :Ordered, 'sevencop/cop_concerns/ordered'
9
6
  end
10
7
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.22.0'
4
+ VERSION = '0.23.0'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -14,22 +14,6 @@ require_relative 'rubocop/cop/sevencop/method_definition_keyword_argument_ordere
14
14
  require_relative 'rubocop/cop/sevencop/method_definition_ordered'
15
15
  require_relative 'rubocop/cop/sevencop/rails_belongs_to_optional'
16
16
  require_relative 'rubocop/cop/sevencop/rails_inferred_spec_type'
17
- require_relative 'rubocop/cop/sevencop/rails_migration_add_check_constraint'
18
- require_relative 'rubocop/cop/sevencop/rails_migration_add_column_with_default_value'
19
- require_relative 'rubocop/cop/sevencop/rails_migration_add_foreign_key'
20
- require_relative 'rubocop/cop/sevencop/rails_migration_add_index_concurrently'
21
- require_relative 'rubocop/cop/sevencop/rails_migration_batch_in_batches'
22
- require_relative 'rubocop/cop/sevencop/rails_migration_batch_in_transaction'
23
- require_relative 'rubocop/cop/sevencop/rails_migration_batch_with_throttling'
24
- require_relative 'rubocop/cop/sevencop/rails_migration_change_column'
25
- require_relative 'rubocop/cop/sevencop/rails_migration_change_column_null'
26
- require_relative 'rubocop/cop/sevencop/rails_migration_create_table_force'
27
- require_relative 'rubocop/cop/sevencop/rails_migration_jsonb'
28
- require_relative 'rubocop/cop/sevencop/rails_migration_remove_column'
29
- require_relative 'rubocop/cop/sevencop/rails_migration_rename_column'
30
- require_relative 'rubocop/cop/sevencop/rails_migration_rename_table'
31
- require_relative 'rubocop/cop/sevencop/rails_migration_reserved_word_mysql'
32
- require_relative 'rubocop/cop/sevencop/rails_migration_unique_index_columns_count'
33
17
  require_relative 'rubocop/cop/sevencop/rails_order_field'
34
18
  require_relative 'rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity'
35
19
  require_relative 'rubocop/cop/sevencop/rails_where_not'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sevencop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.0
4
+ version: 0.23.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-16 00:00:00.000000000 Z
11
+ date: 2022-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,22 +66,6 @@ files:
66
66
  - lib/rubocop/cop/sevencop/method_definition_ordered.rb
67
67
  - lib/rubocop/cop/sevencop/rails_belongs_to_optional.rb
68
68
  - lib/rubocop/cop/sevencop/rails_inferred_spec_type.rb
69
- - lib/rubocop/cop/sevencop/rails_migration_add_check_constraint.rb
70
- - lib/rubocop/cop/sevencop/rails_migration_add_column_with_default_value.rb
71
- - lib/rubocop/cop/sevencop/rails_migration_add_foreign_key.rb
72
- - lib/rubocop/cop/sevencop/rails_migration_add_index_concurrently.rb
73
- - lib/rubocop/cop/sevencop/rails_migration_batch_in_batches.rb
74
- - lib/rubocop/cop/sevencop/rails_migration_batch_in_transaction.rb
75
- - lib/rubocop/cop/sevencop/rails_migration_batch_with_throttling.rb
76
- - lib/rubocop/cop/sevencop/rails_migration_change_column.rb
77
- - lib/rubocop/cop/sevencop/rails_migration_change_column_null.rb
78
- - lib/rubocop/cop/sevencop/rails_migration_create_table_force.rb
79
- - lib/rubocop/cop/sevencop/rails_migration_jsonb.rb
80
- - lib/rubocop/cop/sevencop/rails_migration_remove_column.rb
81
- - lib/rubocop/cop/sevencop/rails_migration_rename_column.rb
82
- - lib/rubocop/cop/sevencop/rails_migration_rename_table.rb
83
- - lib/rubocop/cop/sevencop/rails_migration_reserved_word_mysql.rb
84
- - lib/rubocop/cop/sevencop/rails_migration_unique_index_columns_count.rb
85
69
  - lib/rubocop/cop/sevencop/rails_order_field.rb
86
70
  - lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb
87
71
  - lib/rubocop/cop/sevencop/rails_where_not.rb
@@ -91,9 +75,6 @@ files:
91
75
  - lib/sevencop.rb
92
76
  - lib/sevencop/config_loader.rb
93
77
  - lib/sevencop/cop_concerns.rb
94
- - lib/sevencop/cop_concerns/batch_processing.rb
95
- - lib/sevencop/cop_concerns/column_type_method.rb
96
- - lib/sevencop/cop_concerns/disable_ddl_transaction.rb
97
78
  - lib/sevencop/cop_concerns/ordered.rb
98
79
  - lib/sevencop/rubocop_extension.rb
99
80
  - lib/sevencop/version.rb
@@ -1,111 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Sevencop
6
- # Activate a check constraint in a separate migration in PostgreSQL.
7
- #
8
- # Adding a check constraint without `NOT VALID` blocks reads and writes in Postgres and
9
- # blocks writes in MySQL and MariaDB while every row is checked.
10
- #
11
- # @safety
12
- # Only meaningful in PostgreSQL.
13
- #
14
- # @example
15
- # # bad
16
- # class AddCheckConstraintToOrdersPrice < ActiveRecord::Migration[7.0]
17
- # def change
18
- # add_check_constraint :orders, 'price > 0', name: 'orders_price_positive'
19
- # end
20
- # end
21
- #
22
- # # good
23
- # class AddCheckConstraintToOrdersPriceWithoutValidation < ActiveRecord::Migration[7.0]
24
- # def change
25
- # add_check_constraint :orders, 'price > 0', name: 'orders_price_positive', validate: false
26
- # end
27
- # end
28
- #
29
- # class ActivateCheckConstraintOnOrdersPrice < ActiveRecord::Migration[7.0]
30
- # def change
31
- # validate_check_constraint :orders, name: 'orders_price_positive'
32
- # end
33
- # end
34
- class RailsMigrationAddCheckConstraint < RuboCop::Cop::Base
35
- extend AutoCorrector
36
-
37
- MSG = 'Activate a check constraint in a separate migration in PostgreSQL.'
38
-
39
- RESTRICT_ON_SEND = %i[
40
- add_check_constraint
41
- ].freeze
42
-
43
- # @param node [RuboCop::AST::SendNode]
44
- # @return [void]
45
- def on_send(node)
46
- return unless bad?(node)
47
-
48
- add_offense(node) do |corrector|
49
- autocorrect(corrector, node)
50
- end
51
- end
52
-
53
- private
54
-
55
- # @!method add_check_constraint?(node)
56
- # @param node [RuboCop::AST::SendNode]
57
- # @return [Boolean]
58
- def_node_matcher :add_check_constraint?, <<~PATTERN
59
- (send
60
- nil?
61
- :add_check_constraint
62
- ...
63
- )
64
- PATTERN
65
-
66
- # @!method add_check_constraint_with_validate_false?(node)
67
- # @param node [RuboCop::AST::SendNode]
68
- # @return [Boolean]
69
- def_node_matcher :add_check_constraint_with_validate_false?, <<~PATTERN
70
- (send
71
- nil?
72
- :add_check_constraint
73
- _
74
- _
75
- (hash
76
- <
77
- (pair
78
- (sym :validate)
79
- false
80
- )
81
- ...
82
- >
83
- )
84
- )
85
- PATTERN
86
-
87
- # @param corrector [RuboCop::Cop::Corrector]
88
- # @param node [RuboCop::AST::SendNode]
89
- # @return [void]
90
- def autocorrect(
91
- corrector,
92
- node
93
- )
94
- target = node.last_argument
95
- target = target.pairs.last if target.hash_type?
96
- corrector.insert_after(
97
- target,
98
- ', validate: false'
99
- )
100
- end
101
-
102
- # @param node [RuboCop::AST::SendNode]
103
- # @return [Boolean]
104
- def bad?(node)
105
- add_check_constraint?(node) &&
106
- !add_check_constraint_with_validate_false?(node)
107
- end
108
- end
109
- end
110
- end
111
- end
@@ -1,229 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Sevencop
6
- # Add the column without a default value then change the default.
7
- #
8
- # In earlier versions of Postgres, MySQL, and MariaDB,
9
- # adding a column with a default value to an existing table causes the entire table to be rewritten.
10
- # During this time, reads and writes are blocked in Postgres, and writes are blocked in MySQL and MariaDB.
11
- #
12
- # @safety
13
- # Only meaningful in earlier versions of Postgres, MySQL, and MariaDB.
14
- #
15
- # @example
16
- # # bad
17
- # class AddSomeColumnToUsers < ActiveRecord::Migration[7.0]
18
- # def change
19
- # add_column :users, :some_column, :string, default: 'some value'
20
- # end
21
- # end
22
- #
23
- # # good
24
- # class AddSomeColumnToUsers < ActiveRecord::Migration[7.0]
25
- # def change
26
- # add_column :users, :some_column, :string
27
- # change_column_default :users, :some_column, 'some value'
28
- # end
29
- # end
30
- class RailsMigrationAddColumnWithDefaultValue < RuboCop::Cop::Base
31
- extend AutoCorrector
32
-
33
- include RangeHelp
34
- include ::Sevencop::CopConcerns::ColumnTypeMethod
35
-
36
- MSG = 'Add the column without a default value then change the default.'
37
-
38
- RESTRICT_ON_SEND = [
39
- :add_column,
40
- *COLUMN_TYPE_METHOD_NAMES
41
- ].freeze
42
-
43
- # @param node [RuboCop::AST::SendNode]
44
- # @return [void]
45
- def on_send(node)
46
- return unless target_method?(node)
47
-
48
- default_option_node = non_nil_default_option_node_from(node)
49
- return unless default_option_node
50
-
51
- add_offense(default_option_node) do |corrector|
52
- autocorrect(
53
- corrector,
54
- default_option_node: default_option_node,
55
- send_node: node
56
- )
57
- end
58
- end
59
-
60
- private
61
-
62
- # @!method add_column?(node)
63
- # @param node [RuboCop::AST::SendNode]
64
- # @return [Boolean]
65
- def_node_matcher :add_column?, <<~PATTERN
66
- (send
67
- nil?
68
- :add_column
69
- ...
70
- )
71
- PATTERN
72
-
73
- # @!method column_type_method?(node)
74
- # @param node [RuboCop::AST::SendNode]
75
- # @return [Boolean]
76
- def_node_matcher :column_type_method?, <<~PATTERN
77
- (send
78
- lvar
79
- COLUMN_TYPE_METHOD_NAMES
80
- ...
81
- )
82
- PATTERN
83
-
84
- # @!method non_nil_default_option_node_from(node)
85
- # @param node [RuboCop::AST::SendNode]
86
- # @return [RuboCop::AST::PairNode, nil]
87
- def_node_matcher :non_nil_default_option_node_from, <<~PATTERN
88
- (send
89
- _
90
- _
91
- ...
92
- (hash
93
- <
94
- $(pair
95
- (sym :default)
96
- !nil
97
- )
98
- >
99
- ...
100
- )
101
- )
102
- PATTERN
103
-
104
- # @param corrector [RuboCop::Cop::Corrector]
105
- # @param default_option_node [RuboCop::AST::PairNode]
106
- # @param send_node [RuboCop::AST::SendNode]
107
- # @return [void]
108
- def autocorrect(
109
- corrector,
110
- default_option_node:,
111
- send_node:
112
- )
113
- remove_pair(
114
- corrector,
115
- default_option_node
116
- )
117
- insert_change_column_default(
118
- corrector,
119
- default_option_node: default_option_node,
120
- send_node: send_node
121
- )
122
- end
123
-
124
- # @param node [RuboCop::AST::SendNode]
125
- # @return [RuboCop::AST::SymNode]
126
- def find_column_node_from(node)
127
- case node.method_name
128
- when :add_column
129
- node.arguments[1]
130
- else
131
- node.first_argument
132
- end
133
- end
134
-
135
- # @param node [RuboCop::AST::SendNode]
136
- # @return [RuboCop::AST::SendNode]
137
- def find_insertion_target_node_from(node)
138
- case node.method_name
139
- when :add_column
140
- node
141
- else
142
- node.each_ancestor(:block).first
143
- end
144
- end
145
-
146
- # @param node [RuboCop::AST::SendNode]
147
- # @return [RuboCop::AST::SymNode]
148
- def find_table_node_from(node)
149
- case node.method_name
150
- when :add_column
151
- node.first_argument
152
- else
153
- node.each_ancestor(:block).first.send_node.first_argument
154
- end
155
- end
156
-
157
- # @param node [RuboCop::AST::SendNode]
158
- # @return [Boolean]
159
- def in_change_table?(node)
160
- node.each_ancestor(:block).first&.method?(:change_table)
161
- end
162
-
163
- # @param corrector [RuboCop::Cop::Corrector]
164
- # @param node [RuboCop::AST::Node]
165
- # @param string [String]
166
- def insert_after_with_same_indentation(
167
- corrector,
168
- node,
169
- string
170
- )
171
- corrector.insert_after(
172
- node,
173
- format(
174
- "\n%<indentation>s%<string>s",
175
- indentation: ' ' * node.location.column,
176
- string: string
177
- )
178
- )
179
- end
180
-
181
- # @param corrector [RuboCop::Cop::Corrector]
182
- # @param default_option_node [RuboCop::AST::PairNode]
183
- # @param send_node [RuboCop::AST::SendNode]
184
- # @return [void]
185
- def insert_change_column_default(
186
- corrector,
187
- default_option_node:,
188
- send_node:
189
- )
190
- insert_after_with_same_indentation(
191
- corrector,
192
- find_insertion_target_node_from(send_node),
193
- format(
194
- 'change_column_default %<table>s, %<column>s, %<default>s',
195
- column: find_column_node_from(send_node).source,
196
- default: default_option_node.value.source,
197
- table: find_table_node_from(send_node).source
198
- )
199
- )
200
- end
201
-
202
- # @param corrector [RuboCop::Cop::Corrector]
203
- # @param node [RuboCop::AST::Node]
204
- # @return [void]
205
- def remove_pair(
206
- corrector,
207
- node
208
- )
209
- corrector.remove(
210
- range_with_surrounding_comma(
211
- range_with_surrounding_space(
212
- node.location.expression,
213
- side: :left
214
- ),
215
- :left
216
- )
217
- )
218
- end
219
-
220
- # @param node [RuboCop::AST::SendNode]
221
- # @return [Boolean]
222
- def target_method?(node)
223
- add_column?(node) ||
224
- (column_type_method?(node) && in_change_table?(node))
225
- end
226
- end
227
- end
228
- end
229
- end