Skip to content

Commit 698f198

Browse files
committed
First commit for PSv3 Support
1 parent 07cf301 commit 698f198

17 files changed

+484
-29
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
4343
/// </summary>
4444
[Parameter(Position = 0,
4545
ParameterSetName = "File",
46-
Mandatory = true)]
46+
Mandatory = true,
47+
ValueFromPipeline = true,
48+
ValueFromPipelineByPropertyName = true)]
4749
[ValidateNotNull]
4850
[Alias("PSPath")]
4951
public string Path
@@ -58,7 +60,9 @@ public string Path
5860
/// </summary>
5961
[Parameter(Position = 0,
6062
ParameterSetName = "ScriptDefinition",
61-
Mandatory = true)]
63+
Mandatory = true,
64+
ValueFromPipeline = true,
65+
ValueFromPipelineByPropertyName = true)]
6266
[ValidateNotNull]
6367
public string ScriptDefinition
6468
{

Engine/Generic/IDSCResourceRule.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ public interface IDSCResourceRule : IRule
2828
/// <returns>The results of the analysis</returns>
2929
IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName);
3030

31+
#if !PSV3
32+
3133
/// <summary>
3234
/// Analyze dsc classes (if any) in the file
3335
/// </summary>
3436
/// <param name="ast"></param>
3537
/// <param name="fileName"></param>
3638
/// <returns></returns>
3739
IEnumerable<DiagnosticRecord> AnalyzeDSCClass(Ast ast, string fileName);
38-
40+
41+
#endif
42+
3943
}
4044
}

Engine/Generic/RuleSuppression.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,14 @@ public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> at
367367
targetAsts = scopeAst.FindAll(item => item is FunctionDefinitionAst && reg.IsMatch((item as FunctionDefinitionAst).Name), true);
368368
goto default;
369369

370+
#if !PSV3
371+
370372
case "class":
371373
targetAsts = scopeAst.FindAll(item => item is TypeDefinitionAst && reg.IsMatch((item as TypeDefinitionAst).Name), true);
372374
goto default;
373375

376+
#endif
377+
374378
default:
375379
break;
376380
}

Engine/Helper.cs

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,16 @@ internal VariableAnalysis InitializeVariableAnalysisHelper(Ast ast, VariableAnal
619619
/// <param name="classes"></param>
620620
/// <param name="scriptAst"></param>
621621
/// <returns></returns>
622+
623+
#if PSV3
624+
625+
public string GetTypeFromReturnStatementAst(Ast funcAst, ReturnStatementAst ret)
626+
627+
#else
628+
622629
public string GetTypeFromReturnStatementAst(Ast funcAst, ReturnStatementAst ret, IEnumerable<TypeDefinitionAst> classes)
630+
631+
#endif
623632
{
624633
if (ret == null || funcAst == null)
625634
{
@@ -650,7 +659,15 @@ public string GetTypeFromReturnStatementAst(Ast funcAst, ReturnStatementAst ret,
650659
}
651660
else if (cmAst.Expression is MemberExpressionAst)
652661
{
662+
#if PSV3
663+
664+
result = GetTypeFromMemberExpressionAst(cmAst.Expression as MemberExpressionAst, funcAst);
665+
666+
#else
667+
653668
result = GetTypeFromMemberExpressionAst(cmAst.Expression as MemberExpressionAst, funcAst, classes);
669+
670+
#endif
654671
}
655672
}
656673
}
@@ -673,30 +690,56 @@ public string GetTypeFromReturnStatementAst(Ast funcAst, ReturnStatementAst ret,
673690
/// <param name="scopeAst"></param>
674691
/// <param name="classes"></param>
675692
/// <returns></returns>
693+
694+
#if PSV3
695+
696+
public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast scopeAst)
697+
698+
#else
699+
676700
public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast scopeAst, IEnumerable<TypeDefinitionAst> classes)
701+
702+
#endif
677703
{
678704
if (memberAst == null)
679705
{
680706
return String.Empty;
681707
}
682708

683709
VariableAnalysisDetails details = null;
710+
711+
#if !PSV3
712+
684713
TypeDefinitionAst psClass = null;
685714

715+
#endif
716+
686717
if (memberAst.Expression is VariableExpressionAst && VariableAnalysisDictionary.ContainsKey(scopeAst))
687718
{
688719
VariableAnalysis VarTypeAnalysis = VariableAnalysisDictionary[scopeAst];
689720
// Get the analysis detail for the variable
690721
details = VarTypeAnalysis.GetVariableAnalysis(memberAst.Expression as VariableExpressionAst);
691722

723+
#if !PSV3
724+
692725
if (details != null && classes != null)
693726
{
694727
// Get the class that corresponds to the name of the type (if possible)
695728
psClass = classes.FirstOrDefault(item => String.Equals(item.Name, details.Type.FullName, StringComparison.OrdinalIgnoreCase));
696729
}
730+
731+
#endif
697732
}
698733

699-
return GetTypeFromMemberExpressionAstHelper(memberAst, psClass, details);
734+
#if PSV3
735+
736+
return GetTypeFromMemberExpressionAstHelper(memberAst, details);
737+
738+
#else
739+
740+
return GetTypeFromMemberExpressionAstHelper(memberAst, psClass, details);
741+
742+
#endif
700743
}
701744

702745
/// <summary>
@@ -707,17 +750,30 @@ public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast
707750
/// <param name="psClass"></param>
708751
/// <param name="analysisDetails"></param>
709752
/// <returns></returns>
753+
754+
#if PSV3
755+
756+
internal string GetTypeFromMemberExpressionAstHelper(MemberExpressionAst memberAst, VariableAnalysisDetails analysisDetails)
757+
758+
#else
759+
710760
internal string GetTypeFromMemberExpressionAstHelper(MemberExpressionAst memberAst, TypeDefinitionAst psClass, VariableAnalysisDetails analysisDetails)
761+
762+
#endif
711763
{
712764
//Try to get the type without using psClass first
713765
Type result = AssignmentTarget.GetTypeFromMemberExpressionAst(memberAst);
714766

767+
#if !PSV3
768+
715769
//If we can't get the type, then it may be that the type of the object being invoked on is a powershell class
716770
if (result == null && psClass != null && analysisDetails != null)
717771
{
718772
result = AssignmentTarget.GetTypeFromMemberExpressionAst(memberAst, analysisDetails, psClass);
719773
}
720774

775+
#endif
776+
721777
if (result != null)
722778
{
723779
return result.FullName;
@@ -816,6 +872,8 @@ public Dictionary<string, List<RuleSuppression>> GetRuleSuppression(Ast ast)
816872
ruleSuppressionList.AddRange(GetSuppressionsFunction(funcAst));
817873
}
818874

875+
#if !PSV3
876+
819877
// Get rule suppression from classes
820878
IEnumerable<TypeDefinitionAst> typeAsts = ast.FindAll(item => item is TypeDefinitionAst, true).Cast<TypeDefinitionAst>();
821879

@@ -824,6 +882,8 @@ public Dictionary<string, List<RuleSuppression>> GetRuleSuppression(Ast ast)
824882
ruleSuppressionList.AddRange(GetSuppressionsClass(typeAst));
825883
}
826884

885+
#endif
886+
827887
ruleSuppressionList.Sort((item, item2) => item.StartOffset.CompareTo(item2.StartOffset));
828888

829889
foreach (RuleSuppression ruleSuppression in ruleSuppressionList)
@@ -1089,7 +1149,15 @@ public object VisitScriptBlock(ScriptBlockAst scriptBlockAst)
10891149

10901150
// We already run variable analysis if the parent is a function so skip these.
10911151
// Otherwise, we have to do variable analysis using the outer scope variables.
1152+
#if PSV3
1153+
1154+
if (!(scriptBlockAst.Parent is FunctionDefinitionAst))
1155+
1156+
#else
1157+
10921158
if (!(scriptBlockAst.Parent is FunctionDefinitionAst) && !(scriptBlockAst.Parent is FunctionMemberAst))
1159+
1160+
#endif
10931161
{
10941162
OuterAnalysis = Helper.Instance.InitializeVariableAnalysisHelper(scriptBlockAst, OuterAnalysis);
10951163
}
@@ -1117,7 +1185,15 @@ public object VisitScriptBlock(ScriptBlockAst scriptBlockAst)
11171185
VariableAnalysis innerAnalysis = OuterAnalysis;
11181186
OuterAnalysis = previousOuter;
11191187

1188+
#if PSV3
1189+
1190+
if (!(scriptBlockAst.Parent is FunctionDefinitionAst))
1191+
1192+
#else
1193+
11201194
if (!(scriptBlockAst.Parent is FunctionDefinitionAst) && !(scriptBlockAst.Parent is FunctionMemberAst))
1195+
1196+
#endif
11211197
{
11221198
// Update the variable analysis of the outer script block
11231199
VariableAnalysis.UpdateOuterAnalysis(OuterAnalysis, innerAnalysis);
@@ -1138,6 +1214,12 @@ private object VisitStatementHelper(StatementAst statementAst)
11381214
return null;
11391215
}
11401216

1217+
#if PSV3
1218+
1219+
statementAst.Visit(this);
1220+
1221+
#else
1222+
11411223
TypeDefinitionAst typeAst = statementAst as TypeDefinitionAst;
11421224

11431225
if (typeAst == null)
@@ -1164,9 +1246,13 @@ private object VisitStatementHelper(StatementAst statementAst)
11641246
}
11651247
}
11661248

1249+
#endif
1250+
11671251
return null;
11681252
}
11691253

1254+
#if !PSV3
1255+
11701256
/// <summary>
11711257
/// Do nothing
11721258
/// </summary>
@@ -1177,6 +1263,8 @@ public object VisitUsingStatement(UsingStatementAst usingStatement)
11771263
return null;
11781264
}
11791265

1266+
#endif
1267+
11801268
/// <summary>
11811269
/// Do nothing
11821270
/// </summary>
@@ -1842,8 +1930,12 @@ public class FindPipelineOutput : ICustomAstVisitor
18421930
{
18431931
List<Tuple<string, StatementAst>> outputTypes;
18441932

1933+
#if !PSV3
1934+
18451935
IEnumerable<TypeDefinitionAst> classes;
18461936

1937+
#endif
1938+
18471939
FunctionDefinitionAst myFunction;
18481940
/// <summary>
18491941
/// These binary operators will always return boolean value
@@ -1879,10 +1971,25 @@ static FindPipelineOutput()
18791971
/// Find the pipeline output
18801972
/// </summary>
18811973
/// <param name="ast"></param>
1974+
1975+
#if PSV3
1976+
1977+
public FindPipelineOutput(FunctionDefinitionAst ast)
1978+
1979+
#else
1980+
18821981
public FindPipelineOutput(FunctionDefinitionAst ast, IEnumerable<TypeDefinitionAst> classes)
1982+
1983+
#endif
18831984
{
18841985
outputTypes = new List<Tuple<string, StatementAst>>();
1986+
1987+
#if !PSV3
1988+
18851989
this.classes = classes;
1990+
1991+
#endif
1992+
18861993
myFunction = ast;
18871994

18881995
if (myFunction != null)
@@ -1895,11 +2002,22 @@ public FindPipelineOutput(FunctionDefinitionAst ast, IEnumerable<TypeDefinitionA
18952002
/// Get list of outputTypes from functiondefinitionast funcast
18962003
/// </summary>
18972004
/// <returns></returns>
2005+
2006+
#if PSV3
2007+
2008+
public static List<Tuple<string, StatementAst>> OutputTypes(FunctionDefinitionAst funcAst)
2009+
{
2010+
return (new FindPipelineOutput(funcAst)).outputTypes;
2011+
}
2012+
2013+
#else
18982014
public static List<Tuple<string, StatementAst>> OutputTypes(FunctionDefinitionAst funcAst, IEnumerable<TypeDefinitionAst> classes)
18992015
{
19002016
return (new FindPipelineOutput(funcAst, classes)).outputTypes;
19012017
}
19022018

2019+
#endif
2020+
19032021
/// <summary>
19042022
/// Ignore assignment statement
19052023
/// </summary>
@@ -2351,7 +2469,15 @@ public object VisitCommandExpression(CommandExpressionAst commandAst)
23512469
/// <returns></returns>
23522470
public object VisitReturnStatement(ReturnStatementAst returnStatementAst)
23532471
{
2472+
#if PSV3
2473+
2474+
return Helper.Instance.GetTypeFromReturnStatementAst(myFunction, returnStatementAst);
2475+
2476+
#else
2477+
23542478
return Helper.Instance.GetTypeFromReturnStatementAst(myFunction, returnStatementAst, classes);
2479+
2480+
#endif
23552481
}
23562482

23572483
/// <summary>
@@ -2361,7 +2487,15 @@ public object VisitReturnStatement(ReturnStatementAst returnStatementAst)
23612487
/// <returns></returns>
23622488
public object VisitMemberExpression(MemberExpressionAst memAst)
23632489
{
2490+
#if PSV3
2491+
2492+
return Helper.Instance.GetTypeFromMemberExpressionAst(memAst, myFunction);
2493+
2494+
#else
2495+
23642496
return Helper.Instance.GetTypeFromMemberExpressionAst(memAst, myFunction, classes);
2497+
2498+
#endif
23652499
}
23662500

23672501
/// <summary>
@@ -2371,7 +2505,15 @@ public object VisitMemberExpression(MemberExpressionAst memAst)
23712505
/// <returns></returns>
23722506
public object VisitInvokeMemberExpression(InvokeMemberExpressionAst invokeAst)
23732507
{
2508+
#if PSV3
2509+
2510+
return Helper.Instance.GetTypeFromMemberExpressionAst(invokeAst, myFunction);
2511+
2512+
#else
2513+
23742514
return Helper.Instance.GetTypeFromMemberExpressionAst(invokeAst, myFunction, classes);
2515+
2516+
#endif
23752517
}
23762518

23772519
/// <summary>

0 commit comments

Comments
 (0)