-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Steps to reproduce
function test ([validateset('$test', 'test$', 'test test', '#', '# hello', '#requires more', '<# hello #>')]$test) {echo $test}
test <ALT-SPACE> # value `$test` will not be escaped or quoted due to `$`, nor will the completions that start with `#`, except `#requires more` gets quoted
# also, `<# hello #>` fails to be quoted
test "<ALT-SPAC>" # values `$test` and `test$` will not have the `$` escapedExpected behavior
All special characters (those affecting tokenizing the argument correct intentions) would force quoting or escaping.
Actual behavior
Some characters do, some do not.
This is due to a reliance of using the parser (via CompletionRequiresQuotes() method) to determine if there is more than 1 token, or if the parser has an error parsing the tokens contained in the completion. In the case of $test the parser sees exactly the one successful token and no more. I've also determined the same is happening for #
I understand why the parser is being used, but I think the idea is flawed because the parser doesn't know the full context of the completion, and can be swayed, as I have done here with the $test argument value.
Environment data
PowerShell 7.0.0-preview.2
Considerations
This is partially related to other completion bugs I have been reporting. I'd like to consider writing a more specific helper function/method for handling argument completions that can correctly determine if an argument needs quoted, and/or (if already quoted) properly escaped, and would not rely directly on the parser. I have covered this idea in #9881, and I am working on converting the PowerShell code example to C#. Using this helper, all argument type completions will be funneled through a single method for proper quoting and escaping. This does mean that making tweaks to the parser that might affect how a completion needs to be quoted/escaped would require tweaking this helper as well.