Skip to content

FindVariableDeclaration doesn't find the correct declarations #6

@jplindstrom

Description

@jplindstrom

Hi there,

I wanted to do "Go to variable declaration" in PerlySense, so it seemed convenient to nick that functionality from PPIx::EditorTools::FindVariableDeclaration :)

I can't get it to work though. The basics work, but as soon as I start using it in real source code it seems pretty random.

Some of the findings below are clear-cut, some are still a mystery to me.

I was getting ready to write test cases using the example code below, but now I'm not sure whether most of them are known things or whether they're expected to work.

# Works: $baz_row, $foo_type
# Broken: function_call() errors with "could not find declaration",
#         when it should just not try at all, it doesn't look like
#         a variable
sub var_in_sub_call {
    my (
        $baz_row,
        $foo_type,
    ) = @_;

    my $abc;
    $abc = function_call(
        $baz_row->id,
        { country => $foo_type->{country} },
    );
}

# Works: $foo_type
# Broken: $baz_row goes to my $abc
sub var_in_sub_call_with_assignment {
    # This is a very weird one (compared to the one above, this has an
    # unrelated variable declaration in the same expression, and _one_
    # of the variables work)
    my (
        $baz_row,
        $foo_type,
    ) = @_;

    my $abc = function_call(
        $baz_row->id,
        { country => $foo_type->{country} },
    );
}

# Broken: $abc[0] goes to "my $first"
# Broken: $def{d} goes to "my $d"
sub array_as_scalar_value {
    my $abc = 0; # scalar before array declaration

    my @abc;
    my $first = $abc[0];

    my %def;
    my $def = ""; # scalar after hash declaration

    my $d = $def{d};
}

# Works: all goes to my () declaration.
# Note: This is strange: here the $ghi[i] accurately goes to @ghi,
#       this is broken in the sub above
sub multiple_variables_declared_in_one_statement {
    # Wishlist: This isn't as useful as it would be if it went to the
    # actual variable.
    my (
        $abc,
        $def,
        @ghi,
    ) = (1, "a", (1, 2, 3));

    my $ghi = 8;

    $abc = $def + $ghi[1];
}

# Works: $package_lexical
# Works: $abc = "def"; in inner scope
# Works: $abc++ in outer scope
# Works: $foo{def} in same inner scope (and hash key goes to hash declaration)
my $package_lexical = "hello";
sub nested_scope {
    my $abc = "abc";

    if ($package_lexical && $abc) {
        my $abc = 1;
        my ($def, %foo);
        $abc++;
        $abc = "def";

        $def = $foo{def};
    }

    $abc++;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions