public IfStatementAst If(PipelineBaseAst condition, params StatementAst[] statements)
 {
     return(new IfStatementAst(
                _currentExtent,
                new[] { Tuple.Create(condition, Block(statements)) },
                null));
 }
Ejemplo n.º 2
0
 internal static bool TryFindExplicitReturn(Ast ast, out PipelineBaseAst returnValue)
 {
     var visitor = new ExplicitReturnVisitor();
     ast.Visit(visitor);
     returnValue = visitor._returnPipeline;
     return visitor._wasFound;
 }
Ejemplo n.º 3
0
    public System.Object VisitParenExpression(System.Management.Automation.Language.ParenExpressionAst parenExpressionAst)
    {
        IScriptExtent mappedExtent = MapExtent(parenExpressionAst.Extent);

        PipelineBaseAst mappedPipeline = _VisitPipelineBase(parenExpressionAst.Pipeline);

        return(new ParenExpressionAst(mappedExtent, mappedPipeline));
    }
Ejemplo n.º 4
0
    public System.Object VisitReturnStatement(System.Management.Automation.Language.ReturnStatementAst returnStatementAst)
    {
        IScriptExtent mappedExtent = MapExtent(returnStatementAst.Extent);

        PipelineBaseAst mappedPipeline = returnStatementAst.Pipeline != null?_VisitPipelineBase(returnStatementAst.Pipeline) : null;

        return(new ReturnStatementAst(mappedExtent, mappedPipeline));
    }
Ejemplo n.º 5
0
    public System.Object VisitThrowStatement(System.Management.Automation.Language.ThrowStatementAst throwStatementAst)
    {
        IScriptExtent mappedExtent = MapExtent(throwStatementAst.Extent);

        PipelineBaseAst mappedPipeline = _VisitPipelineBase(throwStatementAst.Pipeline);

        return(new ThrowStatementAst(mappedExtent, mappedPipeline));
    }
Ejemplo n.º 6
0
    public System.Object VisitWhileStatement(System.Management.Automation.Language.WhileStatementAst whileStatementAst)
    {
        IScriptExtent mappedExtent = MapExtent(whileStatementAst.Extent);

        PipelineBaseAst   mappedCondition = _VisitPipelineBase(whileStatementAst.Condition);
        StatementBlockAst mappedBody      = (StatementBlockAst)VisitStatementBlock(whileStatementAst.Body);

        return(new WhileStatementAst(mappedExtent, whileStatementAst.Label, mappedCondition, mappedBody));
    }
 public static ParenExpressionAst As(
     this Ast source,
     AstType <ParenExpressionAst> targetType,
     PipelineBaseAst pipeline)
 {
     return(new ParenExpressionAst(
                source.Extent,
                pipeline));
 }
Ejemplo n.º 8
0
    public System.Object VisitForEachStatement(System.Management.Automation.Language.ForEachStatementAst forEachStatementAst)
    {
        IScriptExtent mappedExtent = MapExtent(forEachStatementAst.Extent);

        VariableExpressionAst mappedVariable   = (VariableExpressionAst)VisitVariableExpression(forEachStatementAst.Variable);
        PipelineBaseAst       mappedExpression = _VisitPipelineBase(forEachStatementAst.Condition);
        StatementBlockAst     mappedBody       = (StatementBlockAst)VisitStatementBlock(forEachStatementAst.Body);

        return(new ForEachStatementAst(mappedExtent, forEachStatementAst.Label, forEachStatementAst.Flags, mappedVariable, mappedExpression, mappedBody));
    }
 public IfStatementAst If(
     PipelineBaseAst condition,
     StatementBlockAst ifTrueBody,
     StatementBlockAst ifFalseBody)
 {
     return(new IfStatementAst(
                _currentExtent,
                new[] { Tuple.Create(condition, ifTrueBody) },
                ifFalseBody));
 }
Ejemplo n.º 10
0
    public PipelineBaseAst _VisitPipelineBase(PipelineBaseAst pipelineBaseAst)
    {
        var @switch = new Dictionary <Type, Func <PipelineBaseAst, PipelineBaseAst> > {
            { typeof(AssignmentStatementAst), (pb) => { return((PipelineBaseAst)VisitAssignmentStatement(pb as AssignmentStatementAst)); } },
            { typeof(ErrorStatementAst), (pb) => { return((PipelineBaseAst)VisitErrorStatement(pb as ErrorStatementAst)); } },
            { typeof(PipelineAst), (pb) => { return((PipelineBaseAst)VisitPipeline(pb as PipelineAst)); } }
        };

        return(@switch[pipelineBaseAst.GetType()](pipelineBaseAst));
    }
 public static IfStatementAst As(
     this Ast source,
     AstType <IfStatementAst> targetType,
     PipelineBaseAst condition,
     params StatementAst[] statements)
 {
     return(new IfStatementAst(
                source.Extent,
                new[] { Tuple.Create(condition, source.As(StatementBlock, statements)) },
                null));
 }
Ejemplo n.º 12
0
    public System.Object VisitForStatement(System.Management.Automation.Language.ForStatementAst forStatementAst)
    {
        IScriptExtent mappedExtent = MapExtent(forStatementAst.Extent);

        PipelineBaseAst   mappedInitializer = _VisitPipelineBase(forStatementAst.Initializer);
        PipelineBaseAst   mappedCondition   = _VisitPipelineBase(forStatementAst.Condition);
        PipelineBaseAst   mappedIterator    = _VisitPipelineBase(forStatementAst.Iterator);
        StatementBlockAst mappedBody        = (StatementBlockAst)VisitStatementBlock(forStatementAst.Body);


        return(new ForStatementAst(mappedExtent, forStatementAst.Label, mappedInitializer, mappedCondition, mappedIterator, mappedBody));
    }
 public static DoWhileStatementAst Update(
     this DoWhileStatementAst ast,
     string label = null,
     PipelineBaseAst condition             = null,
     IEnumerable <StatementAst> statements = null)
 {
     return(new DoWhileStatementAst(
                ast.Extent,
                label ?? ast.Label,
                condition?.Clone() ?? ast.Condition.Clone(),
                ast.Body.Update(statements)));
 }
 public static WhileStatementAst Update(
     this WhileStatementAst ast,
     PipelineBaseAst condition = null,
     StatementBlockAst body    = null,
     string label = null)
 {
     return(new WhileStatementAst(
                ast.Extent,
                label ?? ast.Label,
                condition?.Clone() ?? ast.Condition?.Clone(),
                body?.Clone() ?? ast.Body?.Clone()));
 }
 public static IfStatementAst As(
     this Ast source,
     AstType <IfStatementAst> targetType,
     PipelineBaseAst condition,
     StatementBlockAst ifTrueBody,
     StatementBlockAst ifFalseBody)
 {
     return(new IfStatementAst(
                source.Extent,
                new[] { Tuple.Create(condition, ifTrueBody) },
                ifFalseBody));
 }
 public static SwitchStatementAst Update(
     this SwitchStatementAst ast,
     string label = null,
     PipelineBaseAst condition = null,
     SwitchFlags?flags         = null,
     IEnumerable <Tuple <ExpressionAst, StatementBlockAst> > clauses = null,
     StatementBlockAst @default = null)
 {
     return(new SwitchStatementAst(
                ast.Extent,
                label ?? ast.Label,
                condition?.Clone() ?? ast.Condition?.Clone(),
                flags ?? ast.Flags,
                clauses?.CloneAll() ?? ast.Clauses?.CloneAll(),
                @default?.Clone() ?? ast.Default?.Clone()));
 }
 public static ForEachStatementAst Update(
     this ForEachStatementAst ast,
     VariableExpressionAst variable        = null,
     PipelineBaseAst expression            = null,
     IEnumerable <StatementAst> statements = null,
     string label       = null,
     ForEachFlags?flags = null)
 {
     return(new ForEachStatementAst(
                ast.Extent,
                label ?? ast.Label,
                flags ?? ast.Flags,
                variable?.Clone() ?? ast.Variable.Clone(),
                expression?.Clone() ?? ast.Condition.Clone(),
                ast.Body.Update(statements)));
 }
 public static ForStatementAst Update(
     this ForStatementAst ast,
     PipelineBaseAst initializer           = null,
     PipelineBaseAst condition             = null,
     PipelineBaseAst iterator              = null,
     IEnumerable <StatementAst> statements = null,
     string label = null)
 {
     return(new ForStatementAst(
                ast.Extent,
                label ?? ast.Label,
                initializer?.Clone() ?? ast.Initializer.Clone(),
                condition?.Clone() ?? ast.Condition.Clone(),
                iterator?.Clone() ?? ast.Iterator?.Clone(),
                ast.Body.Update(statements)));
 }
        private DiagnosticRecord AnalyzePipelineBaseAst(PipelineBaseAst pipelineBaseAst, string fileName)
        {
            var assignmentStatementAst = pipelineBaseAst.Find(testAst => testAst is AssignmentStatementAst, searchNestedScriptBlocks: false) as AssignmentStatementAst;

            if (assignmentStatementAst == null)
            {
                return(null);
            }

            // Check if someone used '==', which can easily happen when the person is used to coding a lot in C#.
            // In most cases, this will be a runtime error because PowerShell will look for a cmdlet name starting with '=', which is technically possible to define
            if (assignmentStatementAst.Right.Extent.Text.StartsWith("="))
            {
                return(new DiagnosticRecord(
                           Strings.PossibleIncorrectUsageOfAssignmentOperatorError, assignmentStatementAst.ErrorPosition,
                           GetName(), DiagnosticSeverity.Warning, fileName));
            }

            // Check if LHS is $null and then always warn
            if (assignmentStatementAst.Left is VariableExpressionAst variableExpressionAst)
            {
                if (variableExpressionAst.VariablePath.UserPath.Equals("null", StringComparison.OrdinalIgnoreCase))
                {
                    return(new DiagnosticRecord(
                               Strings.PossibleIncorrectUsageOfAssignmentOperatorError, assignmentStatementAst.ErrorPosition,
                               GetName(), DiagnosticSeverity.Warning, fileName));
                }
            }

            // If the RHS contains a CommandAst at some point, then we do not want to warn  because this could be intentional in cases like 'if ($a = Get-ChildItem){ }'
            var commandAst = assignmentStatementAst.Right.Find(testAst => testAst is CommandAst, searchNestedScriptBlocks: true) as CommandAst;
            // If the RHS contains an InvokeMemberExpressionAst, then we also do not want to warn because this could e.g. be 'if ($f = [System.IO.Path]::GetTempFileName()){ }'
            var invokeMemberExpressionAst = assignmentStatementAst.Right.Find(testAst => testAst is ExpressionAst, searchNestedScriptBlocks: true) as InvokeMemberExpressionAst;
            var doNotWarnBecauseImplicitClangStyleSuppressionWasUsed = ClangSuppresion.ScriptExtendIsWrappedInParenthesis(pipelineBaseAst.Extent);

            if (commandAst == null && invokeMemberExpressionAst == null && !doNotWarnBecauseImplicitClangStyleSuppressionWasUsed)
            {
                return(new DiagnosticRecord(
                           Strings.PossibleIncorrectUsageOfAssignmentOperatorError, assignmentStatementAst.ErrorPosition,
                           GetName(), DiagnosticSeverity.Information, fileName));
            }

            return(null);
        }
Ejemplo n.º 20
0
    public System.Object VisitSwitchStatement(System.Management.Automation.Language.SwitchStatementAst switchStatementAst)
    {
        IScriptExtent mappedExtent = MapExtent(switchStatementAst.Extent);

        PipelineBaseAst mappedCondition = _VisitPipelineBase(switchStatementAst.Condition);
        LinkedList <Tuple <ExpressionAst, StatementBlockAst> > mappedClauses = new LinkedList <Tuple <ExpressionAst, StatementBlockAst> >();

        foreach (Tuple <ExpressionAst, StatementBlockAst> c in switchStatementAst.Clauses)
        {
            mappedClauses.AddLast(
                new Tuple <ExpressionAst, StatementBlockAst>(
                    _VisitExpression(c.Item1),
                    (StatementBlockAst)VisitStatementBlock(c.Item2)
                    )
                );
        }
        StatementBlockAst mappedDefault = switchStatementAst.Default == null ? null : (StatementBlockAst)VisitStatementBlock(switchStatementAst.Default);

        return(new SwitchStatementAst(mappedExtent, switchStatementAst.Label, mappedCondition, switchStatementAst.Flags, mappedClauses, mappedDefault));
    }
Ejemplo n.º 21
0
 public override AstVisitAction VisitReturnStatement(ReturnStatementAst returnStatementAst)
 {
     _wasFound       = true;
     _returnPipeline = returnStatementAst.Pipeline;
     return(AstVisitAction.StopVisit);
 }
 public ParenExpressionAst Paren(PipelineBaseAst pipeline)
 {
     return(new ParenExpressionAst(
                _currentExtent,
                pipeline));
 }
 public static ThrowStatementAst Update(this ThrowStatementAst ast, PipelineBaseAst pipeline = null)
 {
     return(new ThrowStatementAst(ast.Extent, pipeline?.Clone() ?? ast.Pipeline?.Clone()));
 }
 public static ParenExpressionAst Update(this ParenExpressionAst ast, PipelineBaseAst pipeline = null)
 {
     return(new ParenExpressionAst(ast.Extent, pipeline?.Clone() ?? ast.Pipeline?.Clone()));
 }
Ejemplo n.º 25
0
 public static ParenExpressionAst Paren(PipelineBaseAst pipeline) => Current.Paren(pipeline);
Ejemplo n.º 26
0
 public static IfStatementAst If(PipelineBaseAst condition, params StatementAst[] statements)
 => Current.If(condition, statements);
Ejemplo n.º 27
0
 public static IfStatementAst If(
     PipelineBaseAst condition,
     StatementBlockAst ifTrueBody,
     StatementBlockAst ifFalseBody)
 => Current.If(condition, ifTrueBody, ifFalseBody);