Skip to content

VSCode Extension: Refactoring Issues #133

@TMA-2

Description

@TMA-2

VSCode Extension: Refactoring Issues

Description

The poshProTools.Refactor command (at least when extracted to function) has some minor formatting issues, as well as very odd parameter generation.

Another minor issue is that poshProTools.RefactoringInfo is not defined, and as such results in a VSCode error:

Command 'PowerShell Pro Tools: Refactoring Info' resulted in an error. command 'poshProTools.refactoringInfo' not found

Expected Behavior

Correct indentation and parameter detection (how this is determined, I haven't checked). At the very least, crawling the AST for variables that haven't been assigned, determining their type if possible, and adding them as parameters.

Given the following selection within a foreach loop (hence the indentation):

    if($ParamSet -eq 'Name') {
        $Filter = "`$_.DisplayName -like '$CurrentApp'"
    } elseif($ParamSet -eq 'GUID') {
        # format GUID as brackets w/ hyphens
        $CurrentApp = $CurrentApp.ToString('b')
        $Filter = "`$_.PSChildName -eq '$($CurrentApp.ToString('b'))'"
    }

    if($Version) {
        $Filter += " -and [version]'$Version' -ge `$_.DisplayVersion"
    }

    $Filter += " -and `$_.UninstallString -ne `$null"
    $FilterScript = [scriptblock]::Create($Filter)

    $Results = @(Get-ItemProperty -Path $RegKeys | Where-Object -FilterScript $FilterScript)

    log "Found $($Results.Count) applications matching $CurrentApp $Version"

I would expect something like the following:

    Function FuncName
    {
        Param(
            [string]$ParamSet,
            $CurrentApp,
            [version]$Version,
            [string]$RegKeys
        )

        if($ParamSet -eq 'Name') {
            $Filter = "`$_.DisplayName -like '$CurrentApp'"
        } elseif($ParamSet -eq 'GUID') {
            # format GUID as brackets w/ hyphens
            $CurrentApp = $CurrentApp.ToString('b')
            $Filter = "`$_.PSChildName -eq '$($CurrentApp.ToString('b'))'"
        }

        if($Version) {
            $Filter += " -and [version]'$Version' -ge `$_.DisplayVersion"
        }

        $Filter += " -and `$_.UninstallString -ne `$null"
        $FilterScript = [scriptblock]::Create($Filter)

        $Results = @(Get-ItemProperty -Path $RegKeys | Where-Object -FilterScript $FilterScript)

        log "Found $($Results.Count) applications matching $CurrentApp $Version"
    }

Actual Behavior

Refactoring the above snippet as a function gives me the following:

    function FuncName
{
	param($ParamSet, $RegKeys, $RegKeys, $RegKeys)
if($ParamSet -eq 'Name') {
        $Filter = "`$_.DisplayName -like '$CurrentApp'"
    } elseif($ParamSet -eq 'GUID') {
        # format GUID as brackets w/ hyphens
        $CurrentApp = $CurrentApp.ToString('b')
        $Filter = "`$_.PSChildName -eq '$($CurrentApp.ToString('b'))'"
    }

    if($Version) {
        $Filter += " -and [version]'$Version' -ge `$_.DisplayVersion"
    }

    $Filter += " -and `$_.UninstallString -ne `$null"
    $FilterScript = [scriptblock]::Create($Filter)

    $Results = @(Get-ItemProperty -Path $RegKeys | Where-Object -FilterScript $FilterScript)

    log "Found $($Results.Count) applications matching $CurrentApp $Version"
}

Issues

  • The indentation is entirely off — a level not being added, with the brackets and first line unindented (the whitespace was in the selection, just fyi).
  • The parameters generated (if any) are sometimes repeated or strange. I can understand why $CurrentApp wasn't included, as it's assigned a value (albeit coming from itself), but $Version should have been included. And then $RegKeys is repeated thrice...

To be fair, this could potentially be tested on a limitless amount of code, so this is only one small example I came across.

Log

The Extension Host log shows the following:

2025-04-30 18:15:25.882 [error] [ironmansoftware.powershellprotools] provider FAILED
2025-04-30 18:15:25.884 [error] TypeError: Cannot read properties of undefined (reading 'selection')
    at RefactoringCommands.getRefactorRequest (c:\Users\tma2\.vscode\extensions\ironmansoftware.powershellprotools-2024.12.0\out\commands\refactoring.js:150:56)
    at RefactoringCommands.<anonymous> (c:\Users\tma2\.vscode\extensions\ironmansoftware.powershellprotools-2024.12.0\out\commands\refactoring.js:21:32)
    at Generator.next (<anonymous>)
    at c:\Users\tma2\.vscode\extensions\ironmansoftware.powershellprotools-2024.12.0\out\commands\refactoring.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (c:\Users\tma2\.vscode\extensions\ironmansoftware.powershellprotools-2024.12.0\out\commands\refactoring.js:4:12)
    at RefactoringCommands.provideCodeActions (c:\Users\tma2\.vscode\extensions\ironmansoftware.powershellprotools-2024.12.0\out\commands\refactoring.js:18:16)
    at cm.provideCodeActions (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:135:136472)
    at file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:135:165221
    at Wt.s (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:135:160222)
    at Wt.$provideCodeActions (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:135:165209)
    at i4.S (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:29:115936)
    at i4.Q (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:29:115716)
    at i4.M (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:29:114767)
    at i4.L (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:29:113910)
    at Ah.value (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:29:112707)
    at D.B (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:27:2375)
    at D.fire (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:27:2593)
    at Jn.fire (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:29:9459)
    at Ah.value (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:197:3917)
    at D.B (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:27:2375)
    at D.fire (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:27:2593)
    at Jn.fire (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:29:9459)
    at MessagePortMain.<anonymous> (file:///c:/Program%20Files/Microsoft%20VS%20Code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:197:2209)
    at MessagePortMain.emit (node:events:518:28)
    at Object.MessagePortMain._internalPort.emit (node:electron/js2c/utility_init:2:2949)
    at Object.callbackTrampoline (node:internal/async_hooks:130:17)
2025-04-30 18:15:26.736 [warning] TextEditor is closed/disposed
2025-04-30 18:15:26.740 [warning] TextEditor is closed/disposed
2025-04-30 18:15:27.925 [warning] TextEditor is closed/disposed
2025-04-30 18:15:27.930 [warning] TextEditor is closed/disposed
2025-04-30 18:15:27.934 [warning] TextEditor is closed/disposed

Versions

  • OS Windows 11 23H2
  • VS Code 1.99.3
  • PowerShell 7.5.0
  • PowerShell Extension 2025.3.0 (pre-release)
  • PowerShell Pro Tools 2024.12.0

Screenshot

For the poshProTools.refactoringInfo command...
Error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions