◐ Shell
clean mode source ↗

Alternative way of obtaining function AST. by hubuk · Pull Request #1659 · PowerShell/PSScriptAnalyzer

@hubuk

PR Summary

Fixes #966.

Method UseShouldProcessCorrectly.TryGetShouldProcessValueFromAst is trying to obtain function body AST using Find with ScriptBlockAst filter. Unfortunately FunctionInfo exposes only FunctionDefinitionAst so the Find method fails.
This PR improves this behavior by checking existence of FunctionDefinitionAst first and using old code as a fallback mechanism.

PR Checklist

@rjmholt

Playing with this a bit, I wonder if we actually just want searchNestedScriptBlocks to be true?

$ast = {
    function Test { "Hello" }
}.Ast.EndBlock.Statements[0]

# No output, because $ast.Body is a nested scriptblock
$ast.Find({ $args[0] -is [System.Management.Automation.Language.ScriptBlockAst] }, $false)

# Finds the body
$ast.Find({ $args[0] -is [System.Management.Automation.Language.ScriptBlockAst] }, $true)

@hubuk

@rjmholt If this is guaranteed that Find will always return function body even if the body itself contains other script blocks then searchNestedScriptBlocks: $true looks OK and I can change my commit.

@rjmholt

If this is guaranteed that Find will always return function body even if the body itself contains other script blocks

$ast = {
    function Test { { "decoy" }; "Hello" }
}.Ast.EndBlock.Statements[0]

# Gets the body rather than the decoy scriptblock
$ast.Find({ $args[0] -is [System.Management.Automation.Language.ScriptBlockAst] }, $true)

Looks like the answer is yes 🙂. The AST visitor visits the parents before the children, so the body is the first thing it sees.

@rjmholt

A good way to ensure that guarantee though would be to add a regression test

@hubuk

@rjmholt Regression test may be hard to implement as the changed code is executed only when there is an exception thrown due to problem described here: #966 (comment)

The comment in code mentions: // functionInfo.ScriptBlock.Attributes may throw if it cannot resolve an attribute type but I could not trigger this behavior by simply providing an unknown attribute.

I will dig into that a little bit deeper tho.

@hubuk

I updated the changes. Unfortunately was unable to provide test for this :(
After merging this change Issue #966 may be closed.
PR #995 may also be closed without merging.

rjmholt

@hubuk hubuk mentioned this pull request

Apr 23, 2021

7 tasks