From 14abd88648b87fa53912e209b0205578bc00911e Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 2 Jan 2019 15:02:23 -0800 Subject: [PATCH] Clean up usage of FileKinds \n\nCommit migrated from https://github.com/dotnet/aspnetcore-tooling/commit/b45125a75550889b1c10323edd0a72de9722e6a1 --- .../src/MvcImportProjectFeature.cs | 2 +- .../src/MvcImportProjectFeature.cs | 2 +- .../src/MvcImportProjectFeature.cs | 2 +- .../ComponentDocumentClassifierPass.cs | 2 +- .../ComponentImportProjectFeature.cs | 2 +- ...faultRazorIntermediateNodeLoweringPhase.cs | 2 +- .../src/FileKinds.cs | 26 +++++++++++++++++++ .../src/RazorProjectItem.cs | 16 +----------- 8 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/src/MvcImportProjectFeature.cs b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/src/MvcImportProjectFeature.cs index e117a5298c..fa8a40fedb 100644 --- a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/src/MvcImportProjectFeature.cs +++ b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/src/MvcImportProjectFeature.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X } // Don't add MVC imports for a component - this shouldn't happen for v1, but just in case. - if (string.Equals(projectItem.FileKind, FileKinds.Component, StringComparison.OrdinalIgnoreCase)) + if (FileKinds.IsComponent(projectItem.FileKind)) { return Array.Empty(); } diff --git a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/src/MvcImportProjectFeature.cs b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/src/MvcImportProjectFeature.cs index d7844d9eee..b5a5851922 100644 --- a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/src/MvcImportProjectFeature.cs +++ b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/src/MvcImportProjectFeature.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X } // Don't add MVC imports for a component - this shouldn't happen for v2, but just in case. - if (string.Equals(projectItem.FileKind, FileKinds.Component, StringComparison.OrdinalIgnoreCase)) + if (FileKinds.IsComponent(projectItem.FileKind)) { return Array.Empty(); } diff --git a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/src/MvcImportProjectFeature.cs b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/src/MvcImportProjectFeature.cs index 5a56cd19d0..b85402ef79 100644 --- a/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/src/MvcImportProjectFeature.cs +++ b/src/Razor/Microsoft.AspNetCore.Mvc.Razor.Extensions/src/MvcImportProjectFeature.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions } // Don't add MVC imports for a component - if (string.Equals(projectItem.FileKind, FileKinds.Component, StringComparison.OrdinalIgnoreCase)) + if (FileKinds.IsComponent(projectItem.FileKind)) { return Array.Empty(); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDocumentClassifierPass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDocumentClassifierPass.cs index 08b9dc7b03..09197b56b5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDocumentClassifierPass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDocumentClassifierPass.cs @@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Components protected override bool IsMatch(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode) { - return string.Equals(codeDocument.GetFileKind(), FileKinds.Component); + return FileKinds.IsComponent(codeDocument.GetFileKind()); } protected override CodeTarget CreateTarget(RazorCodeDocument codeDocument, RazorCodeGenerationOptions options) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentImportProjectFeature.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentImportProjectFeature.cs index dea07160f1..701c484dae 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentImportProjectFeature.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentImportProjectFeature.cs @@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Components } // Don't add Component imports for a non-component. - if (!string.Equals(projectItem.FileKind, FileKinds.Component, StringComparison.OrdinalIgnoreCase)) + if (!FileKinds.IsComponent(projectItem.FileKind)) { return Array.Empty(); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs index c92f08c1b4..b92fea9890 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorIntermediateNodeLoweringPhase.cs @@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Razor.Language // We need to decide up front if this document is a "component" file. This will affect how // lowering behaves. LoweringVisitor visitor; - if (string.Equals(FileKinds.Component, codeDocument.GetFileKind(), StringComparison.OrdinalIgnoreCase) && + if (FileKinds.IsComponent(codeDocument.GetFileKind()) && syntaxTree.Options.FeatureFlags.AllowComponentFileKind) { visitor = new ComponentFileKindVisitor(document, builder, syntaxTree.Options.FeatureFlags) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/FileKinds.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/FileKinds.cs index 030bcf6b6b..93a5a21c03 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/FileKinds.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/FileKinds.cs @@ -1,6 +1,9 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; +using System.IO; + namespace Microsoft.AspNetCore.Razor.Language { public static class FileKinds @@ -8,5 +11,28 @@ namespace Microsoft.AspNetCore.Razor.Language public static readonly string Component = "component"; public static readonly string Legacy = "mvc"; + + public static bool IsComponent(string fileKind) + { + // fileKind might be null. + return string.Equals(fileKind, FileKinds.Component, StringComparison.OrdinalIgnoreCase); + } + + public static string GetFileKindFromFilePath(string filePath) + { + if (filePath == null) + { + throw new ArgumentNullException(nameof(filePath)); + } + + if (string.Equals(".razor", Path.GetExtension(filePath), StringComparison.OrdinalIgnoreCase)) + { + return FileKinds.Component; + } + else + { + return FileKinds.Legacy; + } + } } } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProjectItem.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProjectItem.cs index cdc50b8948..5ee33824d5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProjectItem.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorProjectItem.cs @@ -1,11 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Diagnostics; using System.IO; -using Microsoft.AspNetCore.Razor.Language.Components; -using Microsoft.AspNetCore.Razor.Language.Intermediate; namespace Microsoft.AspNetCore.Razor.Language { @@ -45,18 +42,7 @@ namespace Microsoft.AspNetCore.Razor.Language { get { - if (FilePath == null) - { - return null; - } - else if (string.Equals(".razor", Path.GetExtension(FilePath), StringComparison.OrdinalIgnoreCase)) - { - return FileKinds.Component; - } - else - { - return FileKinds.Legacy; - } + return FilePath == null ? null : FileKinds.GetFileKindFromFilePath(FilePath); } }