ruby / syntax_suggest

Searching for unexpected `end` syntax errors takes a lot of time. Let this gem do it for you!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improvement Opportunity

schneems opened this issue · comments

From https://gist.github.com/schneems/c80391c83c03f0c1d9be7865ce392cdf

       5  module DerailedBenchmarks
       6    class RequireTree
       7      REQUIRED_BY = {}
       9      attr_reader   :name
      10      attr_writer   :cost
      11      attr_accessor :parent
    ❯ 13      def initialize(name)
    ❯ 27      def <<(tree)
    ❯ 31      end
    ❯ 33      def [](name)
    ❯ 35      end
    ❯ 38      def children
    ❯ 40      end
    ❯ 42      def cost
    ❯ 44      end
    ❯ 47      def sorted_children
    ❯ 49      end
      73    end
      74  end

I'm not sure why ALL of these extra methods are also included in the output. The problem is on line 13

    ❯ 13      def initialize(name)

Debugging. Starting at 57-add-30.txt

       2
       5  module DerailedBenchmarks
       6    class RequireTree
       7      REQUIRED_BY = {}
       8
       9      attr_reader   :name
      10      attr_writer   :cost
      11      attr_accessor :parent
      12
      13      def initialize(name)
      14        @name     = name
      15        @children = {}
      16        @cost = 0
      17
      27      def <<(tree)
      31      end
      32
      33      def [](name)
      35      end
      36
      38      def children
      40      end
      41
      42      def cost
      44      end
      45
      47      def sorted_children
      49      end
      50
      63
      73    end
      74  end

and ending in 73-add-46.txt there is no change in output.

The bug comes from 74-add-47:

       2
       5  module DerailedBenchmarks
       6    class RequireTree
       7      REQUIRED_BY = {}
       8
       9      attr_reader   :name
      10      attr_writer   :cost
      11      attr_accessor :parent
      12
      13      def initialize(name)
      14        @name     = name
      15        @children = {}
      16        @cost = 0
    ❯ 17
      27      def <<(tree)
      31      end
      32
      33      def [](name)
      35      end
      36
      38      def children
      40      end
      41
      42      def cost
      44      end
      45
      47      def sorted_children
      49      end
      50
      63
      73    end
      74  end

Here the empty line is eliminated so when the expandsion phase starts after clearing out the contents of initialize it has a mismatched keword/end.

We shouldn't have an add phase on an empty line.

It actually correctly catches the problem at the end:

       2
       5  module DerailedBenchmarks
       6    class RequireTree
       7      REQUIRED_BY = {}
       8
       9      attr_reader   :name
      10      attr_writer   :cost
      11      attr_accessor :parent
    ❯ 12
    ❯ 13      def initialize(name)
      73    end
      74  end

So maybe the problem is in the formatting rather than the search. Still the above oddities might be worth looking into.

The problem is fixed by disabling capture_last_end_same_indent in CaptureCodeContext. So it looks like that's where the problem is.