summaryrefslogtreecommitdiff
path: root/spec/syntax_suggest/unit/visitor_spec.rb
blob: 94eefd1e958c3b0dca014371b63bf327bfc69e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true

require_relative "../spec_helper"

module SyntaxSuggest
  RSpec.describe Visitor do
    def visit(source)
      ast, _tokens = Prism.parse_lex(source).value
      visitor = Visitor.new
      visitor.visit(ast)
      visitor
    end

    describe "#consecutive_lines" do
      it "detects dot-leading multi-line chains" do
        visitor = visit(<<~RUBY)
          User
            .where(name: "Earlopain")
            .first
        RUBY

        expect(visitor.consecutive_lines).to eq(Set[1, 2])
      end

      it "detects dot-trailing multi-line chains" do
        visitor = visit(<<~RUBY)
          User.
            where(name: "Earlopain").
            first
        RUBY

        expect(visitor.consecutive_lines).to eq(Set[1, 2])
      end

      it "handles chains separated by comments" do
        visitor = visit(<<~RUBY)
          User.
            # comment
            where(name: "Earlopain").
            # another comment
            first
        RUBY

        # The AST sees through comments — every line except
        # the last is consecutive regardless of interleaved comments.
        expect(visitor.consecutive_lines).to eq(Set[1, 2, 3, 4])
      end

      it "returns empty for single-line calls" do
        visitor = visit(<<~RUBY)
          User.where(name: "Earlopain").first
        RUBY

        expect(visitor.consecutive_lines).to be_empty
      end

      it "returns empty when there is no method chain" do
        visitor = visit(<<~RUBY)
          puts "hello"
          puts "world"
        RUBY

        expect(visitor.consecutive_lines).to be_empty
      end

      it "handles deeply nested chains" do
        visitor = visit(<<~RUBY)
          User
            .where(name: "Earlopain")
            .order(:created_at)
            .limit(10)
            .first
        RUBY

        expect(visitor.consecutive_lines).to eq(Set[1, 2, 3, 4])
      end
    end

    describe "#endless_def_keyword_offsets" do
      it "records the def location for endless methods" do
        visitor = visit(<<~RUBY)
          def square(x) = x * x
        RUBY

        expect(visitor.endless_def_keyword_offsets).to eq(Set[0])
      end

      it "does not record regular method definitions" do
        visitor = visit(<<~RUBY)
          def square(x)
            x * x
          end
        RUBY

        expect(visitor.endless_def_keyword_offsets).to be_empty
      end

      it "records multiple endless methods" do
        visitor = visit(<<~RUBY)
          def square(x) = x * x
          def double(x) = x * 2
        RUBY

        expect(visitor.endless_def_keyword_offsets).to eq(Set[0, 22])
      end

      it "distinguishes endless from regular in the same source" do
        visitor = visit(<<~RUBY)
          def square(x) = x * x
          def cube(x)
            x * x * x
          end
        RUBY

        expect(visitor.endless_def_keyword_offsets).to eq(Set[0])
      end
    end
  end
end