Skip to content

Draft: HAML lint: Enable RuboCop rules

Peter Leitzen requested to merge pl-haml-lint-rubocop into master

What does this MR do and why?

This MR enables all 👮 RuboCop in HAML lint and moves all excludes to .rubocop_todo/**/*.yml.

With this, we won't add more offenses silently to 🆕 HAML files because all cops are now enabled 🎉

This MR also adjusts some rule names because they were renamed in the meanwhile.

Follow-up of !123642 (merged).

See #415330.

How to set up and validate locally

bundle exec haml-lint --parallel

2094 files inspected, 0 lints detected

REVEAL_RUBOCOP_TODO=1 bundle exec haml-lint --parallel

...
lib/gitlab/background_migration/mailers/views/unconfirm_mailer/unconfirm_notification_email.html.haml:6 [W] RuboCop: Rails/LinkToBlank: Specify a `:rel` option containing noopener.
lib/gitlab/background_migration/mailers/views/unconfirm_mailer/unconfirm_notification_email.html.haml:12 [W] RuboCop: Rails/LinkToBlank: Specify a `:rel` option containing noopener.

2094 files inspected, 2095 lints detected

But how?

  1. Cop rules enabled in .haml-lint.yml by removing the from ignore_cops list
  2. Run bundle exec haml-lint -r json --parallel | jq > report.json to generate a JSON list of offenses
  3. Run the script to insert HAML paths (plus .rb) into .rubocop_todo/**/*.yml
The script
# frozen_string_literal: true

# What?
# Converts HAML lint JSON reports into RuboCop TODO ymls.
#
# Usage:
#   bundle exec haml-lint -r json --parallel | jq  > report.json
#   ruby .local/haml-lint-report-to-rubocop-todo.rb

require "json"
require "fileutils"

require "active_support/all"

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'RSpec'
  inflect.acronym 'GraphQL'
end

def add_entry(path, cop_name, todo_yml)
  unless File.exist?(todo_yml)
    FileUtils.mkdir_p(File.dirname(todo_yml))
    File.write(todo_yml, <<~CONTENT)
      ---
      #{cop_name}:
        Exclude:
    CONTENT
  end

  File.open(todo_yml, "a+") do |fh|
    fh << "    - '#{path}'\n"
  end
end

report = JSON.parse(File.read("report.json"))

report.fetch("files").each do |file|
  path = file.fetch("path") + ".rb"

  file
    .fetch("offenses")
    .group_by { |offense| offense.fetch("message")[/^[^:]+/] }
    .each do |cop_name, _offense| 
      todo_yml = ".rubocop_todo/#{cop_name.underscore}.yml"
      add_entry(path, cop_name, todo_yml)
    end
end
  1. Revisit each TODO file and sort list in Exclude alphabetically

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Peter Leitzen

Merge request reports