private bool IsNamedAttributeArgumentMandatory(NamedAttributeArgumentAst namedAttrArgAst)
 {
     return(namedAttrArgAst.ArgumentName.Equals("mandatory", StringComparison.OrdinalIgnoreCase) &&
            namedAttrArgAst.GetValue());
 }
示例#2
0
        /// <summary>
        /// Return the boolean value of a named attribute argument.
        /// </summary>
        public static bool GetValue(this NamedAttributeArgumentAst attrAst)
        {
            ExpressionAst argumentAst;

            return(attrAst.GetValue(out argumentAst));
        }
        private List <CorrectionExtent> GetCorrections(
            int whatIfIndex,
            int confirmIndex,
            ParameterAst[] parameterAsts,
            ParamBlockAst paramBlockAst,
            FunctionDefinitionAst funcDefnAst)
        {
            var filePath          = funcDefnAst.Extent.File;
            var correctionExtents = new List <CorrectionExtent>();

            if (paramBlockAst != null)
            {
                if (whatIfIndex != -1)
                {
                    correctionExtents.Add(GetCorrectionToRemoveParam(whatIfIndex, parameterAsts));
                }

                if (confirmIndex != -1)
                {
                    correctionExtents.Add(GetCorrectionToRemoveParam(confirmIndex, parameterAsts));
                }

                AttributeAst attributeAst = paramBlockAst.GetCmdletBindingAttributeAst();

                // check if it has cmdletbinding attribute
                if (attributeAst != null)
                {
                    NamedAttributeArgumentAst shouldProcessAst = attributeAst.GetSupportsShouldProcessAst();
                    if (shouldProcessAst != null)
                    {
                        ExpressionAst argAst;
                        if (!shouldProcessAst.GetValue(out argAst) &&
                            argAst != null)
                        {
                            // SupportsShouldProcess is set to something other than $true.
                            // Set it to $true
                            correctionExtents.Add(GetCorrectionsToSetShouldProcessToTrue(argAst));
                        }
                    }
                    else
                    {
                        // add supportsshouldprocess to the attribute
                        correctionExtents.Add(GetCorrectionToAddShouldProcess(attributeAst));
                    }
                }
                else
                {
                    // has no cmdletbinding attribute
                    // hence, add the attribute and supportsshouldprocess argument
                    correctionExtents.Add(GetCorrectionToAddAttribute(paramBlockAst));
                }
            }
            else
            {
                // function doesn't have param block
                // remove the parameter list
                // and create an equivalent param block
                // add cmdletbinding attribute and add supportsshouldprocess to it.
                correctionExtents.Add(GetCorrectionToRemoveFuncParamDecl(funcDefnAst, ast, tokens));
                correctionExtents.Add(GetCorrectionToAddParamBlock(funcDefnAst, parameterAsts));
            }

            // This is how we handle multiple edits-
            // create separate edits
            // apply those edits to the original script extent
            // and then give the corrected extent as suggested correction.

            // sort in descending order of start position
            correctionExtents.Sort((x, y) =>
            {
                var xRange = (Range)x;
                var yRange = (Range)y;
                return(xRange.Start < yRange.Start ? 1 : (xRange.Start == yRange.Start ? 0 : -1));
            });

            var editableText       = new EditableText(funcDefnAst.Extent.Text);
            var funcDefAstStartPos = funcDefnAst.Extent.ToRange().Start;

            foreach (var correctionExtent in correctionExtents)
            {
                var shiftedCorrectionExtent = Normalize(funcDefAstStartPos, correctionExtent);
                editableText.ApplyEdit(shiftedCorrectionExtent);
            }

            var result = new List <CorrectionExtent>();

            result.Add(
                new CorrectionExtent(
                    funcDefnAst.Extent.StartLineNumber,
                    funcDefnAst.Extent.EndLineNumber,
                    funcDefnAst.Extent.StartColumnNumber,
                    funcDefnAst.Extent.EndColumnNumber,
                    editableText.ToString(),
                    funcDefnAst.Extent.File));
            return(result);
        }