FriedrichWeinmann / ConvertToPsd1

Conversts stuff to psd1
MIT License
3 stars 0 forks source link

Smart Quotes need escaping #2

Closed jborean93 closed 1 hour ago

jborean93 commented 2 days ago

When emitting a quoted value the code currently escapes the ASCII single quotes correctly but it does not do it for the Unicode alternatives that are used as a single quote equivalent:

$psd1 = @{foo = "Hello $([char]0x2018)World$([char]0x2018)" } | ConvertTo-Psd1

$psd1
# @{
#     foo = 'Hello ‘World‘'
# }

$psd1 | Invoke-Expression
# Invoke-Expression: Unexpected token 'World‘'' in expression or statement.

CodeGeneration.EscapeSingleQuotedStringContent can be used to correctly escape all the quotes needed to place a value inside a single quoted string

$value = "Hello $([char]0x2018)World$([char]0x2018)"
@"
@{
    foo = '$([System.Management.Automation.Language.CodeGeneration]::EscapeSingleQuotedStringContent($value))'
}
"@

# @{
#     foo = 'Hello ‘‘World‘‘'
# }
FriedrichWeinmann commented 1 day ago

Thanks for the catch and proposing a solution. Fix implemented and released in v1.0.1

jborean93 commented 1 hour ago

Thanks for the speedy fix.