public void DefinedIPlugin() { var test = new[] { @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] [ExportMetadata(""Title"", ""MastarData"")] [ExportMetadata(""Description"", ""start2 で取得される、艦これのマスター データを閲覧するためのビュー機能を提供します。"")] [ExportMetadata(""Version"", ""1.0"")] [ExportMetadata(""Author"", ""@Grabacr07"")] class Hoge : IPlugin { public void Initialize() { } }", @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(ITool))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga : ITool { public void Initialize() { } }", }; this.VerifyCSharpDiagnostic(test); }
public void TypesShouldNotExtendCertainBaseTypes_CSharp_XmlDocument() { var source = @" using System.Xml; class C1 : XmlDocument { } "; var expected = new[] { GetCSharpXmlDocumentResultAt(4, 7, "C1", "System.Xml.XmlDocument") }; VerifyCSharp(source, expected); }
public void TypesShouldNotExtendCertainBaseTypes_CSharp_ApplicationException() { var source = @" using System; class C1 : ApplicationException { } "; var expected = new[] { GetCSharpApplicationExceptionResultAt(4, 7, "C1", "System.ApplicationException") }; VerifyCSharp(source, expected); }
public static MemoryStream CompileClientProxy(IEnumerable<SyntaxTree> trees, IEnumerable<MetadataReference> references, ILogger logger = null) { #if !NET var assemblys = new[] { "System.Runtime", "mscorlib", "System.Threading.Tasks" }; references = assemblys.Select(i => MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName(i)).Location)).Concat(references); #endif references = new[] { MetadataReference.CreateFromFile(typeof(Task).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(ServiceDescriptor).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(IRemoteInvokeService).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(IServiceProxyGenerater).GetTypeInfo().Assembly.Location) }.Concat(references); return Compile(AssemblyInfo.Create("Rabbit.Rpc.ClientProxys"), trees, references, logger); }
private Project CreateSolution(string source) { var testProjectName = "Test"; var projectId = ProjectId.CreateNewId(testProjectName); var references = new[] { s_CorlibReference, s_SystemCoreReference, s_MSTestReference, s_XunitReference }; var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, testProjectName, testProjectName, LanguageNames.CSharp) .AddMetadataReferences(projectId, references); var fileName = "File.cs"; var documentId = DocumentId.CreateNewId(projectId, fileName); solution = solution.AddDocument(documentId, fileName, SourceText.From(source)); return solution.GetProject(projectId); }
public void TypesShouldNotExtendCertainBaseTypes_Basic_Collection() { var source = @" Imports System.Collections Public Class C1 Inherits CollectionBase End Class Public Class C2 Inherits DictionaryBase End Class Public Class C3 Inherits Queue End Class Public Class C4 Inherits ReadOnlyCollectionBase End Class Public Class C5 Inherits SortedList End Class Public Class C6 Inherits Stack End Class "; var expected = new[] { GetBasicCollectionBaseResultAt(4, 14, "C1", "System.Collections.CollectionBase"), GetBasicDictionaryBaseResultAt(9, 14, "C2", "System.Collections.DictionaryBase"), GetBasicQueueResultAt(14, 14, "C3", "System.Collections.Queue"), GetBasicReadOnlyCollectionBaseResultAt(19, 14, "C4", "System.Collections.ReadOnlyCollectionBase"), GetBasicSortedListResultAt(24, 14, "C5", "System.Collections.SortedList"), GetBasicStackResultAt(29, 14, "C6", "System.Collections.Stack") }; VerifyBasic(source, expected); }
public void TypesShouldNotExtendCertainBaseTypes_Basic_XmlDocument() { var source = @" Imports System.Xml Public Class C1 Inherits XmlDocument End Class "; var expected = new[] { GetBasicXmlDocumentResultAt(4, 14, "C1", "System.Xml.XmlDocument") }; VerifyBasic(source, expected); }
public void TypesShouldNotExtendCertainBaseTypes_Basic_ApplicationException() { var source = @" Imports System Public Class C1 Inherits ApplicationException End Class "; var expected = new[] { GetBasicApplicationExceptionResultAt(4, 14, "C1", "System.ApplicationException") }; VerifyBasic(source, expected); }
public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel) { SyntaxNode newRoot; if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression)) { var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent; var statement = assignmentExpression.Parent as ExpressionStatementSyntax; var identitySymbol = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol; newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol) ? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol) : CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression)); } else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator)) { var variableDeclarator = (VariableDeclaratorSyntax)objectCreation.Parent.Parent; var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent; var statement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent; newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia())); } else if (objectCreation.Parent.IsKind(SyntaxKind.Argument)) { var identifierName = GetIdentifierName(objectCreation, semanticModel); var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var")) .WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName)) .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), objectCreation)))); var arg = objectCreation.Parent as ArgumentSyntax; var args = objectCreation.Parent.Parent as ArgumentListSyntax; var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName))); StatementSyntax statement = objectCreation.FirstAncestorOfType<ExpressionStatementSyntax>(); if (statement != null) { var exprStatement = statement.ReplaceNode(args, newArgs); var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement)) .WithDeclaration(variableDeclaration); return root.ReplaceNode(statement, newUsingStatment); } statement = (StatementSyntax)objectCreation.Ancestors().First(node => node is StatementSyntax); var newStatement = statement.ReplaceNode(args, newArgs); var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement)); var usingBlock = SyntaxFactory.Block(statementsForUsing); var usingStatement = CreateUsingStatement(newStatement, usingBlock) .WithDeclaration(variableDeclaration); var statementsToReplace = new List<StatementSyntax> { statement }; statementsToReplace.AddRange(statementsForUsing.Skip(1)); newRoot = root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null); } else if (objectCreation.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression)) { var newVariableName = objectCreation.Type.ToString(); var newVariableNameParts = newVariableName.Split('.'); newVariableName = newVariableNameParts[newVariableNameParts.Length - 1].ToLowerCaseFirstLetter(); var parentStatement = objectCreation.Parent.FirstAncestorOrSelfThatIsAStatement(); var originalName = newVariableName; for (int nameIncrement = 1; ; nameIncrement++) { var speculativeSymbol = semanticModel.GetSpeculativeSymbolInfo(parentStatement.GetLocation().SourceSpan.Start, SyntaxFactory.IdentifierName(newVariableName), SpeculativeBindingOption.BindAsExpression); if (speculativeSymbol.Symbol == null) break; newVariableName = originalName + nameIncrement; } var newVariable = SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName("var"), SyntaxFactory.SeparatedList(new[] { SyntaxFactory.VariableDeclarator(newVariableName).WithInitializer(SyntaxFactory.EqualsValueClause(objectCreation)) }))); newRoot = root.TrackNodes(parentStatement, objectCreation); newRoot = newRoot.ReplaceNode(newRoot.GetCurrentNode(objectCreation), SyntaxFactory.IdentifierName(newVariableName)); var newTrackedParentStatement = newRoot.GetCurrentNode(parentStatement); newRoot = newRoot.InsertNodesBefore(newTrackedParentStatement, new[] { newVariable }); var statement = (LocalDeclarationStatementSyntax)newRoot.GetCurrentNode(parentStatement).GetPreviousStatement(); var variableDeclaration = statement.Declaration; var variableDeclarator = variableDeclaration.Variables.First(); newRoot = CreateRootWithUsing(newRoot, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia())); } else { newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation)); } return newRoot; }
public void RequireIPlugin() { var test = new[] { @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(ISettings))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Hoge : ISettings { public void Initialize() { } }", @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(ITool))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga : ITool { public void Initialize() { } }", }; var expected = new DiagnosticResult { Id = RequireIPluginAnalyzer.DiagnosticId, Severity = DiagnosticSeverity.Error, Message = message, }; this.VerifyCSharpDiagnostic(test, expected); }
public void ValidGuidConst() { Utility.NewGuidValueForTest = Const.NewGuidValueForTest; var test = new[] { @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", guid)] [ExportMetadata(""Title"", ""MastarData"")] [ExportMetadata(""Description"", ""start2 で取得される、艦これのマスター データを閲覧するためのビュー機能を提供します。"")] [ExportMetadata(""Version"", ""1.0"")] [ExportMetadata(""Author"", ""@Grabacr07"")] class Hoge : IPlugin { private const string guid = ""C8BF00A6-9FD4-4CC4-8FC5-ECCC5675CDEB""; public void Initialize() { } }", }; this.VerifyCSharpDiagnostic(test); }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <param name="semanticModel"></param> /// <param name="typeKind"></param> /// <returns></returns> protected virtual BaseTypeReference CreateBaseTypeReferenceHelper(BaseTypeSyntax node, SemanticModel semanticModel, Roslyn.TypeKind typeKind) { return new BaseTypeReference(node, semanticModel, typeKind); }
private static MefHostServices CreateHostServices() { var assemblies = new[] { Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"), Assembly.Load("Microsoft.CodeAnalysis.VisualBasic.Features") }; var compositionHost = new ContainerConfiguration() .WithAssemblies(MefHostServices.DefaultAssemblies.Concat(assemblies)) .CreateContainer(); return MefHostServices.Create(compositionHost); }
public void DuplicateGuid() { Utility.NewGuidValueForTest = Const.NewGuidValueForTest; var test = @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""65BE3E80-8EC1-41BD-85E0-78AEFD45A757"")] class Hoge1 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""65BE3E80-8EC1-41BD-85E0-78AEFD45A757"")] class Hoge2 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga1 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga2 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga3 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""006130BF-499B-45E5-8EFA-CFFF192D1E65"")] class PiyoPlugin : IPlugin { public void Initialize() { } } [Export(typeof(ITool))] [ExportMetadata(""Guid"", ""006130BF-499B-45E5-8EFA-CFFF192D1E65"")] class Piyo1 : ITool { public void Initialize() { } } [Export(typeof(ITool))] [ExportMetadata(""Guid"", ""C78D5FAA-F686-45E6-B986-E4D5393DBE77"")] class Piyo2 : ITool { public void Initialize() { } }"; var expected = new[] { new DiagnosticResult { Id = DuplicateGuidAnalyzer.DiagnosticId, Severity = DiagnosticSeverity.Warning, Message = string.Format(message, "Hoge2"), Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 1) } }, new DiagnosticResult { Id = DuplicateGuidAnalyzer.DiagnosticId, Severity = DiagnosticSeverity.Warning, Message = string.Format(message, "Hoge1"), Locations = new[] { new DiagnosticResultLocation("Test0.cs", 12, 1) } }, new DiagnosticResult { Id = DuplicateGuidAnalyzer.DiagnosticId, Severity = DiagnosticSeverity.Warning, Message = string.Format(message, "Fuga2, Fuga3"), Locations = new[] { new DiagnosticResultLocation("Test0.cs", 19, 1) } }, new DiagnosticResult { Id = DuplicateGuidAnalyzer.DiagnosticId, Severity = DiagnosticSeverity.Warning, Message = string.Format(message, "Fuga1, Fuga3"), Locations = new[] { new DiagnosticResultLocation("Test0.cs", 26, 1) } }, new DiagnosticResult { Id = DuplicateGuidAnalyzer.DiagnosticId, Severity = DiagnosticSeverity.Warning, Message = string.Format(message, "Fuga1, Fuga2"), Locations = new[] { new DiagnosticResultLocation("Test0.cs", 33, 1) } }, }; this.VerifyCSharpDiagnostic(test, expected); var fixedtest0 = @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"")] class Hoge1 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""65BE3E80-8EC1-41BD-85E0-78AEFD45A757"")] class Hoge2 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga1 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga2 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga3 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""006130BF-499B-45E5-8EFA-CFFF192D1E65"")] class PiyoPlugin : IPlugin { public void Initialize() { } } [Export(typeof(ITool))] [ExportMetadata(""Guid"", ""006130BF-499B-45E5-8EFA-CFFF192D1E65"")] class Piyo1 : ITool { public void Initialize() { } } [Export(typeof(ITool))] [ExportMetadata(""Guid"", ""C78D5FAA-F686-45E6-B986-E4D5393DBE77"")] class Piyo2 : ITool { public void Initialize() { } }"; this.VerifyCSharpFix(test, fixedtest0, 0); var fixedtest1 = @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""C78D5FAA-F686-45E6-B986-E4D5393DBE77"")] class Hoge1 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""65BE3E80-8EC1-41BD-85E0-78AEFD45A757"")] class Hoge2 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga1 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga2 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] class Fuga3 : IPlugin { public void Initialize() { } } [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""006130BF-499B-45E5-8EFA-CFFF192D1E65"")] class PiyoPlugin : IPlugin { public void Initialize() { } } [Export(typeof(ITool))] [ExportMetadata(""Guid"", ""006130BF-499B-45E5-8EFA-CFFF192D1E65"")] class Piyo1 : ITool { public void Initialize() { } } [Export(typeof(ITool))] [ExportMetadata(""Guid"", ""C78D5FAA-F686-45E6-B986-E4D5393DBE77"")] class Piyo2 : ITool { public void Initialize() { } }"; this.VerifyCSharpFix(test, fixedtest1, 1); }
private static SyntaxNode CreateRootWithUsingFromArgument(SyntaxNode root, ExpressionSyntax childOfArgumentNode, string identifierName) { var arg = childOfArgumentNode.Parent as ArgumentSyntax; var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var")) .WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName)) .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), childOfArgumentNode)))); var args = arg.Parent as ArgumentListSyntax; var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName))); StatementSyntax statement = childOfArgumentNode.FirstAncestorOfType<ExpressionStatementSyntax>(); if (statement != null) { var exprStatement = statement.ReplaceNode(args, newArgs); var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement)) .WithDeclaration(variableDeclaration); return root.ReplaceNode(statement, newUsingStatment); } statement = (StatementSyntax)childOfArgumentNode.Ancestors().First(node => node is StatementSyntax); var newStatement = statement.ReplaceNode(args, newArgs); var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement)); var usingBlock = SyntaxFactory.Block(statementsForUsing); var usingStatement = CreateUsingStatement(newStatement, usingBlock) .WithDeclaration(variableDeclaration); var statementsToReplace = new List<StatementSyntax> { statement }; statementsToReplace.AddRange(statementsForUsing.Skip(1)); return root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null); }
public void TypesShouldNotExtendCertainBaseTypes_CSharp_Collection() { var source = @" using System.Collections; class C1 : CollectionBase { } class C2 : DictionaryBase { } class C3 : Queue { } class C4 : ReadOnlyCollectionBase { } class C5 : SortedList { } class C6 : Stack { }"; var expected = new[] { GetCSharpCollectionBaseResultAt(4, 7, "C1", "System.Collections.CollectionBase"), GetCSharpDictionaryBaseResultAt(8, 7, "C2", "System.Collections.DictionaryBase"), GetCSharpQueueResultAt(12, 7, "C3", "System.Collections.Queue"), GetCSharpReadOnlyCollectionResultAt(16, 7, "C4", "System.Collections.ReadOnlyCollectionBase"), GetCSharpSortedListResultAt(20, 7, "C5", "System.Collections.SortedList"), GetCSharpStackResultAt(24, 7, "C6", "System.Collections.Stack") }; VerifyCSharp(source, expected); }
public void MissingGuidValue() { Utility.NewGuidValueForTest = Const.NewGuidValueForTest; var test = new[] { @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(IPlugin))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] [ExportMetadata(""Title"", ""MastarData"")] [ExportMetadata(""Description"", ""start2 で取得される、艦これのマスター データを閲覧するためのビュー機能を提供します。"")] [ExportMetadata(""Version"", ""1.0"")] [ExportMetadata(""Author"", ""@Grabacr07"")] class Hoge : IPlugin { public void Initialize() { } }", @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(ITool))] [ExportMetadata(""Guid"", """")] class Fuga : ITool { public void Initialize() { } }", }; var expected = new DiagnosticResult { Id = InvalidGuidMetadataAnalyzer.DiagnosticId, Severity = DiagnosticSeverity.Warning, Message = invalidGuidMessage, Locations = new[] { new DiagnosticResultLocation("Test1.cs", 6, 1) } }; this.VerifyCSharpDiagnostic(test, expected); }
private void Verify(string source, string language, DiagnosticAnalyzer analyzer, string testProjectName, DiagnosticResult[] expected) { var sources = new[] { source }; var diagnostics = GetSortedDiagnostics(sources.ToFileAndSource(), language, analyzer, addLanguageSpecificCodeAnalysisReference: true, projectName: testProjectName); diagnostics.Verify(analyzer, expected); }
public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel) { SyntaxNode newRoot; if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression)) { var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent; var statement = assignmentExpression.Parent as ExpressionStatementSyntax; var identitySymbol = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol; newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol) ? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol) : CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression)); } else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator)) { var variableDeclarator = (VariableDeclaratorSyntax)objectCreation.Parent.Parent; var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent; var statement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent; newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia())); } else if (objectCreation.Parent.IsKind(SyntaxKind.Argument)) { var identifierName = GetIdentifierName(objectCreation, semanticModel); var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var")) .WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName)) .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), objectCreation)))); var arg = objectCreation.Parent as ArgumentSyntax; var args = objectCreation.Parent.Parent as ArgumentListSyntax; var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName))); StatementSyntax statement = objectCreation.FirstAncestorOfType<ExpressionStatementSyntax>(); if (statement != null) { var exprStatement = statement.ReplaceNode(args, newArgs); var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement)) .WithDeclaration(variableDeclaration); return root.ReplaceNode(statement, newUsingStatment); } statement = (StatementSyntax)objectCreation.Ancestors().First(node => node is StatementSyntax); var newStatement = statement.ReplaceNode(args, newArgs); var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement)); var usingBlock = SyntaxFactory.Block(statementsForUsing); var usingStatement = CreateUsingStatement(newStatement, usingBlock) .WithDeclaration(variableDeclaration); var statementsToReplace = new List<StatementSyntax> { statement }; statementsToReplace.AddRange(statementsForUsing.Skip(1)); newRoot = root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null); } else { newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation)); } return newRoot; }
public void DuplicateAttribute() { Utility.NewGuidValueForTest = Const.NewGuidValueForTest; var test = @" using System; using System.ComponentModel.Composition; using Grabacr07.KanColleViewer.Composition; [Export(typeof(IPlugin))] [Export(typeof(IPlugin))] [Export(typeof(ISettings))] [Export(typeof(ISettings))] [Export(typeof(ISettings))] [ExportMetadata(""Guid"", ""45BF5FE6-7D81-4978-8B8A-84FD80BBEC10"")] [ExportMetadata(""Title"", """")] [ExportMetadata(""Description"", """")] [ExportMetadata(""Version"", """")] [ExportMetadata(""Version"", """")] [ExportMetadata(""Author"", """")] [ExportMetadata(""Author"", """")] class TypeName : IPlugin, ISettings { public void Initialize() { } public object View { get; } }"; var expected = new[] { new DiagnosticResult { Id = DuplicateAttributeAnalyzer.DiagnosticId, Severity = DiagnosticSeverity.Warning, Message = string.Format(exportMessage, "typeof(IPlugin), typeof(ISettings)"), Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 1) } }, new DiagnosticResult { Id = DuplicateAttributeAnalyzer.DiagnosticId, Severity = DiagnosticSeverity.Warning, Message = string.Format(metadataMessage, "Version, Author"), Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 1) } }, }; this.VerifyCSharpDiagnostic(test, expected); }