Skip to content

Suggestion for new rule - Ensure that variables used in a function is always declared in the local scope #632

Open
@hallgeirl

Description

@hallgeirl

In order to reduce the number of hard to debug errors, we run our PowerShell scripts with Set-StrictMode -Version Latest. This lets us catch issues where we e.g. have misspelled a variable name (or forgot to assign it before using it) during runtime. But it would be immensely useful to catch these kinds of errors before we run the scripts, e.g. during an automated build.

We have deployment scripts that run for quite a long time (several hours), and silly misspelled variable names or misplaced variable assignments then costs quite a bit of development time. Catching these errors early would be immensely useful.

Example of something that should cause an error:

function Get-Foobar()
{
    if ($b + 2 -gt 5)
    {
        return $b
    }

    return 5
}

This would be OK:

function Get-Foobar($b)
{
    if ($b + 2 -gt 5)
    {
        return $b
    }

    return 5
}

As would this:

function Get-Foobar()
{
    $b = 123;
    if ($b + 2 -gt 5)
    {
        return $b
    }

    return 5
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions