Skip to content

Commit

Permalink
[Fix #12717] Re-use definition of directive comments from Style/Comme…
Browse files Browse the repository at this point in the history
…ntedKeyword

To make sure we don't have a different, more strict definition of what
constitutes a rubocop:disable or rubocop:todo comment in the cop compared to
elsewhere in the code, use the definition implemented in DirectiveComment.

We also get the rubocop:enable comments included in this definition, but it's
not a problem if we allow those comments in the cop, as they don't make any
sense as end-of-line comments and are unlikely to occur.
  • Loading branch information
jonas054 authored and bbatsov committed Feb 28, 2024
1 parent 8aae48b commit 18004e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/fix_commented_keyword.md
@@ -0,0 +1 @@
* [#12717](https://github.com/rubocop/rubocop/issues/12717): Fix regexp for inline disable comments in `Style/CommentedKeyword`. ([@jonas054][])
7 changes: 5 additions & 2 deletions lib/rubocop/cop/style/commented_keyword.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative '../../directive_comment'

module RuboCop
module Cop
module Style
Expand Down Expand Up @@ -49,8 +51,9 @@ class CommentedKeyword < Base
KEYWORDS = %w[begin class def end module].freeze
KEYWORD_REGEXES = KEYWORDS.map { |w| /^\s*#{w}\s/ }.freeze

ALLOWED_COMMENTS = %w[:nodoc: :yields: rubocop:disable rubocop:todo].freeze
ALLOWED_COMMENT_REGEXES = ALLOWED_COMMENTS.map { |c| /#\s*#{c}/ }.freeze
ALLOWED_COMMENTS = %w[:nodoc: :yields:].freeze
ALLOWED_COMMENT_REGEXES = (ALLOWED_COMMENTS.map { |c| /#\s*#{c}/ } +
[DirectiveComment::DIRECTIVE_COMMENT_REGEXP]).freeze

REGEXP = /(?<keyword>\S+).*#/.freeze

Expand Down
14 changes: 12 additions & 2 deletions spec/rubocop/cop/style/commented_keyword_spec.rb
Expand Up @@ -181,12 +181,22 @@ def y # :yields:
end
RUBY
expect_no_offenses(<<~RUBY)
def x # rubocop:disable Metrics/MethodLength
def x # rubocop:disable Metrics/MethodLength
y
end
RUBY
expect_no_offenses(<<~RUBY)
def x # rubocop:todo Metrics/MethodLength
def x # rubocop: disable Metrics/MethodLength
y
end
RUBY
expect_no_offenses(<<~RUBY)
def x # rubocop :todo Metrics/MethodLength
y
end
RUBY
expect_no_offenses(<<~RUBY)
def x # rubocop: todo Metrics/MethodLength
y
end
RUBY
Expand Down

0 comments on commit 18004e9

Please sign in to comment.