diff --git a/ast.c b/ast.c index fd6a6dea89..d6d3e11d60 100644 --- a/ast.c +++ b/ast.c @@ -775,6 +775,10 @@ node_locations(VALUE ast_value, const NODE *node) { enum node_type type = nd_type(node); switch (type) { + case NODE_ALIAS: + return rb_ary_new_from_args(2, + location_new(nd_code_loc(node)), + location_new(&RNODE_ALIAS(node)->keyword_loc)); case NODE_UNDEF: return rb_ary_new_from_args(2, location_new(nd_code_loc(node)), diff --git a/node_dump.c b/node_dump.c index 2b9322bdb7..6ab91bd853 100644 --- a/node_dump.c +++ b/node_dump.c @@ -922,8 +922,9 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node) ANN("format: alias [nd_1st] [nd_2nd]"); ANN("example: alias bar foo"); F_NODE(nd_1st, RNODE_ALIAS, "new name"); - LAST_NODE; F_NODE(nd_2nd, RNODE_ALIAS, "old name"); + LAST_NODE; + F_LOC(keyword_loc, RNODE_ALIAS); return; case NODE_VALIAS: diff --git a/parse.y b/parse.y index f8bca1ee52..1dffac64f4 100644 --- a/parse.y +++ b/parse.y @@ -1138,7 +1138,7 @@ static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc); static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc); -static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc); +static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc); static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc); static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc); static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc); @@ -1246,7 +1246,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE #define NEW_BLOCK_PASS(b,loc) rb_node_block_pass_new(p,b,loc) #define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc) #define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc) -#define NEW_ALIAS(n,o,loc) (NODE *)rb_node_alias_new(p,n,o,loc) +#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc) #define NEW_VALIAS(n,o,loc) (NODE *)rb_node_valias_new(p,n,o,loc) #define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc) #define NEW_CLASS(n,b,s,loc) (NODE *)rb_node_class_new(p,n,b,s,loc) @@ -3128,7 +3128,7 @@ k_END : keyword_END lex_ctxt stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem { - $$ = NEW_ALIAS($2, $4, &@$); + $$ = NEW_ALIAS($2, $4, &@$, &@1); /*% ripper: alias!($:2, $:4) %*/ } | keyword_alias tGVAR tGVAR @@ -12297,11 +12297,12 @@ rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *lo } static rb_node_alias_t * -rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc) +rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc) { rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc); n->nd_1st = nd_1st; n->nd_2nd = nd_2nd; + n->keyword_loc = *keyword_loc; return n; } diff --git a/rubyparser.h b/rubyparser.h index e46e99616d..a690394928 100644 --- a/rubyparser.h +++ b/rubyparser.h @@ -820,6 +820,7 @@ typedef struct RNode_ALIAS { struct RNode *nd_1st; struct RNode *nd_2nd; + rb_code_location_t keyword_loc; } rb_node_alias_t; typedef struct RNode_VALIAS { diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb index d21c57d88f..cded28cf1f 100644 --- a/test/ruby/test_ast.rb +++ b/test/ruby/test_ast.rb @@ -1330,6 +1330,11 @@ dummy assert_locations(node.locations, [[1, 0, 1, 5]]) end + def test_alias_locations + node = RubyVM::AbstractSyntaxTree.parse("alias foo bar") + assert_locations(node.children[-1].locations, [[1, 0, 1, 13], [1, 0, 1, 5]]) + end + def test_unless_locations node = RubyVM::AbstractSyntaxTree.parse("unless cond then 1 else 2 end") assert_locations(node.children[-1].locations, [[1, 0, 1, 29], [1, 0, 1, 6], [1, 12, 1, 16], [1, 26, 1, 29]])