|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../spec_helper" |
| 4 | + |
| 5 | +module SyntaxSuggest |
| 6 | + RSpec.describe Visitor do |
| 7 | + def visit(source) |
| 8 | + ast, _tokens = Prism.parse_lex(source).value |
| 9 | + visitor = Visitor.new |
| 10 | + visitor.visit(ast) |
| 11 | + visitor |
| 12 | + end |
| 13 | + |
| 14 | + describe "#consecutive_lines" do |
| 15 | + it "detects dot-leading multi-line chains" do |
| 16 | + visitor = visit(<<~RUBY) |
| 17 | + User |
| 18 | + .where(name: "Earlopain") |
| 19 | + .first |
| 20 | + RUBY |
| 21 | + |
| 22 | + expect(visitor.consecutive_lines).to eq([1, 2]) |
| 23 | + end |
| 24 | + |
| 25 | + it "detects dot-trailing multi-line chains" do |
| 26 | + visitor = visit(<<~RUBY) |
| 27 | + User. |
| 28 | + where(name: "Earlopain"). |
| 29 | + first |
| 30 | + RUBY |
| 31 | + |
| 32 | + expect(visitor.consecutive_lines).to eq([1, 2]) |
| 33 | + end |
| 34 | + |
| 35 | + it "handles chains separated by comments" do |
| 36 | + visitor = visit(<<~RUBY) |
| 37 | + User. |
| 38 | + # comment |
| 39 | + where(name: "Earlopain"). |
| 40 | + # another comment |
| 41 | + first |
| 42 | + RUBY |
| 43 | + |
| 44 | + # The AST sees through comments — every line except |
| 45 | + # the last is consecutive regardless of interleaved comments. |
| 46 | + expect(visitor.consecutive_lines).to eq([1, 2, 3, 4]) |
| 47 | + end |
| 48 | + |
| 49 | + it "returns empty for single-line calls" do |
| 50 | + visitor = visit(<<~RUBY) |
| 51 | + User.where(name: "Earlopain").first |
| 52 | + RUBY |
| 53 | + |
| 54 | + expect(visitor.consecutive_lines).to eq([]) |
| 55 | + end |
| 56 | + |
| 57 | + it "returns empty when there is no method chain" do |
| 58 | + visitor = visit(<<~RUBY) |
| 59 | + puts "hello" |
| 60 | + puts "world" |
| 61 | + RUBY |
| 62 | + |
| 63 | + expect(visitor.consecutive_lines).to eq([]) |
| 64 | + end |
| 65 | + |
| 66 | + it "handles deeply nested chains" do |
| 67 | + visitor = visit(<<~RUBY) |
| 68 | + User |
| 69 | + .where(name: "Earlopain") |
| 70 | + .order(:created_at) |
| 71 | + .limit(10) |
| 72 | + .first |
| 73 | + RUBY |
| 74 | + |
| 75 | + expect(visitor.consecutive_lines).to eq([1, 2, 3, 4]) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + describe "#endless_def_keyword_locs" do |
| 80 | + it "records the def location for endless methods" do |
| 81 | + visitor = visit(<<~RUBY) |
| 82 | + def square(x) = x * x |
| 83 | + RUBY |
| 84 | + |
| 85 | + expect(visitor.endless_def_keyword_locs.length).to eq(1) |
| 86 | + expect(visitor.endless_def_keyword_locs.first.start_line).to eq(1) |
| 87 | + end |
| 88 | + |
| 89 | + it "does not record regular method definitions" do |
| 90 | + visitor = visit(<<~RUBY) |
| 91 | + def square(x) |
| 92 | + x * x |
| 93 | + end |
| 94 | + RUBY |
| 95 | + |
| 96 | + expect(visitor.endless_def_keyword_locs).to be_empty |
| 97 | + end |
| 98 | + |
| 99 | + it "records multiple endless methods" do |
| 100 | + visitor = visit(<<~RUBY) |
| 101 | + def square(x) = x * x |
| 102 | + def double(x) = x * 2 |
| 103 | + RUBY |
| 104 | + |
| 105 | + expect(visitor.endless_def_keyword_locs.length).to eq(2) |
| 106 | + end |
| 107 | + |
| 108 | + it "distinguishes endless from regular in the same source" do |
| 109 | + visitor = visit(<<~RUBY) |
| 110 | + def square(x) = x * x |
| 111 | + def cube(x) |
| 112 | + x * x * x |
| 113 | + end |
| 114 | + RUBY |
| 115 | + |
| 116 | + expect(visitor.endless_def_keyword_locs.length).to eq(1) |
| 117 | + expect(visitor.endless_def_keyword_locs.first.start_line).to eq(1) |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments