Class: OffenseToCorrector::OffenseParser

Inherits:
Object
  • Object
show all
Includes:
AstTools
Defined in:
lib/offense_to_corrector/offense_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AstTools

#ast_from, #atom?, #get_children, #get_corrector, #processed_source_from, #range_overlap?, #range_overlap_count, #string_intersection

Constructor Details

#initialize(string) ⇒ OffenseParser

Returns a new instance of OffenseParser.



7
8
9
10
11
12
13
# File 'lib/offense_to_corrector/offense_parser.rb', line 7

def initialize(string)
  @ast_lines,  @offense = parse_string(string)
  @source = processed_source_from(@ast_lines.join("\n"))
  @ast = ast_from(@source)
  @ast_nodes = get_children(@ast)
  @template = OffenseTemplate.new
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



5
6
7
# File 'lib/offense_to_corrector/offense_parser.rb', line 5

def ast
  @ast
end

#ast_nodesObject (readonly)

Returns the value of attribute ast_nodes.



5
6
7
# File 'lib/offense_to_corrector/offense_parser.rb', line 5

def ast_nodes
  @ast_nodes
end

#offenseObject (readonly)

Returns the value of attribute offense.



5
6
7
# File 'lib/offense_to_corrector/offense_parser.rb', line 5

def offense
  @offense
end

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/offense_to_corrector/offense_parser.rb', line 5

def source
  @source
end

Instance Method Details

#deconstruct_keys(keys) ⇒ Object

Quick info on what all is being worked on and what info we got



35
36
37
# File 'lib/offense_to_corrector/offense_parser.rb', line 35

def deconstruct_keys(keys)
  { ast:, source:, offense:, node_offense_info: }
end

#node_offense_infoObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/offense_to_corrector/offense_parser.rb', line 39

def node_offense_info
  return @node_offense_info if defined?(@node_offense_info)

  # Find the node, or atom, with the most overlap with the offense
  # range defined by that underline. That's defined as a pair of
  # the length of the intersection as well as what percentage that
  # intersection makes of the full node source.
  offending_node = @ast_nodes.max_by do |node|
    intersection = string_intersection(node.source, @offense.source)
    [intersection.size, intersection.size.fdiv(node.source.size)]
  end

  @node_offense_info ||= {
    offending_node:,
    offending_node_matcher: offending_node.to_s
  }
end

#renderObject

Render that into the cop skeleton. Perhaps having too much fun here with rightward assignment.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/offense_to_corrector/offense_parser.rb', line 17

def render
  call => {
    offense: { error: },
    node_offense_info: { offending_node:, offending_node_matcher: }
  }

  @template.render(
    class_name: "TODO",
    match_pattern: offending_node_matcher,
    error_message: error,
    node_type: offending_node.type,
    cop_type: "Lint",
    node_location: "selector",
    offense_severity: "warning"
  )
end