Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 278
Migrate document to use YARP#863
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
29cb081654719cf9074f3b366fc3417c94cc67a367bb01444af350b4abd051ade49dc0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # typed: true | ||
| module YARP | ||
| class << self | ||
| sig { params(source: String).returns(ParseResult) } | ||
| def parse(*source); end | ||
| end | ||
| class ParseResult | ||
| sig { returns(YARP::Node) } | ||
| def value; end | ||
| sig { returns(T::Boolean) } | ||
| def failure?; end | ||
| sig { returns(T::Boolean) } | ||
| def success?; end | ||
| end | ||
| class Node | ||
| sig { returns(T::Array[T.nilable(YARP::Node)]) } | ||
| def child_nodes; end | ||
| sig { returns(Location) } | ||
| def location; end | ||
| end | ||
| class Location | ||
| sig { returns(Integer) } | ||
| def start_offset; end | ||
| sig { returns(Integer) } | ||
| def end_offset; end | ||
| sig { returns(Integer) } | ||
| def start_line; end | ||
| sig { returns(Integer) } | ||
| def end_line; end | ||
| sig { returns(Integer) } | ||
| def start_column; end | ||
| sig { returns(Integer) } | ||
| def end_column; end | ||
| sig { returns(String) } | ||
| def slice; end | ||
| end | ||
| class ClassNode | ||
| sig { returns(ConstantPathNode) } | ||
| def constant_path; end | ||
| end | ||
| class ModuleNode | ||
| sig { returns(ConstantPathNode) } | ||
| def constant_path; end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -320,20 +320,11 @@ def test_pushing_edits_to_document_with_unicode | ||
| end | ||
| def test_parsed_returns_true_when_parsed_successfully | ||
| # We are just ensuring that no error is raised | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this test. If instantiating a new document with a valid Ruby string is failing, then pretty much every other test will also fail. | ||
| document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: URI("file:///foo.rb")) | ||
| # frozen_string_literal: true | ||
| puts 'hello' | ||
| RUBY | ||
| assert_predicate(document, :parsed?) | ||
| end | ||
| def test_parsed_returns_false_when_parsing_fails | ||
| document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: URI("file:///foo.rb")) | ||
| class Foo | ||
| RUBY | ||
| refute_predicate(document, :parsed?) | ||
| end | ||
| def test_document_handle_4_byte_unicode_characters | ||
| @@ -351,7 +342,6 @@ class Foo | ||
| ) | ||
| document.parse | ||
| assert_predicate(document, :parsed?) | ||
| assert_equal(<<~RUBY, document.source) | ||
| class Foo | ||
| @@ -397,7 +387,6 @@ class Foo | ||
| ) | ||
| document.parse | ||
| assert_predicate(document, :parsed?) | ||
| assert_equal(<<~RUBY, document.source) | ||
| class Foo | ||
| @@ -447,33 +436,33 @@ class Post < ActiveRecord::Base | ||
| # Locate the `ActiveRecord` module | ||
| found, parent = document.locate_node({ line: 0, character: 19 }) | ||
| assert_instance_of(SyntaxTree::Const, found) | ||
| assert_equal("ActiveRecord", T.cast(found, SyntaxTree::Const).value) | ||
| assert_instance_of(YARP::ConstantReadNode, found) | ||
| assert_equal("ActiveRecord", T.cast(found, YARP::ConstantReadNode).location.slice) | ||
| assert_instance_of(SyntaxTree::VarRef, parent) | ||
| assert_equal("ActiveRecord", T.cast(parent, SyntaxTree::VarRef).value.value) | ||
| assert_instance_of(YARP::ConstantPathNode, parent) | ||
| assert_equal("ActiveRecord", T.cast(parent, YARP::ConstantPathNode).child_nodes.first.location.slice) | ||
| # Locate the `Base` class | ||
| found, parent = T.cast( | ||
| document.locate_node({ line: 0, character: 27 }), | ||
| [SyntaxTree::Const, SyntaxTree::ConstPathRef, T::Array[String]], | ||
| [YARP::ConstantReadNode, YARP::ConstantPathNode, T::Array[String]], | ||
| ) | ||
| assert_instance_of(SyntaxTree::Const, found) | ||
| assert_equal("Base", found.value) | ||
| assert_instance_of(YARP::ConstantReadNode, found) | ||
| assert_equal("Base", found.location.slice) | ||
| assert_instance_of(SyntaxTree::ConstPathRef, parent) | ||
| assert_equal("Base", parent.constant.value) | ||
| assert_equal("ActiveRecord", T.cast(parent.parent, SyntaxTree::VarRef).value.value) | ||
| assert_instance_of(YARP::ConstantPathNode, parent) | ||
| assert_equal("Base", parent.child_nodes[1].location.slice) | ||
| assert_equal("ActiveRecord", parent.child_nodes[0].location.slice) | ||
| # Locate the `where` invocation | ||
| found, parent = T.cast( | ||
| document.locate_node({ line: 3, character: 4 }), | ||
| [SyntaxTree::Ident, SyntaxTree::CallNode, T::Array[String]], | ||
| [YARP::CallNode, YARP::StatementsNode, T::Array[String]], | ||
| ) | ||
| assert_instance_of(SyntaxTree::Ident, found) | ||
| assert_equal("where", found.value) | ||
| assert_instance_of(YARP::CallNode, found) | ||
| assert_equal("where", found.message_loc.slice) | ||
| assert_instance_of(SyntaxTree::CallNode, parent) | ||
| assert_instance_of(YARP::StatementsNode, parent) | ||
| end | ||
| def test_locate_returns_nesting | ||
| @@ -494,11 +483,11 @@ def baz | ||
| RUBY | ||
| found, _parent, nesting = document.locate_node({ line: 9, character: 6 }) | ||
| assert_equal("Qux", T.cast(found, SyntaxTree::Const).value) | ||
| assert_equal("Qux", T.cast(found, YARP::ConstantReadNode).location.slice) | ||
| assert_equal(["Foo", "Bar"], nesting) | ||
| found, _parent, nesting = document.locate_node({ line: 3, character: 6 }) | ||
| assert_equal("Hello", T.cast(found, SyntaxTree::Const).value) | ||
| assert_equal("Hello", T.cast(found, YARP::ConstantReadNode).location.slice) | ||
| assert_equal(["Foo", "Other"], nesting) | ||
| end | ||
| @@ -513,8 +502,8 @@ def baz | ||
| end | ||
| RUBY | ||
| found, _parent, nesting = document.locate_node({ line: 3, character: 6 }, node_types: [SyntaxTree::Const]) | ||
| assert_equal("Qux", T.cast(found, SyntaxTree::Const).value) | ||
| found, _parent, nesting = document.locate_node({ line: 3, character: 6 }, node_types: [YARP::ConstantReadNode]) | ||
| assert_equal("Qux", T.cast(found, YARP::ConstantReadNode).location.slice) | ||
| assert_equal(["Foo", "Bar"], nesting) | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are introducing
@parse_result, I eliminated@tree, to reduce the number of state variables needed.