From e5d86464b663530e92ee6acd2639652cde7c5442 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:27:38 +0100 Subject: [PATCH] [Fix #12663] Prevent `Lint/Syntax` being disabled by directive comments This would swallow the syntax error, making it look like the file is all good. In reality all other cops are not being run. --- ...ax_being_disabled_by_directive_comments.md | 1 + lib/rubocop/directive_comment.rb | 18 +++++----- spec/rubocop/cli_spec.rb | 36 +++++++++++++++++++ spec/rubocop/comment_config_spec.rb | 8 ++--- 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 changelog/fix_lint_syntax_being_disabled_by_directive_comments.md diff --git a/changelog/fix_lint_syntax_being_disabled_by_directive_comments.md b/changelog/fix_lint_syntax_being_disabled_by_directive_comments.md new file mode 100644 index 00000000000..ddcae7502f1 --- /dev/null +++ b/changelog/fix_lint_syntax_being_disabled_by_directive_comments.md @@ -0,0 +1 @@ +* [#12663](https://github.com/rubocop/rubocop/issues/12663): Fix `Lint/Syntax` getting disabled by `rubocop:disable Lint/Syntax`. ([@earlopain][]) diff --git a/lib/rubocop/directive_comment.rb b/lib/rubocop/directive_comment.rb index d8bc614ed48..f18e29a277e 100644 --- a/lib/rubocop/directive_comment.rb +++ b/lib/rubocop/directive_comment.rb @@ -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 @@ -118,9 +120,10 @@ 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) @@ -128,17 +131,16 @@ def 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 diff --git a/spec/rubocop/cli_spec.rb b/spec/rubocop/cli_spec.rb index 015d6ff1b2c..bd736c41622 100644 --- a/spec/rubocop/cli_spec.rb +++ b/spec/rubocop/cli_spec.rb @@ -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 diff --git a/spec/rubocop/comment_config_spec.rb b/spec/rubocop/comment_config_spec.rb index e5156480a4c..d47ac0ac90b 100644 --- a/spec/rubocop/comment_config_spec.rb +++ b/spec/rubocop/comment_config_spec.rb @@ -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)