Exemple #1
0
 public override object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     script_.Write("@(");
     VisitElement(arrayExpressionAst.SubExpression);
     script_.Write(")");
     return(arrayExpressionAst);
 }
        private List <string> GetArrayFromAst(ExpressionAst exprAst)
        {
            ArrayLiteralAst arrayLitAst = exprAst as ArrayLiteralAst;
            var             result      = new List <string>();

            if (arrayLitAst == null && exprAst is ArrayExpressionAst)
            {
                ArrayExpressionAst arrayExp = (ArrayExpressionAst)exprAst;
                return(arrayExp == null ? null : GetArrayFromArrayExpressionAst(arrayExp));
            }

            if (arrayLitAst == null)
            {
                ThrowInvalidDataException(arrayLitAst);
            }

            foreach (var element in arrayLitAst.Elements)
            {
                var elementValue = element as StringConstantExpressionAst;
                if (elementValue == null)
                {
                    ThrowInvalidDataException(element);
                }

                result.Add(elementValue.Value);
            }

            return(result);
        }
        public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
        {
            // An Array expression *may* be safe, if its components are safe
            var arrayExpressionAstResult = (object[])arrayExpressionAst.SubExpression.Accept(this);

            return(arrayExpressionAstResult);
        }
        private List <string> GetArrayFromArrayExpressionAst(ArrayExpressionAst arrayExp)
        {
            var result = new List <string>();

            if (arrayExp.SubExpression != null)
            {
                StatementAst stateAst = arrayExp.SubExpression.Statements.FirstOrDefault();
                if (stateAst != null && stateAst is PipelineAst)
                {
                    CommandBaseAst cmdBaseAst = (stateAst as PipelineAst).PipelineElements.FirstOrDefault();
                    if (cmdBaseAst != null && cmdBaseAst is CommandExpressionAst)
                    {
                        CommandExpressionAst cmdExpAst = cmdBaseAst as CommandExpressionAst;
                        if (cmdExpAst.Expression is StringConstantExpressionAst)
                        {
                            return(new List <string>()
                            {
                                (cmdExpAst.Expression as StringConstantExpressionAst).Value
                            });
                        }
                        else
                        {
                            // It should be an ArrayLiteralAst at this point
                            return(GetArrayFromAst(cmdExpAst.Expression));
                        }
                    }
                }
            }

            return(result);
        }
 public static ArrayExpressionAst Update(this ArrayExpressionAst ast, IEnumerable <StatementAst> statements)
 {
     return(new ArrayExpressionAst(
                ast.Extent,
                new StatementBlockAst(
                    ast.SubExpression.Extent,
                    statements.CloneAll(),
                    ast.SubExpression.Traps.CloneAll())));
 }
Exemple #6
0
        public override AstVisitAction VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
        {
            foreach (var statement in arrayExpressionAst.SubExpression.Statements)
            {
                var value = EvaluateAst(statement, false);
                this._pipelineCommandRuntime.WriteObject(value, true);
            }

            return(AstVisitAction.SkipChildren);
        }
Exemple #7
0
        private Dictionary <string, string> GetParameters(ReadOnlyCollection <CommandElementAst> commandElements)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            bool   flag = false;
            string key  = null;
            int    num  = 0;

            foreach (CommandElementAst ast in commandElements)
            {
                if (!flag)
                {
                    flag = true;
                }
                else
                {
                    CommandParameterAst ast2 = ast as CommandParameterAst;
                    if (ast2 != null)
                    {
                        string parameterName = ast2.ParameterName;
                        if (ast2.Argument != null)
                        {
                            dictionary.Add(parameterName, ast2.Argument.ToString());
                            key = null;
                        }
                        else
                        {
                            key = parameterName;
                        }
                    }
                    else if (key != null)
                    {
                        ArrayExpressionAst ast3 = ast as ArrayExpressionAst;
                        if (ast3 != null)
                        {
                            dictionary.Add(key, ast3.SubExpression.ToString());
                        }
                        else
                        {
                            dictionary.Add(key, ast.ToString());
                        }
                        key = null;
                    }
                    else
                    {
                        dictionary.Add(num.ToString(CultureInfo.InvariantCulture), ast.ToString());
                        num++;
                    }
                }
            }
            return(dictionary);
        }
        public override AstVisitAction VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
        {
            var helpResult = HelpTableQuery("about_arrays");

            helpResult.DocumentationLink += "#the-array-sub-expression-operator";

            explanations.Add(
                new Explanation()
            {
                Description     = "The array sub-expression operator creates an array from the statements inside it. Whatever the statement inside the operator produces, the operator will place it in an array. Even if there is zero or one object.",
                CommandName     = "Array expression",
                HelpResult      = helpResult,
                TextToHighlight = "@(",
            }.AddDefaults(arrayExpressionAst, explanations));

            return(base.VisitArrayExpression(arrayExpressionAst));
        }
 #pragma warning disable SA1600, SA1201, SA1516
 public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst) => null;
Exemple #10
0
 public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     Console.WriteLine("Visited an ArrayExpressionAst.");
     return(arrayExpressionAst);
 }
 public virtual ExpressionAst VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     return(new ArrayExpressionAst(
                arrayExpressionAst.Extent,
                VisitStatementBlock(arrayExpressionAst.SubExpression)));
 }
Exemple #12
0
 /// <summary/>
 public virtual AstVisitAction VisitArrayExpression(ArrayExpressionAst arrayExpressionAst) => DefaultVisit(arrayExpressionAst);
Exemple #13
0
        public void ArraySubexpression()
        {
            ArrayExpressionAst arrayExpressionAst = ParseStatement("@(1)").PipelineElements[0].Expression;

            Assert.AreEqual(1, arrayExpressionAst.SubExpression.Statements.Count);
        }
 object ICustomAstVisitor.VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 => ProcessRewriter(VisitArrayExpression, arrayExpressionAst);
Exemple #15
0
        /// <summary>
        /// AnalyzeScript: Analyzes the ast to check that cmdlets have help.
        /// </summary>
        /// <param name="ast">The script's ast</param>
        /// <param name="fileName">The name of the script</param>
        /// <returns>A List of diagnostic results of this rule</returns>
        public IEnumerable <DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
        {
            if (ast == null)
            {
                throw new ArgumentNullException(Strings.NullAstErrorMessage);
            }

            DiagnosticRecords.Clear();
            this.fileName     = fileName;
            exportedFunctions = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            List <string> exportFunctionsCmdlet = Helper.Instance.CmdletNameAndAliases("export-modulemember");

            // find functions exported
            IEnumerable <Ast> cmdAsts = ast.FindAll(item => item is CommandAst &&
                                                    exportFunctionsCmdlet.Contains((item as CommandAst).GetCommandName(), StringComparer.OrdinalIgnoreCase), true);

            CommandInfo exportMM = Helper.Instance.GetCommandInfo("export-modulemember", CommandTypes.Cmdlet);

            // switch parameters
            IEnumerable <ParameterMetadata> switchParams = (exportMM != null) ? exportMM.Parameters.Values.Where <ParameterMetadata>(pm => pm.SwitchParameter) : Enumerable.Empty <ParameterMetadata>();

            if (exportMM == null)
            {
                return(DiagnosticRecords);
            }

            foreach (CommandAst cmdAst in cmdAsts)
            {
                if (cmdAst.CommandElements == null || cmdAst.CommandElements.Count < 2)
                {
                    continue;
                }

                int i = 1;

                while (i < cmdAst.CommandElements.Count)
                {
                    CommandElementAst ceAst   = cmdAst.CommandElements[i];
                    ExpressionAst     exprAst = null;

                    if (ceAst is CommandParameterAst)
                    {
                        var paramAst = ceAst as CommandParameterAst;
                        var param    = exportMM.ResolveParameter(paramAst.ParameterName);

                        if (param == null)
                        {
                            i += 1;
                            continue;
                        }

                        if (string.Equals(param.Name, "function", StringComparison.OrdinalIgnoreCase))
                        {
                            // checks for the case of -Function:"verb-nouns"
                            if (paramAst.Argument != null)
                            {
                                exprAst = paramAst.Argument;
                            }
                            // checks for the case of -Function "verb-nouns"
                            else if (i < cmdAst.CommandElements.Count - 1)
                            {
                                i      += 1;
                                exprAst = cmdAst.CommandElements[i] as ExpressionAst;
                            }
                        }
                        // some other parameter. we just checks whether the one after this is positional
                        else if (i < cmdAst.CommandElements.Count - 1)
                        {
                            // the next element is a parameter like -module so just move to that one
                            if (cmdAst.CommandElements[i + 1] is CommandParameterAst)
                            {
                                i += 1;
                                continue;
                            }

                            // not a switch parameter so the next element is definitely the argument to this parameter
                            if (paramAst.Argument == null && !switchParams.Contains(param))
                            {
                                // skips the next element
                                i += 1;
                            }

                            i += 1;
                            continue;
                        }
                    }
                    else if (ceAst is ExpressionAst)
                    {
                        exprAst = ceAst as ExpressionAst;
                    }

                    if (exprAst != null)
                    {
                        // One string so just add this to the list
                        if (exprAst is StringConstantExpressionAst)
                        {
                            exportedFunctions.Add((exprAst as StringConstantExpressionAst).Value);
                        }
                        // Array of the form "v-n", "v-n1"
                        else if (exprAst is ArrayLiteralAst)
                        {
                            exportedFunctions.UnionWith(Helper.Instance.GetStringsFromArrayLiteral(exprAst as ArrayLiteralAst));
                        }
                        // Array of the form @("v-n", "v-n1")
                        else if (exprAst is ArrayExpressionAst)
                        {
                            ArrayExpressionAst arrExAst = exprAst as ArrayExpressionAst;
                            if (arrExAst.SubExpression != null && arrExAst.SubExpression.Statements != null)
                            {
                                foreach (StatementAst stAst in arrExAst.SubExpression.Statements)
                                {
                                    if (stAst is PipelineAst)
                                    {
                                        PipelineAst pipeAst = stAst as PipelineAst;
                                        if (pipeAst.PipelineElements != null)
                                        {
                                            foreach (CommandBaseAst cmdBaseAst in pipeAst.PipelineElements)
                                            {
                                                if (cmdBaseAst is CommandExpressionAst)
                                                {
                                                    exportedFunctions.UnionWith(Helper.Instance.GetStringsFromArrayLiteral((cmdBaseAst as CommandExpressionAst).Expression as ArrayLiteralAst));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    i += 1;
                }
            }

            ast.Visit(this);

            return(DiagnosticRecords);
        }
 public override AstVisitAction VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     return(Visit(arrayExpressionAst));
 }
 object ICustomAstVisitor.VisitArrayExpression(ArrayExpressionAst arrayExpressionAst) => VisitArrayExpression(arrayExpressionAst);
Exemple #18
0
 public override AstVisitAction VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     Console.WriteLine("Visited an ArrayExpressionAst.");
     Console.WriteLine("    " + arrayExpressionAst.ToString().Replace(Environment.NewLine, Environment.NewLine + "    "));
     return(AstVisitAction.Continue);
 }
Exemple #19
0
 public override AstVisitAction VisitArrayExpression(ArrayExpressionAst ast)
 {
     return(AstVisitAction.Continue);
 }
Exemple #20
0
 public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     return(Visit(arrayExpressionAst.SubExpression));
 }
 public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     // An Array expression *may* be safe, if its components are safe
     return(arrayExpressionAst.SubExpression.Accept(this));
 }
Exemple #22
0
 public virtual object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     VisitElement(arrayExpressionAst.SubExpression);
     return(arrayExpressionAst);
 }
 public virtual TResult VisitArrayExpression(ArrayExpressionAst arrayExpressionAst) => default(TResult);
Exemple #24
0
 public override AstVisitAction VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     throw new NotImplementedException(); //VisitArrayExpression(arrayExpressionAst);
 }
Exemple #25
0
 public override AstVisitAction VisitArrayExpression(ArrayExpressionAst ast)
 {
     return(DoNextAction(ast));
 }
        public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
        {
            var newStatementBlock = VisitElement(arrayExpressionAst.SubExpression);

            return(new ArrayExpressionAst(arrayExpressionAst.Extent, newStatementBlock));
        }
Exemple #27
0
 public override ExpressionAst VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 => VisitExpression(base.VisitArrayExpression(arrayExpressionAst));
 public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst)
 {
     return(arrayExpressionAst.SubExpression.Accept(this));
 }
Exemple #29
0
 public override AstVisitAction VisitArrayExpression(ArrayExpressionAst arrayExpressionAst) => VisitAst(arrayExpressionAst);