|
| 1 | +using System.Linq; |
| 2 | +using System.Threading; |
| 3 | +using System.Collections.Generic; |
| 4 | +using Microsoft.CodeAnalysis.CodeRefactorings; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.CodeAnalysis.CodeActions; |
| 8 | +using Microsoft.CodeAnalysis.Text; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | +using Microsoft.CodeAnalysis.CSharp; |
| 11 | +using Microsoft.CodeAnalysis.Simplification; |
| 12 | +using Microsoft.CodeAnalysis.Formatting; |
| 13 | + |
| 14 | +namespace RefactoringEssentials.CSharp.CodeRefactorings |
| 15 | +{ |
| 16 | + [ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = "Add a Contract to specify the string parameter must not be null or empty")] |
| 17 | + /// <summary> |
| 18 | + /// Creates a 'Contract.Requires(param != null);' contract for a parameter. |
| 19 | + /// </summary> |
| 20 | + public class ContractRequiresStringNotNullOrEmptyCodeRefactoringProvider : CodeContractsCodeRefactoringProvider |
| 21 | + { |
| 22 | + #region ICodeActionProvider implementation |
| 23 | + public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) |
| 24 | + { |
| 25 | + var codeContractsContext = await CodeContractsContext(context); |
| 26 | + if (codeContractsContext == null) |
| 27 | + return; |
| 28 | + |
| 29 | + var foundNode = (ParameterSyntax)codeContractsContext.Node.AncestorsAndSelf().FirstOrDefault(n => n is ParameterSyntax); |
| 30 | + if (foundNode == null) |
| 31 | + return; |
| 32 | + |
| 33 | + foreach (var action in GetActions(codeContractsContext.Document, codeContractsContext.SemanticModel, codeContractsContext.Root, codeContractsContext.TextSpan, foundNode)) |
| 34 | + context.RegisterRefactoring(action); |
| 35 | + } |
| 36 | + #endregion |
| 37 | + |
| 38 | + protected IEnumerable<CodeAction> GetActions(Document document, SemanticModel semanticModel, SyntaxNode root, TextSpan span, ParameterSyntax node) |
| 39 | + { |
| 40 | + if (!node.Identifier.Span.Contains(span)) |
| 41 | + yield break; |
| 42 | + |
| 43 | + var parameter = node; |
| 44 | + var bodyStatement = parameter.Parent.Parent.ChildNodes().OfType<BlockSyntax>().FirstOrDefault(); |
| 45 | + if (bodyStatement == null) |
| 46 | + yield break; |
| 47 | + |
| 48 | + var parameterSymbol = semanticModel.GetDeclaredSymbol(node); |
| 49 | + var type = parameterSymbol.Type; |
| 50 | + if (type.SpecialType != SpecialType.System_String || HasReturnContract(bodyStatement, parameterSymbol.Name)) |
| 51 | + yield break; |
| 52 | + |
| 53 | + yield return CreateAction( |
| 54 | + node.Identifier.Span |
| 55 | + , t2 => { |
| 56 | + var newBody = bodyStatement.WithStatements(SyntaxFactory.List<StatementSyntax>(new[] { CreateContractRequiresCall(node.Identifier.ToString()) }.Concat(bodyStatement.Statements))); |
| 57 | + |
| 58 | + var newRoot = (CompilationUnitSyntax)root.ReplaceNode((SyntaxNode)bodyStatement, newBody); |
| 59 | + |
| 60 | + if (UsingStatementNotPresent(newRoot)) newRoot = AddUsingStatement(node, newRoot); |
| 61 | + |
| 62 | + return Task.FromResult(document.WithSyntaxRoot(newRoot)); |
| 63 | + } |
| 64 | + , "Add contract requiring parameter must not be null or empty" |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + static StatementSyntax CreateContractRequiresCall(string parameterName) |
| 69 | + { |
| 70 | + return SyntaxFactory.ParseStatement($"Contract.Requires(string.IsNullOrEmpty({parameterName}) == false);\r\n").WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation); |
| 71 | + } |
| 72 | + |
| 73 | + static bool HasReturnContract(BlockSyntax bodyStatement, string parameterName) |
| 74 | + { |
| 75 | + var workspace = new AdhocWorkspace(); |
| 76 | + |
| 77 | + foreach (var expression in bodyStatement.DescendantNodes().OfType<ExpressionStatementSyntax>()) |
| 78 | + { |
| 79 | + var formatted = Formatter.Format(expression, workspace).ToString(); |
| 80 | + |
| 81 | + if (formatted == $"Contract.Requires(string.IsNullOrEmpty({parameterName}) == false);") |
| 82 | + return true; |
| 83 | + |
| 84 | + if (formatted == $"Contract.Requires(false == string.IsNullOrEmpty({parameterName}));") |
| 85 | + return true; |
| 86 | + |
| 87 | + if (formatted == $"Contract.Requires(!string.IsNullOrEmpty({parameterName}));") |
| 88 | + return true; |
| 89 | + } |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments