@@ -21,6 +21,7 @@ public class ScriptFile
21
21
#region Private Fields
22
22
23
23
private Token [ ] scriptTokens ;
24
+ private Version powerShellVersion ;
24
25
25
26
#endregion
26
27
@@ -126,12 +127,18 @@ public string[] ReferencedFiles
126
127
/// <param name="filePath">The path at which the script file resides.</param>
127
128
/// <param name="clientFilePath">The path which the client uses to identify the file.</param>
128
129
/// <param name="textReader">The TextReader to use for reading the file's contents.</param>
129
- public ScriptFile ( string filePath , string clientFilePath , TextReader textReader )
130
+ /// <param name="powerShellVersion">The version of PowerShell for which the script is being parsed.</param>
131
+ public ScriptFile (
132
+ string filePath ,
133
+ string clientFilePath ,
134
+ TextReader textReader ,
135
+ Version powerShellVersion )
130
136
{
131
137
this . FilePath = filePath ;
132
138
this . ClientFilePath = clientFilePath ;
133
139
this . IsAnalysisEnabled = true ;
134
140
this . IsInMemory = Workspace . IsPathInMemory ( filePath ) ;
141
+ this . powerShellVersion = powerShellVersion ;
135
142
136
143
this . SetFileContents ( textReader . ReadToEnd ( ) ) ;
137
144
}
@@ -142,11 +149,17 @@ public ScriptFile(string filePath, string clientFilePath, TextReader textReader)
142
149
/// <param name="filePath">The path at which the script file resides.</param>
143
150
/// <param name="clientFilePath">The path which the client uses to identify the file.</param>
144
151
/// <param name="initialBuffer">The initial contents of the script file.</param>
145
- public ScriptFile ( string filePath , string clientFilePath , string initialBuffer )
152
+ /// <param name="powerShellVersion">The version of PowerShell for which the script is being parsed.</param>
153
+ public ScriptFile (
154
+ string filePath ,
155
+ string clientFilePath ,
156
+ string initialBuffer ,
157
+ Version powerShellVersion )
146
158
{
147
159
this . FilePath = filePath ;
148
160
this . ClientFilePath = clientFilePath ;
149
161
this . IsAnalysisEnabled = true ;
162
+ this . powerShellVersion = powerShellVersion ;
150
163
151
164
this . SetFileContents ( initialBuffer ) ;
152
165
}
@@ -358,15 +371,39 @@ private void ParseFileContents()
358
371
359
372
try
360
373
{
374
+ #if PowerShellv5r2
375
+ // This overload appeared with Windows 10 Update 1
376
+ if ( this . powerShellVersion . Major >= 5 &&
377
+ this . powerShellVersion . Build >= 10586 )
378
+ {
379
+ // Include the file path so that module relative
380
+ // paths are evaluated correctly
381
+ this . ScriptAst =
382
+ Parser . ParseInput (
383
+ this . Contents ,
384
+ this . FilePath ,
385
+ out this . scriptTokens ,
386
+ out parseErrors ) ;
387
+ }
388
+ else
389
+ {
390
+ this . ScriptAst =
391
+ Parser . ParseInput (
392
+ this . Contents ,
393
+ out this . scriptTokens ,
394
+ out parseErrors ) ;
395
+ }
396
+ #else
361
397
this . ScriptAst =
362
398
Parser . ParseInput (
363
- this . Contents ,
364
- out this . scriptTokens ,
399
+ this . Contents ,
400
+ out this . scriptTokens ,
365
401
out parseErrors ) ;
402
+ #endif
366
403
}
367
404
catch ( RuntimeException ex )
368
405
{
369
- var parseError =
406
+ var parseError =
370
407
new ParseError (
371
408
null ,
372
409
ex . ErrorRecord . FullyQualifiedErrorId ,
@@ -388,6 +425,6 @@ private void ParseFileContents()
388
425
AstOperations . FindDotSourcedIncludes ( this . ScriptAst ) ;
389
426
}
390
427
391
- #endregion
428
+ #endregion
392
429
}
393
430
}
0 commit comments