@@ -883,7 +883,7 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PathIn
883883 }
884884
885885 #endregion
886-
886+
887887
888888 /// <summary>
889889 /// Analyzes a script file or a directory containing script files.
@@ -924,6 +924,49 @@ public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursi
924924 }
925925 }
926926
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+
927970 private void BuildScriptPathList (
928971 string path ,
929972 bool searchRecursively ,
@@ -1038,7 +1081,9 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
10381081 /// </summary>
10391082 /// <param name="scriptAst">The ScriptBlockAst from the parsed script.</param>
10401083 /// <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>
10421087 /// <returns>An enumeration of DiagnosticRecords that were found by rules.</returns>
10431088 public IEnumerable < DiagnosticRecord > AnalyzeSyntaxTree (
10441089 ScriptBlockAst scriptAst ,
@@ -1052,8 +1097,12 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
10521097
10531098 // Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
10541099 List < KeyValuePair < CommandInfo , IScriptExtent > > cmdInfoTable = new List < KeyValuePair < CommandInfo , IScriptExtent > > ( ) ;
1100+ bool filePathIsNullOrWhiteSpace = String . IsNullOrWhiteSpace ( filePath ) ;
1101+ filePath = filePathIsNullOrWhiteSpace ? String . Empty : filePath ;
10551102
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 ) ;
10571106
10581107 if ( ! helpFile )
10591108 {
@@ -1083,7 +1132,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
10831132
10841133 #region Run ScriptRules
10851134 //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 ) ;
10871136
10881137 if ( this . ScriptRules != null )
10891138 {
@@ -1285,7 +1334,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
12851334 }
12861335
12871336 // 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 ) )
12891338 {
12901339 // Run all DSC Rules
12911340 foreach ( IDSCResourceRule dscResourceRule in this . DSCResourceRules )
0 commit comments