From 76ace06bf32dc7d41128cac5e3f88b3f84bf72a6 Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Mon, 1 Apr 2024 06:42:47 +0100 Subject: [PATCH] Apply suggestions from code review Co-authored-by: David Widmann --- src/varname.jl | 9 ++++----- test/varname.jl | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/varname.jl b/src/varname.jl index e8d3c88..adcd568 100644 --- a/src/varname.jl +++ b/src/varname.jl @@ -673,10 +673,9 @@ end # Accessors doesn't have the same support for interpolation # so this function is copied and altered from `Setfield._parse_obj_lens` function _parse_obj_optics(ex) - if ex isa Expr && ex.head == :$ - @assert length(ex.args) == 1 + if Meta.isexpr(ex, :$, 1) return esc(:_), (esc(ex.args[1]),) - elseif Meta.isexpr(ex, :ref) + elseif Meta.isexpr(ex, :ref) && !isempty(ex.args) front, indices... = ex.args obj, frontoptics = _parse_obj_optics(front) if any(Accessors.need_dynamic_optic, indices) @@ -689,13 +688,13 @@ function _parse_obj_optics(ex) index = esc(Expr(:tuple, indices...)) optics = :($(Accessors.IndexLens)($index)) end - elseif Meta.isexpr(ex, :.) + elseif Meta.isexpr(ex, :., 2) front = ex.args[1] property = ex.args[2].value # ex.args[2] is a QuoteNode obj, frontoptics = _parse_obj_optics(front) if property isa Union{Symbol,String} optics = :($(Accessors.PropertyLens){$(QuoteNode(property))}()) - elseif property isa Expr && property.head == :$ + elseif Meta.isexpr(property, :$, 1) optics = :($(Accessors.PropertyLens){$(esc(property.args[1]))}()) else throw(ArgumentError( diff --git a/test/varname.jl b/test/varname.jl index 5087e8c..b117f0e 100644 --- a/test/varname.jl +++ b/test/varname.jl @@ -89,7 +89,6 @@ end @test collect(get(B, @varname(B[1, :], true))) == collect(get(B, @varname(B[1, -4:5]))) end - @testset "type stability" begin @inferred VarName{:a}() @inferred VarName{:a}(IndexLens(1))