Class: Txray::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/txray/source_file.rb

Constant Summary collapse

DIRECTIVE =
/\A#\s*txray:(?<action>disable|enable)\b(?<rules>[\w,\s-]*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, root:, comments: [], code: "") ⇒ SourceFile

Returns a new instance of SourceFile.



19
20
21
22
23
24
25
26
# File 'lib/txray/source_file.rb', line 19

def initialize(path:, root:, comments: [], code: "")
  @path = path
  @root = root
  @lines = code.lines
  @inline = {}
  @regions = []
  index(comments)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/txray/source_file.rb', line 7

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/txray/source_file.rb', line 7

def root
  @root
end

Class Method Details

.parse(path, code: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/txray/source_file.rb', line 9

def self.parse(path, code: nil)
  code ||= File.read(path)
  result = Prism.parse(code)
  return nil if result.failure?

  new(path: path, root: result.value, comments: result.comments, code: code)
rescue StandardError
  nil
end

Instance Method Details

#disabled?(line, rule_id) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/txray/source_file.rb', line 28

def disabled?(line, rule_id)
  return true if covers?(@inline[line], rule_id)

  @regions.any? { |region| line.between?(region[:from], region[:to]) && covers?(region[:rules], rule_id) }
end