@@ -908,9 +908,9 @@ private void BuildScriptPathList(
908
908
bool searchRecursively ,
909
909
IList < string > scriptFilePaths )
910
910
{
911
- const string ps1Suffix = "ps1" ;
912
- const string psm1Suffix = "psm1" ;
913
- const string psd1Suffix = "psd1" ;
911
+ const string ps1Suffix = ". ps1" ;
912
+ const string psm1Suffix = ". psm1" ;
913
+ const string psd1Suffix = ". psd1" ;
914
914
915
915
if ( Directory . Exists ( path ) )
916
916
{
@@ -935,9 +935,14 @@ private void BuildScriptPathList(
935
935
}
936
936
else if ( File . Exists ( path ) )
937
937
{
938
- if ( ( path . Length > ps1Suffix . Length && path . Substring ( path . Length - ps1Suffix . Length ) . Equals ( ps1Suffix , StringComparison . OrdinalIgnoreCase ) ) ||
939
- ( path . Length > psm1Suffix . Length && path . Substring ( path . Length - psm1Suffix . Length ) . Equals ( psm1Suffix , StringComparison . OrdinalIgnoreCase ) ) ||
940
- ( path . Length > psd1Suffix . Length && path . Substring ( path . Length - psd1Suffix . Length ) . Equals ( psd1Suffix , StringComparison . OrdinalIgnoreCase ) ) )
938
+ String fileName = Path . GetFileName ( path ) ;
939
+ if ( ( fileName . Length > ps1Suffix . Length && String . Equals ( Path . GetExtension ( path ) , ps1Suffix , StringComparison . OrdinalIgnoreCase ) ) ||
940
+ ( fileName . Length > psm1Suffix . Length && String . Equals ( Path . GetExtension ( path ) , psm1Suffix , StringComparison . OrdinalIgnoreCase ) ) ||
941
+ ( fileName . Length > psd1Suffix . Length && String . Equals ( Path . GetExtension ( path ) , psd1Suffix , StringComparison . OrdinalIgnoreCase ) ) )
942
+ {
943
+ scriptFilePaths . Add ( path ) ;
944
+ }
945
+ else if ( Helper . Instance . IsHelpFile ( path ) )
941
946
{
942
947
scriptFilePaths . Add ( path ) ;
943
948
}
@@ -964,7 +969,28 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
964
969
//Parse the file
965
970
if ( File . Exists ( filePath ) )
966
971
{
967
- scriptAst = Parser . ParseFile ( filePath , out scriptTokens , out errors ) ;
972
+ // processing for non help script
973
+ if ( ! ( Path . GetFileName ( filePath ) . StartsWith ( "about_" ) && Path . GetFileName ( filePath ) . EndsWith ( ".help.txt" ) ) )
974
+ {
975
+ scriptAst = Parser . ParseFile ( filePath , out scriptTokens , out errors ) ;
976
+
977
+ if ( errors != null && errors . Length > 0 )
978
+ {
979
+ foreach ( ParseError error in errors )
980
+ {
981
+ string parseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParserErrorFormat , error . Extent . File , error . Message . TrimEnd ( '.' ) , error . Extent . StartLineNumber , error . Extent . StartColumnNumber ) ;
982
+ this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( parseErrorMessage ) , parseErrorMessage , ErrorCategory . ParserError , error . ErrorId ) ) ;
983
+ }
984
+ }
985
+
986
+ if ( errors . Length > 10 )
987
+ {
988
+ string manyParseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParserErrorMessage , System . IO . Path . GetFileName ( filePath ) ) ;
989
+ this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( manyParseErrorMessage ) , manyParseErrorMessage , ErrorCategory . ParserError , filePath ) ) ;
990
+
991
+ return new List < DiagnosticRecord > ( ) ;
992
+ }
993
+ }
968
994
}
969
995
else
970
996
{
@@ -975,23 +1001,6 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
975
1001
return null ;
976
1002
}
977
1003
978
- if ( errors != null && errors . Length > 0 )
979
- {
980
- foreach ( ParseError error in errors )
981
- {
982
- string parseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParserErrorFormat , error . Extent . File , error . Message . TrimEnd ( '.' ) , error . Extent . StartLineNumber , error . Extent . StartColumnNumber ) ;
983
- this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( parseErrorMessage ) , parseErrorMessage , ErrorCategory . ParserError , error . ErrorId ) ) ;
984
- }
985
- }
986
-
987
- if ( errors . Length > 10 )
988
- {
989
- string manyParseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParserErrorMessage , System . IO . Path . GetFileName ( filePath ) ) ;
990
- this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( manyParseErrorMessage ) , manyParseErrorMessage , ErrorCategory . ParserError , filePath ) ) ;
991
-
992
- return new List < DiagnosticRecord > ( ) ;
993
- }
994
-
995
1004
return this . AnalyzeSyntaxTree ( scriptAst , scriptTokens , filePath ) ;
996
1005
}
997
1006
@@ -1007,36 +1016,41 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1007
1016
Token [ ] scriptTokens ,
1008
1017
string filePath )
1009
1018
{
1010
- Dictionary < string , List < RuleSuppression > > ruleSuppressions ;
1019
+ Dictionary < string , List < RuleSuppression > > ruleSuppressions = new Dictionary < string , List < RuleSuppression > > ( ) ;
1011
1020
ConcurrentBag < DiagnosticRecord > diagnostics = new ConcurrentBag < DiagnosticRecord > ( ) ;
1012
1021
ConcurrentBag < SuppressedRecord > suppressed = new ConcurrentBag < SuppressedRecord > ( ) ;
1013
1022
BlockingCollection < List < object > > verboseOrErrors = new BlockingCollection < List < object > > ( ) ;
1014
1023
1015
1024
// Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
1016
1025
List < KeyValuePair < CommandInfo , IScriptExtent > > cmdInfoTable = new List < KeyValuePair < CommandInfo , IScriptExtent > > ( ) ;
1017
1026
1018
- ruleSuppressions = Helper . Instance . GetRuleSuppression ( scriptAst ) ;
1027
+ bool helpFile = ( scriptAst == null ) && Helper . Instance . IsHelpFile ( filePath ) ;
1019
1028
1020
- foreach ( List < RuleSuppression > ruleSuppressionsList in ruleSuppressions . Values )
1029
+ if ( ! helpFile )
1021
1030
{
1022
- foreach ( RuleSuppression ruleSuppression in ruleSuppressionsList )
1031
+ ruleSuppressions = Helper . Instance . GetRuleSuppression ( scriptAst ) ;
1032
+
1033
+ foreach ( List < RuleSuppression > ruleSuppressionsList in ruleSuppressions . Values )
1023
1034
{
1024
- if ( ! String . IsNullOrWhiteSpace ( ruleSuppression . Error ) )
1035
+ foreach ( RuleSuppression ruleSuppression in ruleSuppressionsList )
1025
1036
{
1026
- this . outputWriter . WriteError ( new ErrorRecord ( new ArgumentException ( ruleSuppression . Error ) , ruleSuppression . Error , ErrorCategory . InvalidArgument , ruleSuppression ) ) ;
1037
+ if ( ! String . IsNullOrWhiteSpace ( ruleSuppression . Error ) )
1038
+ {
1039
+ this . outputWriter . WriteError ( new ErrorRecord ( new ArgumentException ( ruleSuppression . Error ) , ruleSuppression . Error , ErrorCategory . InvalidArgument , ruleSuppression ) ) ;
1040
+ }
1027
1041
}
1028
1042
}
1029
- }
1030
1043
1031
- #region Run VariableAnalysis
1032
- try
1033
- {
1034
- Helper . Instance . InitializeVariableAnalysis ( scriptAst ) ;
1035
- }
1036
- catch { }
1037
- #endregion
1044
+ #region Run VariableAnalysis
1045
+ try
1046
+ {
1047
+ Helper . Instance . InitializeVariableAnalysis ( scriptAst ) ;
1048
+ }
1049
+ catch { }
1050
+ #endregion
1038
1051
1039
- Helper . Instance . Tokens = scriptTokens ;
1052
+ Helper . Instance . Tokens = scriptTokens ;
1053
+ }
1040
1054
1041
1055
#region Run ScriptRules
1042
1056
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
@@ -1067,6 +1081,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1067
1081
}
1068
1082
}
1069
1083
1084
+ bool helpRule = String . Equals ( scriptRule . GetName ( ) , "PSUseUTF8EncodingForHelpFile" , StringComparison . OrdinalIgnoreCase ) ;
1085
+
1070
1086
if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
1071
1087
{
1072
1088
List < object > result = new List < object > ( ) ;
@@ -1077,14 +1093,25 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1077
1093
// We want the Engine to continue functioning even if one or more Rules throws an exception
1078
1094
try
1079
1095
{
1080
- var records = Helper . Instance . SuppressRule ( scriptRule . GetName ( ) , ruleSuppressions , scriptRule . AnalyzeScript ( scriptAst , scriptAst . Extent . File ) . ToList ( ) ) ;
1081
- foreach ( var record in records . Item2 )
1096
+ if ( helpRule && helpFile )
1082
1097
{
1083
- diagnostics . Add ( record ) ;
1098
+ var records = scriptRule . AnalyzeScript ( scriptAst , filePath ) ;
1099
+ foreach ( var record in records )
1100
+ {
1101
+ diagnostics . Add ( record ) ;
1102
+ }
1084
1103
}
1085
- foreach ( var suppressedRec in records . Item1 )
1104
+ else if ( ! helpRule && ! helpFile )
1086
1105
{
1087
- suppressed . Add ( suppressedRec ) ;
1106
+ var records = Helper . Instance . SuppressRule ( scriptRule . GetName ( ) , ruleSuppressions , scriptRule . AnalyzeScript ( scriptAst , scriptAst . Extent . File ) . ToList ( ) ) ;
1107
+ foreach ( var record in records . Item2 )
1108
+ {
1109
+ diagnostics . Add ( record ) ;
1110
+ }
1111
+ foreach ( var suppressedRec in records . Item1 )
1112
+ {
1113
+ suppressed . Add ( suppressedRec ) ;
1114
+ }
1088
1115
}
1089
1116
}
1090
1117
catch ( Exception scriptRuleException )
@@ -1122,7 +1149,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1122
1149
1123
1150
#region Run Token Rules
1124
1151
1125
- if ( this . TokenRules != null )
1152
+ if ( this . TokenRules != null && ! helpFile )
1126
1153
{
1127
1154
foreach ( ITokenRule tokenRule in this . TokenRules )
1128
1155
{
@@ -1173,7 +1200,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1173
1200
#endregion
1174
1201
1175
1202
#region DSC Resource Rules
1176
- if ( this . DSCResourceRules != null )
1203
+ if ( this . DSCResourceRules != null && ! helpFile )
1177
1204
{
1178
1205
// Invoke AnalyzeDSCClass only if the ast is a class based resource
1179
1206
if ( Helper . Instance . IsDscResourceClassBased ( scriptAst ) )
@@ -1282,7 +1309,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1282
1309
1283
1310
#region Run External Rules
1284
1311
1285
- if ( this . ExternalRules != null )
1312
+ if ( this . ExternalRules != null && ! helpFile )
1286
1313
{
1287
1314
List < ExternalRule > exRules = new List < ExternalRule > ( ) ;
1288
1315
0 commit comments