public CSharpSyntaxNode AddRequires(ParameterSyntax parameter, SemanticModel semanticModel) { Contract.Requires(parameter != null); Contract.Requires(semanticModel != null); if (_method != null) { var anchor = GetParentForCurrentParameter(parameter, ContractBlock.CreateForMethodAsync(_method, semanticModel).Result); return(RequiresUtils.AddRequires(parameter, _method, anchor)); } var accessors = new SyntaxList <AccessorDeclarationSyntax>(); foreach (var accessor in _indexer.AccessorList.Accessors) { var contractBlock = ContractBlock.CreateForMethodAsync(accessor, semanticModel).Result; var anchor = GetParentForCurrentParameter(parameter, contractBlock); if (contractBlock.Preconditions.Any(x => x.ChecksForNotNull(parameter))) { accessors = accessors.Add(accessor); } else { var body = RequiresUtils.AddRequires(parameter, accessor.Body, anchor); accessors = accessors.Add(accessor.WithBody(body)); } } return(_indexer.WithAccessorList(_indexer.AccessorList.WithAccessors(accessors))); }
public async Task AddContractNamespaceIfNeededDoesNotAddsNamespaceIfAlreadyExists() { string src = @"using System; using System.Diagnostics.Contracts; internal class SampleClass { private SampleClass(string s{caret}tr) { } }"; var doc = await ClassTemplate.FromFullSource(src); var newRoot = RequiresUtils.AddContractNamespaceIfNeeded(doc.Root); Console.WriteLine(newRoot.GetText().ToString()); string expected = @"using System; using System.Diagnostics.Contracts; internal class SampleClass { private SampleClass(string str) { } }"; Assert.AreEqual(expected, newRoot.GetText().ToString()); }
public async Task AddContractNamespaceIfNeededAddsNamespaceOnlyOnce() { string src = @"internal class SampleClass { private SampleClass(string s{caret}tr) { } }"; var doc = await ClassTemplate.FromFullSource(src); var newRoot = RequiresUtils.AddContractNamespaceIfNeeded(doc.Root); Console.WriteLine(newRoot.GetText().ToString()); string expected = @"using System.Diagnostics.Contracts; internal class SampleClass { private SampleClass(string str) { } }"; Assert.AreEqual(expected, newRoot.GetText().ToString()); // Calling the same method once more time newRoot = RequiresUtils.AddContractNamespaceIfNeeded(newRoot); Assert.AreEqual(expected, newRoot.GetText().ToString(), "Second call to AddContractNamespacedIfNeeded should have no effect"); }
public async Task <Document> ApplyRefactoringAsync(Document document, CancellationToken token) { MethodDeclarationSyntax method = _selectedNode.AncestorsAndSelf().OfType <MethodDeclarationSyntax>().First(); Option <ExpressionStatementSyntax> lastRequires = await GetLastRequiresStatement(document, method, token); SyntaxNode root = await document.GetSyntaxRootAsync(token); SyntaxNode rootWithRequires = root.ReplaceNode(method, RequiresUtils.AddEnsures(method, _semanticModel, lastRequires)); SyntaxNode rootWithUsings = RequiresUtils.AddContractNamespaceIfNeeded(rootWithRequires); return(document.WithSyntaxRoot(rootWithUsings)); }
public async Task <Document> ApplyRefactoringAsync(Document document, CancellationToken token) { Contract.Assert(_parameter.HasValue); SyntaxNode root = await document.GetSyntaxRootAsync(token); SemanticModel semanticModel = await document.GetSemanticModelAsync(token); var rootWithRequires = root.ReplaceNode(_enclosingMethod.Value.CurrentMethod, _enclosingMethod.Value.AddRequires(_parameter.Value, semanticModel)); var rootWithUsings = RequiresUtils.AddContractNamespaceIfNeeded(rootWithRequires); return(document.WithSyntaxRoot(rootWithUsings)); }
public async Task <string> AddNotNullPrecondition(string method) { var doc = await ClassTemplate.FromMethodAsync(method); var parameterSyntax = doc.SelectedNode as ParameterSyntax; Assert.IsNotNull(parameterSyntax); var newMethod = RequiresUtils.AddRequires(parameterSyntax); Console.WriteLine("Original method:\r\n" + method); //var rr = Formatter.Format(method, Formatter.Annotation, doc.Document.Project.Solution.Workspace); var newMethodString = newMethod.WithLeadingTrivia().GetText().ToString(); Console.WriteLine("New method (without leading trivia):\r\n" + newMethodString); return(newMethodString); }