From 91e2e3e44b2175bb0240ea3e48fc09d069480c2d Mon Sep 17 00:00:00 2001 From: Shojiro Yanagisawa Date: Mon, 3 Jul 2023 23:00:33 +0900 Subject: [PATCH] Fix create_table with multi t columns for `Rails/SchemaComment` --- ..._when_create_table_with_multi_t_columns.md | 1 + lib/rubocop/cop/rails/schema_comment.rb | 26 ++++++++++++------- spec/rubocop/cop/rails/schema_comment_spec.rb | 22 ++++++++++++++++ 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 changelog/fix_no_offences_for_schema_comment_when_create_table_with_multi_t_columns.md diff --git a/changelog/fix_no_offences_for_schema_comment_when_create_table_with_multi_t_columns.md b/changelog/fix_no_offences_for_schema_comment_when_create_table_with_multi_t_columns.md new file mode 100644 index 0000000000..666df28a8b --- /dev/null +++ b/changelog/fix_no_offences_for_schema_comment_when_create_table_with_multi_t_columns.md @@ -0,0 +1 @@ +* [#1042](https://github.com/rubocop/rubocop-rails/pull/1042): Fix no offences for `Rails/SchemaComment` when create_table with multi t columns. ([@nipe0324][]) diff --git a/lib/rubocop/cop/rails/schema_comment.rb b/lib/rubocop/cop/rails/schema_comment.rb index 0e1c23028d..0e98e2eca9 100644 --- a/lib/rubocop/cop/rails/schema_comment.rb +++ b/lib/rubocop/cop/rails/schema_comment.rb @@ -74,17 +74,25 @@ class SchemaComment < Base def on_send(node) if add_column_without_comment?(node) add_offense(node, message: COLUMN_MSG) - elsif create_table?(node) - if create_table_without_comment?(node) - add_offense(node, message: TABLE_MSG) - elsif create_table_column_call_without_comment?(node) - add_offense(node.parent.body, message: COLUMN_MSG) - end + elsif create_table_without_comment?(node) + add_offense(node, message: TABLE_MSG) + elsif create_table_with_block?(node.parent) + check_column_within_create_table_block(node.parent.body) end end private + def check_column_within_create_table_block(node) + if node.begin_type? + node.child_nodes.each do |child_node| + add_offense(child_node, message: COLUMN_MSG) if t_column_without_comment?(child_node) + end + elsif t_column_without_comment?(node) + add_offense(node, message: COLUMN_MSG) + end + end + def add_column_without_comment?(node) add_column?(node) && !add_column_with_comment?(node) end @@ -93,10 +101,8 @@ def create_table_without_comment?(node) create_table?(node) && !create_table_with_comment?(node) end - def create_table_column_call_without_comment?(node) - create_table_with_block?(node.parent) && - t_column?(node.parent.body) && - !t_column_with_comment?(node.parent.body) + def t_column_without_comment?(node) + t_column?(node) && !t_column_with_comment?(node) end end end diff --git a/spec/rubocop/cop/rails/schema_comment_spec.rb b/spec/rubocop/cop/rails/schema_comment_spec.rb index 8a9565cdfe..e7ee07aecb 100644 --- a/spec/rubocop/cop/rails/schema_comment_spec.rb +++ b/spec/rubocop/cop/rails/schema_comment_spec.rb @@ -87,6 +87,28 @@ RUBY end + it 'registers two offenses when two `t.column` have no `comment` option' do + expect_offense(<<~RUBY) + create_table :users, comment: 'Table' do |t| + t.column :column1, :integer + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ New database column without `comment`. + t.column :column2, :integer + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ New database column without `comment`. + end + RUBY + end + + it 'registers two offenses when two `t.integer` have no `comment` option' do + expect_offense(<<~RUBY) + create_table :users, comment: 'Table' do |t| + t.integer :column1 + ^^^^^^^^^^^^^^^^^^ New database column without `comment`. + t.integer :column2 + ^^^^^^^^^^^^^^^^^^ New database column without `comment`. + end + RUBY + end + it 'does not register an offense when `t.column` has `comment` option' do expect_no_offenses(<<~RUBY) create_table :users, comment: 'Table' do |t|