@@ -31,6 +31,7 @@ public sealed class ScriptAnalyzer
31
31
32
32
private IOutputWriter outputWriter ;
33
33
private Dictionary < string , object > settings ;
34
+ private readonly Regex s_aboutHelpRegex = new Regex ( "^about_.*help\\ .txt$" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
34
35
#if ! CORECLR
35
36
private CompositionContainer container ;
36
37
#endif // !CORECLR
@@ -1526,23 +1527,26 @@ public IEnumerable<DiagnosticRecord> AnalyzeScriptDefinition(string scriptDefini
1526
1527
1527
1528
var relevantParseErrors = RemoveTypeNotFoundParseErrors ( errors , out List < DiagnosticRecord > diagnosticRecords ) ;
1528
1529
1529
- if ( relevantParseErrors != null && relevantParseErrors . Count > 0 )
1530
+ // Add parse errors first if requested!
1531
+ if ( relevantParseErrors != null && ( severity == null || severity . Contains ( "ParseError" , StringComparer . OrdinalIgnoreCase ) ) )
1530
1532
{
1531
- foreach ( var parseError in relevantParseErrors )
1533
+ var results = new List < DiagnosticRecord > ( ) ;
1534
+ foreach ( ParseError parseError in relevantParseErrors )
1532
1535
{
1533
1536
string parseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParseErrorFormatForScriptDefinition , parseError . Message . TrimEnd ( '.' ) , parseError . Extent . StartLineNumber , parseError . Extent . StartColumnNumber ) ;
1534
- this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( parseErrorMessage ) , parseErrorMessage , ErrorCategory . ParserError , parseError . ErrorId ) ) ;
1537
+ results . Add ( new DiagnosticRecord (
1538
+ parseError . Message ,
1539
+ parseError . Extent ,
1540
+ parseError . ErrorId . ToString ( ) ,
1541
+ DiagnosticSeverity . ParseError ,
1542
+ String . Empty // no script file
1543
+ )
1544
+ ) ;
1535
1545
}
1546
+ diagnosticRecords . AddRange ( results ) ;
1536
1547
}
1537
1548
1538
- if ( relevantParseErrors != null && relevantParseErrors . Count > 10 )
1539
- {
1540
- string manyParseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParserErrorMessageForScriptDefinition ) ;
1541
- this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( manyParseErrorMessage ) , manyParseErrorMessage , ErrorCategory . ParserError , scriptDefinition ) ) ;
1542
-
1543
- return new List < DiagnosticRecord > ( ) ;
1544
- }
1545
-
1549
+ // now, analyze the script definition
1546
1550
return diagnosticRecords . Concat ( this . AnalyzeSyntaxTree ( scriptAst , scriptTokens , String . Empty ) ) ;
1547
1551
}
1548
1552
@@ -1839,49 +1843,8 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
1839
1843
this . outputWriter . WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseFileMessage , filePath ) ) ;
1840
1844
var diagnosticRecords = new List < DiagnosticRecord > ( ) ;
1841
1845
1842
- //Parse the file
1843
- if ( File . Exists ( filePath ) )
1844
- {
1845
- // processing for non help script
1846
- if ( ! ( Path . GetFileName ( filePath ) . ToLower ( ) . StartsWith ( "about_" ) && Path . GetFileName ( filePath ) . ToLower ( ) . EndsWith ( ".help.txt" ) ) )
1847
- {
1848
- try
1849
- {
1850
- scriptAst = Parser . ParseFile ( filePath , out scriptTokens , out errors ) ;
1851
- }
1852
- catch ( Exception e )
1853
- {
1854
- this . outputWriter . WriteWarning ( e . ToString ( ) ) ;
1855
- return null ;
1856
- }
1857
- #if ! PSV3
1858
- //try parsing again
1859
- if ( TrySaveModules ( errors , scriptAst ) )
1860
- {
1861
- scriptAst = Parser . ParseFile ( filePath , out scriptTokens , out errors ) ;
1862
- }
1863
- #endif //!PSV3
1864
- var relevantParseErrors = RemoveTypeNotFoundParseErrors ( errors , out diagnosticRecords ) ;
1865
-
1866
- //Runspace.DefaultRunspace = oldDefault;
1867
- if ( relevantParseErrors != null && relevantParseErrors . Count > 0 )
1868
- {
1869
- foreach ( var parseError in relevantParseErrors )
1870
- {
1871
- string parseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParserErrorFormat , parseError . Extent . File , parseError . Message . TrimEnd ( '.' ) , parseError . Extent . StartLineNumber , parseError . Extent . StartColumnNumber ) ;
1872
- this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( parseErrorMessage ) , parseErrorMessage , ErrorCategory . ParserError , parseError . ErrorId ) ) ;
1873
- }
1874
- }
1875
-
1876
- if ( relevantParseErrors != null && relevantParseErrors . Count > 10 )
1877
- {
1878
- string manyParseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParserErrorMessage , System . IO . Path . GetFileName ( filePath ) ) ;
1879
- this . outputWriter . WriteError ( new ErrorRecord ( new ParseException ( manyParseErrorMessage ) , manyParseErrorMessage , ErrorCategory . ParserError , filePath ) ) ;
1880
- return new List < DiagnosticRecord > ( ) ;
1881
- }
1882
- }
1883
- }
1884
- else
1846
+ // If the file doesn't exist, return
1847
+ if ( ! File . Exists ( filePath ) )
1885
1848
{
1886
1849
this . outputWriter . ThrowTerminatingError ( new ErrorRecord ( new FileNotFoundException ( ) ,
1887
1850
string . Format ( CultureInfo . CurrentCulture , Strings . InvalidPath , filePath ) ,
@@ -1890,6 +1853,50 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
1890
1853
return null ;
1891
1854
}
1892
1855
1856
+ // short-circuited processing for a help file
1857
+ // no parsing can really be done, but there are other rules to run (specifically encoding).
1858
+ if ( s_aboutHelpRegex . IsMatch ( Path . GetFileName ( filePath ) ) )
1859
+ {
1860
+ return diagnosticRecords . Concat ( this . AnalyzeSyntaxTree ( scriptAst , scriptTokens , filePath ) ) ;
1861
+ }
1862
+
1863
+ // Process script
1864
+ try
1865
+ {
1866
+ scriptAst = Parser . ParseFile ( filePath , out scriptTokens , out errors ) ;
1867
+ }
1868
+ catch ( Exception e )
1869
+ {
1870
+ this . outputWriter . WriteWarning ( e . ToString ( ) ) ;
1871
+ return null ;
1872
+ }
1873
+ #if ! PSV3
1874
+ //try parsing again
1875
+ if ( TrySaveModules ( errors , scriptAst ) )
1876
+ {
1877
+ scriptAst = Parser . ParseFile ( filePath , out scriptTokens , out errors ) ;
1878
+ }
1879
+ #endif //!PSV3
1880
+ IEnumerable < ParseError > relevantParseErrors = RemoveTypeNotFoundParseErrors ( errors , out diagnosticRecords ) ;
1881
+
1882
+ // First, add all parse errors if they've been requested
1883
+ if ( relevantParseErrors != null && ( severity == null || severity . Contains ( "ParseError" , StringComparer . OrdinalIgnoreCase ) ) )
1884
+ {
1885
+ List < DiagnosticRecord > results = new List < DiagnosticRecord > ( ) ;
1886
+ foreach ( ParseError parseError in relevantParseErrors )
1887
+ {
1888
+ string parseErrorMessage = String . Format ( CultureInfo . CurrentCulture , Strings . ParseErrorFormatForScriptDefinition , parseError . Message . TrimEnd ( '.' ) , parseError . Extent . StartLineNumber , parseError . Extent . StartColumnNumber ) ;
1889
+ results . Add ( new DiagnosticRecord (
1890
+ parseError . Message ,
1891
+ parseError . Extent ,
1892
+ parseError . ErrorId . ToString ( ) ,
1893
+ DiagnosticSeverity . ParseError ,
1894
+ filePath )
1895
+ ) ;
1896
+ }
1897
+ diagnosticRecords . AddRange ( results ) ;
1898
+ }
1899
+
1893
1900
return diagnosticRecords . Concat ( this . AnalyzeSyntaxTree ( scriptAst , scriptTokens , filePath ) ) ;
1894
1901
}
1895
1902
0 commit comments