Skip to content

Conversation

@jonpryor
Copy link
Contributor

Context: dotnet/android#4431

Commit e7c5f54 added IdentifierValidator.IsValidIdentifier(),
replacing use of CSharpCodeProvider.IsValidIdentifier(), and this
change caused a unit test failure within xamarin-android:

// Xamarin.Android.Build.Tests.BuildTest.GeneratorValidateEventName
obj/Debug/generated/src/Com.Xamarin.Testing.Test.cs(350,29,350,32): error CS1001: Identifier expected

The line in question?

public event EventHandler 123 {

Oops.

It Turns Out™ that IdentifierValidator.IsValidIdentifier("123")
returned True, so we attempted to create an event named 123, which
the C# compiler Does Not Like.

Fix IdentifierValidator.IsValidIdentifier() by using a new
IsValidIdentifierRegex regex to match against identifiers, instead
of attempting to reuse the validIdentifier regex, which matches for
invalid characters (it negates the match via [^...]), and thus
cannot differentiate between "starting" characters and everything
else, which is fine for CreateValidIdentifier(), but not for
IsValidIdentifier().

Add unit tests for IsValidIdentifier().

@jonpryor jonpryor requested a review from jpobst March 20, 2020 17:17

private const string Identifier = IdentifierStartCharacter + "(" + IdentifierPartCharacter + ")";

static Regex IsValidIdentifierRegex = new Regex ($"^[{IdentifierStartCharacter}][{IdentifierPartCharacter}]*$");
Copy link
Contributor

Choose a reason for hiding this comment

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

Presumably this will be called a bunch, probably worth adding RegexOptions.Compiled?

@jonpryor jonpryor force-pushed the jonp-fix-identifier-check branch from dfba529 to bb7f276 Compare March 20, 2020 18:35
Context: dotnet/android#4431

Commit e7c5f54 added `IdentifierValidator.IsValidIdentifier()`,
replacing use of `CSharpCodeProvider.IsValidIdentifier()`, and this
change caused a unit test failure within xamarin-android:

	// Xamarin.Android.Build.Tests.BuildTest.GeneratorValidateEventName
	obj/Debug/generated/src/Com.Xamarin.Testing.Test.cs(350,29,350,32): error CS1001: Identifier expected

The line in question?

	public event EventHandler 123 {

Oops.

It Turns Out™ that `IdentifierValidator.IsValidIdentifier("123")`
returned True, so we attempted to create an event named `123`, which
the C# compiler Does Not Like.

Fix `IdentifierValidator.IsValidIdentifier()` by using a new
`IsValidIdentifierRegex` regex to match against identifiers, instead
of attempting to reuse the `validIdentifier` regex, which matches for
*invalid* characters (it negates the match via `[^...]`), and thus
cannot differentiate between "starting" characters and everything
else, which is fine for `IdentifierValidator.CreateValidIdentifier()`,
but not for `IdentifierValidator.IsValidIdentifier()`.

Add unit tests for `IdentifierValidator.IsValidIdentifier()`.

Update `make run-test-generator-core` tests so that we add a
`View.setOn123Listener()` method, which prior to this commit would add
a `View.123` event:

	partial class View {
	  public event EventHandler 123 {
	    add { … }
	    remove { … }
	  }
	}

With a corrected `IdentifierValidator.IsValidIdentifier()`, this event
is no longer generated.
@jonpryor jonpryor force-pushed the jonp-fix-identifier-check branch from bb7f276 to a584de1 Compare March 20, 2020 18:36
@jpobst jpobst merged commit 1a086ff into dotnet:master Mar 20, 2020
@jpobst jpobst added this to the d16-7 milestone Apr 10, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants