Class: Steep::Services::TypeCheckService::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/services/type_check_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, node:, content:, typing:, ignores:, errors:) ⇒ SourceFile

Returns a new instance of SourceFile.



18
19
20
21
22
23
24
25
# File 'lib/steep/services/type_check_service.rb', line 18

def initialize(path:, node:, content:, typing:, ignores:, errors:)
  @path = path
  @node = node
  @content = content
  @typing = typing
  @ignores = ignores
  @errors = errors
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



12
13
14
# File 'lib/steep/services/type_check_service.rb', line 12

def content
  @content
end

#errorsObject (readonly)

Returns the value of attribute errors.



15
16
17
# File 'lib/steep/services/type_check_service.rb', line 15

def errors
  @errors
end

#ignoresObject (readonly)

Returns the value of attribute ignores.



16
17
18
# File 'lib/steep/services/type_check_service.rb', line 16

def ignores
  @ignores
end

#nodeObject (readonly)

Returns the value of attribute node.



13
14
15
# File 'lib/steep/services/type_check_service.rb', line 13

def node
  @node
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/steep/services/type_check_service.rb', line 10

def path
  @path
end

#targetObject (readonly)

Returns the value of attribute target.



11
12
13
# File 'lib/steep/services/type_check_service.rb', line 11

def target
  @target
end

#typingObject (readonly)

Returns the value of attribute typing.



14
15
16
# File 'lib/steep/services/type_check_service.rb', line 14

def typing
  @typing
end

Class Method Details

.no_data(path:, content:) ⇒ Object



35
36
37
# File 'lib/steep/services/type_check_service.rb', line 35

def self.no_data(path:, content:)
  new(path: path, content: content, node: false, errors: nil, typing: nil, ignores: nil)
end

.with_syntax_error(path:, content:, error:) ⇒ Object



27
28
29
# File 'lib/steep/services/type_check_service.rb', line 27

def self.with_syntax_error(path:, content:, error:)
  new(path: path, node: false, content: content, errors: [error], typing: nil, ignores: nil)
end

.with_typing(path:, content:, typing:, node:, ignores:) ⇒ Object



31
32
33
# File 'lib/steep/services/type_check_service.rb', line 31

def self.with_typing(path:, content:, typing:, node:, ignores:)
  new(path: path, node: node, content: content, errors: nil, typing: typing, ignores: ignores)
end

Instance Method Details

#diagnosticsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/steep/services/type_check_service.rb', line 50

def diagnostics
  case
  when errors
    errors
  when typing && ignores
    errors = [] #: Array[Diagnostic::Ruby::Base]
    error_lines = [] #: Array[Integer]

    used_comments = Set[].compare_by_identity #: Set[Source::IgnoreRanges::ignore]

    typing.errors.each do |diagnostic|
      case diagnostic.location
      when ::Parser::Source::Range
        error_lines |= (diagnostic.location.first_line..diagnostic.location.last_line).to_a
        if ignore = ignores.ignore?(diagnostic.location.first_line, diagnostic.location.last_line, diagnostic.diagnostic_code)
          used_comments << ignore
          next
        end
      when RBS::Location
        if ignore = ignores.ignore?(diagnostic.location.start_line, diagnostic.location.end_line, diagnostic.diagnostic_code)
          used_comments << ignore
          next
        end
      end

      errors << diagnostic
    end

    ignores.each_ignore do |ignore|
      next if used_comments.include?(ignore)

      case ignore
      when Array
        location = RBS::Location.new(ignore[0].location.buffer, ignore[0].location.start_pos, ignore[1].location.end_pos)
      else
        location = ignore.location
      end

      errors << Diagnostic::Ruby::RedundantIgnoreComment.new(location: location)
    end

    ignores.error_ignores.each do |ignore|
      errors << Diagnostic::Ruby::InvalidIgnoreComment.new(comment: ignore.comment)
    end

    errors
  else
    []
  end
end

#update_content(content) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/steep/services/type_check_service.rb', line 39

def update_content(content)
  self.class.new(
    path: path,
    content: content,
    node: node,
    errors: errors,
    typing: typing,
    ignores: ignores
  )
end