-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Rails/AddColumnIndex
cop to prevent passing an unused but confu…
…sing `index:` key to `add_column` migrations.
- Loading branch information
1 parent
cf1ba4d
commit c0d827e
Showing
7 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks for migrations using `add_column` that have an `index` | ||
# key. `add_column` does not accept `index`, but also does not raise an | ||
# error for extra keys, so it is possible to mistakenly add the key without | ||
# realizing it will not actually add an index. | ||
# | ||
# @example | ||
# # bad (will not add an index) | ||
# add_column :table, :column, :integer, index: true | ||
# | ||
# # good | ||
# add_column :table, :column, :integer | ||
# add_index :table, :column | ||
# | ||
class AddColumnIndex < Base | ||
extend AutoCorrector | ||
include RangeHelp | ||
|
||
MSG = '`add_column` does not accept an `index` key, use `add_index` instead.' | ||
RESTRICT_ON_SEND = %i[add_column].freeze | ||
|
||
def_node_matcher :add_column_with_index, <<~PATTERN | ||
( | ||
send nil? :add_column $_table $_column | ||
<(hash <$(pair {(sym :index) (str "index")} $_) ...>) ...> | ||
) | ||
PATTERN | ||
|
||
def on_send(node) | ||
table, column, pair, value = add_column_with_index(node) | ||
return unless pair | ||
|
||
add_offense(pair) do |corrector| | ||
corrector.remove(index_range(pair)) | ||
|
||
add_index = "add_index #{table.source}, #{column.source}" | ||
add_index_opts = '' | ||
|
||
if value.hash_type? | ||
hash = value.loc.expression.adjust(begin_pos: 1, end_pos: -1).source.strip | ||
add_index_opts = ", #{hash}" | ||
end | ||
|
||
corrector.insert_after(node, "\n#{add_index}#{add_index_opts}") | ||
end | ||
end | ||
|
||
private | ||
|
||
def index_range(pair_node) | ||
range_with_surrounding_comma( | ||
range_with_surrounding_space(range: pair_node.loc.expression, side: :left), | ||
:left | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::AddColumnIndex, :config do | ||
it 'registers an offense and corrects when an `add_column` call has `index: true`' do | ||
expect_offense(<<~RUBY) | ||
add_column :table, :column, :integer, default: 0, index: true | ||
^^^^^^^^^^^ `add_column` does not accept an `index` key, use `add_index` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
add_column :table, :column, :integer, default: 0 | ||
add_index :table, :column | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when an `add_column` call has `index:` with a hash' do | ||
expect_offense(<<~RUBY) | ||
add_column :table, :column, :integer, default: 0, index: { unique: true, name: 'my_unique_index' } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `add_column` does not accept an `index` key, use `add_index` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
add_column :table, :column, :integer, default: 0 | ||
add_index :table, :column, unique: true, name: 'my_unique_index' | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects with there is another hash key after `index`' do | ||
expect_offense(<<~RUBY) | ||
add_column :table, :column, :integer, index: true, default: 0 | ||
^^^^^^^^^^^ `add_column` does not accept an `index` key, use `add_index` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
add_column :table, :column, :integer, default: 0 | ||
add_index :table, :column | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects with string keys' do | ||
expect_offense(<<~RUBY) | ||
add_column :table, :column, :integer, 'index' => true, default: 0 | ||
^^^^^^^^^^^^^^^ `add_column` does not accept an `index` key, use `add_index` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
add_column :table, :column, :integer, default: 0 | ||
add_index :table, :column | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when on multiple lines' do | ||
expect_offense(<<~RUBY) | ||
add_column :table, :column, :integer, | ||
index: true, | ||
^^^^^^^^^^^ `add_column` does not accept an `index` key, use `add_index` instead. | ||
default: 0 | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
add_column :table, :column, :integer, | ||
default: 0 | ||
add_index :table, :column | ||
RUBY | ||
end | ||
|
||
it 'can correct multiple `add_column` calls' do | ||
expect_offense(<<~RUBY) | ||
add_column :table, :column, :integer, default: 0, index: true | ||
^^^^^^^^^^^ `add_column` does not accept an `index` key, use `add_index` instead. | ||
add_column :table, :column2, :integer, default: 0, index: true | ||
^^^^^^^^^^^ `add_column` does not accept an `index` key, use `add_index` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
add_column :table, :column, :integer, default: 0 | ||
add_index :table, :column | ||
add_column :table, :column2, :integer, default: 0 | ||
add_index :table, :column2 | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense without an `index` key to `add_column`' do | ||
expect_no_offenses(<<~RUBY) | ||
add_column :table, :column, :integer, default: 0 | ||
RUBY | ||
end | ||
end |