Class: Hone::Patterns::Base

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/hone/patterns/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Base

Returns a new instance of Base.



53
54
55
56
# File 'lib/hone/patterns/base.rb', line 53

def initialize(file_path)
  @file_path = file_path
  @findings = []
end

Class Attribute Details

.optimization_typeObject

Returns the value of attribute optimization_type.



37
38
39
# File 'lib/hone/patterns/base.rb', line 37

def optimization_type
  @optimization_type
end

.pattern_idObject

Returns the value of attribute pattern_id.



37
38
39
# File 'lib/hone/patterns/base.rb', line 37

def pattern_id
  @pattern_id
end

.scopeObject



39
40
41
# File 'lib/hone/patterns/base.rb', line 39

def scope
  @scope || :ruby
end

Instance Attribute Details

#findingsObject (readonly)

Returns the value of attribute findings.



58
59
60
# File 'lib/hone/patterns/base.rb', line 58

def findings
  @findings
end

Class Method Details

.inherited(subclass) ⇒ Object



33
34
35
# File 'lib/hone/patterns/base.rb', line 33

def self.inherited(subclass)
  Patterns.register(subclass)
end

.scan_file(path) ⇒ Object



46
47
48
49
50
51
# File 'lib/hone/patterns/base.rb', line 46

def self.scan_file(path)
  result = Prism.parse_file(path)
  pattern = new(path)
  result.value.accept(pattern)
  pattern.findings
end

Instance Method Details

#add_finding(node, message:, speedup: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hone/patterns/base.rb', line 60

def add_finding(node, message:, speedup: nil)
  @findings << Finding.new(
    file: @file_path,
    line: node.location.start_line,
    column: node.location.start_column,
    pattern_id: self.class.pattern_id,
    optimization_type: self.class.optimization_type,
    source: :hone,
    message: message,
    speedup: speedup,
    code: node.location.slice
  )
end