From dc923ba4d340cad1d816ddec2b83093e11849a89 Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Mon, 6 Feb 2017 20:36:44 -0800 Subject: [PATCH] Fix generation of events in interfaces. --- .../CSharp/Portable/Parser/LanguageParser.cs | 4 ++-- .../CodeGeneration/CSharpSyntaxGenerator.cs | 14 +++++++------- .../CodeGeneration/SyntaxGeneratorTests.cs | 10 +++------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Parser/LanguageParser.cs b/src/Compilers/CSharp/Portable/Parser/LanguageParser.cs index b63456ec343..22c6b53182f 100644 --- a/src/Compilers/CSharp/Portable/Parser/LanguageParser.cs +++ b/src/Compilers/CSharp/Portable/Parser/LanguageParser.cs @@ -4083,7 +4083,7 @@ private void ParseParameterModifiers(SyntaxListBuilder modifiers) } } - private MemberDeclarationSyntax ParseEventDeclarationWithAccessors( + private EventDeclarationSyntax ParseEventDeclarationWithAccessors( SyntaxListBuilder attributes, SyntaxListBuilder modifiers, SyntaxToken eventToken, @@ -4206,7 +4206,7 @@ private void ParseParameterModifiers(SyntaxListBuilder modifiers) } } - private MemberDeclarationSyntax ParseEventFieldDeclaration( + private EventFieldDeclarationSyntax ParseEventFieldDeclaration( SyntaxListBuilder attributes, SyntaxListBuilder modifiers, SyntaxToken eventToken, diff --git a/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs b/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs index 72fca19f9cf..697b4fbe3ca 100644 --- a/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs +++ b/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs @@ -733,18 +733,18 @@ internal override SyntaxNode AsInterfaceMember(SyntaxNode m) .WithModifiers(default(SyntaxTokenList)) .WithAccessorList(WithoutBodies(indexer.AccessorList)); + // convert event into field event case SyntaxKind.EventDeclaration: var ev = (EventDeclarationSyntax)member; - return ev - .WithModifiers(default(SyntaxTokenList)) - .WithAccessorList(WithoutBodies(ev.AccessorList)); + return this.EventDeclaration( + ev.Identifier.ValueText, + ev.Type, + Accessibility.NotApplicable, + DeclarationModifiers.None); - // convert event field into event case SyntaxKind.EventFieldDeclaration: var ef = (EventFieldDeclarationSyntax)member; - this.GetAccessibilityAndModifiers(ef.Modifiers, out acc, out modifiers); - var ep = this.CustomEventDeclaration(this.GetName(ef), this.ClearTrivia(this.GetType(ef)), acc, modifiers, parameters: null, addAccessorStatements: null, removeAccessorStatements: null); - return this.AsInterfaceMember(ep); + return ef.WithModifiers(default(SyntaxTokenList)); // convert field into property case SyntaxKind.FieldDeclaration: diff --git a/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs b/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs index ed818a319e0..3a20cd0d961 100644 --- a/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs +++ b/src/Workspaces/CSharpTest/CodeGeneration/SyntaxGeneratorTests.cs @@ -1212,11 +1212,11 @@ public void TestInterfaceDeclarations() VerifySyntax( _g.InterfaceDeclaration("i", members: new[] { _g.CustomEventDeclaration("ep", _g.IdentifierName("t"), accessibility: Accessibility.Public, modifiers: DeclarationModifiers.Static) }), - "interface i\r\n{\r\n event t ep\r\n {\r\n add;\r\n remove;\r\n }\r\n}"); + "interface i\r\n{\r\n event t ep;\r\n}"); VerifySyntax( _g.InterfaceDeclaration("i", members: new[] { _g.EventDeclaration("ef", _g.IdentifierName("t"), accessibility: Accessibility.Public, modifiers: DeclarationModifiers.Static) }), - "interface i\r\n{\r\n event t ef\r\n {\r\n add;\r\n remove;\r\n }\r\n}"); + "interface i\r\n{\r\n event t ef;\r\n}"); VerifySyntax( _g.InterfaceDeclaration("i", members: new[] { _g.FieldDeclaration("f", _g.IdentifierName("t"), accessibility: Accessibility.Public, modifiers: DeclarationModifiers.Sealed) }), @@ -1677,11 +1677,7 @@ public void TestInterfaceDeclarationWithEventFromSymbol() _g.Declaration(_emptyCompilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanged")), @"public interface INotifyPropertyChanged { - event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged - { - add; - remove; - } + event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged; }"); } #endregion -- GitLab