diff --git a/src/PowerShellEditorServices/Workspace/ScriptFile.cs b/src/PowerShellEditorServices/Workspace/ScriptFile.cs index dab2918cc..0e0d4d6e7 100644 --- a/src/PowerShellEditorServices/Workspace/ScriptFile.cs +++ b/src/PowerShellEditorServices/Workspace/ScriptFile.cs @@ -26,7 +26,6 @@ public class ScriptFile "\n" }; - private Token[] scriptTokens; private Version powerShellVersion; #endregion @@ -116,7 +115,8 @@ public ScriptBlockAst ScriptAst /// public Token[] ScriptTokens { - get { return this.scriptTokens; } + get; + private set; } /// @@ -152,6 +152,7 @@ public ScriptFile( this.IsInMemory = Workspace.IsPathInMemory(filePath); this.powerShellVersion = powerShellVersion; + // SetFileContents() calls ParseFileContents() which initializes the rest of the properties. this.SetFileContents(textReader.ReadToEnd()); } @@ -599,6 +600,8 @@ private void ParseFileContents() try { + Token[] scriptTokens; + #if PowerShellv5r2 // This overload appeared with Windows 10 Update 1 if (this.powerShellVersion.Major >= 5 && @@ -610,7 +613,7 @@ private void ParseFileContents() Parser.ParseInput( this.Contents, this.FilePath, - out this.scriptTokens, + out scriptTokens, out parseErrors); } else @@ -618,16 +621,18 @@ private void ParseFileContents() this.ScriptAst = Parser.ParseInput( this.Contents, - out this.scriptTokens, + out scriptTokens, out parseErrors); } #else this.ScriptAst = Parser.ParseInput( this.Contents, - out this.scriptTokens, + out scriptTokens, out parseErrors); #endif + + this.ScriptTokens = scriptTokens; } catch (RuntimeException ex) { @@ -638,7 +643,7 @@ private void ParseFileContents() ex.Message); parseErrors = new[] { parseError }; - this.scriptTokens = new Token[0]; + this.ScriptTokens = new Token[0]; this.ScriptAst = null; } @@ -654,6 +659,8 @@ private void ParseFileContents() // users should save the file. if (IsUntitledPath(this.FilePath)) { + // Need to initialize the ReferencedFiles property to an empty array. + this.ReferencedFiles = new string[0]; return; } diff --git a/test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs b/test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs index adfc2727e..993e56999 100644 --- a/test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs +++ b/test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs @@ -164,10 +164,10 @@ public void CanAppendToEndOfFile() public void FindsDotSourcedFiles() { string exampleScriptContents = - @". .\athing.ps1"+"\r\n"+ - @". .\somefile.ps1"+"\r\n" + - @". .\somefile.ps1"+"\r\n" + - @"Do-Stuff $uri"+"\r\n" + + @". .\athing.ps1" + "\r\n" + + @". .\somefile.ps1" + "\r\n" + + @". .\somefile.ps1" + "\r\n" + + @"Do-Stuff $uri" + "\r\n" + @". simpleps.ps1"; using (StringReader stringReader = new StringReader(exampleScriptContents)) @@ -532,4 +532,51 @@ private void AssertNewPosition( Assert.Equal(expectedColumn, newPosition.Column); } } + + public class ScriptFileConstructorTests + { + private static readonly Version PowerShellVersion = new Version("5.0"); + + [Fact] + public void PropertiesInitializedCorrectlyForFile() + { + var path = "TestFile.ps1"; + var scriptFile = ScriptFileChangeTests.CreateScriptFile(""); + + Assert.Equal(path, scriptFile.FilePath); + Assert.Equal(path, scriptFile.ClientFilePath); + Assert.True(scriptFile.IsAnalysisEnabled); + Assert.False(scriptFile.IsInMemory); + Assert.Empty(scriptFile.ReferencedFiles); + Assert.Empty(scriptFile.SyntaxMarkers); + Assert.Single(scriptFile.ScriptTokens); + Assert.Single(scriptFile.FileLines); + } + + [Fact] + public void PropertiesInitializedCorrectlyForUntitled() + { + var path = "untitled:untitled-1"; + + // 3 lines and 10 tokens in this script. + var script = @"function foo() { + 'foo' +}"; + + using (StringReader stringReader = new StringReader(script)) + { + // Create an in-memory file from the StringReader + var scriptFile = new ScriptFile(path, path, stringReader, PowerShellVersion); + + Assert.Equal(path, scriptFile.FilePath); + Assert.Equal(path, scriptFile.ClientFilePath); + Assert.True(scriptFile.IsAnalysisEnabled); + Assert.True(scriptFile.IsInMemory); + Assert.Empty(scriptFile.ReferencedFiles); + Assert.Empty(scriptFile.SyntaxMarkers); + Assert.Equal(10, scriptFile.ScriptTokens.Length); + Assert.Equal(3, scriptFile.FileLines.Count); + } + } + } }