Repository navigation
Distinguish script-defined functions from DSC resources - #2297
Christoph Bergmeister (bergmeister) with Copilot wants to merge 21 commits into
Conversation
Resolve visible function declarations from the AST when classifying DSC resource syntax and checking implicit Get-prefix aliases. Preserve analysis of executable function arguments for both brace layouts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Scoped function declarations are mishandled, causing incorrect alias rewrites and suppressed diagnostics, while repeated scope scans introduce quadratic analysis overhead.
2 open issues
What changed in this PR
This follow-up to #2270 distinguishes script-defined function calls from DSC resource declarations so alias analysis preserves function names while checking commands in their script-block arguments.
Changes:
- Adds case-insensitive function lookup in enclosing script-block scopes and reuses it in DSC classification.
- Adds regression coverage for function scope, brace layouts, unrelated scopes, and formatter behavior.
- Documents the distinction between functions, DSC resources, and implicit aliases.
| File | Description |
|---|---|
| Tests/Rules/AvoidUsingAlias.tests.ps1 | Adds function-detection and formatter regression tests. |
| Rules/AvoidAlias.cs | Uses script-defined function detection in alias and DSC handling. |
| docs/Rules/AvoidUsingCmdletAliases.md | Explains function and DSC resource exclusions from implicit alias correction. |
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Scope handling still changes command resolution for dot-sourced private functions and incorrectly treats script/global declarations in unexecuted nested functions as visible, causing missed diagnostics and unsafe corrections.
2 open issues
2 resolved since last review
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Dot-sourced scope handling still misindexes declarations and incorrectly skips script-block argument scopes, causing both erroneous alias corrections and missed diagnostics.
2 open issues
2 resolved since last review
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The argument-count restriction incorrectly treats parameterized dot-sourced blocks as child scopes, causing erroneous alias formatting and missed declarations in the caller’s scope.
1 open issue
2 resolved since last review
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved scope and execution-order errors in AvoidAlias suppress expected Get-Verb diagnostics and cause valid function calls to be incorrectly rewritten.
6 open issues
Check declaration visibility for immediately invoked child blocks · New Compare named-block execution phases before source offsets · New Resolve callback scopes without declarations from the callback itself · New Resolve positional member arguments before selecting callback parameters · New Traverse parenthesized literal script blocks · New Recognize module-qualified ThreadJob commands for isolation checks · New
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved scope-resolution bugs in AvoidAlias incorrectly rewrite valid Verb calls as Get-Verb in callbacks and nested or parenthesized dot-sourced blocks.
3 open issues
6 resolved since last review
Recognize module-qualified ThreadJob commands for isolation checks Traverse parenthesized literal script blocks Resolve positional member arguments before selecting callback parameters Resolve callback scopes without declarations from the callback itself Compare named-block execution phases before source offsets Check declaration visibility for immediately invoked child blocks
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved execution-order and scope-resolution bugs cause incorrect or suppressed alias diagnostics and incorrect formatter replacements across script phases, stored callbacks, DSC assignments, and job initialization.
4 open issues
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Job-initializer scope handling still rewrites valid initializer-local calls and suppresses expected alias diagnostics when a script-defined function shadows the job command.
2 open issues
4 resolved since last review
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The initializer exception misses nested job-body scopes, causing valid calls to initialized functions to be incorrectly rewritten when -ScriptBlock precedes -InitializationScript.
1 open issue
2 resolved since last review
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because unresolved scope and alias-resolution edge cases can cause incorrect formatter replacements, while regression coverage misses a property-name collision.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Active scope-resolution defects can suppress valid alias diagnostics for next-line resource bodies and incorrectly flag or rewrite function calls across named execution blocks.
2 open issues
4 resolved since last review
Review effort: Balanced
| } | ||
|
|
||
| int functionNamedBlockPhase = GetNamedBlockExecutionPhase(function, out ScriptBlockAst functionExecutionBlock); | ||
| int commandNamedBlockPhase = GetNamedBlockExecutionPhase(cmdAst, out ScriptBlockAst commandExecutionBlock); |
There was a problem hiding this comment.
Fixed in 18fedd1. Named-block phase detection now uses the immediate invocation when available, with a begin/process regression test.
| if (parent is CommandAst resource | ||
| && String.Equals(resource.GetCommandName(), "Script", StringComparison.OrdinalIgnoreCase) | ||
| && GetDscResourceBody(resource) != null | ||
| && IsDscResourceCommand(resource, functionAnalysis)) |
There was a problem hiding this comment.
Fixed in 18fedd1. Following-line resource bodies now resolve back to their declaration for Script-property isolation, with regression coverage.
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

PR Summary
Follow-up to #2270, targeting its branch rather than
main.The review finding is reproducible: a resource-shaped call to a script-defined function is treated as a DSC declaration because command discovery uses a separate runspace. Commands inside its script block arguments are then incorrectly suppressed. Both new regression cases returned zero diagnostics before the fix instead of the two expected diagnostics.
Get-prefix corrections to script-defined function calls; preserve explicit alias handling.Validation: .NET 8 and .NET Framework 4.6.2 builds succeeded. Alias tests: 23 passed, 2 Windows-only tests skipped on Linux. Formatter integration tests: 10 passed. Executed using PowerShell 7.4.13 and Pester 5.7.1; the configured Pester 6.0.1 package was unavailable from the accessible mirror.
PR Checklist