Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An escaped String and a string seem identical #81

Closed
LaurentDardenne opened this issue Jul 1, 2022 · 1 comment
Closed

An escaped String and a string seem identical #81

LaurentDardenne opened this issue Jul 1, 2022 · 1 comment
Labels
good first issue Good for newcomers

Comments

@LaurentDardenne
Copy link

With

Write-host "$Name, action !"; Write-host "`$Name, action !"; Write-host "Name, action !" 

For the second , the escape character is not explained
For the last : "String in which variables would be expanded.", but it is just a string whitout variable.

@Jawz84
Copy link
Owner

Jawz84 commented Jul 1, 2022

About the second expamle:

"`$Name, action !"

Where the backtick ` is not explained: this is also a Tokenizer/AST issue I think. After tokenizing has taken place, the backtick is gone, and the string is just a string with a $ character in it, without the semantic meaning of a variable marker.

Here we can see it in action:
image

So the solution could be simply to check the Extent for backtics, and if they are present, explain them. If you are interested, this is not a complicated fix. This is the relevant code where this could be added:

public override AstVisitAction VisitStringConstantExpression(StringConstantExpressionAst stringConstantExpressionAst)
{
var explanation = new Explanation
{
CommandName = stringConstantExpressionAst.StringConstantType.ToString(),
HelpResult = HelpTableQuery("about_quoting_rules")
}.AddDefaults(stringConstantExpressionAst, explanations);
var hasDollarSign = stringConstantExpressionAst.Value.Contains('$');
switch (stringConstantExpressionAst.StringConstantType)
{
case StringConstantType.SingleQuoted:
explanation.Description = "String in which variables will not be expanded.";
break;
case StringConstantType.SingleQuotedHereString:
explanation.Description = "Multiline here-string in which variables will not be expanded.";
break;
case StringConstantType.DoubleQuoted:
if (hasDollarSign)
{
explanation.Description = "String in which variables will be expanded.";
}
else
{
explanation.Description = "String in which variables would be expanded.";
}
break;
case StringConstantType.DoubleQuotedHereString:
if (hasDollarSign)
{
explanation.Description = "Multiline here-string in which variables will be expanded.";
}
else
{
explanation.Description = "Multiline here-string in which variables would be expanded.";
}
break;
case StringConstantType.BareWord:
return AstVisitAction.SkipChildren;
}
explanations.Add(explanation);
return AstVisitAction.Continue;
}

A test for this could be added here:

As for the last one, that explanation is subtle, it only mentions that variables would be expanded (if they were present). It's just there to explain the difference between single quotes and double qoutes. Do you feel it's confusing?

@Jawz84 Jawz84 added the good first issue Good for newcomers label Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants