From 27ea45316cd2feb85cd0164cdb648631ef53556c Mon Sep 17 00:00:00 2001 From: filipw Date: Mon, 11 Feb 2019 09:22:44 +0100 Subject: [PATCH] code review changes --- src/Scripting/CSharp/CSharpScriptCompiler.cs | 8 ++++---- .../CSharp/CSharpScriptingResources.Designer.cs | 14 +++++++++++--- .../CSharp/CSharpScriptingResources.resx | 17 ++++++++++------- src/Scripting/CSharp/ScriptOptionsExtensions.cs | 8 +++++--- .../CSharp/xlf/CSharpScriptingResources.cs.xlf | 5 +++++ .../CSharp/xlf/CSharpScriptingResources.de.xlf | 5 +++++ .../CSharp/xlf/CSharpScriptingResources.es.xlf | 5 +++++ .../CSharp/xlf/CSharpScriptingResources.fr.xlf | 5 +++++ .../CSharp/xlf/CSharpScriptingResources.it.xlf | 5 +++++ .../CSharp/xlf/CSharpScriptingResources.ja.xlf | 5 +++++ .../CSharp/xlf/CSharpScriptingResources.ko.xlf | 5 +++++ .../CSharp/xlf/CSharpScriptingResources.pl.xlf | 5 +++++ .../xlf/CSharpScriptingResources.pt-BR.xlf | 5 +++++ .../CSharp/xlf/CSharpScriptingResources.ru.xlf | 5 +++++ .../CSharp/xlf/CSharpScriptingResources.tr.xlf | 5 +++++ .../xlf/CSharpScriptingResources.zh-Hans.xlf | 5 +++++ .../xlf/CSharpScriptingResources.zh-Hant.xlf | 5 +++++ src/Scripting/CSharpTest/ScriptOptionsTests.cs | 11 ++++++++++- .../Hosting/CommandLine/CommandLineRunner.cs | 2 +- src/Scripting/Core/ScriptCompiler.cs | 2 +- .../VisualBasic/VisualBasicScriptCompiler.vb | 4 ++-- 21 files changed, 109 insertions(+), 22 deletions(-) diff --git a/src/Scripting/CSharp/CSharpScriptCompiler.cs b/src/Scripting/CSharp/CSharpScriptCompiler.cs index 15b14dfade251..342745d3fdc77 100644 --- a/src/Scripting/CSharp/CSharpScriptCompiler.cs +++ b/src/Scripting/CSharp/CSharpScriptCompiler.cs @@ -11,7 +11,7 @@ internal sealed class CSharpScriptCompiler : ScriptCompiler { public static readonly ScriptCompiler Instance = new CSharpScriptCompiler(); - private static readonly CSharpParseOptions s_defaultOptions = new CSharpParseOptions(kind: SourceCodeKind.Script, languageVersion: LanguageVersion.Latest); + internal static readonly CSharpParseOptions DefaultParseOptions = new CSharpParseOptions(kind: SourceCodeKind.Script, languageVersion: LanguageVersion.Latest); private CSharpScriptCompiler() { @@ -23,8 +23,8 @@ private CSharpScriptCompiler() public override bool IsCompleteSubmission(SyntaxTree tree) => SyntaxFactory.IsCompleteSubmission(tree); - public override SyntaxTree ParseSubmission(SourceText text, ScriptOptions scriptOptions, CancellationToken cancellationToken) => - SyntaxFactory.ParseSyntaxTree(text, scriptOptions.ParseOptions ?? s_defaultOptions, cancellationToken: cancellationToken); + public override SyntaxTree ParseSubmission(SourceText text, ParseOptions parseOptions, CancellationToken cancellationToken) => + SyntaxFactory.ParseSyntaxTree(text, parseOptions ?? DefaultParseOptions, cancellationToken: cancellationToken); public override Compilation CreateSubmission(Script script) { @@ -40,7 +40,7 @@ public override Compilation CreateSubmission(Script script) // TODO: report diagnostics diagnostics.Free(); - var tree = SyntaxFactory.ParseSyntaxTree(script.SourceText, script.Options.ParseOptions ?? s_defaultOptions, script.Options.FilePath); + var tree = SyntaxFactory.ParseSyntaxTree(script.SourceText, script.Options.ParseOptions ?? DefaultParseOptions, script.Options.FilePath); string assemblyName, submissionTypeName; script.Builder.GenerateSubmissionId(out assemblyName, out submissionTypeName); diff --git a/src/Scripting/CSharp/CSharpScriptingResources.Designer.cs b/src/Scripting/CSharp/CSharpScriptingResources.Designer.cs index 555b8cfb5c45d..852acd45264be 100644 --- a/src/Scripting/CSharp/CSharpScriptingResources.Designer.cs +++ b/src/Scripting/CSharp/CSharpScriptingResources.Designer.cs @@ -10,7 +10,6 @@ namespace Microsoft.CodeAnalysis.CSharp.Scripting { using System; - using System.Reflection; /// @@ -20,7 +19,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Scripting { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class CSharpScriptingResources { @@ -40,7 +39,7 @@ internal CSharpScriptingResources() { internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScriptingResources", typeof(CSharpScriptingResources).GetTypeInfo().Assembly); + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScriptingResources", typeof(CSharpScriptingResources).Assembly); resourceMan = temp; } return resourceMan; @@ -61,6 +60,15 @@ internal CSharpScriptingResources() { } } + /// + /// Looks up a localized string similar to Cannot set C# LanguageVersion because ParseOptions were already configured for a different language.. + /// + internal static string CannotSetCSharpLanguageVersion { + get { + return ResourceManager.GetString("CannotSetCSharpLanguageVersion", resourceCulture); + } + } + /// /// Looks up a localized string similar to Usage: csi [option] ... [script-file.csx] [script-argument] ... /// diff --git a/src/Scripting/CSharp/CSharpScriptingResources.resx b/src/Scripting/CSharp/CSharpScriptingResources.resx index 331c9eb3efe00..d9f81cdbaf085 100644 --- a/src/Scripting/CSharp/CSharpScriptingResources.resx +++ b/src/Scripting/CSharp/CSharpScriptingResources.resx @@ -124,7 +124,7 @@ Copyright (C) Microsoft Corporation. All rights reserved. - Usage: csi [option] ... [script-file.csx] [script-argument] ... Executes script-file.csx if specified, otherwise launches an interactive REPL (Read Eval Print Loop). @@ -132,13 +132,16 @@ Options: /help Display this usage message (alternative form: /?) /version Display the version and exit /i Drop to REPL after executing the specified script. - /r: Reference metadata from the specified assembly file (alternative form: /reference) - /r: Reference metadata from the specified assembly files (alternative form: /reference) - /lib: List of directories where to look for libraries specified by #r directive. + /r:<file> Reference metadata from the specified assembly file (alternative form: /reference) + /r:<file list> Reference metadata from the specified assembly files (alternative form: /reference) + /lib:<path list> List of directories where to look for libraries specified by #r directive. (alternative forms: /libPath /libPaths) - /u: Define global namespace using (alternative forms: /using, /usings, /import, /imports) - @ Read response file for more options + /u:<namespace> Define global namespace using (alternative forms: /using, /usings, /import, /imports) + @<file> Read response file for more options -- Indicates that the remaining arguments should not be treated as options. -]]> + + + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. \ No newline at end of file diff --git a/src/Scripting/CSharp/ScriptOptionsExtensions.cs b/src/Scripting/CSharp/ScriptOptionsExtensions.cs index 9fc29f1e8fb0e..394e7fcf03324 100644 --- a/src/Scripting/CSharp/ScriptOptionsExtensions.cs +++ b/src/Scripting/CSharp/ScriptOptionsExtensions.cs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using Microsoft.CodeAnalysis.Scripting; namespace Microsoft.CodeAnalysis.CSharp.Scripting @@ -8,10 +9,11 @@ public static class ScriptOptionsExtensions { public static ScriptOptions WithLanguageVersion(this ScriptOptions options, LanguageVersion languageVersion) { - if (options.ParseOptions is CSharpParseOptions csharpParseOptions && csharpParseOptions.LanguageVersion == languageVersion) - return options; + var parseOptions = (options.ParseOptions is null) + ? CSharpScriptCompiler.DefaultParseOptions + : (options.ParseOptions is CSharpParseOptions existing) ? existing : throw new InvalidOperationException(CSharpScriptingResources.CannotSetCSharpLanguageVersion); - return options.WithParseOptions(new CSharpParseOptions(kind: SourceCodeKind.Script, languageVersion: languageVersion)); + return options.WithParseOptions(parseOptions.WithLanguageVersion(languageVersion)); } } } diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.cs.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.cs.xlf index e11c97b1e1006..cb946b07f8b0f 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.cs.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.cs.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Verze kompilátoru Microsoft (R) Visual C# Interactive {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.de.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.de.xlf index 84782ad974661..48b6a39fd87da 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.de.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.de.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Microsoft (R) Visual C# Interactive – Compilerversion {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.es.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.es.xlf index d8d6193caabe2..e6dc19b51a477 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.es.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.es.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Compilador de Microsoft (R) Visual C# interactivo, versión {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.fr.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.fr.xlf index 0ae776e0c6353..3bd88cc85700a 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.fr.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.fr.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Compilateur Microsoft (R) Visual C# Interactive version {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.it.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.it.xlf index 09e93d9e81cd4..879a44dc6dbd8 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.it.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.it.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Compilatore Microsoft (R) Visual C# Interactive versione {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.ja.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.ja.xlf index f3d7f3348ed73..c15f4b54d0b26 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.ja.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.ja.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Microsoft (R) Visual C# インタラクティブ コンパイラ バージョン {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.ko.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.ko.xlf index 2872f578a7a53..c7b2a2bcfc7ac 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.ko.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.ko.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Microsoft (R) Visual C# 대화형 컴파일러 버전 {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.pl.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.pl.xlf index b6cdc5a2c23ac..6fc509813f71a 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.pl.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.pl.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Microsoft (R) Visual C# Interactive — wersja kompilatora {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.pt-BR.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.pt-BR.xlf index 0047ea8d9312a..872b82dab642c 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.pt-BR.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.pt-BR.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Versão {0} do Compilador C# Interativo do Microsoft (R) Visual diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.ru.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.ru.xlf index a20149869a16e..421a11581988e 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.ru.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.ru.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Компилятор Microsoft (R) Visual C# Interactive версии {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.tr.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.tr.xlf index d05372396f404..e44b056153e3d 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.tr.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.tr.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Microsoft (R) Visual C# Etkileşimli Derleyici sürümü {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.zh-Hans.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.zh-Hans.xlf index f567d574e27de..c851d78293b5b 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.zh-Hans.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.zh-Hans.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Microsoft (R) Visual C# 交互窗口编译器版本 {0} diff --git a/src/Scripting/CSharp/xlf/CSharpScriptingResources.zh-Hant.xlf b/src/Scripting/CSharp/xlf/CSharpScriptingResources.zh-Hant.xlf index 16615f27b5b5d..bc905d9257b3b 100644 --- a/src/Scripting/CSharp/xlf/CSharpScriptingResources.zh-Hant.xlf +++ b/src/Scripting/CSharp/xlf/CSharpScriptingResources.zh-Hant.xlf @@ -2,6 +2,11 @@ + + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + Cannot set C# LanguageVersion because ParseOptions were already configured for a different language. + + Microsoft (R) Visual C# Interactive Compiler version {0} Microsoft (R) Visual C# 互動編譯器 {0} 版 diff --git a/src/Scripting/CSharpTest/ScriptOptionsTests.cs b/src/Scripting/CSharpTest/ScriptOptionsTests.cs index 3fb4ec22c11a5..011a4dd1f1828 100644 --- a/src/Scripting/CSharpTest/ScriptOptionsTests.cs +++ b/src/Scripting/CSharpTest/ScriptOptionsTests.cs @@ -1,8 +1,10 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.Scripting; +using Microsoft.CodeAnalysis.VisualBasic; using Roslyn.Test.Utilities; using Xunit; @@ -18,10 +20,17 @@ public void WithLanguageVersion() } [Fact] - public void WithEmitDebugInformation_SameValueTwice_DoesNotCreateNewInstance() + public void WithWithLanguageVersion_SameValueTwice_DoesNotCreateNewInstance() { var options = ScriptOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8); Assert.Same(options, options.WithLanguageVersion(LanguageVersion.CSharp8)); } + + [Fact] + public void WithWithLanguageVersion_NonCSharpParseOptions_Throws() + { + var options = ScriptOptions.Default.WithParseOptions(new VisualBasicParseOptions(kind: SourceCodeKind.Script, languageVersion: VisualBasic.LanguageVersion.Latest)); + Assert.Throws(() => options.WithLanguageVersion(LanguageVersion.CSharp8)); + } } } diff --git a/src/Scripting/Core/Hosting/CommandLine/CommandLineRunner.cs b/src/Scripting/Core/Hosting/CommandLine/CommandLineRunner.cs index 42955745319ac..f2724e8c59edb 100644 --- a/src/Scripting/Core/Hosting/CommandLine/CommandLineRunner.cs +++ b/src/Scripting/Core/Hosting/CommandLine/CommandLineRunner.cs @@ -240,7 +240,7 @@ private void RunInteractiveLoop(ScriptOptions options, string initialScriptCodeO input.AppendLine(line); - var tree = _scriptCompiler.ParseSubmission(SourceText.From(input.ToString()), options, cancellationToken); + var tree = _scriptCompiler.ParseSubmission(SourceText.From(input.ToString()), options.ParseOptions, cancellationToken); if (_scriptCompiler.IsCompleteSubmission(tree)) { break; diff --git a/src/Scripting/Core/ScriptCompiler.cs b/src/Scripting/Core/ScriptCompiler.cs index 9f9a9d7499db4..5048341dde794 100644 --- a/src/Scripting/Core/ScriptCompiler.cs +++ b/src/Scripting/Core/ScriptCompiler.cs @@ -13,7 +13,7 @@ internal abstract class ScriptCompiler public abstract DiagnosticFormatter DiagnosticFormatter { get; } public abstract StringComparer IdentifierComparer { get; } - public abstract SyntaxTree ParseSubmission(SourceText text, ScriptOptions scriptOptions, CancellationToken cancellationToken); + public abstract SyntaxTree ParseSubmission(SourceText text, ParseOptions parseOptions, CancellationToken cancellationToken); public abstract bool IsCompleteSubmission(SyntaxTree tree); } } diff --git a/src/Scripting/VisualBasic/VisualBasicScriptCompiler.vb b/src/Scripting/VisualBasic/VisualBasicScriptCompiler.vb index 44fcbaa927c48..0da1df0d298a0 100644 --- a/src/Scripting/VisualBasic/VisualBasicScriptCompiler.vb +++ b/src/Scripting/VisualBasic/VisualBasicScriptCompiler.vb @@ -35,8 +35,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Scripting Return SyntaxFactory.IsCompleteSubmission(tree) End Function - Public Overrides Function ParseSubmission(text As SourceText, scriptOptions As ScriptOptions, cancellationToken As CancellationToken) As SyntaxTree - Return SyntaxFactory.ParseSyntaxTree(text, If(scriptOptions.ParseOptions, s_defaultOptions), cancellationToken:=cancellationToken) + Public Overrides Function ParseSubmission(text As SourceText, parseOptions As ParseOptions, cancellationToken As CancellationToken) As SyntaxTree + Return SyntaxFactory.ParseSyntaxTree(text, If(parseOptions, s_defaultOptions), cancellationToken:=cancellationToken) End Function Private Shared Function GetGlobalImportsForCompilation(script As Script) As IEnumerable(Of GlobalImport)