Class: QueryPackwerk::RuleRewriter::RuleSetRewriter

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/query_packwerk/rule_rewriter/rule_set_rewriter.rb

Overview

Coordinates the application of multiple rewriting rules to source code. Processes Ruby code using RuboCop’s source processing capabilities and applies each configured rule in sequence to transform source code for analysis purposes.

Constant Summary collapse

RULES =
[
  RuleRewriter::AnonymizeKeywordArgumentsRule,
  RuleRewriter::AnonymizeArgumentsRule
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, rules: RULES) ⇒ RuleSetRewriter

Returns a new instance of RuleSetRewriter.



27
28
29
30
31
32
33
# File 'lib/query_packwerk/rule_rewriter/rule_set_rewriter.rb', line 27

def initialize(string, rules: RULES)
  @source = processed_source(string)
  @ast = @source.ast
  @source_buffer = @source.buffer
  @rewriter = Parser::Source::TreeRewriter.new(@source_buffer)
  @rules = rules
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



17
18
19
# File 'lib/query_packwerk/rule_rewriter/rule_set_rewriter.rb', line 17

def ast
  @ast
end

#rewriterObject (readonly)

Returns the value of attribute rewriter.



20
21
22
# File 'lib/query_packwerk/rule_rewriter/rule_set_rewriter.rb', line 20

def rewriter
  @rewriter
end

#sourceObject (readonly)

Returns the value of attribute source.



14
15
16
# File 'lib/query_packwerk/rule_rewriter/rule_set_rewriter.rb', line 14

def source
  @source
end

Instance Method Details

#processObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/query_packwerk/rule_rewriter/rule_set_rewriter.rb', line 35

def process
  @rules.each do |rule_class|
    rule = rule_class.new(@rewriter)
    @ast.each_node { |node| rule.process(node) }
  end

  @rewriter
    .process
    .delete("\n").squeeze(' ') # ...and multiple spaces, probably indents from above
    .gsub('( ', '(') # Remove paren spacing after previous
    .gsub(' )', ')') # Remove paren spacing after previous
    .gsub('. ', '.') # Remove suffix-dot spacing
end