Class: Steep::Source
- Extended by:
- ModuleHelper, NodeHelper
- Defined in:
- lib/steep/source.rb,
lib/steep/source/ignore_ranges.rb
Defined Under Namespace
Classes: Builder, IgnoreRanges
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#comments ⇒ Object
readonly
Returns the value of attribute comments.
-
#ignores ⇒ Object
readonly
Returns the value of attribute ignores.
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .adjust_location(node) ⇒ Object
- .assertion_node(node, type) ⇒ Object
- .construct_mapping(node:, annotations:, mapping:, line_range: nil) ⇒ Object
- .delete_defs(node, allow_list) ⇒ Object
- .insert_type_node(node, comments) ⇒ Object
- .map_child_node(node, type = nil, skip: nil) ⇒ Object
- .new_parser ⇒ Object
- .parse(source_code, path:, factory:) ⇒ Object
- .sendish_node?(node) ⇒ Boolean
- .skip_arg_assertions(node) ⇒ Object
- .type_application_node(node, tapp) ⇒ Object
Instance Method Summary collapse
- #annotations(block:, factory:, context:) ⇒ Object
- #each_annotation(&block) ⇒ Object
- #each_block_annotation(node, &block) ⇒ Object
- #each_heredoc_node(node = self.node, parents = [], &block) ⇒ Object
- #find_block_node(nodes) ⇒ Object
- #find_comment(line:, column:) ⇒ Object
- #find_heredoc_nodes(line, column, position) ⇒ Object
- #find_nodes(line:, column:) ⇒ Object
- #find_nodes_loc(node, position, parents) ⇒ Object
-
#initialize(buffer:, path:, node:, mapping:, comments:, ignores:) ⇒ Source
constructor
A new instance of Source.
- #without_unrelated_defs(line:, column:) ⇒ Object
Methods included from NodeHelper
clone_node, deconstruct_case_node, deconstruct_case_node!, deconstruct_if_node, deconstruct_if_node!, deconstruct_resbody_node, deconstruct_resbody_node!, deconstruct_rescue_node, deconstruct_rescue_node!, deconstruct_send_node, deconstruct_send_node!, deconstruct_sendish_and_block_nodes, deconstruct_when_node, deconstruct_when_node!, deconstruct_whileish_node, deconstruct_whileish_node!, each_child_node, each_descendant_node, private_send?, test_case_node, test_if_node, test_resbody_node, test_rescue_node, test_send_node, test_when_node, test_whileish_node, value_node?
Methods included from ModuleHelper
module_name_from_node, namespace_from_node
Constructor Details
#initialize(buffer:, path:, node:, mapping:, comments:, ignores:) ⇒ Source
Returns a new instance of Source.
13 14 15 16 17 18 19 20 |
# File 'lib/steep/source.rb', line 13 def initialize(buffer:, path:, node:, mapping:, comments:, ignores:) @buffer = buffer @path = path @node = node @mapping = mapping @comments = comments @ignores = ignores end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
3 4 5 |
# File 'lib/steep/source.rb', line 3 def buffer @buffer end |
#comments ⇒ Object (readonly)
Returns the value of attribute comments.
7 8 9 |
# File 'lib/steep/source.rb', line 7 def comments @comments end |
#ignores ⇒ Object (readonly)
Returns the value of attribute ignores.
8 9 10 |
# File 'lib/steep/source.rb', line 8 def ignores @ignores end |
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
6 7 8 |
# File 'lib/steep/source.rb', line 6 def mapping @mapping end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
5 6 7 |
# File 'lib/steep/source.rb', line 5 def node @node end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/steep/source.rb', line 4 def path @path end |
Class Method Details
.adjust_location(node) ⇒ Object
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 |
# File 'lib/steep/source.rb', line 672 def self.adjust_location(node) if end_pos = node.location.expression&.end_pos if last_pos = each_child_node(node).map {|node| node.location.expression&.end_pos }.compact.max if last_pos > end_pos props = { location: node.location.with_expression(node.location.expression.with(end_pos: last_pos)) } end end end if props node.updated(nil, nil, props) else node end end |
.assertion_node(node, type) ⇒ Object
688 689 690 691 |
# File 'lib/steep/source.rb', line 688 def self.assertion_node(node, type) map = Parser::Source::Map.new(node.location.expression.with(end_pos: type.location.end_pos)) Parser::AST::Node.new(:assertion, [node, type], { location: map }) end |
.construct_mapping(node:, annotations:, mapping:, line_range: nil) ⇒ Object
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 269 270 271 272 273 274 275 276 |
# File 'lib/steep/source.rb', line 101 def self.construct_mapping(node:, annotations:, mapping:, line_range: nil) case node.type when :if cond_node, truthy_node, falsy_node, loc = deconstruct_if_node!(node) if node.loc.respond_to?(:question) # Skip ternary operator each_child_node node do |child| construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end else if_loc = loc #: NodeHelper::condition_loc if if_loc.expression.begin_pos == if_loc.keyword.begin_pos construct_mapping(node: cond_node,annotations: annotations, mapping: mapping, line_range: nil) if truthy_node if if_loc.keyword.source == "if" || if_loc.keyword.source == "elsif" # if foo # bar <= # end then_start = if_loc.begin&.last_line || cond_node.loc.last_line then_end = if_loc.else&.line || if_loc.last_line else # unless foo # else # bar <= # end if_loc.else or raise then_start = if_loc.else.last_line then_end = loc.last_line end construct_mapping(node: truthy_node, annotations: annotations, mapping: mapping, line_range: then_start...then_end) end if falsy_node if if_loc.keyword.source == "if" || if_loc.keyword.source == "elsif" # if foo # else # bar <= # end if_loc.else or raise else_start = if_loc.else.last_line else_end = if_loc.last_line else # unless foo # bar <= # end else_start = if_loc.begin&.last_line || cond_node.loc.last_line else_end = if_loc.else&.line || if_loc.last_line end construct_mapping(node: falsy_node, 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 cond_node, body_node, loc = deconstruct_whileish_node!(node) if loc.expression.begin_pos == loc.keyword.begin_pos # prefix while loc.end or raise construct_mapping(node: cond_node, annotations: annotations, mapping: mapping, line_range: nil) if body_node body_start = cond_node.loc.last_line body_end = loc.end.line construct_mapping(node: body_node, 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 cond_node, body_node, loc = deconstruct_whileish_node!(node) construct_mapping(node: cond_node, annotations: annotations, mapping: mapping, line_range: nil) if body_node body_start = loc.expression.line body_end = loc.keyword.line construct_mapping(node: body_node, annotations: annotations, mapping: mapping, line_range: body_start...body_end) end when :case cond_node, when_nodes, else_node, loc = deconstruct_case_node!(node) if cond_node construct_mapping(node: cond_node, annotations: annotations, mapping: mapping, line_range: nil) end if else_node loc.else or raise loc.end or raise else_start = loc.else.last_line else_end = loc.end.line construct_mapping(node: else_node, annotations: annotations, mapping: mapping, line_range: else_start...else_end) end when_nodes.each do |child| if child.type == :when construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end end when :when operands, body, loc = deconstruct_when_node!(node) last_cond = operands.last or raise operands.each do |child| construct_mapping(node: child, annotations: annotations, mapping: mapping, line_range: nil) end if body cond_end = loc.begin&.last_line || 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 body, resbodies, else_node, loc = deconstruct_rescue_node!(node) if else_node loc.else or raise else_start = loc.else.last_line else_end = 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, other_annotations = annotations.partition do |annot| location = node.loc annot.line or next case node.type when :def, :module, :class, :block, :numblock, :ensure, :defs, :resbody location = node.loc location.line <= annot.line && annot.line < location.last_line else if line_range line_range.begin <= annot.line && annot.line < line_range.end end end end associated_annotations.each do |annot| mapping[node] ||= [] mapping.fetch(node) << annot end annotations.replace(other_annotations) end |
.delete_defs(node, allow_list) ⇒ Object
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/steep/source.rb', line 422 def self.delete_defs(node, allow_list) case node.type when :def if allow_list.include?(node) node else node.updated(:nil, []) end when :defs if allow_list.include?(node) node else delete_defs(node.children[0], allow_list) end else map_child_node(node) do |child| delete_defs(child, allow_list) end end end |
.insert_type_node(node, comments) ⇒ Object
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'lib/steep/source.rb', line 494 def self.insert_type_node(node, comments) if node.location.expression first_line = node.location.expression.first_line last_line = node.location.expression.last_line last_comment = comments[last_line] if (first_line..last_line).none? {|l| comments.key?(l) } return node end case when last_comment.is_a?(AST::Node::TypeAssertion) case node.type when :lvasgn, :ivasgn, :gvasgn, :cvasgn, :casgn # Skip when :return, :break, :next # Skip when :def, :defs # Skip when :kwargs # skip when :when # skip when :pair key, value = node.children key = insert_type_node(key, comments.except(last_line)) value = insert_type_node(value, comments) node = node.updated(nil, [key, value]) return adjust_location(node) when :masgn lhs, rhs = node.children node = node.updated(nil, [lhs, insert_type_node(rhs, comments)]) return adjust_location(node) when :begin location = node.loc #: Parser::Source::Map & Parser::AST::_Collection if location.begin # paren child_assertions = comments.except(last_line) node = map_child_node(node) {|child| insert_type_node(child, child_assertions) } node = adjust_location(node) return assertion_node(node, last_comment) end else if (receiver, name, * = deconstruct_send_node(node)) if receiver.nil? if name == :attr_reader || name == :attr_writer || name == :attr_accessor child_assertions = comments.except(last_line) node = map_child_node(node) {|child| insert_type_node(child, child_assertions) } return adjust_location(node) end end end child_assertions = comments.except(last_line) node = map_child_node(node) {|child| insert_type_node(child, child_assertions) } node = adjust_location(node) return assertion_node(node, last_comment) end when selector_line = sendish_node?(node) if (comment = comments[selector_line]).is_a?(AST::Node::TypeApplication) child_assertions = comments.except(selector_line) case node.type when :block send, *children = node.children node = node.updated( nil, [ map_child_node(send) {|child| insert_type_node(child, child_assertions) }, *children.map do |child| if child insert_type_node(child, child_assertions) end end ] ) when :numblock send, size, body = node.children node = node.updated( nil, [ map_child_node(send) {|child| insert_type_node(child, child_assertions) }, size, insert_type_node(body, child_assertions) ] ) else node = map_child_node(node) {|child| insert_type_node(child, child_assertions) } end node = adjust_location(node) return type_application_node(node, comment) end end end case node.type when :class class_name, super_class, class_body = node.children adjust_location( node.updated( nil, [ class_name, super_class, class_body ? insert_type_node(class_body, comments) : nil ] ) ) when :module module_name, module_body = node.children adjust_location( node.updated( nil, [ module_name, module_body ? insert_type_node(module_body, comments) : nil ] ) ) when :def name, args, body = node.children assertion_location = args&.location&.expression || (_ = node.location).name no_assertion_comments = comments.except(assertion_location.last_line) args = insert_type_node(args, no_assertion_comments) body = insert_type_node(body, comments) if body return adjust_location(node.updated(nil, [name, args, body])) when :defs object, name, args, body = node.children assertion_location = args&.location&.expression || (_ = node.location).name no_assertion_comments = comments.except(assertion_location.last_line) object = insert_type_node(object, no_assertion_comments) args = insert_type_node(args, no_assertion_comments) body = insert_type_node(body, comments) if body return adjust_location(node.updated(nil, [object, name, args, body])) else if skip_arg_assertions(node) # Data.define, Struct.new, ...?? if node.location.expression first_line = node.location.expression.first_line last_line = node.location.expression.last_line child_assertions = comments.delete_if {|line, _ | first_line < line && line < last_line } node = map_child_node(node) {|child| insert_type_node(child, child_assertions) } return adjust_location(node) end end adjust_location( map_child_node(node, nil) {|child| insert_type_node(child, comments) } ) end end |
.map_child_node(node, type = nil, skip: nil) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/steep/source.rb', line 278 def self.map_child_node(node, type = nil, skip: nil) children = node.children.map do |child| if child.is_a?(Parser::AST::Node) if skip if skip.member?(child) child else yield child end else yield child end else child end end node.updated(type, children) end |
.new_parser ⇒ Object
33 34 35 36 37 38 |
# File 'lib/steep/source.rb', line 33 def self.new_parser Prism::Translation::Parser33.new(Builder.new).tap do |parser| parser.diagnostics.all_errors_are_fatal = true parser.diagnostics.ignore_warnings = true end end |
.parse(source_code, path:, factory:) ⇒ Object
40 41 42 43 44 45 46 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 91 92 93 94 95 96 97 98 99 |
# File 'lib/steep/source.rb', line 40 def self.parse(source_code, path:, factory:) buffer = ::Parser::Source::Buffer.new(path.to_s, 1, source: source_code) node, comments = new_parser().parse_with_comments(buffer) # @type var annotations: Array[AST::Annotation::t] annotations = [] # @type var type_comments: Hash[Integer, type_comment] type_comments = {} buffer = RBS::Buffer.new(name: path, content: source_code) annotation_parser = AnnotationParser.new(factory: factory) comments = comments.sort_by do |comment| comment.loc.expression.begin_pos end comments.each do |comment| if comment.inline? content = comment.text.delete_prefix('#') content.lstrip! prefix = comment.text.size - content.size content.rstrip! suffix = comment.text.size - content.size - prefix location = RBS::Location.new( buffer: buffer, start_pos: comment.location.expression.begin_pos + prefix, end_pos: comment.location.expression.end_pos - suffix ) case when annotation = annotation_parser.parse(content, location: location) annotations << annotation when assertion = AST::Node::TypeAssertion.parse(location) type_comments[assertion.line] = assertion when tapp = AST::Node::TypeApplication.parse(location) type_comments[tapp.line] = tapp end end end map = {} #: Hash[Parser::AST::Node, Array[AST::Annotation::t]] map.compare_by_identity if node node = insert_type_node(node, type_comments) construct_mapping(node: node, annotations: annotations, mapping: map) end annotations.each do |annot| map[node] ||= [] map.fetch(node) << annot end ignores = comments.filter_map do |comment| AST::Ignore.parse(comment, buffer) end new(buffer: buffer, path: path, node: node, mapping: map, comments: comments, ignores: ignores) end |
.sendish_node?(node) ⇒ Boolean
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
# File 'lib/steep/source.rb', line 646 def self.sendish_node?(node) send_node = case node.type when :send, :csend node when :block, :numblock send = node.children[0] case send.type when :send, :csend send end end if send_node receiver_node, name, _, location = deconstruct_send_node!(send_node) if receiver_node if location.dot && location.selector location.selector.line end else location.selector.line end end end |
.skip_arg_assertions(node) ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/steep/source.rb', line 468 def self.skip_arg_assertions(node) send_node, _ = deconstruct_sendish_and_block_nodes(node) return false unless send_node if send_node.type == :send receiver, method, args = deconstruct_send_node!(send_node) return false unless receiver if receiver.type == :const if type_name = module_name_from_node(receiver.children[0], receiver.children[1]) if type_name.namespace.empty? if type_name.name == :Data && method == :define return true end if type_name.name == :Struct && method == :new return true end end end end end false end |
.type_application_node(node, tapp) ⇒ Object
693 694 695 696 697 698 699 700 701 702 703 |
# File 'lib/steep/source.rb', line 693 def self.type_application_node(node, tapp) if node.location.expression.end_pos > tapp.location.end_pos map = Parser::Source::Map.new(node.location.expression) else map = Parser::Source::Map.new(node.location.expression.with(end_pos: tapp.location.end_pos)) end node = Parser::AST::Node.new(:tapp, [node, tapp], { location: map }) tapp.set_node(node) node end |
Instance Method Details
#annotations(block:, factory:, context:) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/steep/source.rb', line 298 def annotations(block:, factory:, context:) annotations = if block mapping.fetch(block, []) else [] end #: Array[AST::Annotation::t] AST::Annotation::Collection.new( annotations: annotations, factory: factory, context: context ) end |
#each_annotation(&block) ⇒ Object
312 313 314 315 316 317 318 319 320 |
# File 'lib/steep/source.rb', line 312 def each_annotation(&block) if block_given? mapping.each do |node, annots| yield [node, annots] end else enum_for :each_annotation end end |
#each_block_annotation(node, &block) ⇒ Object
322 323 324 325 326 327 328 329 330 |
# File 'lib/steep/source.rb', line 322 def each_block_annotation(node, &block) if block if annots = mapping.fetch(node, nil) annots.each(&block) end else enum_for :each_block_annotation, node end end |
#each_heredoc_node(node = self.node, parents = [], &block) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/steep/source.rb', line 336 def each_heredoc_node(node = self.node, parents = [], &block) if block return unless node case node.type when :dstr, :str if node.location.respond_to?(:heredoc_body) yield [[node, *parents], _ = node.location] end end parents.unshift(node) Source.each_child_node(node) do |child| each_heredoc_node(child, parents, &block) end parents.shift() else enum_for :each_heredoc_node, node end end |
#find_block_node(nodes) ⇒ Object
332 333 334 |
# File 'lib/steep/source.rb', line 332 def find_block_node(nodes) nodes.find { mapping.key?(_1) } end |
#find_comment(line:, column:) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/steep/source.rb', line 408 def find_comment(line:, column:) pos = buffer.loc_to_pos([line, column]) comment = comments.bsearch do |comment| pos <= comment.loc.expression.end_pos end if comment if comment.loc.expression.begin_pos < pos comment end end end |
#find_heredoc_nodes(line, column, position) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/steep/source.rb', line 357 def find_heredoc_nodes(line, column, position) each_heredoc_node() do |nodes, location| node = nodes[0] loc = location.heredoc_body #: Parser::Source::Range if range = loc.to_range if range.begin <= position && position <= range.end return nodes end end end nil end |
#find_nodes(line:, column:) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/steep/source.rb', line 390 def find_nodes(line:, column:) return [] unless node position = buffer.loc_to_pos([line, column]) if heredoc_nodes = find_heredoc_nodes(line, column, position) Source.each_child_node(heredoc_nodes.fetch(0)) do |child| if nodes = find_nodes_loc(child, position, heredoc_nodes) return nodes end end return heredoc_nodes else find_nodes_loc(node, position, []) end end |
#find_nodes_loc(node, position, parents) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/steep/source.rb', line 372 def find_nodes_loc(node, position, parents) range = node.location.expression&.to_range if range if range.begin <= position && position <= range.end parents.unshift node Source.each_child_node(node) do |child| if ns = find_nodes_loc(child, position, parents) return ns end end parents end end end |
#without_unrelated_defs(line:, column:) ⇒ Object
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/steep/source.rb', line 443 def (line:, column:) if node nodes = find_nodes(line: line, column: column) || [] defs = Set[].compare_by_identity.merge(nodes.select {|node| node.type == :def || node.type == :defs }) node_ = Source.delete_defs(node, defs) # @type var mapping: Hash[Parser::AST::Node, Array[AST::Annotation::t]] mapping = {} mapping.compare_by_identity annotations = self.mapping.values.flatten Source.construct_mapping(node: node_, annotations: annotations, mapping: mapping) annotations.each do |annot| mapping[node_] ||= [] mapping.fetch(node_) << annot end Source.new(buffer: buffer, path: path, node: node_, mapping: mapping, comments: comments, ignores: ignores) else self end end |