Class: Guardrails::Audit::AutoFixer

Inherits:
Object
  • Object
show all
Defined in:
lib/guardrails/audit/auto_fixer.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

COMPATIBLE_SYNTAX =

Each violation type maps to the token syntaxes that can substitute for it in source. raw_color in a view attribute can only become var(–name); tailwind_arbitrary in a class string can only become a named utility derived from a Tailwind theme color.

{
  raw_color: [:css_var],
  tailwind_arbitrary: [:tailwind]
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(root, output: $stdout, tokens: [], near_match_policy: "notify", near_match_threshold: TokenMatcher::NEAR_MATCH_THRESHOLD) ⇒ AutoFixer

Returns a new instance of AutoFixer.



21
22
23
24
25
26
27
# File 'lib/guardrails/audit/auto_fixer.rb', line 21

def initialize(root, output: $stdout, tokens: [], near_match_policy: "notify",
               near_match_threshold: TokenMatcher::NEAR_MATCH_THRESHOLD)
  @root = Pathname(root)
  @output = output
  @near_match_policy = near_match_policy
  @matchers = build_matchers(tokens, near_match_threshold)
end

Instance Method Details

#applicable?(violation) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/guardrails/audit/auto_fixer.rb', line 41

def applicable?(violation)
  !applicable_match(violation).nil?
end

#apply(violations) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/guardrails/audit/auto_fixer.rb', line 29

def apply(violations)
  applicable = violations.select { |v| applicable?(v) }
  return [] if applicable.empty?

  applied = applicable.group_by(&:file).flat_map do |file, file_violations|
    process_file(@root.join(file), file_violations)
  end

  report(applied)
  applied
end