From 996be72c531bd9cf2d8321a059f863f38386adca Mon Sep 17 00:00:00 2001 From: c42f Date: Mon, 28 Nov 2022 20:50:21 +1000 Subject: [PATCH] Fix Expr parameters in parenthesized macro calls --- src/expr.jl | 3 ++- test/expr.jl | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/expr.jl b/src/expr.jl index 5082b296..24c732c0 100644 --- a/src/expr.jl +++ b/src/expr.jl @@ -148,10 +148,11 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true, # Special cases for various expression heads loc = source_location(LineNumberNode, node.source, node.position) if headsym === :macrocall - insert!(args, 2, loc) if args[1] == Symbol("@.") args[1] = Symbol("@__dot__") end + reorder_parameters!(args, 2) + insert!(args, 2, loc) elseif headsym in (:dotcall, :call, :ref) # Julia's standard `Expr` ASTs have children stored in a canonical # order which is often not always source order. We permute the children diff --git a/test/expr.jl b/test/expr.jl index 9461bb76..a8a1a07c 100644 --- a/test/expr.jl +++ b/test/expr.jl @@ -239,4 +239,17 @@ @testset "where" begin @test parse(Expr, "A where {X, Y; Z}") == Expr(:where, :A, Expr(:parameters, :Z), :X, :Y) end + + @testset "macrocall" begin + # line numbers + @test parse(Expr, "@m\n") == Expr(:macrocall, Symbol("@m"), LineNumberNode(1)) + @test parse(Expr, "\n@m") == Expr(:macrocall, Symbol("@m"), LineNumberNode(2)) + # parameters + @test parse(Expr, "@m(x; a)") == Expr(:macrocall, Symbol("@m"), LineNumberNode(1), + Expr(:parameters, :a), :x) + @test parse(Expr, "@m(a=1; b=2)") == Expr(:macrocall, Symbol("@m"), LineNumberNode(1), + Expr(:parameters, Expr(:kw, :b, 2)), Expr(:(=), :a, 1)) + # @__dot__ + @test parse(Expr, "@.") == Expr(:macrocall, Symbol("@__dot__"), LineNumberNode(1)) + end end