Skip to content

Commit 0374851

Browse files
committed
Change check for help file to use static regex
Update documentation to include new behavior for ParseErrors and fix up logic to be a bit cleaner when emiting ParseErrors
1 parent d34b8cb commit 0374851

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

Engine/ScriptAnalyzer.cs

+9-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public sealed class ScriptAnalyzer
3131

3232
private IOutputWriter outputWriter;
3333
private Dictionary<string, object> settings;
34+
private readonly Regex s_aboutHelpRegex = new Regex("^about_.*help\\.txt$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
3435
#if !CORECLR
3536
private CompositionContainer container;
3637
#endif // !CORECLR
@@ -1526,20 +1527,19 @@ public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefini
15261527

15271528
var relevantParseErrors = RemoveTypeNotFoundParseErrors(errors, out List<DiagnosticRecord> diagnosticRecords);
15281529

1529-
int emitParseErrors = severity == null ? 1 : severity.Count(item => item == "ParseError");
15301530
// Add parse errors first if requested!
1531-
if ( relevantParseErrors != null && emitParseErrors == 1)
1531+
if ( relevantParseErrors != null && (severity == null || severity.Contains("ParseError", StringComparer.OrdinalIgnoreCase)))
15321532
{
1533-
List<DiagnosticRecord> results = new List<DiagnosticRecord>();
1534-
foreach ( var parseError in relevantParseErrors )
1533+
var results = new List<DiagnosticRecord>();
1534+
foreach ( ParseError parseError in relevantParseErrors )
15351535
{
15361536
string parseErrorMessage = String.Format(CultureInfo.CurrentCulture, Strings.ParseErrorFormatForScriptDefinition, parseError.Message.TrimEnd('.'), parseError.Extent.StartLineNumber, parseError.Extent.StartColumnNumber);
15371537
results.Add(new DiagnosticRecord(
15381538
parseError.Message,
15391539
parseError.Extent,
15401540
parseError.ErrorId.ToString(),
15411541
DiagnosticSeverity.ParseError,
1542-
"" // no script file
1542+
String.Empty // no script file
15431543
)
15441544
);
15451545
}
@@ -1855,7 +1855,7 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
18551855

18561856
// short-circuited processing for a help file
18571857
// no parsing can really be done, but there are other rules to run (specifically encoding).
1858-
if ( Regex.Matches(Path.GetFileName(filePath), @"^about_.*help.txt$", RegexOptions.IgnoreCase).Count != 0)
1858+
if ( s_aboutHelpRegex.IsMatch(Path.GetFileName(filePath)) )
18591859
{
18601860
return diagnosticRecords.Concat(this.AnalyzeSyntaxTree(scriptAst, scriptTokens, filePath));
18611861
}
@@ -1877,14 +1877,13 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
18771877
scriptAst = Parser.ParseFile(filePath, out scriptTokens, out errors);
18781878
}
18791879
#endif //!PSV3
1880-
var relevantParseErrors = RemoveTypeNotFoundParseErrors(errors, out diagnosticRecords);
1880+
IEnumerable<ParseError> relevantParseErrors = RemoveTypeNotFoundParseErrors(errors, out diagnosticRecords);
18811881

18821882
// First, add all parse errors if they've been requested
1883-
int emitParseErrors = severity == null ? 1 : severity.Count(item => item == "ParseError");
1884-
if ( relevantParseErrors != null && emitParseErrors == 1 )
1883+
if ( relevantParseErrors != null && (severity == null || severity.Contains("ParseError", StringComparer.OrdinalIgnoreCase)))
18851884
{
18861885
List<DiagnosticRecord> results = new List<DiagnosticRecord>();
1887-
foreach ( var parseError in relevantParseErrors )
1886+
foreach ( ParseError parseError in relevantParseErrors )
18881887
{
18891888
string parseErrorMessage = String.Format(CultureInfo.CurrentCulture, Strings.ParseErrorFormatForScriptDefinition, parseError.Message.TrimEnd('.'), parseError.Extent.StartLineNumber, parseError.Extent.StartColumnNumber);
18901889
results.Add(new DiagnosticRecord(

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,43 @@ Get-TestFailures
187187

188188
[Back to ToC](#table-of-contents)
189189

190+
Parser Errors
191+
=============
192+
193+
In prior versions of ScriptAnalyer, errors found during parsing were reported as errors and diagnostic records were not created.
194+
ScriptAnalyzer now emits parser errors as diagnostic records in the output stream with other diagnostic records.
195+
196+
```powershell
197+
PS> Invoke-ScriptAnalyzer -ScriptDefinition '"b" = "b"; function eliminate-file () { }'
198+
199+
RuleName Severity ScriptName Line Message
200+
-------- -------- ---------- ---- -------
201+
InvalidLeftHandSide ParseError 1 The assignment expression is not
202+
valid. The input to an
203+
assignment operator must be an
204+
object that is able to accept
205+
assignments, such as a variable
206+
or a property.
207+
PSUseApprovedVerbs Warning 1 The cmdlet 'eliminate-file' uses an
208+
unapproved verb.
209+
```
210+
211+
The RuleName is set to the `ErrorId` of the parser error.
212+
213+
If ParseErrors would like to be suppressed, do not include it as a value in the `-Severity` parameter.
214+
215+
```powershell
216+
PS> Invoke-ScriptAnalyzer -ScriptDefinition '"b" = "b"; function eliminate-file () { }' -Severity Warning
217+
218+
RuleName Severity ScriptName Line Message
219+
-------- -------- ---------- ---- -------
220+
PSUseApprovedVerbs Warning 1 The cmdlet 'eliminate-file' uses an
221+
unapproved verb.
222+
```
223+
224+
225+
226+
190227
Suppressing Rules
191228
=================
192229

@@ -272,6 +309,8 @@ Param()
272309

273310
**Note**: Rule suppression is currently supported only for built-in rules.
274311

312+
**Note**: Parser Errors cannot be suppressed via the `SuppressMessageAttribute`
313+
275314
[Back to ToC](#table-of-contents)
276315

277316
Settings Support in ScriptAnalyzer

0 commit comments

Comments
 (0)