Class: Rubydex::CLI::Command::Lint::Explain

Inherits:
Rubydex::CLI::Command show all
Defined in:
lib/rubydex/cli/command/lint/explain.rb

Overview

rdx lint explain <RULE> — prints documentation for rules with a matching name.

Constant Summary collapse

BASE_PATHS =
[
  File.expand_path("../../../linter/custom_rule.rb", __dir__),
  File.expand_path("../../../rule.rb", __dir__),
]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rubydex::CLI::Command

all, arguments, command, declared, find, #initialize, summary

Constructor Details

This class inherits a constructor from Rubydex::CLI::Command

Class Method Details

.usage_formObject

: -> String



19
20
21
# File 'lib/rubydex/cli/command/lint/explain.rb', line 19

def usage_form
  "lint explain <RULE>"
end

Instance Method Details

#runObject

: -> void



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubydex/cli/command/lint/explain.rb', line 25

def run
  parse_options!

  rule_name = argv.shift
  abort_with_usage("`lint explain` requires a rule name argument") unless rule_name
  abort_with_usage("unexpected argument: #{argv.first}") unless argv.empty?

  workspace_path = current_workspace_path

  graph = Rubydex::Graph.configure_for_workspace(workspace_path)
  graph.index_all([*BASE_PATHS, *Rubydex::Linter::RuleLoader.paths(workspace_path)])
  graph.resolve

  base_rule = graph["Rubydex::Rule"]
  abort("Base rule class is not found. This is an issue in Rubydex itself") unless base_rule.is_a?(Rubydex::Class)

  rule_declarations = base_rule.descendants.grep(Rubydex::Class) #: as Array[Rubydex::Class]

  matched_rule_names = rule_declarations.filter_map do |declaration|
    declaration_name = declaration.name
    next unless declaration_name
    next if declaration_name == "Rubydex::Rule" || declaration_name == "Rubydex::Linter::CustomRule"
    next unless declaration_name.end_with?(rule_name)

    declaration
  end
  abort("Rule does not exist: #{rule_name}") if matched_rule_names.empty?

  puts(matched_rule_names.sort_by(&:name).map { |rule| documentation_for(rule) }.join("\n"))
end