Skip to content

Commit

Permalink
Merge pull request #12760 from Earlopain/fix-12663
Browse files Browse the repository at this point in the history
[Fix #12663] Prevent `Lint/Syntax` being disabled by directive comments
  • Loading branch information
koic committed Mar 9, 2024
2 parents 2ec75fa + e5d8646 commit 5a4664c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
@@ -0,0 +1 @@
* [#12663](https://github.com/rubocop/rubocop/issues/12663): Fix `Lint/Syntax` getting disabled by `rubocop:disable Lint/Syntax`. ([@earlopain][])
18 changes: 10 additions & 8 deletions lib/rubocop/directive_comment.rb
Expand Up @@ -6,9 +6,11 @@ module RuboCop
# cops it contains.
class DirectiveComment
# @api private
REDUNDANT_DIRECTIVE_COP_DEPARTMENT = 'Lint'
LINT_DEPARTMENT = 'Lint'
# @api private
REDUNDANT_DIRECTIVE_COP = "#{REDUNDANT_DIRECTIVE_COP_DEPARTMENT}/RedundantCopDisableDirective"
LINT_REDUNDANT_DIRECTIVE_COP = "#{LINT_DEPARTMENT}/RedundantCopDisableDirective"
# @api private
LINT_SYNTAX_COP = "#{LINT_DEPARTMENT}/Syntax"
# @api private
COP_NAME_PATTERN = '([A-Z]\w+/)*(?:[A-Z]\w+)'
# @api private
Expand Down Expand Up @@ -118,27 +120,27 @@ def splitted_cops_string
end

def parsed_cop_names
splitted_cops_string.map do |name|
cops = splitted_cops_string.map do |name|
department?(name) ? cop_names_for_department(name) : name
end.flatten
cops - [LINT_SYNTAX_COP]
end

def department?(name)
cop_registry.department?(name)
end

def all_cop_names
exclude_redundant_directive_cop(cop_registry.names)
exclude_lint_department_cops(cop_registry.names)
end

def cop_names_for_department(department)
names = cop_registry.names_for_department(department)
has_redundant_directive_cop = department == REDUNDANT_DIRECTIVE_COP_DEPARTMENT
has_redundant_directive_cop ? exclude_redundant_directive_cop(names) : names
department == LINT_DEPARTMENT ? exclude_lint_department_cops(names) : names
end

def exclude_redundant_directive_cop(cops)
cops - [REDUNDANT_DIRECTIVE_COP]
def exclude_lint_department_cops(cops)
cops - [LINT_REDUNDANT_DIRECTIVE_COP, LINT_SYNTAX_COP]
end
end
end
36 changes: 36 additions & 0 deletions spec/rubocop/cli_spec.rb
Expand Up @@ -368,6 +368,42 @@ def and_with_args
$stdout.string.include?('F: 1: 7: Lint/Syntax: unexpected token tINTEGER')
).to be(true)
end

it '`Lint/Syntax` must be enabled when disabled by directive comment' do
create_file('example.rb', <<~RUBY)
# rubocop:disable Lint/Syntax
1 /// 2
RUBY

expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
expect(
$stdout.string.include?('F: 2: 7: Lint/Syntax: unexpected token tINTEGER')
).to be(true)
end

it '`Lint/Syntax` must be enabled when disabled by directive department comment' do
create_file('example.rb', <<~RUBY)
# rubocop:disable Lint
1 /// 2
RUBY

expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
expect(
$stdout.string.include?('F: 2: 7: Lint/Syntax: unexpected token tINTEGER')
).to be(true)
end

it '`Lint/Syntax` must be enabled when disabled by directive all comment' do
create_file('example.rb', <<~RUBY)
# rubocop:disable all
1 /// 2
RUBY

expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
expect(
$stdout.string.include?('F: 2: 7: Lint/Syntax: unexpected token tINTEGER')
).to be(true)
end
end

describe 'rubocop:disable comment' do
Expand Down
8 changes: 3 additions & 5 deletions spec/rubocop/comment_config_spec.rb
Expand Up @@ -140,13 +140,11 @@ def disabled_lines_of_cop(cop)
expect(loop_disabled_lines.include?(20)).to be(false)
end

it 'supports disabling all cops except Lint/RedundantCopDisableDirective with keyword all' do
it 'supports disabling all cops except Lint/RedundantCopDisableDirective and Lint/Syntax with keyword all' do
expected_part = (7..8).to_a

cops = RuboCop::Cop::Registry.all.reject do |klass|
klass == RuboCop::Cop::Lint::RedundantCopDisableDirective
end

excluded = [RuboCop::Cop::Lint::RedundantCopDisableDirective, RuboCop::Cop::Lint::Syntax]
cops = RuboCop::Cop::Registry.all - excluded
cops.each do |cop|
disabled_lines = disabled_lines_of_cop(cop)
expect(disabled_lines & expected_part).to eq(expected_part)
Expand Down

0 comments on commit 5a4664c

Please sign in to comment.