Skip to content

Control statements in assignment RHS #231

Description

@stevepositiv

Hi!

I noticed that syntax highlighting does not work properly for control statements used on the right-hand side of assignments.
I tried a small fix and for now it seems to work fine without unexpected side-effects.

$x = if ($a) { 1 } else { 2 }
$x = switch ($v) { 1 { 'one' } default { 'other' } }
$x = foreach ($i in 1..3) { $i * 2 }
$x = try { Get-Thing } catch { $null } 
Syntax Highlighting Example Before Change Syntax Highlighting Example After Change

My testing is limited, so I can't say anything definitive about edge cases or possible performance implications.

Here's my change:

--- PowerShell.sublime-syntax
+++ PowerShell.sublime-syntax
@@ -948,8 +948,11 @@
 
   in-assignment:
     - include: pop-end-of-command
     # INFO: Someday maybe we can nix the workflow stuff
     - include: workflow-execution-contexts
+    - include: conditionals
+    - include: loops
+    - include: exceptions
     - include: expressions
 
   redirection:

I'm not opening this as a PR because I don't know how to write syntax tests, and I'm unsure if this needs to be implemented differently as a proper fix.

Thanks for your work!

Best Regards
Steve

Activity

michaelblyons commented on Jun 23, 2026

@michaelblyons
Collaborator

Hi Steve, thanks for reporting!

Do you know if those conditionals, loops, and exceptions are available in any arbitrary expression?

Like can I do this with a try/catch?

if (2 * try { Get-Thing } catch { 4 } -gt $sentinel )
{ echo Foo }

michaelblyons commented on Jun 23, 2026

@michaelblyons
Collaborator

The OP's pattern for if is seen in the official docs in Assignment Operators:

You can also assign the value created by a statement to a variable, as in the following example:

$a = if ($b -lt 0) { 0 } else { $b }

This example assigns zero to the $a variable if the value of $b is less than zero. It assigns the value of $b to $a if the value of $b isn't less than zero.

And as an example, not itself documented at foreach:

      $filesToProcess = if ($_ -is [System.IO.FileSystemInfo]) {
        $_
      } else {
        Get-Item -Path $Path
      }

michaelblyons commented on Jun 23, 2026

@michaelblyons
Collaborator

I think I've found the real documentation for this at Statement values. These can get crazy:

$i = 1
$v = while ($i -le 5) {
    $i                   # $i is written to the pipeline
    if ($i -band 1) {
        "odd"            # conditionally written to the pipeline
    }
    ++$i                 # not written to the pipeline
}
# $v is object[], Length 8, value 1,"odd",2,3,"odd",4,5,"odd"

Does this mean that technically every statement is an expression? Including function and class definitions?

michaelblyons commented on Jun 23, 2026

@michaelblyons
Collaborator

As usual, PowerShell is more complicated.

# legal, $foo is 4 (= 0 + 4)
$foo =     try { Get-Thing } catch { 0 } + 4

# crashes
$foo = 4 + try { Get-Thing } catch { 0 }

# compiles, but unclear what $foo becomes
$foo = function Name { 5; Write-Host 6; return 7 }

I think we move exceptions, loops, flow, and conditionals from statements to expressions-without-commands and let the user sort it out if they try to add 4 + try {....

added a commit that references this issue on Jun 23, 2026
linked a pull request that will close this issueSupport statement values #233on Jun 23, 2026
added a commit that references this issue on Jun 24, 2026
added a commit that references this issue on Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions