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

OnlyCheckWhitespaceInsideParenthesis Fixes #5863 #5868

Merged
merged 8 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/Build.UnitTests/Scanner_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Build.Evaluation;
using Microsoft.Build.Exceptions;
using Microsoft.Build.Utilities;
using Shouldly;
using Xunit;


Expand Down Expand Up @@ -67,11 +68,7 @@ public void ErrorPosition()
/// <param name="lexer"></param>
private void AdvanceToScannerError(Scanner lexer)
{
while (true)
{
if (!lexer.Advance()) break;
if (lexer.IsNext(Token.TokenType.EndOfInput)) break;
}
while (lexer.Advance() && !lexer.IsNext(Token.TokenType.EndOfInput)) ;
Forgind marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down Expand Up @@ -104,16 +101,31 @@ public void IllFormedProperty()
/// <summary>
/// Tests the space errors case
/// </summary>
[Fact]
public void SpaceProperty()
[Theory]
[InlineData("$(x )")]
[InlineData("$( x)")]
[InlineData("$([MSBuild]::DoSomething($(space ))")]
[InlineData("$([MSBuild]::DoSomething($(_space ))")]
public void SpaceProperty(string pattern)
{
Scanner lexer = new Scanner("$(x )", ParserOptions.AllowProperties);
Scanner lexer = new Scanner(pattern, ParserOptions.AllowProperties);
AdvanceToScannerError(lexer);
Assert.Equal("IllFormedPropertySpaceInCondition", lexer.GetErrorResource());
}

lexer = new Scanner("$( x)", ParserOptions.AllowProperties);
/// <summary>
/// Tests the space not next to end so no errors case
/// </summary>
[Theory]
[InlineData("$(x.StartsWith( 'y' ))")]
[InlineData("$(x.StartsWith ('y'))")]
rainersigwald marked this conversation as resolved.
Show resolved Hide resolved
[InlineData("$( x.StartsWith( $(SpacelessProperty) ) )")]
[InlineData("$( x.StartsWith( $(_SpacelessProperty) ) )")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One case I ran into was something like:

[InlineData("$(x.StartsWith('Foo', StringComparison.InvariantCultureIgnoreCase))")]

It might already work with the changes here, but might be good to add this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do please add this multiple-argument case before merging.

public void SpaceInMiddleOfProperty(string pattern)
{
Scanner lexer = new Scanner(pattern, ParserOptions.AllowProperties);
AdvanceToScannerError(lexer);
Assert.Equal("IllFormedPropertySpaceInCondition", lexer.GetErrorResource());
lexer._errorState.ShouldBeFalse();
}

[Fact]
Expand Down
31 changes: 24 additions & 7 deletions src/Build/Evaluation/Conditionals/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal sealed class Scanner
private string _expression;
private int _parsePoint;
private Token _lookahead;
private bool _errorState;
internal bool _errorState;
private int _errorPosition;
// What we found instead of what we were looking for
private string _unexpectedlyFound = null;
Expand Down Expand Up @@ -321,8 +321,9 @@ private string ParsePropertyOrItemMetadata()
private static bool ScanForPropertyExpressionEnd(string expression, int index, out int indexResult)
{
int nestLevel = 0;
bool whitespaceCheck = false;

bool whitespaceFound = false;
bool nonIdentifierCharacterFound = false;
indexResult = -1;
unsafe
{
fixed (char* pchar = expression)
Expand All @@ -333,24 +334,40 @@ private static bool ScanForPropertyExpressionEnd(string expression, int index, o
if (character == '(')
{
nestLevel++;
whitespaceCheck = true;
}
else if (character == ')')
{
nestLevel--;
whitespaceCheck = false;
}
else if (whitespaceCheck && char.IsWhiteSpace(character) && ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave16_10))
else if (char.IsWhiteSpace(character))
{
whitespaceFound = true;
indexResult = index;
return false;
}
else if (!XmlUtilities.IsValidSubsequentElementNameCharacter(character))
{
nonIdentifierCharacterFound = true;
}

if (character == '$')
Forgind marked this conversation as resolved.
Show resolved Hide resolved
{
if (!ScanForPropertyExpressionEnd(expression, index + 1, out index))
rainersigwald marked this conversation as resolved.
Show resolved Hide resolved
{
indexResult = index;
return false;
}
}

// We have reached the end of the parenthesis nesting
// this should be the end of the property expression
// If it is not then the calling code will determine that
if (nestLevel == 0)
{
if (whitespaceFound && !nonIdentifierCharacterFound && ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave16_10))
{
return false;
}

indexResult = index;
return true;
}
Expand Down