protected override async Task <Solution> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken) { var root = syntaxNode as CompilationUnitSyntax; if (root == null) { return(document.Project.Solution); } if (!LoadMSTestNamespaces()) { return(document.Project.Solution); } var originalRoot = root; SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); List <UsingDirectiveSyntax> newUsings = new List <UsingDirectiveSyntax>(); bool needsChanges = false; foreach (var usingSyntax in root.Usings) { var symbolInfo = semanticModel.GetSymbolInfo(usingSyntax.Name); if (symbolInfo.Symbol != null) { string namespaceDocID = symbolInfo.Symbol.GetDocumentationCommentId(); if (s_mstestNamespaces.Contains(namespaceDocID)) { needsChanges = true; } else { newUsings.Add(RemoveLeadingAndTrailingCompilerDirectives(usingSyntax)); } } else { newUsings.Add(RemoveLeadingAndTrailingCompilerDirectives(usingSyntax)); } } if (!needsChanges) { return(document.Project.Solution); } TransformationTracker transformationTracker = new TransformationTracker(); RemoveTestClassAttributes(root, semanticModel, transformationTracker); RemoveContractsRequiredAttributes(root, semanticModel, transformationTracker); ChangeTestInitializeToCtor(root, semanticModel, transformationTracker); ChangeTestCleanupToIDisposable(root, semanticModel, transformationTracker); ChangeTestMethodAttributesToFact(root, semanticModel, transformationTracker); ChangeAssertCalls(root, semanticModel, transformationTracker); root = transformationTracker.TransformRoot(root); // Remove compiler directives before the first member of the file (e.g. an #endif after the using statements) var firstMember = root.Members.FirstOrDefault(); if (firstMember != null) { if (firstMember.HasLeadingTrivia) { var newLeadingTrivia = RemoveCompilerDirectives(firstMember.GetLeadingTrivia()); root = root.ReplaceNode(firstMember, firstMember.WithLeadingTrivia(newLeadingTrivia)); } } var isIDisposableImplemented = ( from baseType in root.DescendantNodes().OfType <SimpleBaseTypeSyntax>() select baseType.Type into baseTypeDefinition where baseTypeDefinition is IdentifierNameSyntax && ((IdentifierNameSyntax)baseTypeDefinition).Identifier.ValueText == nameof(IDisposable) select baseTypeDefinition).Any(); var isSystemAbsent = root .Usings .All(usingNode => usingNode.Name.ToString() != nameof(System)); if (isIDisposableImplemented && isSystemAbsent) { var systemUsing = SyntaxFactory .UsingDirective(SyntaxFactory.ParseName(nameof(System))) .NormalizeWhitespace() .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); newUsings.Add(systemUsing); } var xUnitUsing = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("Xunit")).NormalizeWhitespace(); newUsings.Add(xUnitUsing); // Apply trailing trivia from original last using statement to new last using statement SyntaxTriviaList usingTrailingTrivia = RemoveCompilerDirectives(originalRoot.Usings.Last().GetTrailingTrivia()); newUsings[newUsings.Count - 1] = newUsings.Last().WithTrailingTrivia(usingTrailingTrivia); root = root.WithUsings(SyntaxFactory.List <UsingDirectiveSyntax>(newUsings)); return(document.WithSyntaxRoot(root).Project.Solution); }
protected override async Task<Solution> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken) { var root = syntaxNode as CompilationUnitSyntax; if (root == null) return document.Project.Solution; if (!LoadMSTestNamespaces()) { return document.Project.Solution; } var originalRoot = root; SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); List<UsingDirectiveSyntax> newUsings = new List<UsingDirectiveSyntax>(); bool needsChanges = false; foreach (var usingSyntax in root.Usings) { var symbolInfo = semanticModel.GetSymbolInfo(usingSyntax.Name); if (symbolInfo.Symbol != null) { string namespaceDocID = symbolInfo.Symbol.GetDocumentationCommentId(); if (s_mstestNamespaces.Contains(namespaceDocID)) { needsChanges = true; } else { newUsings.Add(RemoveLeadingAndTrailingCompilerDirectives(usingSyntax)); } } else { newUsings.Add(RemoveLeadingAndTrailingCompilerDirectives(usingSyntax)); } } if (!needsChanges) { return document.Project.Solution; } TransformationTracker transformationTracker = new TransformationTracker(); RemoveTestClassAttributes(root, semanticModel, transformationTracker); RemoveContractsRequiredAttributes(root, semanticModel, transformationTracker); ChangeTestMethodAttributesToFact(root, semanticModel, transformationTracker); ChangeAssertCalls(root, semanticModel, transformationTracker); root = transformationTracker.TransformRoot(root); // Remove compiler directives before the first member of the file (e.g. an #endif after the using statements) var firstMember = root.Members.FirstOrDefault(); if (firstMember != null) { if (firstMember.HasLeadingTrivia) { var newLeadingTrivia = RemoveCompilerDirectives(firstMember.GetLeadingTrivia()); root = root.ReplaceNode(firstMember, firstMember.WithLeadingTrivia(newLeadingTrivia)); } } var xUnitUsing = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("Xunit")).NormalizeWhitespace(); newUsings.Add(xUnitUsing); // Apply trailing trivia from original last using statement to new last using statement SyntaxTriviaList usingTrailingTrivia = RemoveCompilerDirectives(originalRoot.Usings.Last().GetTrailingTrivia()); newUsings[newUsings.Count - 1] = newUsings.Last().WithTrailingTrivia(usingTrailingTrivia); root = root.WithUsings(SyntaxFactory.List<UsingDirectiveSyntax>(newUsings)); return document.WithSyntaxRoot(root).Project.Solution; }
protected override async Task <Solution> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken) { var root = syntaxNode as CompilationUnitSyntax; if (root == null) { return(document.Project.Solution); } if (!LoadMSTestNamespaces()) { return(document.Project.Solution); } var originalRoot = root; SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); List <UsingDirectiveSyntax> newUsings = new List <UsingDirectiveSyntax>(); bool needsChanges = false; foreach (var usingSyntax in root.Usings) { var symbolInfo = semanticModel.GetSymbolInfo(usingSyntax.Name); if (symbolInfo.Symbol != null) { string namespaceDocID = symbolInfo.Symbol.GetDocumentationCommentId(); if (s_mstestNamespaces.Contains(namespaceDocID)) { needsChanges = true; } else { newUsings.Add(RemoveLeadingAndTrailingCompilerDirectives(usingSyntax)); } } else { newUsings.Add(RemoveLeadingAndTrailingCompilerDirectives(usingSyntax)); } } if (!needsChanges) { return(document.Project.Solution); } TransformationTracker transformationTracker = new TransformationTracker(); RemoveTestClassAttributes(root, semanticModel, transformationTracker); RemoveContractsRequiredAttributes(root, semanticModel, transformationTracker); ChangeTestMethodAttributesToFact(root, semanticModel, transformationTracker); ChangeAssertCalls(root, semanticModel, transformationTracker); root = transformationTracker.TransformRoot(root); // Remove compiler directives before the first member of the file (e.g. an #endif after the using statements) var firstMember = root.Members.FirstOrDefault(); if (firstMember != null) { if (firstMember.HasLeadingTrivia) { var newLeadingTrivia = RemoveCompilerDirectives(firstMember.GetLeadingTrivia()); root = root.ReplaceNode(firstMember, firstMember.WithLeadingTrivia(newLeadingTrivia)); } } var xUnitUsing = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("Xunit")).NormalizeWhitespace(); newUsings.Add(xUnitUsing); // Apply trailing trivia from original last using statement to new last using statement SyntaxTriviaList usingTrailingTrivia = RemoveCompilerDirectives(originalRoot.Usings.Last().GetTrailingTrivia()); newUsings[newUsings.Count - 1] = newUsings.Last().WithTrailingTrivia(usingTrailingTrivia); root = root.WithUsings(SyntaxFactory.List <UsingDirectiveSyntax>(newUsings)); return(document.WithSyntaxRoot(root).Project.Solution); }