CodeAction GetAction(ObjectCreateExpression objectCreateExpression, PrimitiveExpression firstParam, PrimitiveExpression secondParam) { return(new CodeAction(context.TranslateString("Swap parameters"), script => { var newOCE = objectCreateExpression.Clone() as ObjectCreateExpression; newOCE.Arguments.Clear(); newOCE.Arguments.Add(secondParam.Clone()); newOCE.Arguments.Add(firstParam.Clone()); script.Replace(objectCreateExpression, newOCE); }, objectCreateExpression)); }
public override void VisitObjectCreateExpression (ObjectCreateExpression objectCreateExpression) { base.VisitObjectCreateExpression (objectCreateExpression); if (objectCreateExpression.Initializer.IsNull || objectCreateExpression.Initializer.Elements.Count > 0) return; AddIssue (objectCreateExpression.Initializer, ctx.TranslateString ("Remove redundant empty initializer"), script => { var expr = (ObjectCreateExpression)objectCreateExpression.Clone (); expr.Initializer = ArrayInitializerExpression.Null; script.Replace (objectCreateExpression, expr); }); }
public override void VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression) { base.VisitObjectCreateExpression(objectCreateExpression); if (objectCreateExpression.Initializer.IsNull || objectCreateExpression.Initializer.Elements.Count > 0) { return; } AddIssue(objectCreateExpression.Initializer, ctx.TranslateString("Remove redundant empty initializer"), script => { var expr = (ObjectCreateExpression)objectCreateExpression.Clone(); expr.Initializer = ArrayInitializerExpression.Null; script.Replace(objectCreateExpression, expr); }); }
public override void VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression) { base.VisitObjectCreateExpression(objectCreateExpression); if (objectCreateExpression.Initializer.IsNull || objectCreateExpression.Initializer.Elements.Count > 0) { return; } AddIssue(new CodeIssue(objectCreateExpression.Initializer, ctx.TranslateString("Empty object or collection initializer is redundant"), ctx.TranslateString("Remove initializer"), script => { var expr = (ObjectCreateExpression)objectCreateExpression.Clone(); expr.Initializer = ArrayInitializerExpression.Null; script.Replace(objectCreateExpression, expr); }) { IssueMarker = IssueMarker.GrayOut }); }
bool HandleAnonymousMethod(ObjectCreateExpression objectCreateExpression, Expression target, MethodReference methodRef) { if (!context.Settings.AnonymousMethods) { return(false); // anonymous method decompilation is disabled } if (target != null && !(target is IdentifierExpression || target is ThisReferenceExpression || target is NullReferenceExpression)) { return(false); // don't copy arbitrary expressions, deal with identifiers only } // Anonymous methods are defined in the same assembly MethodDefinition method = methodRef.ResolveWithinSameModule(); if (!IsAnonymousMethod(context, method)) { return(false); } // Create AnonymousMethodExpression and prepare parameters AnonymousMethodExpression ame = new AnonymousMethodExpression(); ame.CopyAnnotationsFrom(objectCreateExpression); // copy ILRanges etc. ame.RemoveAnnotations <MethodReference>(); // remove reference to delegate ctor ame.AddAnnotation(method); // add reference to anonymous method ame.Parameters.AddRange(AstBuilder.MakeParameters(method, isLambda: true)); ame.HasParameterList = true; // rename variables so that they don't conflict with the parameters: foreach (ParameterDeclaration pd in ame.Parameters) { EnsureVariableNameIsAvailable(objectCreateExpression, pd.Name); } // Decompile the anonymous method: DecompilerContext subContext = context.Clone(); subContext.CurrentMethod = method; subContext.CurrentMethodIsAsync = false; subContext.ReservedVariableNames.AddRange(currentlyUsedVariableNames); BlockStatement body = AstMethodBodyBuilder.CreateMethodBody(method, subContext, ame.Parameters); TransformationPipeline.RunTransformationsUntil(body, v => v is DelegateConstruction, subContext); body.AcceptVisitor(this, null); bool isLambda = false; if (ame.Parameters.All(p => p.ParameterModifier == ParameterModifier.None)) { isLambda = (body.Statements.Count == 1 && body.Statements.Single() is ReturnStatement); } // Remove the parameter list from an AnonymousMethodExpression if the original method had no names, // and the parameters are not used in the method body if (!isLambda && method.Parameters.All(p => string.IsNullOrEmpty(p.Name))) { var parameterReferencingIdentifiers = from ident in body.Descendants.OfType <IdentifierExpression>() let v = ident.Annotation <ILVariable>() where v != null && v.IsParameter && method.Parameters.Contains(v.OriginalParameter) select ident; if (!parameterReferencingIdentifiers.Any()) { ame.Parameters.Clear(); ame.HasParameterList = false; } } // Replace all occurrences of 'this' in the method body with the delegate's target: foreach (AstNode node in body.Descendants) { if (node is ThisReferenceExpression) { node.ReplaceWith(target.Clone()); } } Expression replacement; if (isLambda) { LambdaExpression lambda = new LambdaExpression(); lambda.CopyAnnotationsFrom(ame); ame.Parameters.MoveTo(lambda.Parameters); Expression returnExpr = ((ReturnStatement)body.Statements.Single()).Expression; returnExpr.Remove(); lambda.Body = returnExpr; replacement = lambda; } else { ame.Body = body; replacement = ame; } var expectedType = objectCreateExpression.Annotation <TypeInformation>().ExpectedType.Resolve(); if (expectedType != null && !expectedType.IsDelegate()) { var simplifiedDelegateCreation = (ObjectCreateExpression)objectCreateExpression.Clone(); simplifiedDelegateCreation.Arguments.Clear(); simplifiedDelegateCreation.Arguments.Add(replacement); replacement = simplifiedDelegateCreation; } objectCreateExpression.ReplaceWith(replacement); return(true); }
IEnumerable<CodeAction> GetActions(ObjectCreateExpression objectCreateExpression, PrimitiveExpression firstParam, PrimitiveExpression secondParam) { yield return new CodeAction(context.TranslateString("Swap parameters"), script => { var newOCE = objectCreateExpression.Clone() as ObjectCreateExpression; newOCE.Arguments.Clear(); newOCE.Arguments.Add(secondParam.Clone()); newOCE.Arguments.Add(firstParam.Clone()); script.Replace(objectCreateExpression, newOCE); }); }
public override void VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression) { base.VisitObjectCreateExpression(objectCreateExpression); Expression paramNode; Expression altParamNode; bool canAddParameterName; if (!CheckExceptionType(objectCreateExpression, out paramNode, out altParamNode, out canAddParameterName)) { return; } var paramName = GetArgumentParameterName(paramNode); if (paramName == null) { return; } var validNames = GetValidParameterNames(objectCreateExpression); if (!validNames.Contains(paramName)) { // Case 1: Parameter name is swapped var altParamName = GetArgumentParameterName(altParamNode); if (altParamName != null && validNames.Contains(altParamName)) { AddIssue(new CodeIssue( paramNode, string.Format(context.TranslateString("The parameter '{0}' can't be resolved"), paramName), context.TranslateString("Swap parameter."), script => { var newAltNode = paramNode.Clone(); script.Replace(paramNode, altParamNode.Clone()); script.Replace(altParamNode, newAltNode); } )); AddIssue(new CodeIssue( altParamNode, context.TranslateString("The parameter name is on the wrong argument."), context.TranslateString("Swap parameter."), script => { var newAltNode = paramNode.Clone(); script.Replace(paramNode, altParamNode.Clone()); script.Replace(altParamNode, newAltNode); } )); return; } var guessName = GuessParameterName(objectCreateExpression, validNames); if (guessName != null) { var actions = new List <CodeAction>(); actions.Add(new CodeAction( string.Format(context.TranslateString("Replace with '\"{0}\"'."), guessName), script => { script.Replace(paramNode, new PrimitiveExpression(guessName)); }, paramNode )); if (canAddParameterName) { actions.Add(new CodeAction( string.Format(context.TranslateString("Add '\"{0}\"' parameter."), guessName), script => { var oce = (ObjectCreateExpression)objectCreateExpression.Clone(); oce.Arguments.Clear(); oce.Arguments.Add(new PrimitiveExpression(guessName)); oce.Arguments.Add(objectCreateExpression.Arguments.First().Clone()); script.Replace(objectCreateExpression, oce); }, paramNode )); } AddIssue(new CodeIssue( paramNode, string.Format(context.TranslateString("The parameter '{0}' can't be resolved"), paramName), actions )); return; } // General case: mark only AddIssue(new CodeIssue( paramNode, string.Format(context.TranslateString("The parameter '{0}' can't be resolved"), paramName) )); } }