Alternative way of obtaining function AST.#1659
Alternative way of obtaining function AST.#1659rjmholt merged 1 commit intoPowerShell:masterfrom hubuk:fix/UseShouldProcessCorrectly
Conversation
|
Playing with this a bit, I wonder if we actually just want $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) |
|
@rjmholt If this is guaranteed that |
$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. |
|
A good way to ensure that guarantee though would be to add a regression test |
|
@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: I will dig into that a little bit deeper tho. |
PR Summary
Fixes #966.
Method
UseShouldProcessCorrectly.TryGetShouldProcessValueFromAstis trying to obtain function body AST usingFindwithScriptBlockAstfilter. UnfortunatelyFunctionInfoexposes onlyFunctionDefinitionAstso theFindmethod fails.This PR improves this behavior by checking existence of FunctionDefinitionAst first and using old code as a fallback mechanism.
PR Checklist
.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.