diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index c5bb9e23984e..c77522027a4b 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -39,7 +39,7 @@ output by `rubocop -V`, include them as well. Here's an example: ``` $ [bundle exec] rubocop -V -1.75.3 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 3.3, running on ruby 3.3.5) [x86_64-linux] +1.75.4 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 3.3, running on ruby 3.3.5) [x86_64-linux] - rubocop-performance 1.22.1 - rubocop-rspec 3.1.0 ``` diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c4215cb3f38..ad512957ccd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,24 @@ ## master (unreleased) +## 1.75.4 (2025-04-28) + +### Bug fixes + +* [#14123](https://github.com/rubocop/rubocop/issues/14123): Fix an infinite loop error for `Lint/BooleanSymbol` when using the rocket hash syntax with a boolean symbol key. ([@koic][]) +* [#14134](https://github.com/rubocop/rubocop/pull/14134): Fix an error for `Style/ComparableBetween` when comparing the value with itself. ([@earlopain][]) +* [#14111](https://github.com/rubocop/rubocop/issues/14111): Fix an error for `Style/SafeNavigation` when the RHS of `&&` is a complex `||` expression composed of `&&` conditions. ([@koic][]) +* [#14129](https://github.com/rubocop/rubocop/issues/14129): Fix false positives for `Style/ArgumentsForwarding` when using default positional arg, keyword arg, and block arg in Ruby 3.1. ([@koic][]) +* [#14110](https://github.com/rubocop/rubocop/pull/14110): Fix false positives for `Style/RedundantParentheses` when parens around basic conditional as the second argument of a parenthesized method call. ([@koic][]) +* [#14120](https://github.com/rubocop/rubocop/issues/14120): Fix false positives for `Style/RedundantParentheses` when parens around unparenthesized method call as the second argument of a parenthesized method call. ([@koic][]) +* [#14133](https://github.com/rubocop/rubocop/pull/14133): Fix `Lint/LiteralAsCondition` autocorrect when a literal is the condition of an elsif followed by an else. ([@zopolis4][]) +* [#14116](https://github.com/rubocop/rubocop/issues/14116): Make `Style/TrailingCommaInArguments` cop aware of trailing commas in `[]` method call. ([@viralpraxis][]) +* [#14114](https://github.com/rubocop/rubocop/pull/14114): Fix `Style/ClassAndModuleChildren` cop error on tab-intended compactable modules. ([@viralpraxis][]) + +### Changes + +* [#13611](https://github.com/rubocop/rubocop/issues/13611): Enable `Lint/CircularArgumentReference` on Ruby 3.4. ([@earlopain][]) + ## 1.75.3 (2025-04-22) ### Bug fixes @@ -208,7 +226,7 @@ ### Changes -* [#13839](https://github.com/rubocop/rubocop/pull/13839): Extension plugin is loaded automatically with `require 'rubocop/rspec/support'. ([@koic][]) +* [#13840](https://github.com/rubocop/rubocop/pull/13840): Extension plugin is loaded automatically with `require 'rubocop/rspec/support'. ([@koic][]) ## 1.72.0 (2025-02-14) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d528b54239e4..7aacf0dd4b68 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -17,7 +17,7 @@ do so. ```console $ rubocop -V -1.75.3 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 3.3, running on ruby 3.3.5) [x86_64-linux] +1.75.4 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 3.3, running on ruby 3.3.5) [x86_64-linux] - rubocop-performance 1.22.1 - rubocop-rspec 3.1.0 ``` diff --git a/docs/modules/ROOT/pages/cops_lint.adoc b/docs/modules/ROOT/pages/cops_lint.adoc index 50e5a03510f5..80e7bc59dd78 100644 --- a/docs/modules/ROOT/pages/cops_lint.adoc +++ b/docs/modules/ROOT/pages/cops_lint.adoc @@ -612,8 +612,6 @@ false [#lintcircularargumentreference] == Lint/CircularArgumentReference -NOTE: Requires Ruby version <= 2.6 - |=== | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed @@ -627,9 +625,8 @@ NOTE: Requires Ruby version <= 2.6 Checks for circular argument references in optional keyword arguments and optional ordinal arguments. -This cop mirrors a warning produced by MRI since 2.2. - -NOTE: This syntax is no longer valid on Ruby 2.7 or higher. +NOTE: This syntax was made invalid on Ruby 2.7 - Ruby 3.3 but is allowed +again since Ruby 3.4. [#examples-lintcircularargumentreference] === Examples diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index d7b284ea0a56..494f7632a4be 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -17935,9 +17935,15 @@ method( # bad method(1, 2,) +# bad +object[1, 2,] + # good method(1, 2) +# good +object[1, 2] + # good method( 1, diff --git a/docs/modules/ROOT/pages/integration_with_other_tools.adoc b/docs/modules/ROOT/pages/integration_with_other_tools.adoc index 60ff32830d06..6454843bbbce 100644 --- a/docs/modules/ROOT/pages/integration_with_other_tools.adoc +++ b/docs/modules/ROOT/pages/integration_with_other_tools.adoc @@ -125,7 +125,7 @@ following to your `.pre-commit-config.yaml` file: [source,yaml] ---- - repo: https://github.com/rubocop/rubocop - rev: v1.75.3 + rev: v1.75.4 hooks: - id: rubocop ---- @@ -136,7 +136,7 @@ entries in `additional_dependencies`: [source,yaml] ---- - repo: https://github.com/rubocop/rubocop - rev: v1.75.3 + rev: v1.75.4 hooks: - id: rubocop additional_dependencies: diff --git a/lib/rubocop/cop/lint/boolean_symbol.rb b/lib/rubocop/cop/lint/boolean_symbol.rb index d6d4a8190e3b..94c593114426 100644 --- a/lib/rubocop/cop/lint/boolean_symbol.rb +++ b/lib/rubocop/cop/lint/boolean_symbol.rb @@ -48,7 +48,7 @@ def on_sym(node) def autocorrect(corrector, node) boolean_literal = node.source.delete(':') parent = node.parent - if parent&.pair_type? && node.equal?(parent.children[0]) + if parent&.pair_type? && parent.colon? && node.equal?(parent.children[0]) corrector.remove(parent.loc.operator) boolean_literal = "#{node.source} =>" end diff --git a/lib/rubocop/cop/lint/circular_argument_reference.rb b/lib/rubocop/cop/lint/circular_argument_reference.rb index 6566cd6b1cc5..9ba4b1ae3982 100644 --- a/lib/rubocop/cop/lint/circular_argument_reference.rb +++ b/lib/rubocop/cop/lint/circular_argument_reference.rb @@ -6,9 +6,8 @@ module Lint # Checks for circular argument references in optional keyword # arguments and optional ordinal arguments. # - # This cop mirrors a warning produced by MRI since 2.2. - # - # NOTE: This syntax is no longer valid on Ruby 2.7 or higher. + # NOTE: This syntax was made invalid on Ruby 2.7 - Ruby 3.3 but is allowed + # again since Ruby 3.4. # # @example # @@ -41,8 +40,6 @@ class CircularArgumentReference < Base MSG = 'Circular argument reference - `%s`.' - maximum_target_ruby_version 2.6 - def on_kwoptarg(node) check_for_circular_argument_references(*node) end diff --git a/lib/rubocop/cop/lint/literal_as_condition.rb b/lib/rubocop/cop/lint/literal_as_condition.rb index c1219740fb0d..85b8d14101ea 100644 --- a/lib/rubocop/cop/lint/literal_as_condition.rb +++ b/lib/rubocop/cop/lint/literal_as_condition.rb @@ -57,13 +57,9 @@ def on_and(node) def on_if(node) cond = condition(node) - if node.unless? - correct_if_node(node, cond, true) if cond.falsey_literal? - correct_if_node(node, cond, false) if cond.truthy_literal? - else - correct_if_node(node, cond, true) if cond.truthy_literal? - correct_if_node(node, cond, false) if cond.falsey_literal? - end + return unless cond.falsey_literal? || cond.truthy_literal? + + correct_if_node(node, cond) end def on_while(node) @@ -232,9 +228,27 @@ def when_conditions_range(when_node) ) end - # rubocop:disable Metrics/AbcSize, Metrics/MethodLength - def correct_if_node(node, cond, result) - if result + def condition_evaluation(node, cond) + if node.unless? + cond.falsey_literal? + else + cond.truthy_literal? + end + end + + # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity + def correct_if_node(node, cond) + result = condition_evaluation(node, cond) + + if node.elsif? && result + add_offense(cond) do |corrector| + corrector.replace(node, "else\n #{node.if_branch.source}") + end + elsif node.elsif? && !result + add_offense(cond) do |corrector| + corrector.replace(node, "else\n #{node.else_branch.source}") + end + elsif result add_offense(cond) do |corrector| corrector.replace(node, node.if_branch.source) end @@ -252,7 +266,7 @@ def correct_if_node(node, cond, result) end end end - # rubocop:enable Metrics/AbcSize, Metrics/MethodLength + # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity end end end diff --git a/lib/rubocop/cop/mixin/trailing_comma.rb b/lib/rubocop/cop/mixin/trailing_comma.rb index b6b481cd13c9..e8d4e958c183 100644 --- a/lib/rubocop/cop/mixin/trailing_comma.rb +++ b/lib/rubocop/cop/mixin/trailing_comma.rb @@ -107,7 +107,7 @@ def method_name_and_arguments_on_same_line?(node) # of the argument is not considered multiline, even if the argument # itself might span multiple lines. def allowed_multiline_argument?(node) - elements(node).one? && !Util.begins_its_line?(node.loc.end) + elements(node).one? && (!node.loc.end || !Util.begins_its_line?(node.loc.end)) end def elements(node) diff --git a/lib/rubocop/cop/style/arguments_forwarding.rb b/lib/rubocop/cop/style/arguments_forwarding.rb index 0c2267ad833a..30f560a02e25 100644 --- a/lib/rubocop/cop/style/arguments_forwarding.rb +++ b/lib/rubocop/cop/style/arguments_forwarding.rb @@ -146,7 +146,7 @@ class ArgumentsForwarding < Base minimum_target_ruby_version 2.7 FORWARDING_LVAR_TYPES = %i[splat kwsplat block_pass].freeze - ADDITIONAL_ARG_TYPES = %i[lvar arg].freeze + ADDITIONAL_ARG_TYPES = %i[lvar arg optarg].freeze FORWARDING_MSG = 'Use shorthand syntax `...` for arguments forwarding.' ARGS_MSG = 'Use anonymous positional arguments forwarding (`*`).' diff --git a/lib/rubocop/cop/style/class_and_module_children.rb b/lib/rubocop/cop/style/class_and_module_children.rb index 1a3ad08ba2f6..ce38dff70869 100644 --- a/lib/rubocop/cop/style/class_and_module_children.rb +++ b/lib/rubocop/cop/style/class_and_module_children.rb @@ -149,9 +149,9 @@ def unindent(corrector, node) return unless node.body.children.last last_child_leading_spaces = leading_spaces(node.body.children.last) - return if leading_spaces(node).size == last_child_leading_spaces.size + return if spaces_size(leading_spaces(node)) == spaces_size(last_child_leading_spaces) - column_delta = configured_indentation_width - last_child_leading_spaces.size + column_delta = configured_indentation_width - spaces_size(last_child_leading_spaces) return if column_delta.zero? AlignmentCorrector.correct(corrector, processed_source, node, column_delta) @@ -161,6 +161,16 @@ def leading_spaces(node) node.source_range.source_line[/\A\s*/] end + def spaces_size(spaces_string) + mapping = { "\t" => tab_indentation_width } + spaces_string.chars.sum { |character| mapping.fetch(character, 1) } + end + + def tab_indentation_width + config.for_cop('Layout/IndentationStyle')['IndentationWidth'] || + configured_indentation_width + end + def check_style(node, body, style) return if node.identifier.namespace&.cbase_type? diff --git a/lib/rubocop/cop/style/comparable_between.rb b/lib/rubocop/cop/style/comparable_between.rb index 846f0976cfdc..4e6c956593c4 100644 --- a/lib/rubocop/cop/style/comparable_between.rb +++ b/lib/rubocop/cop/style/comparable_between.rb @@ -61,8 +61,8 @@ def on_and(node) def register_offense(node, min_and_value, max_and_value) value = (min_and_value & max_and_value).first - min = min_and_value.find { _1 != value } - max = max_and_value.find { _1 != value } + min = min_and_value.find { _1 != value } || value + max = max_and_value.find { _1 != value } || value prefer = "#{value.source}.between?(#{min.source}, #{max.source})" add_offense(node, message: format(MSG, prefer: prefer)) do |corrector| diff --git a/lib/rubocop/cop/style/redundant_parentheses.rb b/lib/rubocop/cop/style/redundant_parentheses.rb index 4e49aa7c4ec5..b5bbcc2fe2ed 100644 --- a/lib/rubocop/cop/style/redundant_parentheses.rb +++ b/lib/rubocop/cop/style/redundant_parentheses.rb @@ -180,10 +180,18 @@ def find_offense_message(begin_node, node) # @!method interpolation?(node) def_node_matcher :interpolation?, '[^begin ^^dstr]' - def argument_of_parenthesized_method_call?(node) - return false unless (parent = node.parent) + def argument_of_parenthesized_method_call?(begin_node) + node = begin_node.children.first + return false if node.basic_conditional? || method_call_parentheses_required?(node) + return false unless (parent = begin_node.parent) + + parent.call_type? && parent.parenthesized? && parent.receiver != begin_node + end + + def method_call_parentheses_required?(node) + return false unless node.call_type? - parent.call_type? && parent.parenthesized? && parent.receiver != node + (node.receiver.nil? || node.loc.dot) && node.arguments.any? end def allow_in_multiline_conditions? diff --git a/lib/rubocop/cop/style/safe_navigation.rb b/lib/rubocop/cop/style/safe_navigation.rb index 81f4edef09b0..86371d8d0f18 100644 --- a/lib/rubocop/cop/style/safe_navigation.rb +++ b/lib/rubocop/cop/style/safe_navigation.rb @@ -321,7 +321,7 @@ def matching_nodes?(left, right) end def matching_call_nodes?(left, right) - return false unless left && right + return false unless left && right.respond_to?(:call_type?) left.call_type? && right.call_type? && left.children == right.children end diff --git a/lib/rubocop/cop/style/trailing_comma_in_arguments.rb b/lib/rubocop/cop/style/trailing_comma_in_arguments.rb index b5368e20999a..cf65be5cf84f 100644 --- a/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +++ b/lib/rubocop/cop/style/trailing_comma_in_arguments.rb @@ -79,10 +79,16 @@ module Style # # bad # method(1, 2,) # + # # bad + # object[1, 2,] + # # # good # method(1, 2) # # # good + # object[1, 2] + # + # # good # method( # 1, # 2 @@ -96,7 +102,7 @@ def self.autocorrect_incompatible_with end def on_send(node) - return unless node.arguments? && node.parenthesized? + return unless node.arguments? && (node.parenthesized? || node.method?(:[])) check(node, node.arguments, 'parameter of %
s method call', node.last_argument.source_range.end_pos, diff --git a/lib/rubocop/rspec/cop_helper.rb b/lib/rubocop/rspec/cop_helper.rb index 241041c907a8..b2596c675eb1 100644 --- a/lib/rubocop/rspec/cop_helper.rb +++ b/lib/rubocop/rspec/cop_helper.rb @@ -11,8 +11,8 @@ module CopHelper ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : RuboCop::TargetRuby::DEFAULT_VERSION end let(:parser_engine) do - # The maximum version Parser can parse is 3.4. - ruby_version >= 3.5 ? :parser_prism : ENV.fetch('PARSER_ENGINE', :parser_whitequark).to_sym + # The maximum version Parser can correctly parse is 3.3. + ruby_version >= 3.4 ? :parser_prism : ENV.fetch('PARSER_ENGINE', :parser_whitequark).to_sym end let(:rails_version) { false } diff --git a/lib/rubocop/rspec/shared_contexts.rb b/lib/rubocop/rspec/shared_contexts.rb index ad2c59a74250..2afe134d523e 100644 --- a/lib/rubocop/rspec/shared_contexts.rb +++ b/lib/rubocop/rspec/shared_contexts.rb @@ -267,6 +267,5 @@ def source_range(range, buffer: source_buffer) end RSpec.shared_context 'ruby 3.5' do - # Parser supports parsing Ruby <= 3.4. - let(:ruby_version) { ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.5 : 3.4 } + let(:ruby_version) { 3.5 } end diff --git a/lib/rubocop/version.rb b/lib/rubocop/version.rb index 9b714d9e1ad1..45d1196ab462 100644 --- a/lib/rubocop/version.rb +++ b/lib/rubocop/version.rb @@ -3,7 +3,7 @@ module RuboCop # This module holds the RuboCop version information. module Version - STRING = '1.75.3' + STRING = '1.75.4' MSG = '%s (using %s, ' \ 'rubocop-ast %s, ' \ diff --git a/relnotes/v1.72.1.md b/relnotes/v1.72.1.md index 69e669945531..f498aae1ff5f 100644 --- a/relnotes/v1.72.1.md +++ b/relnotes/v1.72.1.md @@ -5,6 +5,6 @@ ### Changes -* [#13839](https://github.com/rubocop/rubocop/pull/13839): Extension plugin is loaded automatically with `require 'rubocop/rspec/support'. ([@koic][]) +* [#13840](https://github.com/rubocop/rubocop/pull/13840): Extension plugin is loaded automatically with `require 'rubocop/rspec/support'. ([@koic][]) [@koic]: https://github.com/koic diff --git a/relnotes/v1.75.4.md b/relnotes/v1.75.4.md new file mode 100644 index 000000000000..5fa1ce6eec12 --- /dev/null +++ b/relnotes/v1.75.4.md @@ -0,0 +1,20 @@ +### Bug fixes + +* [#14123](https://github.com/rubocop/rubocop/issues/14123): Fix an infinite loop error for `Lint/BooleanSymbol` when using the rocket hash syntax with a boolean symbol key. ([@koic][]) +* [#14134](https://github.com/rubocop/rubocop/pull/14134): Fix an error for `Style/ComparableBetween` when comparing the value with itself. ([@earlopain][]) +* [#14111](https://github.com/rubocop/rubocop/issues/14111): Fix an error for `Style/SafeNavigation` when the RHS of `&&` is a complex `||` expression composed of `&&` conditions. ([@koic][]) +* [#14129](https://github.com/rubocop/rubocop/issues/14129): Fix false positives for `Style/ArgumentsForwarding` when using default positional arg, keyword arg, and block arg in Ruby 3.1. ([@koic][]) +* [#14110](https://github.com/rubocop/rubocop/pull/14110): Fix false positives for `Style/RedundantParentheses` when parens around basic conditional as the second argument of a parenthesized method call. ([@koic][]) +* [#14120](https://github.com/rubocop/rubocop/issues/14120): Fix false positives for `Style/RedundantParentheses` when parens around unparenthesized method call as the second argument of a parenthesized method call. ([@koic][]) +* [#14133](https://github.com/rubocop/rubocop/pull/14133): Fix `Lint/LiteralAsCondition` autocorrect when a literal is the condition of an elsif followed by an else. ([@zopolis4][]) +* [#14116](https://github.com/rubocop/rubocop/issues/14116): Make `Style/TrailingCommaInArguments` cop aware of trailing commas in `[]` method call. ([@viralpraxis][]) +* [#14114](https://github.com/rubocop/rubocop/pull/14114): Fix `Style/ClassAndModuleChildren` cop error on tab-intended compactable modules. ([@viralpraxis][]) + +### Changes + +* [#13611](https://github.com/rubocop/rubocop/issues/13611): Enable `Lint/CircularArgumentReference` on Ruby 3.4. ([@earlopain][]) + +[@koic]: https://github.com/koic +[@earlopain]: https://github.com/earlopain +[@zopolis4]: https://github.com/zopolis4 +[@viralpraxis]: https://github.com/viralpraxis diff --git a/spec/rubocop/cop/layout/block_alignment_spec.rb b/spec/rubocop/cop/layout/block_alignment_spec.rb index bc31fd18c10e..44513c5a24b6 100644 --- a/spec/rubocop/cop/layout/block_alignment_spec.rb +++ b/spec/rubocop/cop/layout/block_alignment_spec.rb @@ -1002,7 +1002,7 @@ def self.foo = bar do end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'accepts end aligned with a call chain left hand side' do expect_no_offenses(<<~RUBY) parser.diagnostics.consumer = lambda do diff --git a/spec/rubocop/cop/layout/block_end_newline_spec.rb b/spec/rubocop/cop/layout/block_end_newline_spec.rb index 6e9fd6f77b85..5b35dfb55d0a 100644 --- a/spec/rubocop/cop/layout/block_end_newline_spec.rb +++ b/spec/rubocop/cop/layout/block_end_newline_spec.rb @@ -246,7 +246,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using method chain' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/layout/else_alignment_spec.rb b/spec/rubocop/cop/layout/else_alignment_spec.rb index c744a71b16b8..ab237badf1c8 100644 --- a/spec/rubocop/cop/layout/else_alignment_spec.rb +++ b/spec/rubocop/cop/layout/else_alignment_spec.rb @@ -597,7 +597,7 @@ def my_func RUBY end - it 'accepts a correctly aligned else from itblock', :ruby34, unsupported_on: :parser do + it 'accepts a correctly aligned else from itblock', :ruby34 do expect_no_offenses(<<~RUBY) array_like.each do it diff --git a/spec/rubocop/cop/layout/empty_line_between_defs_spec.rb b/spec/rubocop/cop/layout/empty_line_between_defs_spec.rb index 01c1f7f60da2..fc7bac1e5829 100644 --- a/spec/rubocop/cop/layout/empty_line_between_defs_spec.rb +++ b/spec/rubocop/cop/layout/empty_line_between_defs_spec.rb @@ -728,7 +728,7 @@ def foo_first_foo RUBY end - it 'registers offense if next to itblock', :ruby34, unsupported_on: :parser do + it 'registers offense if next to itblock', :ruby34 do expect_offense(<<~RUBY) foo 'first foo' do #foo body diff --git a/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb index deb6fabfa411..a7f2e41ee9c3 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb @@ -602,7 +602,7 @@ def foo end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do %w[private protected public module_function].each do |access_modifier| it "registers an offense for missing around line before #{access_modifier}" do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb index 52b867b39b49..160ac7ca2f2e 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb @@ -58,7 +58,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense for block body ending with a blank' do expect_offense(<<~RUBY) some_method #{open} diff --git a/spec/rubocop/cop/layout/indentation_width_spec.rb b/spec/rubocop/cop/layout/indentation_width_spec.rb index 5d38e56bcd69..4f8033b5c866 100644 --- a/spec/rubocop/cop/layout/indentation_width_spec.rb +++ b/spec/rubocop/cop/layout/indentation_width_spec.rb @@ -1629,7 +1629,7 @@ def bar end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense for bad indentation of a {} body' do expect_offense(<<~RUBY) func { diff --git a/spec/rubocop/cop/layout/line_length_spec.rb b/spec/rubocop/cop/layout/line_length_spec.rb index 0cc0339e2207..5425304a04fe 100644 --- a/spec/rubocop/cop/layout/line_length_spec.rb +++ b/spec/rubocop/cop/layout/line_length_spec.rb @@ -1826,7 +1826,7 @@ def baz(bar) end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'adds an offense for {} block does correct it' do expect_offense(<<~RUBY) foo.select { it + 4444000039123123129993912312312999199291203123123 } diff --git a/spec/rubocop/cop/layout/multiline_block_layout_spec.rb b/spec/rubocop/cop/layout/multiline_block_layout_spec.rb index e1fe3063652b..2abbdc2f6b9d 100644 --- a/spec/rubocop/cop/layout/multiline_block_layout_spec.rb +++ b/spec/rubocop/cop/layout/multiline_block_layout_spec.rb @@ -379,7 +379,7 @@ def f end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense and corrects for missing newline in {} block w/o params' do expect_offense(<<~RUBY) test { it diff --git a/spec/rubocop/cop/layout/multiline_method_call_indentation_spec.rb b/spec/rubocop/cop/layout/multiline_method_call_indentation_spec.rb index 8b158707a979..349fd557e7a4 100644 --- a/spec/rubocop/cop/layout/multiline_method_call_indentation_spec.rb +++ b/spec/rubocop/cop/layout/multiline_method_call_indentation_spec.rb @@ -423,7 +423,7 @@ def foo end end - context '>= Ruby 3.4', :ruby34, unsupported_on: :parser do + context '>= Ruby 3.4', :ruby34 do it 'accepts methods being aligned with method that is an argument' \ 'when using `it` parameter' do expect_no_offenses(<<~RUBY) @@ -779,7 +779,7 @@ def a RUBY end - it 'accepts aligned methods in multiline `it` block chain', :ruby34, unsupported_on: :parser do + it 'accepts aligned methods in multiline `it` block chain', :ruby34 do expect_no_offenses(<<~RUBY) do_something.foo do bar(it) diff --git a/spec/rubocop/cop/layout/redundant_line_break_spec.rb b/spec/rubocop/cop/layout/redundant_line_break_spec.rb index d1b9e9d7a964..ab9c65b2db6b 100644 --- a/spec/rubocop/cop/layout/redundant_line_break_spec.rb +++ b/spec/rubocop/cop/layout/redundant_line_break_spec.rb @@ -83,7 +83,7 @@ RUBY end - it 'accepts a method call chained onto a single line `it` block', :ruby34, unsupported_on: :parser do + it 'accepts a method call chained onto a single line `it` block', :ruby34 do expect_no_offenses(<<~RUBY) e.select { it.cond? } .join @@ -758,7 +758,7 @@ def assignment? RUBY end - it 'accepts a method call chained onto a multiline `it` block', :ruby34, unsupported_on: :parser do + it 'accepts a method call chained onto a multiline `it` block', :ruby34 do expect_no_offenses(<<~RUBY) e.select do it.cond? diff --git a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb index 621e7a4169cb..53ff992509bf 100644 --- a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb +++ b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb @@ -642,7 +642,7 @@ def foo end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'accepts aligned rescue in do-end `it` block in a method' do expect_no_offenses(<<~RUBY) def foo diff --git a/spec/rubocop/cop/layout/single_line_block_chain_spec.rb b/spec/rubocop/cop/layout/single_line_block_chain_spec.rb index a6655b9092c1..e15331fab589 100644 --- a/spec/rubocop/cop/layout/single_line_block_chain_spec.rb +++ b/spec/rubocop/cop/layout/single_line_block_chain_spec.rb @@ -25,7 +25,7 @@ RUBY end - it 'registers an offense for method call chained on the same line as a itblock', :ruby34, unsupported_on: :parser do + it 'registers an offense for method call chained on the same line as a itblock', :ruby34 do expect_offense(<<~RUBY) example.select { it.cond? }.join('-') ^^^^^ Put method call on a separate line if chained to a single line block. diff --git a/spec/rubocop/cop/layout/space_before_block_braces_spec.rb b/spec/rubocop/cop/layout/space_before_block_braces_spec.rb index 704c06d8d18b..925a0244cabd 100644 --- a/spec/rubocop/cop/layout/space_before_block_braces_spec.rb +++ b/spec/rubocop/cop/layout/space_before_block_braces_spec.rb @@ -82,7 +82,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense and corrects opposite + correct style' do expect_offense(<<~RUBY) each{ it } diff --git a/spec/rubocop/cop/layout/space_inside_block_braces_spec.rb b/spec/rubocop/cop/layout/space_inside_block_braces_spec.rb index 33e8e181bb13..887da274ee21 100644 --- a/spec/rubocop/cop/layout/space_inside_block_braces_spec.rb +++ b/spec/rubocop/cop/layout/space_inside_block_braces_spec.rb @@ -104,7 +104,7 @@ end end - context 'Ruby >= 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby >= 3.4', :ruby34 do it 'registers an offense for itblocks without inner space' do expect_offense(<<~RUBY) [1, 2, 3].each {it * 2} diff --git a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb index 4f27867be23b..fcba74970fde 100644 --- a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb +++ b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb @@ -58,7 +58,7 @@ end end - context 'without receiver and itblock', :ruby34, unsupported_on: :parser do + context 'without receiver and itblock', :ruby34 do it 'registers an offense' do expect_offense(<<~RUBY) some_method a { puts it } diff --git a/spec/rubocop/cop/lint/assignment_in_condition_spec.rb b/spec/rubocop/cop/lint/assignment_in_condition_spec.rb index 3ab3fe4f2aa1..abb5ca36aaee 100644 --- a/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +++ b/spec/rubocop/cop/lint/assignment_in_condition_spec.rb @@ -143,7 +143,7 @@ expect_no_offenses('return 1 if any_errors? { o = _1 }.present?') end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'accepts = in an itblock that is called in a condition' do expect_no_offenses('return 1 if any_errors? { o = inspect(it) }') end diff --git a/spec/rubocop/cop/lint/boolean_symbol_spec.rb b/spec/rubocop/cop/lint/boolean_symbol_spec.rb index 05cf4de22be2..ab19db0e1cf7 100644 --- a/spec/rubocop/cop/lint/boolean_symbol_spec.rb +++ b/spec/rubocop/cop/lint/boolean_symbol_spec.rb @@ -23,6 +23,19 @@ RUBY end + context 'when using the rocket hash syntax' do + it 'registers an offense when using a boolean symbol key' do + expect_offense(<<~RUBY) + { :false => 42 } + ^^^^^^ Symbol with a boolean name - you probably meant to use `false`. + RUBY + + expect_correction(<<~RUBY) + { false => 42 } + RUBY + end + end + context 'when using the new hash syntax' do it 'registers an offense when using `true:`' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/lint/circular_argument_reference_spec.rb b/spec/rubocop/cop/lint/circular_argument_reference_spec.rb index 5cc07c6829ea..23e4e71ab16a 100644 --- a/spec/rubocop/cop/lint/circular_argument_reference_spec.rb +++ b/spec/rubocop/cop/lint/circular_argument_reference_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -# Run test with Ruby 2.6 because this cop cannot handle invalid syntax in Ruby 2.7+. -RSpec.describe RuboCop::Cop::Lint::CircularArgumentReference, :config, :ruby26, unsupported_on: :prism do # rubocop:disable Layout/LineLength +# Run test with Ruby 3.4 because this cop cannot handle invalid syntax between Ruby 2.7 and 3.3. +RSpec.describe RuboCop::Cop::Lint::CircularArgumentReference, :config, :ruby34 do describe 'circular argument references in ordinal arguments' do context 'when the method contains a circular argument reference' do it 'registers an offense' do diff --git a/spec/rubocop/cop/lint/constant_definition_in_block_spec.rb b/spec/rubocop/cop/lint/constant_definition_in_block_spec.rb index fd9ce60ffa20..779df0ddfa2c 100644 --- a/spec/rubocop/cop/lint/constant_definition_in_block_spec.rb +++ b/spec/rubocop/cop/lint/constant_definition_in_block_spec.rb @@ -117,7 +117,7 @@ module Foo; end RUBY end - it 'registers an offense for a module defined within an itblock', :ruby34, unsupported_on: :parser do + it 'registers an offense for a module defined within an itblock', :ruby34 do expect_offense(<<~RUBY) describe do module Foo; end @@ -202,7 +202,7 @@ module Foo RUBY end - it 'does not register an offense for a module defined within an itblock of `enums` method', :ruby34, unsupported_on: :parser do + it 'does not register an offense for a module defined within an itblock of `enums` method', :ruby34 do expect_no_offenses(<<~RUBY) enums do module Foo diff --git a/spec/rubocop/cop/lint/debugger_spec.rb b/spec/rubocop/cop/lint/debugger_spec.rb index dbe04980ca7c..107250ec06af 100644 --- a/spec/rubocop/cop/lint/debugger_spec.rb +++ b/spec/rubocop/cop/lint/debugger_spec.rb @@ -40,7 +40,7 @@ RUBY end - it 'registers an offense for a `custom_debugger` call when used in `it` block', :ruby34, unsupported_on: :parser do + it 'registers an offense for a `custom_debugger` call when used in `it` block', :ruby34 do expect_offense(<<~RUBY) x.y = do_something do z(it) diff --git a/spec/rubocop/cop/lint/literal_as_condition_spec.rb b/spec/rubocop/cop/lint/literal_as_condition_spec.rb index 459487391c7b..7fd36b8968c0 100644 --- a/spec/rubocop/cop/lint/literal_as_condition_spec.rb +++ b/spec/rubocop/cop/lint/literal_as_condition_spec.rb @@ -15,6 +15,61 @@ RUBY end + it "registers an offense for truthy literal #{lit} in if-else" do + expect_offense(<<~RUBY, lit: lit) + if %{lit} + ^{lit} Literal `#{lit}` appeared as a condition. + top + else + foo + end + RUBY + + expect_correction(<<~RUBY) + top + RUBY + end + + it "registers an offense for truthy literal #{lit} in if-elsif" do + expect_offense(<<~RUBY, lit: lit) + if condition + top + elsif %{lit} + ^{lit} Literal `#{lit}` appeared as a condition. + foo + end + RUBY + + expect_correction(<<~RUBY) + if condition + top + else + foo + end + RUBY + end + + it "registers an offense for truthy literal #{lit} in if-elsif-else" do + expect_offense(<<~RUBY, lit: lit) + if condition + top + elsif %{lit} + ^{lit} Literal `#{lit}` appeared as a condition. + foo + else + bar + end + RUBY + + expect_correction(<<~RUBY) + if condition + top + else + foo + end + RUBY + end + it "registers an offense for truthy literal #{lit} in modifier if" do expect_offense(<<~RUBY, lit: lit) top if %{lit} @@ -432,6 +487,27 @@ RUBY end + it "registers an offense for falsey literal #{lit} in if-elsif-else" do + expect_offense(<<~RUBY, lit: lit) + if condition + top + elsif %{lit} + ^{lit} Literal `#{lit}` appeared as a condition. + foo + else + bar + end + RUBY + + expect_correction(<<~RUBY) + if condition + top + else + bar + end + RUBY + end + it "registers an offense for falsey literal #{lit} in modifier `if`" do expect_offense(<<~RUBY, lit: lit) top if %{lit} diff --git a/spec/rubocop/cop/lint/missing_super_spec.rb b/spec/rubocop/cop/lint/missing_super_spec.rb index 3b1d223094a7..27cfb9a7d8ae 100644 --- a/spec/rubocop/cop/lint/missing_super_spec.rb +++ b/spec/rubocop/cop/lint/missing_super_spec.rb @@ -148,7 +148,7 @@ def initialize end end - context '`Class.new` `it` block', :ruby34, unsupported_on: :parser do + context '`Class.new` `it` block', :ruby34 do it 'registers an offense and does not autocorrect when no `super` call' do expect_offense(<<~RUBY) Class.new(Parent) do diff --git a/spec/rubocop/cop/lint/nested_method_definition_spec.rb b/spec/rubocop/cop/lint/nested_method_definition_spec.rb index 035183d8b842..b03226fb2f78 100644 --- a/spec/rubocop/cop/lint/nested_method_definition_spec.rb +++ b/spec/rubocop/cop/lint/nested_method_definition_spec.rb @@ -393,7 +393,7 @@ def y end end - context 'Ruby >= 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby >= 3.4', :ruby34 do it 'does not register offense for nested definition inside `Module.new` with itblock' do expect_no_offenses(<<~RUBY) class Foo diff --git a/spec/rubocop/cop/lint/non_local_exit_from_iterator_spec.rb b/spec/rubocop/cop/lint/non_local_exit_from_iterator_spec.rb index 6ff468ce0c60..a337ea895250 100644 --- a/spec/rubocop/cop/lint/non_local_exit_from_iterator_spec.rb +++ b/spec/rubocop/cop/lint/non_local_exit_from_iterator_spec.rb @@ -23,7 +23,7 @@ RUBY end - it 'registers an offense for itblocks', :ruby34, unsupported_on: :parser do + it 'registers an offense for itblocks', :ruby34 do expect_offense(<<~RUBY) items.each do return if baz?(it) diff --git a/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb b/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb index 40b85de0fa03..cda33be75bd0 100644 --- a/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +++ b/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb @@ -59,7 +59,7 @@ end end - context 'when using `it` parameter', :ruby34, unsupported_on: :parser do + context 'when using `it` parameter', :ruby34 do it 'registers an offense and corrects for method call with space before the parenthesis when block argument and parenthesis' do expect_offense(<<~RUBY) a.concat ((1..1).map { it * 10 }) diff --git a/spec/rubocop/cop/lint/redundant_with_index_spec.rb b/spec/rubocop/cop/lint/redundant_with_index_spec.rb index bb83b28a0a4a..1a9eb28b7574 100644 --- a/spec/rubocop/cop/lint/redundant_with_index_spec.rb +++ b/spec/rubocop/cop/lint/redundant_with_index_spec.rb @@ -114,7 +114,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense for `ary.each_with_index { it }` and corrects to `ary.each`' do expect_offense(<<~RUBY) ary.each_with_index { it } diff --git a/spec/rubocop/cop/lint/redundant_with_object_spec.rb b/spec/rubocop/cop/lint/redundant_with_object_spec.rb index b5fff447657d..590265cddec2 100644 --- a/spec/rubocop/cop/lint/redundant_with_object_spec.rb +++ b/spec/rubocop/cop/lint/redundant_with_object_spec.rb @@ -114,7 +114,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense and corrects when using `ary.each_with_object { it }`' do expect_offense(<<~RUBY) ary.each_with_object([]) { it } diff --git a/spec/rubocop/cop/lint/return_in_void_context_spec.rb b/spec/rubocop/cop/lint/return_in_void_context_spec.rb index 0d0efc73df17..1407397e7dac 100644 --- a/spec/rubocop/cop/lint/return_in_void_context_spec.rb +++ b/spec/rubocop/cop/lint/return_in_void_context_spec.rb @@ -40,7 +40,7 @@ def initialize RUBY end - it 'registers an offense when the value is returned in an itblock', :ruby34, unsupported_on: :parser do + it 'registers an offense when the value is returned in an itblock', :ruby34 do expect_offense(<<~RUBY) class A def initialize diff --git a/spec/rubocop/cop/lint/safe_navigation_chain_spec.rb b/spec/rubocop/cop/lint/safe_navigation_chain_spec.rb index 30cffa65f40d..13596d7e1a3f 100644 --- a/spec/rubocop/cop/lint/safe_navigation_chain_spec.rb +++ b/spec/rubocop/cop/lint/safe_navigation_chain_spec.rb @@ -495,7 +495,7 @@ def something end end - context '>= Ruby 3.4', :ruby34, unsupported_on: :parser do + context '>= Ruby 3.4', :ruby34 do it 'registers an offense for ordinary method chain exists after ' \ 'safe navigation method call with a block using `it` parameter' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb b/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb index c67f165a0d9a..1bf6d3a1d343 100644 --- a/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +++ b/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb @@ -448,7 +448,7 @@ def x(array) end end - context 'when assigning an `it` block parameter', :ruby34, unsupported_on: :parser do + context 'when assigning an `it` block parameter', :ruby34 do it 'does not register an offense' do expect_no_offenses(<<~RUBY) def x(array) diff --git a/spec/rubocop/cop/lint/unexpected_block_arity_spec.rb b/spec/rubocop/cop/lint/unexpected_block_arity_spec.rb index 4907ef965c35..0871cb1c446b 100644 --- a/spec/rubocop/cop/lint/unexpected_block_arity_spec.rb +++ b/spec/rubocop/cop/lint/unexpected_block_arity_spec.rb @@ -172,7 +172,7 @@ end end - context 'with an itblock', :ruby34, unsupported_on: :parser do + context 'with an itblock', :ruby34 do context 'when given one parameter' do it 'registers an offense' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb b/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb index 8c0357a608c8..39143ccbcb54 100644 --- a/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb +++ b/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb @@ -588,7 +588,7 @@ RUBY end - it 'does not look inside inner itblocks', :ruby34, unsupported_on: :parser do + it 'does not look inside inner itblocks', :ruby34 do expect_no_offenses(<<~RUBY) foo.#{method}(bar) do |acc, el| values.map do diff --git a/spec/rubocop/cop/lint/unreachable_code_spec.rb b/spec/rubocop/cop/lint/unreachable_code_spec.rb index e75765fcbaee..99502091d14f 100644 --- a/spec/rubocop/cop/lint/unreachable_code_spec.rb +++ b/spec/rubocop/cop/lint/unreachable_code_spec.rb @@ -296,7 +296,7 @@ def #{t}; end RUBY end - it "accepts `#{t}` if called in `instance_eval` with itblock", :ruby34, unsupported_on: :parser do + it "accepts `#{t}` if called in `instance_eval` with itblock", :ruby34 do expect_no_offenses <<~RUBY class Dummy def #{t}; end diff --git a/spec/rubocop/cop/lint/unreachable_loop_spec.rb b/spec/rubocop/cop/lint/unreachable_loop_spec.rb index 36bc2de7e02f..d56bd4dd802f 100644 --- a/spec/rubocop/cop/lint/unreachable_loop_spec.rb +++ b/spec/rubocop/cop/lint/unreachable_loop_spec.rb @@ -243,7 +243,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense' do expect_offense(<<~RUBY) 2.times { raise it } @@ -335,7 +335,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense when using `return do_something(value) || break` in a loop' do expect_offense(<<~RUBY) [1, 2, 3].each do diff --git a/spec/rubocop/cop/lint/useless_access_modifier_spec.rb b/spec/rubocop/cop/lint/useless_access_modifier_spec.rb index 38dc7bb7b515..ab0ca6cdf82c 100644 --- a/spec/rubocop/cop/lint/useless_access_modifier_spec.rb +++ b/spec/rubocop/cop/lint/useless_access_modifier_spec.rb @@ -1059,7 +1059,7 @@ def foo RUBY end - it 'registers an offense if no method is defined in `Data.define` with itblock', :ruby34, unsupported_on: :parser do + it 'registers an offense if no method is defined in `Data.define` with itblock', :ruby34 do expect_offense(<<~RUBY, modifier: modifier) Data.define do %{modifier} diff --git a/spec/rubocop/cop/lint/useless_assignment_spec.rb b/spec/rubocop/cop/lint/useless_assignment_spec.rb index 752c3f93046a..cdaea9aa3dd6 100644 --- a/spec/rubocop/cop/lint/useless_assignment_spec.rb +++ b/spec/rubocop/cop/lint/useless_assignment_spec.rb @@ -462,7 +462,7 @@ def some_method RUBY end - it 'registers offenses for self assignment in itblock', :ruby34, unsupported_on: :parser do + it 'registers offenses for self assignment in itblock', :ruby34 do expect_offense(<<~RUBY) do_something { foo += it } ^^^ Useless assignment to variable - `foo`. Use `+` instead of `+=`. @@ -2112,7 +2112,7 @@ def some_method(name: value, **) end end - context 'using `it` block parameter', :ruby34, unsupported_on: :parser do + context 'using `it` block parameter', :ruby34 do it 'does not register an offense when the variable is used' do expect_no_offenses(<<~RUBY) var = 42 diff --git a/spec/rubocop/cop/lint/useless_ruby2_keywords_spec.rb b/spec/rubocop/cop/lint/useless_ruby2_keywords_spec.rb index 61ac7adda82f..b8c69b5248e8 100644 --- a/spec/rubocop/cop/lint/useless_ruby2_keywords_spec.rb +++ b/spec/rubocop/cop/lint/useless_ruby2_keywords_spec.rb @@ -173,7 +173,7 @@ def foo(**kwargs) RUBY end - it 'registers an offense for an itblock', :ruby34, unsupported_on: :parser do + it 'registers an offense for an itblock', :ruby34 do expect_offense(<<~RUBY) define_method(:foo) { it } ruby2_keywords :foo diff --git a/spec/rubocop/cop/lint/void_spec.rb b/spec/rubocop/cop/lint/void_spec.rb index a55157584895..6eeb9a8aa117 100644 --- a/spec/rubocop/cop/lint/void_spec.rb +++ b/spec/rubocop/cop/lint/void_spec.rb @@ -549,7 +549,7 @@ def foo end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers offense for nonmutating method that takes an `it` parameter block' do expect_offense(<<~RUBY) [1,2,3].map do diff --git a/spec/rubocop/cop/metrics/abc_size_spec.rb b/spec/rubocop/cop/metrics/abc_size_spec.rb index c46211b68359..ee6fbb4556c3 100644 --- a/spec/rubocop/cop/metrics/abc_size_spec.rb +++ b/spec/rubocop/cop/metrics/abc_size_spec.rb @@ -80,7 +80,7 @@ def method_name end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense for a `define_method` with itblock' do expect_offense(<<~RUBY) define_method :method_name do diff --git a/spec/rubocop/cop/metrics/block_length_spec.rb b/spec/rubocop/cop/metrics/block_length_spec.rb index 4955246d4b66..5649eb446bdb 100644 --- a/spec/rubocop/cop/metrics/block_length_spec.rb +++ b/spec/rubocop/cop/metrics/block_length_spec.rb @@ -137,7 +137,7 @@ end end - context 'when using `it` parameter', :ruby34, unsupported_on: :parser do + context 'when using `it` parameter', :ruby34 do it 'rejects a block with more than 5 lines' do expect_offense(<<~RUBY) something do diff --git a/spec/rubocop/cop/metrics/block_nesting_spec.rb b/spec/rubocop/cop/metrics/block_nesting_spec.rb index 43578f3fcc82..15061581444c 100644 --- a/spec/rubocop/cop/metrics/block_nesting_spec.rb +++ b/spec/rubocop/cop/metrics/block_nesting_spec.rb @@ -314,7 +314,7 @@ end end - context 'when `it` parameter', :ruby34, unsupported_on: :parser do + context 'when `it` parameter', :ruby34 do context 'nested multiline block' do it 'registers an offense' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/metrics/class_length_spec.rb b/spec/rubocop/cop/metrics/class_length_spec.rb index 30f1c5e3f28b..40dcb123411a 100644 --- a/spec/rubocop/cop/metrics/class_length_spec.rb +++ b/spec/rubocop/cop/metrics/class_length_spec.rb @@ -458,7 +458,7 @@ class << self end end - context 'when using `it` parameter', :ruby34, unsupported_on: :parser do + context 'when using `it` parameter', :ruby34 do context 'when inspecting a class defined with Class.new' do it 'registers an offense' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/metrics/method_length_spec.rb b/spec/rubocop/cop/metrics/method_length_spec.rb index 25e68901918d..23e0d6a93fbf 100644 --- a/spec/rubocop/cop/metrics/method_length_spec.rb +++ b/spec/rubocop/cop/metrics/method_length_spec.rb @@ -105,7 +105,7 @@ def m end end - context 'when using `it` parameter', :ruby34, unsupported_on: :parser do + context 'when using `it` parameter', :ruby34 do context 'when method is defined with `define_method`' do it 'registers an offense' do expect_offense(<<~RUBY) @@ -344,7 +344,7 @@ def foo RUBY end - it 'accepts dynamically defined matching method name with an itblock', :ruby34, unsupported_on: :parser do + it 'accepts dynamically defined matching method name with an itblock', :ruby34 do expect_no_offenses(<<~RUBY) define_method(:foo) do a = it @@ -400,7 +400,7 @@ def user_name RUBY end - it 'accepts dynamically defined matching method name with an itblock', :ruby34, unsupported_on: :parser do + it 'accepts dynamically defined matching method name with an itblock', :ruby34 do expect_no_offenses(<<~RUBY) define_method(:user_name) do a = it diff --git a/spec/rubocop/cop/metrics/module_length_spec.rb b/spec/rubocop/cop/metrics/module_length_spec.rb index e05bf259ae2a..4b2ff2ce7603 100644 --- a/spec/rubocop/cop/metrics/module_length_spec.rb +++ b/spec/rubocop/cop/metrics/module_length_spec.rb @@ -346,7 +346,7 @@ module Test end end - context 'when using `it` parameter', :ruby34, unsupported_on: :parser do + context 'when using `it` parameter', :ruby34 do context 'when inspecting a class defined with Module.new' do it 'registers an offense' do expect_offense(<<~RUBY) diff --git a/spec/rubocop/cop/style/arguments_forwarding_spec.rb b/spec/rubocop/cop/style/arguments_forwarding_spec.rb index a71f2fd473df..11338e2ad6d8 100644 --- a/spec/rubocop/cop/style/arguments_forwarding_spec.rb +++ b/spec/rubocop/cop/style/arguments_forwarding_spec.rb @@ -181,6 +181,22 @@ def foo(&block) RUBY end + it 'registers an offense when using default positional arg, keyword arg, and block arg', :ruby31, unsupported_on: :prism do + expect_offense(<<~RUBY) + def foo(arg = {}, **kwargs, &block) + ^^^^^^^^^^^^^^^^ Use shorthand syntax `...` for arguments forwarding. + bar(arg, **kwargs, &block) + ^^^^^^^^^^^^^^^^ Use shorthand syntax `...` for arguments forwarding. + end + RUBY + + expect_correction(<<~RUBY) + def foo(arg = {}, ...) + bar(arg, ...) + end + RUBY + end + it 'registers an offense when using block arg', :ruby31 do expect_offense(<<~RUBY) def foo(&block) diff --git a/spec/rubocop/cop/style/block_delimiters_spec.rb b/spec/rubocop/cop/style/block_delimiters_spec.rb index b7a4c4cdaf8b..a134c9d15d52 100644 --- a/spec/rubocop/cop/style/block_delimiters_spec.rb +++ b/spec/rubocop/cop/style/block_delimiters_spec.rb @@ -99,7 +99,7 @@ end end - context 'Ruby >= 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby >= 3.4', :ruby34 do it 'accepts a multi-line itblock' do expect_no_offenses(<<~RUBY) puts [1, 2, 3].map { diff --git a/spec/rubocop/cop/style/class_and_module_children_spec.rb b/spec/rubocop/cop/style/class_and_module_children_spec.rb index 684862cd61a9..4655c1c5155e 100644 --- a/spec/rubocop/cop/style/class_and_module_children_spec.rb +++ b/spec/rubocop/cop/style/class_and_module_children_spec.rb @@ -385,6 +385,25 @@ class BarClass RUBY end + it 'registers an offense for tab-intended nested children' do + expect_offense(<<~RUBY) + module A + ^ Use compact module/class definition instead of nested style. + \tmodule B + \t\tmodule C + \t\t\tbody + \t\tend + \tend + end + RUBY + + expect_correction(<<~RUBY) + module A::B::C + \t\t\tbody + end + RUBY + end + context 'with unindented nested nodes' do it 'registers an offense and autocorrects unindented class' do expect_offense(<<~RUBY) @@ -434,6 +453,65 @@ def foo; 1; end end RUBY end + + context 'with tab-intended nested nodes' do + it 'registers an offense and autocorrects when 3rd-level module has unintended body' do + expect_offense(<<~RUBY) + module A + ^ Use compact module/class definition instead of nested style. + \tmodule B + \t\tmodule C + \t\tbody + \t\tend + \tend + end + RUBY + + expect_correction(<<~RUBY) + module A::B::C + \t\tbody + end + RUBY + end + + it 'registers an offense and autocorrects when all nested modules have the same indentation' do + expect_offense(<<~RUBY) + module A + ^ Use compact module/class definition instead of nested style. + \tmodule B + \tmodule C + \tbody + \tend + \tend + end + RUBY + + expect_correction(<<~RUBY) + module A::B::C + \tbody + end + RUBY + end + + it 'registers an offense and autocorrects when all nested modules have the same indentation and nested body is intended' do + expect_offense(<<~RUBY) + module A + ^ Use compact module/class definition instead of nested style. + \tmodule B + \tmodule C + \t\tbody + \tend + \tend + end + RUBY + + expect_correction(<<~RUBY) + module A::B::C + \t\tbody + end + RUBY + end + end end context 'with one-liner class defintiion' do diff --git a/spec/rubocop/cop/style/collection_methods_spec.rb b/spec/rubocop/cop/style/collection_methods_spec.rb index fc1bea550665..c4b1f3586ec8 100644 --- a/spec/rubocop/cop/style/collection_methods_spec.rb +++ b/spec/rubocop/cop/style/collection_methods_spec.rb @@ -68,7 +68,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do context "#{method} with itblock" do it 'registers an offense' do expect_offense(<<~RUBY, method: method) diff --git a/spec/rubocop/cop/style/combinable_loops_spec.rb b/spec/rubocop/cop/style/combinable_loops_spec.rb index 8c1a7a55b851..d169cd5ce10e 100644 --- a/spec/rubocop/cop/style/combinable_loops_spec.rb +++ b/spec/rubocop/cop/style/combinable_loops_spec.rb @@ -86,7 +86,7 @@ RUBY end - it 'registers an offense when looping over the same data for the third consecutive time with `it` blocks', :ruby34, unsupported_on: :parser do + it 'registers an offense when looping over the same data for the third consecutive time with `it` blocks', :ruby34 do expect_offense(<<~RUBY) items.each { foo(it) } items.each { bar(it) } diff --git a/spec/rubocop/cop/style/comparable_between_spec.rb b/spec/rubocop/cop/style/comparable_between_spec.rb index 72277aef23aa..98ee79010421 100644 --- a/spec/rubocop/cop/style/comparable_between_spec.rb +++ b/spec/rubocop/cop/style/comparable_between_spec.rb @@ -34,6 +34,28 @@ RUBY end + it 'registers an offense when comparing with itself as the min value' do + expect_offense(<<~RUBY) + x >= x and x <= max + ^^^^^^^^^^^^^^^^^^^ Prefer `x.between?(x, max)` over logical comparison. + RUBY + + expect_correction(<<~RUBY) + x.between?(x, max) + RUBY + end + + it 'registers an offense when comparing with itself as both the min and max value' do + expect_offense(<<~RUBY) + x >= x and x <= x + ^^^^^^^^^^^^^^^^^ Prefer `x.between?(x, x)` over logical comparison. + RUBY + + expect_correction(<<~RUBY) + x.between?(x, x) + RUBY + end + it 'does not register an offense when logical comparison excludes max value' do expect_no_offenses(<<~RUBY) x >= min && x < max diff --git a/spec/rubocop/cop/style/for_spec.rb b/spec/rubocop/cop/style/for_spec.rb index 43bd6655bb9c..70fe955de883 100644 --- a/spec/rubocop/cop/style/for_spec.rb +++ b/spec/rubocop/cop/style/for_spec.rb @@ -463,7 +463,7 @@ def func end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense for each without an item and uses _ as the item' do expect_offense(<<~RUBY) def func diff --git a/spec/rubocop/cop/style/guard_clause_spec.rb b/spec/rubocop/cop/style/guard_clause_spec.rb index 464824695522..3f4ebc467605 100644 --- a/spec/rubocop/cop/style/guard_clause_spec.rb +++ b/spec/rubocop/cop/style/guard_clause_spec.rb @@ -112,7 +112,7 @@ def func RUBY end - it 'reports an offense if `define_method` block body is if / unless without else', :ruby34, unsupported_on: :parser do + it 'reports an offense if `define_method` block body is if / unless without else', :ruby34 do expect_offense(<<~RUBY) define_method(:func) do if it diff --git a/spec/rubocop/cop/style/hash_each_methods_spec.rb b/spec/rubocop/cop/style/hash_each_methods_spec.rb index 9c2473a5fd82..7b6bb1f9807f 100644 --- a/spec/rubocop/cop/style/hash_each_methods_spec.rb +++ b/spec/rubocop/cop/style/hash_each_methods_spec.rb @@ -328,7 +328,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers offense, autocorrects foo#keys.each to foo#each_key with itblock' do expect_offense(<<~RUBY) foo.keys.each { p it } diff --git a/spec/rubocop/cop/style/it_block_parameter_spec.rb b/spec/rubocop/cop/style/it_block_parameter_spec.rb index 321f090dc6dc..e62513510a22 100644 --- a/spec/rubocop/cop/style/it_block_parameter_spec.rb +++ b/spec/rubocop/cop/style/it_block_parameter_spec.rb @@ -148,7 +148,7 @@ context 'EnforcedStyle: disallow' do let(:cop_config) { { 'EnforcedStyle' => 'disallow' } } - it 'registers an offense when using `it` block parameters', unsupported_on: :parser do + it 'registers an offense when using `it` block parameters' do expect_offense(<<~RUBY) block { do_something(it) } ^^ Avoid using `it` block parameter. @@ -157,7 +157,7 @@ expect_no_corrections end - it 'registers an offense when using twice `it` block parameters', unsupported_on: :parser do + it 'registers an offense when using twice `it` block parameters' do expect_offense(<<~RUBY) block do foo(it) diff --git a/spec/rubocop/cop/style/lambda_spec.rb b/spec/rubocop/cop/style/lambda_spec.rb index b163ee81a313..4433643a3722 100644 --- a/spec/rubocop/cop/style/lambda_spec.rb +++ b/spec/rubocop/cop/style/lambda_spec.rb @@ -253,7 +253,7 @@ end end - context '>= Ruby 3.4', :ruby34, unsupported_on: :parser do + context '>= Ruby 3.4', :ruby34 do context 'when using `it` parameter' do context 'with a single line lambda method call' do it 'registers an offense' do diff --git a/spec/rubocop/cop/style/map_into_array_spec.rb b/spec/rubocop/cop/style/map_into_array_spec.rb index 7583a97ee322..5404cc9f3b60 100644 --- a/spec/rubocop/cop/style/map_into_array_spec.rb +++ b/spec/rubocop/cop/style/map_into_array_spec.rb @@ -221,7 +221,7 @@ RUBY end - it 'registers an offense and corrects when using a itblock', :ruby34, unsupported_on: :parser do + it 'registers an offense and corrects when using a itblock', :ruby34parser do expect_offense(<<~RUBY) dest = [] src.each { dest << it * 2 } diff --git a/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb b/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb index aea1d62fc96f..3db3dac8d837 100644 --- a/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +++ b/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb @@ -79,7 +79,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense for a chained call' do expect_offense(<<~RUBY) a do diff --git a/spec/rubocop/cop/style/multiline_block_chain_spec.rb b/spec/rubocop/cop/style/multiline_block_chain_spec.rb index 86025e3e536c..82ddf66f8730 100644 --- a/spec/rubocop/cop/style/multiline_block_chain_spec.rb +++ b/spec/rubocop/cop/style/multiline_block_chain_spec.rb @@ -48,7 +48,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense for a slightly more complicated case' do expect_offense(<<~RUBY) a do diff --git a/spec/rubocop/cop/style/next_spec.rb b/spec/rubocop/cop/style/next_spec.rb index 0b175db4ab60..4ca39ac83ee2 100644 --- a/spec/rubocop/cop/style/next_spec.rb +++ b/spec/rubocop/cop/style/next_spec.rb @@ -44,7 +44,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it "registers an offense for #{condition} inside of downto itblock" do expect_offense(<<~RUBY, condition: condition) 3.downto(1) do diff --git a/spec/rubocop/cop/style/object_then_spec.rb b/spec/rubocop/cop/style/object_then_spec.rb index 9adf1d79225e..c1de290698f6 100644 --- a/spec/rubocop/cop/style/object_then_spec.rb +++ b/spec/rubocop/cop/style/object_then_spec.rb @@ -77,7 +77,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense for yield_self with itblock' do expect_offense(<<~RUBY) obj.yield_self { it.test } diff --git a/spec/rubocop/cop/style/proc_spec.rb b/spec/rubocop/cop/style/proc_spec.rb index 54de612ac208..69ea5ebc06c8 100644 --- a/spec/rubocop/cop/style/proc_spec.rb +++ b/spec/rubocop/cop/style/proc_spec.rb @@ -44,7 +44,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers an offense for a Proc.new call' do expect_offense(<<~RUBY) f = Proc.new { puts it } diff --git a/spec/rubocop/cop/style/redundant_begin_spec.rb b/spec/rubocop/cop/style/redundant_begin_spec.rb index 3db15858161c..9da8dbb81edc 100644 --- a/spec/rubocop/cop/style/redundant_begin_spec.rb +++ b/spec/rubocop/cop/style/redundant_begin_spec.rb @@ -570,7 +570,7 @@ def a_method end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'reports an offense when assigning nested blocks which contain `begin` blocks' do expect_offense(<<~RUBY) var = do_something do diff --git a/spec/rubocop/cop/style/redundant_parentheses_spec.rb b/spec/rubocop/cop/style/redundant_parentheses_spec.rb index db50d5891750..c037be85adbd 100644 --- a/spec/rubocop/cop/style/redundant_parentheses_spec.rb +++ b/spec/rubocop/cop/style/redundant_parentheses_spec.rb @@ -258,6 +258,54 @@ RUBY end + it 'does not register an offense for parens around `if` as the second argument of a parenthesized method call' do + expect_no_offenses(<<~RUBY) + x(1, (2 if y?)) + RUBY + end + + it 'does not register an offense for parens around `unless` as the second argument of a parenthesized method call' do + expect_no_offenses(<<~RUBY) + x(1, (2 unless y?)) + RUBY + end + + it 'does not register an offense for parens around `while` as the second argument of a parenthesized method call' do + expect_no_offenses(<<~RUBY) + x(1, (2 while y?)) + RUBY + end + + it 'does not register an offense for parens around `until` as the second argument of a parenthesized method call' do + expect_no_offenses(<<~RUBY) + x(1, (2 until y?)) + RUBY + end + + it 'does not register an offense for parens around unparenthesized method call as the second argument of a parenthesized method call' do + expect_no_offenses(<<~RUBY) + x(1, (y arg)) + RUBY + end + + it 'does not register an offense for parens around unparenthesized safe navigation method call as the second argument of a parenthesized method call' do + expect_no_offenses(<<~RUBY) + x(1, (y&.z arg)) + RUBY + end + + it 'does not register an offense for parens around unparenthesized operator dot method call as the second argument of a parenthesized method call' do + expect_no_offenses(<<~RUBY) + x(1, (y.+ arg)) + RUBY + end + + it 'does not register an offense for parens around unparenthesized operator safe navigation method call as the second argument of a parenthesized method call' do + expect_no_offenses(<<~RUBY) + x(1, (y&.+ arg)) + RUBY + end + it 'registers an offense for parens around an expression method argument of a parenthesized method call' do expect_offense(<<~RUBY) x.y((z + w)) diff --git a/spec/rubocop/cop/style/redundant_self_spec.rb b/spec/rubocop/cop/style/redundant_self_spec.rb index b7814d03f714..9c1f6e5c34fe 100644 --- a/spec/rubocop/cop/style/redundant_self_spec.rb +++ b/spec/rubocop/cop/style/redundant_self_spec.rb @@ -184,7 +184,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'registers offense for self usage in itblocks' do expect_offense(<<~RUBY) %w[x y z].select do diff --git a/spec/rubocop/cop/style/redundant_sort_by_spec.rb b/spec/rubocop/cop/style/redundant_sort_by_spec.rb index 29ccba8b7e17..962692001acb 100644 --- a/spec/rubocop/cop/style/redundant_sort_by_spec.rb +++ b/spec/rubocop/cop/style/redundant_sort_by_spec.rb @@ -47,7 +47,7 @@ end end - context 'Ruby 3.4', :ruby34, unsupported_on: :parser do + context 'Ruby 3.4', :ruby34 do it 'autocorrects array.sort_by { |x| x }' do expect_offense(<<~RUBY) array.sort_by { it } diff --git a/spec/rubocop/cop/style/safe_navigation_spec.rb b/spec/rubocop/cop/style/safe_navigation_spec.rb index 6120360cf64e..e513800dc7f7 100644 --- a/spec/rubocop/cop/style/safe_navigation_spec.rb +++ b/spec/rubocop/cop/style/safe_navigation_spec.rb @@ -1467,6 +1467,16 @@ def foobar RUBY end + it 'does not register an offense when the RHS of `&&` is a complex `||` expression composed of `&&` conditions' do + expect_no_offenses(<<~RUBY) + foo && ( + (foo >= 1 && foo < 2) || + (foo >= 3 && foo < 4) || + (foo >= 5 && foo < 6) + ) + RUBY + end + context 'respond_to?' do it 'allows method calls safeguarded by a respond_to check' do expect_no_offenses('foo.bar if foo.respond_to?(:bar)') diff --git a/spec/rubocop/cop/style/select_by_regexp_spec.rb b/spec/rubocop/cop/style/select_by_regexp_spec.rb index 3085c59909c5..3b5d4964f804 100644 --- a/spec/rubocop/cop/style/select_by_regexp_spec.rb +++ b/spec/rubocop/cop/style/select_by_regexp_spec.rb @@ -646,7 +646,7 @@ end end - context 'when Ruby >= 3.4', :ruby34, unsupported_on: :parser do + context 'when Ruby >= 3.4', :ruby34 do include_examples('regexp match with `itblock`s', 'select', 'grep') include_examples('regexp match with `itblock`s', 'find_all', 'grep') include_examples('regexp match with `itblock`s', 'filter', 'grep') diff --git a/spec/rubocop/cop/style/single_line_do_end_block_spec.rb b/spec/rubocop/cop/style/single_line_do_end_block_spec.rb index 4281eca1ef6e..99c53b183c1b 100644 --- a/spec/rubocop/cop/style/single_line_do_end_block_spec.rb +++ b/spec/rubocop/cop/style/single_line_do_end_block_spec.rb @@ -53,7 +53,7 @@ RUBY end - it 'registers an offense when using single line `do`...`end` with `it` block argument', :ruby34, unsupported_on: :parser do + it 'registers an offense when using single line `do`...`end` with `it` block argument', :ruby34 do expect_offense(<<~RUBY) foo do bar(it) end ^^^^^^^^^^^^^^^^^^ Prefer multiline `do`...`end` block. diff --git a/spec/rubocop/cop/style/symbol_proc_spec.rb b/spec/rubocop/cop/style/symbol_proc_spec.rb index ac94470f4077..6fcbc4ccee6e 100644 --- a/spec/rubocop/cop/style/symbol_proc_spec.rb +++ b/spec/rubocop/cop/style/symbol_proc_spec.rb @@ -493,7 +493,7 @@ end end - context 'itblocks', :ruby34, unsupported_on: :parser do + context 'itblocks', :ruby34 do %w[reject select].each do |method| it "registers an offense when receiver is an array literal and using `#{method}` with a itblock" do expect_offense(<<~RUBY, method: method) diff --git a/spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb b/spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb index 1b2245e1c882..7da19dddd363 100644 --- a/spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb +++ b/spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb @@ -2,66 +2,97 @@ RSpec.describe RuboCop::Cop::Style::TrailingCommaInArguments, :config do shared_examples 'single line lists' do |extra_info| - it 'registers an offense for trailing comma in a method call' do - expect_offense(<<~RUBY) - some_method(a, b, c, ) - ^ Avoid comma after the last parameter of a method call#{extra_info}. - RUBY + [%w[( )], %w[[ ]]].each do |start_bracket, end_bracket| + it 'registers an offense for trailing comma in a method call' do + expect_offense(<<~RUBY, start_bracket: start_bracket, end_bracket: end_bracket) + some_method#{start_bracket}a, b, c, #{end_bracket} + _{start_bracket} ^ Avoid comma after the last parameter of a method call#{extra_info}. + RUBY - expect_correction(<<~RUBY) - some_method(a, b, c ) - RUBY - end + expect_correction(<<~RUBY) + some_method#{start_bracket}a, b, c #{end_bracket} + RUBY + end - it 'registers an offense for trailing comma preceded by whitespace in a method call' do - expect_offense(<<~RUBY) - some_method(a, b, c , ) - ^ Avoid comma after the last parameter of a method call#{extra_info}. - RUBY + it 'registers an offense for trailing comma preceded by whitespace in a method call' do + expect_offense(<<~RUBY, start_bracket: start_bracket, end_bracket: end_bracket) + some_method#{start_bracket}a, b, c , #{end_bracket} + _{start_bracket} ^ Avoid comma after the last parameter of a method call#{extra_info}. + RUBY - expect_correction(<<~RUBY) - some_method(a, b, c ) - RUBY - end + expect_correction(<<~RUBY) + some_method#{start_bracket}a, b, c #{end_bracket} + RUBY + end - it 'registers an offense for trailing comma in a method call with hash parameters at the end' do - expect_offense(<<~RUBY) - some_method(a, b, c: 0, d: 1, ) - ^ Avoid comma after the last parameter of a method call#{extra_info}. - RUBY + it 'registers an offense for trailing comma in a method call with hash parameters at the end' do + expect_offense(<<~RUBY, start_bracket: start_bracket, end_bracket: end_bracket) + some_method#{start_bracket}a, b, c: 0, d: 1, #{end_bracket} + _{start_bracket} ^ Avoid comma after the last parameter of a method call#{extra_info}. + RUBY - expect_correction(<<~RUBY) - some_method(a, b, c: 0, d: 1 ) - RUBY - end + expect_correction(<<~RUBY) + some_method#{start_bracket}a, b, c: 0, d: 1 #{end_bracket} + RUBY + end - it 'accepts method call without trailing comma' do - expect_no_offenses('some_method(a, b, c)') - end + it 'accepts method call without trailing comma' do + expect_no_offenses("some_method#{start_bracket}a, b, c#{end_bracket}") + end - it 'accepts method call without trailing comma when a line break before a method call' do - expect_no_offenses(<<~RUBY) - obj - .do_something(:foo, :bar) - RUBY - end + it 'accepts method call without trailing comma when a line break before a method call' do + expect_no_offenses(<<~RUBY) + obj + .do_something#{start_bracket}:foo, :bar#{end_bracket} + RUBY + end + + it 'accepts method call without trailing comma with single element hash ' \ + 'parameters at the end' do + expect_no_offenses("some_method#{start_bracket}a: 1#{end_bracket}") + end + + it 'accepts method call without parameters' do + expect_no_offenses('some_method') + end + + it 'accepts chained single-line method calls' do + expect_no_offenses(<<~RUBY) + target + .some_method#{start_bracket}a#{end_bracket} + RUBY + end + + context 'when using safe navigation operator' do + it 'registers an offense for trailing comma in a method call' do + expect_offense(<<~RUBY, start_bracket: start_bracket, end_bracket: end_bracket) + receiver&.some_method#{start_bracket}a, b, c, #{end_bracket} + _{start_bracket} ^ Avoid comma after the last parameter of a method call#{extra_info}. + RUBY + + expect_correction(<<~RUBY) + receiver&.some_method#{start_bracket}a, b, c #{end_bracket} + RUBY + end + + it 'registers an offense for trailing comma in a method call with hash ' \ + 'parameters at the end' do + expect_offense(<<~RUBY, start_bracket: start_bracket, end_bracket: end_bracket) + receiver&.some_method#{start_bracket}a, b, c: 0, d: 1, #{end_bracket} + _{start_bracket} ^ Avoid comma after the last parameter of a method call#{extra_info}. + RUBY - it 'accepts method call without trailing comma with single element hash ' \ - 'parameters at the end' do - expect_no_offenses('some_method(a: 1)') + expect_correction(<<~RUBY) + receiver&.some_method#{start_bracket}a, b, c: 0, d: 1 #{end_bracket} + RUBY + end + end end it 'accepts method call without parameters' do expect_no_offenses('some_method') end - it 'accepts chained single-line method calls' do - expect_no_offenses(<<~RUBY) - target - .some_method(a) - RUBY - end - it 'accepts heredoc without trailing comma' do expect_no_offenses(<<~RUBY) route(1, <<-HELP.chomp) @@ -69,31 +100,6 @@ HELP RUBY end - - context 'when using safe navigation operator' do - it 'registers an offense for trailing comma in a method call' do - expect_offense(<<~RUBY) - receiver&.some_method(a, b, c, ) - ^ Avoid comma after the last parameter of a method call#{extra_info}. - RUBY - - expect_correction(<<~RUBY) - receiver&.some_method(a, b, c ) - RUBY - end - - it 'registers an offense for trailing comma in a method call with hash ' \ - 'parameters at the end' do - expect_offense(<<~RUBY) - receiver&.some_method(a, b, c: 0, d: 1, ) - ^ Avoid comma after the last parameter of a method call#{extra_info}. - RUBY - - expect_correction(<<~RUBY) - receiver&.some_method(a, b, c: 0, d: 1 ) - RUBY - end - end end context 'with single line list of values' do @@ -120,13 +126,17 @@ context 'when EnforcedStyleForMultiline is consistent_comma' do let(:cop_config) { { 'EnforcedStyleForMultiline' => 'consistent_comma' } } - it 'accepts a single argument with no trailing comma' do - expect_no_offenses(<<~RUBY) - EmailWorker.perform_async({ - subject: "hey there", - email: "foo@bar.com" - }) - RUBY + [%w[( )], %w[[ ]]].each do |start_bracket, end_bracket| + context "with `#{start_bracket}#{end_bracket}` brackets" do + it 'accepts a single argument with no trailing comma' do + expect_no_offenses(<<~RUBY) + EmailWorker.perform_async#{start_bracket}{ + subject: "hey there", + email: "foo@bar.com" + }#{end_bracket} + RUBY + end + end end end end @@ -171,6 +181,26 @@ RUBY end + it 'registers an offense for trailing comma in a `[]` method call with ' \ + 'hash parameters at the end' do + expect_offense(<<~RUBY) + object[ + a, + b, + c: 0, + d: 1,] + ^ Avoid comma after the last parameter of a method call. + RUBY + + expect_correction(<<~RUBY) + object[ + a, + b, + c: 0, + d: 1] + RUBY + end + it 'accepts a method call with hash parameters at the end and no trailing comma' do expect_no_offenses(<<~RUBY) some_method(a, diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8aa432745f6c..9161acebee19 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -65,7 +65,4 @@ # Prism supports Ruby 3.3+ parsing. config.filter_run_excluding unsupported_on: :prism if ENV['PARSER_ENGINE'] == 'parser_prism' - - # With whitequark/parser, RuboCop supports Ruby syntax compatible with 2.0 to 3.3. - config.filter_run_excluding unsupported_on: :parser if ENV['PARSER_ENGINE'] != 'parser_prism' end