Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve ruby parsing #1085

Merged
merged 1 commit into from
Jan 15, 2025
Merged

Conversation

aaronlifton
Copy link
Contributor

I noticed the ruby parsing didn't work for most ruby files, so I adjusted the treesitter query and implemented module and class definitions with proper namespaced names. Also implemented detection of private class methods.
Check the test result for test_ruby2:

It parses this:

# frozen_string_literal: true

require('jwt')

top_level_var = 1

def top_level_func
  inner_var_in_func = 2
end

module A
  module B
    @module_var = :foo

    def module_method
      @module_var
    end

    class C < Base
      TEST_CONST = 1
      @class_var = :bar
      attr_accessor :a, :b

      def initialize(a, b)
        @a = a
        @b = b
        super
      end

      def bar
        inner_var_in_method = 1
        true
      end

      private

      def baz(request, params)
        auth_header = request.headers['Authorization']
        parts = auth_header.try(:split, /\s+/)
        JWT.decode(parts.last)
      end
    end
  end
end

into

var top_level_var
func top_level_func() -> void
module A{}
module A::B{
  func module_method() -> void
  var @module_var
}
class A::B::C{
  func initialize(a, b) -> void
  func bar() -> void
  private func baz(request, params) -> void
  var TEST_CONST
  var @class_var
}

I create a new test test_ruby2 since I wanted to show the results of test_ruby with the changes. I noticed test_ruby includes invalid ruby syntax (functions in functions, classes in functions), so I figured it would be best to replace test_ruby with test_ruby2 since test_ruby2 includes the same tests but only valid syntax. If a user wants to create a repo map for a file with errors, I would think they should expect the parser to pick up the invalid syntax.

@aaronlifton aaronlifton force-pushed the feat/better-ruby-parsing branch from d3e89b5 to 139874f Compare January 14, 2025 19:42
@yetone yetone merged commit 0a837a4 into yetone:main Jan 15, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants