GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Alternative way of obtaining function AST. by hubuk · Pull Request #1659 · PowerShell/PSScriptAnalyzer

Conversation

@hubuk

@hubuk Hubert Bukowski (hubuk) commented Apr 4, 2021 •
edited by rjmholt
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation.
    • Changed code is executed only when an exception is thrown in a "regular" execution path. This exception is hard to trigger in a controller environment. No already created tests were checking the changed code.
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rjmholt

Rob Holt (rjmholt) commented Apr 4, 2021

Copy link
Copy Markdown
Contributor

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)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hubuk

Hubert Bukowski (hubuk) commented Apr 4, 2021

Copy link
Copy Markdown
Contributor Author

Rob Holt (@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rjmholt

Rob Holt (rjmholt) commented Apr 5, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rjmholt

Rob Holt (rjmholt) commented Apr 6, 2021

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hubuk

Hubert Bukowski (hubuk) commented Apr 6, 2021

Copy link
Copy Markdown
Contributor Author

Rob Holt (@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hubuk

Hubert Bukowski (hubuk) commented Apr 6, 2021

Copy link
Copy Markdown
Contributor Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@SydneyhSmith Sydney Smith (SydneyhSmith) assigned Rob Holt (rjmholt) Apr 20, 2021
rjmholt
Rob Holt (rjmholt) approved these changes Apr 21, 2021
View reviewed changes
@hubuk @rjmholt
Include nested script blocks when searching for ShouldProcess.
2a7eb59
@rjmholt
Rob Holt (rjmholt) merged commit 158812b into PowerShell:master Apr 23, 2021
@hubuk Hubert Bukowski (hubuk) mentioned this pull request Apr 23, 2021
WIP Fix Exception when trying to analyze custom attribute that is being defined in different file #995
Closed
4 of 7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@rjmholt Rob Holt (rjmholt) rjmholt approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@rjmholt Rob Holt (rjmholt)

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Exception occurred while constructing custom validator attribute.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@hubuk @rjmholt
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.