@@ -883,7 +883,7 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PathIn
883
883
}
884
884
885
885
#endregion
886
-
886
+
887
887
888
888
/// <summary>
889
889
/// Analyzes a script file or a directory containing script files.
@@ -924,6 +924,49 @@ public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursi
924
924
}
925
925
}
926
926
927
+ /// <summary>
928
+ /// Analyzes a script definition in the form of a string input
929
+ /// </summary>
930
+ /// <param name="scriptDefinition">The script to be analyzed</param>
931
+ /// <returns></returns>
932
+ public IEnumerable < DiagnosticRecord > AnalyzeScriptDefinition ( string scriptDefinition )
933
+ {
934
+ ScriptBlockAst scriptAst = null ;
935
+ Token [ ] scriptTokens = null ;
936
+ ParseError [ ] errors = null ;
937
+
938
+ this . outputWriter . WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseScriptDefinitionMessage ) ) ;
939
+
940
+ try
941
+ {
942
+ scriptAst = Parser . ParseInput ( scriptDefinition , out scriptTokens , out errors ) ;
943
+ }
944
+ catch ( Exception e )
945
+ {
946
+ this . outputWriter . WriteWarning ( e . ToString ( ) ) ;
947
+ return null ;
948
+ }
949
+
950
+ if ( errors != null && errors . Length > 0 )
951
+ {
952
+ foreach ( ParseError error in errors )
953
+ {
954
+ string parseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParseErrorFormatForScriptDefinition , error . Message . TrimEnd ( '.' ) , error . Extent . StartLineNumber , error . Extent . StartColumnNumber ) ;
955
+ this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( parseErrorMessage ) , parseErrorMessage , ErrorCategory . ParserError , error . ErrorId ) ) ;
956
+ }
957
+ }
958
+
959
+ if ( errors != null && errors . Length > 10 )
960
+ {
961
+ string manyParseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParserErrorMessageForScriptDefinition ) ;
962
+ this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( manyParseErrorMessage ) , manyParseErrorMessage , ErrorCategory . ParserError , scriptDefinition ) ) ;
963
+
964
+ return new List < DiagnosticRecord > ( ) ;
965
+ }
966
+
967
+ return this . AnalyzeSyntaxTree ( scriptAst , scriptTokens , String . Empty ) ;
968
+ }
969
+
927
970
private void BuildScriptPathList (
928
971
string path ,
929
972
bool searchRecursively ,
@@ -1038,7 +1081,9 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
1038
1081
/// </summary>
1039
1082
/// <param name="scriptAst">The ScriptBlockAst from the parsed script.</param>
1040
1083
/// <param name="scriptTokens">The tokens found in the script.</param>
1041
- /// <param name="filePath">The path to the file that was parsed.</param>
1084
+ /// <param name="filePath">The path to the file that was parsed.
1085
+ /// If AnalyzeSyntaxTree is called from an ast that we get from ParseInput, then this field will be String.Empty
1086
+ /// </param>
1042
1087
/// <returns>An enumeration of DiagnosticRecords that were found by rules.</returns>
1043
1088
public IEnumerable < DiagnosticRecord > AnalyzeSyntaxTree (
1044
1089
ScriptBlockAst scriptAst ,
@@ -1052,8 +1097,12 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1052
1097
1053
1098
// Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
1054
1099
List < KeyValuePair < CommandInfo , IScriptExtent > > cmdInfoTable = new List < KeyValuePair < CommandInfo , IScriptExtent > > ( ) ;
1100
+ bool filePathIsNullOrWhiteSpace = String . IsNullOrWhiteSpace ( filePath ) ;
1101
+ filePath = filePathIsNullOrWhiteSpace ? String . Empty : filePath ;
1055
1102
1056
- bool helpFile = ( scriptAst == null ) && Helper . Instance . IsHelpFile ( filePath ) ;
1103
+ // check whether the script we are analyzing is a help file or not.
1104
+ // this step is not applicable for scriptdefinition, whose filepath is null
1105
+ bool helpFile = ( scriptAst == null ) && ( ! filePathIsNullOrWhiteSpace ) && Helper . Instance . IsHelpFile ( filePath ) ;
1057
1106
1058
1107
if ( ! helpFile )
1059
1108
{
@@ -1083,7 +1132,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1083
1132
1084
1133
#region Run ScriptRules
1085
1134
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
1086
- string fileName = System . IO . Path . GetFileName ( filePath ) ;
1135
+ string fileName = filePathIsNullOrWhiteSpace ? String . Empty : System . IO . Path . GetFileName ( filePath ) ;
1087
1136
1088
1137
if ( this . ScriptRules != null )
1089
1138
{
@@ -1285,7 +1334,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1285
1334
}
1286
1335
1287
1336
// Check if the supplied artifact is indeed part of the DSC resource
1288
- if ( Helper . Instance . IsDscResourceModule ( filePath ) )
1337
+ if ( ! filePathIsNullOrWhiteSpace && Helper . Instance . IsDscResourceModule ( filePath ) )
1289
1338
{
1290
1339
// Run all DSC Rules
1291
1340
foreach ( IDSCResourceRule dscResourceRule in this . DSCResourceRules )
0 commit comments