Skip to content

Rule Request: UseCompatibleStrings #2015

Open
@daprahamian

Description

@daprahamian

Summary of the new feature

The `e and `u{xxxx} special characters in double-quoted strings were added in Powershell 6. It would be nice to have a compatibility rule that detects these escape characters when writing for Powershell <6 and suggests a different syntax (ex. suggesting $([char]0x1b) for `e)

What is the latest version of PSScriptAnalyzer at the point of writing
v1.22

Activity

iRon7

iRon7 commented on Jul 25, 2024

@iRon7

I would name it UseCompatibleEscapeSequences as it specifically concerns the use of compatible escape sequences

SydneyhSmith

SydneyhSmith commented on Jul 30, 2024

@SydneyhSmith
Collaborator

Thanks... would want to make sure this came with great test coverage 😄

iRon7

iRon7 commented on Aug 2, 2024

@iRon7

Specifically for the escape character (`e):
$([char]0x1b) is a nice suggestion for the auto correction (-fix), yet my recommendation for a (documented) more idiomatic (and presumably more efficient) compatible correction would be to define a new (common) $Esc variable also knowing that in most cases this doesn't require any Subexpression operator $( ) as it is usually followed by a special character:

$Esc = $([char]0x1b)
"This is $Esc[7mInverse$Esc[27m text."

The same might apply to any specific unicode escape sequences (although less common):

$Smiley = $([char]0x263a)
"Please $Smiley, I am taking a picture"
liamjpeters

liamjpeters commented on Aug 8, 2024

@liamjpeters
Contributor

@iRon7 I'm not sure I agree that we should be defining a new variable.

There's a minutia of complexities with doing so. Not least of all:

  • What do you call the variable so that it doesn't clash or shadow an existing one?

  • Where do you define the variable?

With PowerShell's dynamic scopes, lexical analysis would be insufficient to find a suitable name or place. Though perhaps I'm over-thinking it? 🤔

I've made a first stab at the MVP of this and learned a fair bit.

UseCompatibleEscapeSequences

As Sydney says, much testing is going to be needed; There are many edge cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Rule Request: UseCompatibleStrings · Issue #2015 · PowerShell/PSScriptAnalyzer