Macro-in-expression transformed to invalid code
dermesser opened this issue · comments
Lewin Bormann commented
This issue looks very similar to #248 - but I am on v1.0.20, and it still occurs. In my case with @sprintf
:
original code:
[...]
plot!(xs, cossq_model_func(xs, fp.param);
color=palette(:tab10)[colorindex],
linestyle=:dash, linewidth=2,
label=@sprintf "cos² fit: n_eff = %.4f dn/dλ = %.3e d²n/dλ² = %.3e" cossq_fit.a cossq_fit.b cossq_fit.c);
[...]
formatted code:
[...]
plot!(
xs,
cossq_model_func(xs, fp.param);
color = palette(:tab10)[colorindex],
linestyle = :dash,
linewidth = 2,
label = @sprintf "cos² fit: n_eff = %.4f dn/dλ = %.3e d²n/dλ² = %.3e" cossq_fit.a cossq_fit.b cossq_fit.c,
)
[...]
fails with
Error occurred on line 650, offset 1 of formatted text.
The error might not be precisely on this line but it should be in the region of the code block. Try commenting the region out and see if that removes the error.
Stacktrace:
[1] error
@ ./error.jl:35
[2] format_text(cst::CSTParser.EXPR, style::DefaultStyle, s::JuliaFormatter.State)
@ JuliaFormatter ~/.julia/packages/JuliaFormatter/w296a/src/JuliaFormatter.jl:742
[3] format_text(text::String, style::DefaultStyle, opts::JuliaFormatter.Options)
@ JuliaFormatter ~/.julia/packages/JuliaFormatter/w296a/src/JuliaFormatter.jl:675
[4] #format_text#236
@ ~/.julia/packages/JuliaFormatter/w296a/src/JuliaFormatter.jl:647 [inlined]
[5] #format_text#235
@ ~/.julia/packages/JuliaFormatter/w296a/src/JuliaFormatter.jl:641 [inlined]
[6] _format_file(filename::String; overwrite::Bool, verbose::Bool, format_markdown::Bool, format_options::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:config_applied,), Tuple{Bool}}})
@ JuliaFormatter ~/.julia/packages/JuliaFormatter/w296a/src/JuliaFormatter.jl:766
[7] kwcall(::NamedTuple{(:config_applied,), Tuple{Bool}}, ::typeof(JuliaFormatter._format_file), filename::String)
@ JuliaFormatter ~/.julia/packages/JuliaFormatter/w296a/src/JuliaFormatter.jl:749
[8] format(path::String, options::JuliaFormatter.Configuration)
@ JuliaFormatter ~/.julia/packages/JuliaFormatter/w296a/src/JuliaFormatter.jl:907
[9] #format#245
@ ~/.julia/packages/JuliaFormatter/w296a/src/JuliaFormatter.jl:877 [inlined]
[10] format(path::String)
@ JuliaFormatter ~/.julia/packages/JuliaFormatter/w296a/src/JuliaFormatter.jl:877
[11] top-level scope
@ REPL[2]:1
Note that this line of code works once there are ()
around the macro invocation:
[...]
plot!(xs, cossq_model_func(xs, fp.param);
color=palette(:tab10)[colorindex],
linestyle=:dash, linewidth=2,
label=(@sprintf "cos² fit: n_eff = %.4f dn/dλ = %.3e d²n/dλ² = %.3e" cossq_fit.a cossq_fit.b cossq_fit.c));
[...]
plot!(
xs,
cossq_model_func(xs, fp.param);
color = palette(:tab10)[colorindex],
linestyle = :dash,
linewidth = 2,
label = (@sprintf "cos² fit: n_eff = %.4f dn/dλ = %.3e d²n/dλ² = %.3e" cossq_fit.a cossq_fit.b cossq_fit.c),
)
It looks again related to the trailing comma and macro parsing. The simplest solution appears to be adding parentheses around macro calls in a function call (which looks cleaner anyway), although you are the expert on Julia Formatting, no question :)
In any case, thank you very much for providing this wonderful tool.