From f23485a8d693cb69fd7b84c1ab93cb4198ecfe4a Mon Sep 17 00:00:00 2001 From: yui-knk Date: Wed, 10 Jul 2024 22:28:22 +0900 Subject: [Feature #20624] Enhance `RubyVM::AbstractSyntaxTree::Node#locations` This commit introduce `RubyVM::AbstractSyntaxTree::Node#locations` method and `RubyVM::AbstractSyntaxTree::Location` class. Ruby AST node will hold multiple locations information. `RubyVM::AbstractSyntaxTree::Node#locations` provides a way to access these locations information. `RubyVM::AbstractSyntaxTree::Location` is a class which holds these location information: * `#first_lineno` * `#first_column` * `#last_lineno` * `#last_column` --- test/ruby/test_ast.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb index ebef14c14b..4c579287a9 100644 --- a/test/ruby/test_ast.rb +++ b/test/ruby/test_ast.rb @@ -1297,6 +1297,13 @@ dummy end; end + def test_locations + node = RubyVM::AbstractSyntaxTree.parse("1 + 2") + locations = node.locations + + assert_equal(RubyVM::AbstractSyntaxTree::Location, locations[0].class) + end + private def assert_error_tolerant(src, expected, keep_tokens: false) @@ -1316,4 +1323,16 @@ dummy assert_equal(expected, str) node end + + class TestLocation < Test::Unit::TestCase + def test_lineno_and_column + node = RubyVM::AbstractSyntaxTree.parse("1 + 2") + location = node.locations[0] + + assert_equal(1, location.first_lineno) + assert_equal(0, location.first_column) + assert_equal(1, location.last_lineno) + assert_equal(5, location.last_column) + end + end end -- cgit v1.2.3