Class: Steep::Source

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

Defined Under Namespace

Classes: Builder, LocatedAnnotation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, node:, mapping:) ⇒ Source

Returns a new instance of Source.



25
26
27
28
29
# File 'lib/steep/source.rb', line 25

def initialize(path:, node:, mapping:)
  @path = path
  @node = node
  @mapping = mapping
end

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



23
24
25
# File 'lib/steep/source.rb', line 23

def mapping
  @mapping
end

#nodeObject (readonly)

Returns the value of attribute node.



22
23
24
# File 'lib/steep/source.rb', line 22

def node
  @node
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/steep/source.rb', line 21

def path
  @path
end

Class Method Details

.construct_mapping(node:, annotations:, mapping:, line_range: nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/steep/source.rb', line 92

def self.construct_mapping(node:, annotations:, mapping:, line_range: nil)
  case node.type
  when :if
    if node.loc.is_a?(::Parser::Source::Map::Ternary)
      # Skip ternary operator
      each_child_node node do |child|
        construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil)
      end
    else
      if node.loc.expression.begin_pos == node.loc.keyword.begin_pos
        construct_mapping(node: node.children[0],
                          annotations: annotations,
                          mapping: mapping,
                          line_range: nil)

        if node.children[1]
          if node.loc.keyword.source == "if" || node.loc.keyword.source == "elsif"
            then_start = node.loc.begin&.loc&.last_line || node.children[0].loc.last_line
            then_end = node.children[2] ? node.loc.else.line : node.loc.last_line
          else
            then_start = node.loc.else.last_line
            then_end = node.loc.last_line
          end
          construct_mapping(node: node.children[1],
                            annotations: annotations,
                            mapping: mapping,
                            line_range: then_start...then_end)
        end

        if node.children[2]
          if node.loc.keyword.source == "if" || node.loc.keyword.source == "elsif"
            else_start = node.loc.else.last_line
            else_end = node.loc.last_line
          else
            else_start = node.loc.begin&.last_line || node.children[0].loc.last_line
            else_end = node.children[1] ? node.loc.else.line : node.loc.last_line
          end
          construct_mapping(node: node.children[2],
                            annotations: annotations,
                            mapping: mapping,
                            line_range: else_start...else_end)
        end

      else
        # postfix if/unless
        each_child_node(node) do |child|
          construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil)
        end
      end
    end

  when :while, :until
    if node.loc.expression.begin_pos == node.loc.keyword.begin_pos
      construct_mapping(node: node.children[0],
                        annotations: annotations,
                        mapping: mapping,
                        line_range: nil)

      if node.children[1]
        body_start = node.children[0].loc.last_line
        body_end = node.loc.end.line

        construct_mapping(node: node.children[1],
                          annotations: annotations,
                          mapping: mapping,
                          line_range: body_start...body_end)
      end

    else
      # postfix while
      each_child_node(node) do |child|
        construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil)
      end
    end

  when :while_post, :until_post
    construct_mapping(node: node.children[0],
                      annotations: annotations,
                      mapping: mapping,
                      line_range: nil)

    if node.children[1]
      body_start = node.loc.expression.line
      body_end = node.loc.keyword.line

      construct_mapping(node: node.children[1],
                        annotations: annotations,
                        mapping: mapping,
                        line_range: body_start...body_end)
    end

  when :case
    if node.children[0]
      construct_mapping(node: node.children[0], annotations: annotations, mapping: mapping, line_range: nil)
    end

    if node.loc.else
      else_node = node.children.last
      else_start = node.loc.else.last_line
      else_end = node.loc.end.line

      construct_mapping(node: else_node,
                        annotations: annotations,
                        mapping: mapping,
                        line_range: else_start...else_end)
    end

    node.children.drop(1).each do |child|
      if child&.type == :when
        construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil)
      end
    end

  when :when
    last_cond = node.children[-2]
    body = node.children.last

    node.children.take(node.children.size-1) do |child|
      construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil)
    end

    if body
      cond_end = last_cond.loc.last_line+1
      body_end = body.loc.last_line
      construct_mapping(node: body,
                        annotations: annotations,
                        mapping: mapping,
                        line_range: cond_end...body_end)
    end

  when :rescue
    if node.children.last
      else_node = node.children.last
      else_start = node.loc.else.last_line
      else_end = node.loc.last_line

      construct_mapping(node: else_node,
                        annotations: annotations,
                        mapping: mapping,
                        line_range: else_start...else_end)
    end

    each_child_node(node) do |child|
      construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil)
    end

  else
    each_child_node(node) do |child|
      construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil)
    end
  end

  associated_annotations = annotations.select do |annot|
    case node.type
    when :def, :module, :class, :block, :ensure, :defs
      loc = node.loc
      loc.line <= annot.line && annot.line < loc.last_line

    when :resbody
      if node.loc.keyword.begin_pos == node.loc.expression.begin_pos
        # skip postfix rescue
        loc = node.loc
        loc.line <= annot.line && annot.line < loc.last_line
      end
    else
      if line_range
        line_range.begin <= annot.line && annot.line < line_range.end
      end
    end
  end

  associated_annotations.each do |annot|
    mapping[node.__id__] = [] unless mapping.key?(node.__id__)
    mapping[node.__id__] << annot.annotation
    annotations.delete annot
  end
end

.each_child_node(node) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/steep/source.rb', line 270

def self.each_child_node(node)
  node.children.each do |child|
    if child.is_a?(::AST::Node)
      yield child
    end
  end
end

.parse(source_code, path:, factory:, labeling: ASTUtils::Labeling.new) ⇒ Object



47
48
49
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
# File 'lib/steep/source.rb', line 47

def self.parse(source_code, path:, factory:, labeling: ASTUtils::Labeling.new)
  buffer = ::Parser::Source::Buffer.new(path.to_s, 1)
  buffer.source = source_code
  node = parser.parse(buffer).yield_self do |n|
    if n
      labeling.translate(n, {})
    else
      return
    end
  end

  annotations = []

  _, comments, _ = yield_self do
    buffer = ::Parser::Source::Buffer.new(path.to_s)
    buffer.source = source_code
    parser = ::Parser::Ruby25.new

    parser.tokenize(buffer)
  end

  buffer = AST::Buffer.new(name: path, content: source_code)

  comments.each do |comment|
    src = comment.text.gsub(/\A#\s*/, '')
    location = AST::Location.new(buffer: buffer,
                                 start_pos: comment.location.expression.begin_pos + 1,
                                 end_pos: comment.location.expression.end_pos)
    annotation = AnnotationParser.new(factory: factory).parse(src, location: location)
    if annotation
      annotations << LocatedAnnotation.new(line: comment.location.line, source: src, annotation: annotation)
    end
  end

  mapping = {}
  construct_mapping(node: node, annotations: annotations, mapping: mapping)

  annotations.each do |annot|
    mapping[node.__id__] = [] unless mapping.key?(node.__id__)
    mapping[node.__id__] << annot.annotation
  end

  new(path: path, node: node, mapping: mapping)
end

.parserObject



40
41
42
43
44
45
# File 'lib/steep/source.rb', line 40

def self.parser
  ::Parser::Ruby25.new(Builder.new).tap do |parser|
    parser.diagnostics.all_errors_are_fatal = true
    parser.diagnostics.ignore_warnings = true
  end
end

Instance Method Details

#annotations(block:, factory:, current_module:) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/steep/source.rb', line 278

def annotations(block:, factory:, current_module:)
  AST::Annotation::Collection.new(
    annotations: mapping[block.__id__] || [],
    factory: factory,
    current_module: current_module
  )
end

#each_annotationObject



286
287
288
289
290
291
292
293
294
295
# File 'lib/steep/source.rb', line 286

def each_annotation
  if block_given?
    mapping.each_key do |id|
      node = ObjectSpace._id2ref(id)
      yield node, mapping[id]
    end
  else
    enum_for :each_annotation
  end
end

#find_nodes(line:, column:, node: self.node, position: nil, parents: []) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/steep/source.rb', line 297

def find_nodes(line:, column:, node: self.node, position: nil, parents: [])
  position ||= (line-1).times.sum do |i|
    node.location.expression.source_buffer.source_line(i+1).size + 1
  end + column

  range = node.location.expression&.yield_self do |r|
    r.begin_pos..r.end_pos
  end

  if range
    if range === position
      parents.unshift node

      Source.each_child_node(node) do |child|
        ns = find_nodes(line: line, column: column, node: child, position: position, parents: parents) and return ns
      end

      parents
    end
  end
end