Class: HakumiComponents::StylesheetOwnershipChecker

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/hakumi_components/stylesheet_ownership_checker.rb

Defined Under Namespace

Classes: Finding, Rule

Constant Summary collapse

REPO_ROOT =
T.let(Pathname.new(__dir__).join("../..").expand_path, Pathname)
CONFIG_PATH =
T.let(Pathname.new("docs/stylesheet_ownership.yml"), Pathname)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: REPO_ROOT, rules: nil) ⇒ StylesheetOwnershipChecker

Returns a new instance of StylesheetOwnershipChecker.



57
58
59
60
# File 'lib/hakumi_components/stylesheet_ownership_checker.rb', line 57

def initialize(root: REPO_ROOT, rules: nil)
  @root = T.let(root, Pathname)
  @rules = T.let(rules || self.class.load_rules(root: root), T::Array[Rule])
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



63
64
65
# File 'lib/hakumi_components/stylesheet_ownership_checker.rb', line 63

def rules
  @rules
end

Class Method Details

.load_rules(root: REPO_ROOT, config_path: CONFIG_PATH) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hakumi_components/stylesheet_ownership_checker.rb', line 30

def self.load_rules(root: REPO_ROOT, config_path: CONFIG_PATH)
  path = root.join(config_path)
  return [] unless path.exist?

  data = YAML.safe_load(path.read, aliases: false) || {}
  rules = T.let([], T::Array[Rule])

  T.cast(data, T::Hash[String, T.untyped]).each do |name, config|
    next unless config.is_a?(Hash)

    owner_partial = config["owner_partial"]
    owned_selectors = string_array(config["owned_selectors"])
    forbidden_partials = string_array(config["forbidden_partials"])
    next unless owner_partial.is_a?(String) && owned_selectors.any?

    rules << Rule.new(
      name: name,
      owner_partial: owner_partial,
      owned_selectors: owned_selectors,
      forbidden_partials: forbidden_partials
    )
  end

  rules
end

Instance Method Details

#findingsObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/hakumi_components/stylesheet_ownership_checker.rb', line 66

def findings
  current_findings = T.let([], T::Array[Finding])

  @rules.each do |rule|
    current_findings.concat(owner_findings(rule))
    current_findings.concat(forbidden_partial_findings(rule))
  end

  current_findings
end

#reportObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hakumi_components/stylesheet_ownership_checker.rb', line 88

def report
  current_findings = findings
  lines = [ "Stylesheet ownership guardrail", "" ]

  if @rules.empty?
    lines << "No stylesheet ownership rules are configured."
    return lines.join("\n")
  end

  if current_findings.empty?
    lines << "All configured stylesheet ownership rules pass."
    return lines.join("\n")
  end

  lines << "Violations:"
  current_findings.each do |finding|
    lines << format_finding(finding)
  end

  lines.join("\n")
end

#success?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/hakumi_components/stylesheet_ownership_checker.rb', line 83

def success?
  violations.empty?
end

#violationsObject



78
79
80
# File 'lib/hakumi_components/stylesheet_ownership_checker.rb', line 78

def violations
  findings
end