From 9359f12f3857183210df9ed98ca002c6f2d0ef5a Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 7 Sep 2021 11:51:29 -0700 Subject: [PATCH] Guard the 'clean' block behind an experimental feature --- .../engine/ExperimentalFeature/ExperimentalFeature.cs | 4 ++++ .../engine/parser/tokenizer.cs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs b/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs index dd0f72096350..cb0be3193a74 100644 --- a/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs +++ b/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs @@ -23,6 +23,7 @@ public class ExperimentalFeature internal const string EngineSource = "PSEngine"; internal const string PSNativeCommandArgumentPassingFeatureName = "PSNativeCommandArgumentPassing"; internal const string PSNativeCommandErrorActionPreferenceFeatureName = "PSNativeCommandErrorActionPreference"; + internal const string PSCleanBlockFeatureName = "PSCleanBlock"; #endregion @@ -126,6 +127,9 @@ static ExperimentalFeature() new ExperimentalFeature( name: PSNativeCommandErrorActionPreferenceFeatureName, description: "Native commands with non-zero exit codes issue errors according to $ErrorActionPreference when $PSNativeCommandUseErrorActionPreference is $true"), + new ExperimentalFeature( + name: PSCleanBlockFeatureName, + description: "Add support of a 'Clean' block to functions and script cmdlets for easy resource cleanup"), }; EngineExperimentalFeatures = new ReadOnlyCollection(engineFeatures); diff --git a/src/System.Management.Automation/engine/parser/tokenizer.cs b/src/System.Management.Automation/engine/parser/tokenizer.cs index a2b5bf87cb5e..43046c9d29e6 100644 --- a/src/System.Management.Automation/engine/parser/tokenizer.cs +++ b/src/System.Management.Automation/engine/parser/tokenizer.cs @@ -699,8 +699,16 @@ static Tokenizer() Diagnostics.Assert(s_keywordText.Length == s_keywordTokenKind.Length, "Keyword table sizes must match"); Diagnostics.Assert(_operatorText.Length == s_operatorTokenKind.Length, "Operator table sizes must match"); + bool isCleanBlockFeatureEnabled = ExperimentalFeature.IsEnabled(ExperimentalFeature.PSCleanBlockFeatureName); + for (int i = 0; i < s_keywordText.Length; ++i) { + if (!isCleanBlockFeatureEnabled && s_keywordText[i] == "clean") + { + // Skip adding the 'clean' keyword when the feature is disabled. + continue; + } + s_keywordTable.Add(s_keywordText[i], s_keywordTokenKind[i]); }