Clean up usage of FileKinds

\n\nCommit migrated from b45125a755
This commit is contained in:
Ryan Nowak 2019-01-02 15:02:23 -08:00
parent 053121e0b7
commit 14abd88648
8 changed files with 33 additions and 21 deletions

View File

@ -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. // 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<RazorProjectItem>(); return Array.Empty<RazorProjectItem>();
} }

View File

@ -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. // 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<RazorProjectItem>(); return Array.Empty<RazorProjectItem>();
} }

View File

@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
} }
// Don't add MVC imports for a component // 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<RazorProjectItem>(); return Array.Empty<RazorProjectItem>();
} }

View File

@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
protected override bool IsMatch(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode) 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) protected override CodeTarget CreateTarget(RazorCodeDocument codeDocument, RazorCodeGenerationOptions options)

View File

@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
} }
// Don't add Component imports for a non-component. // 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<RazorProjectItem>(); return Array.Empty<RazorProjectItem>();
} }

View File

@ -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 // We need to decide up front if this document is a "component" file. This will affect how
// lowering behaves. // lowering behaves.
LoweringVisitor visitor; LoweringVisitor visitor;
if (string.Equals(FileKinds.Component, codeDocument.GetFileKind(), StringComparison.OrdinalIgnoreCase) && if (FileKinds.IsComponent(codeDocument.GetFileKind()) &&
syntaxTree.Options.FeatureFlags.AllowComponentFileKind) syntaxTree.Options.FeatureFlags.AllowComponentFileKind)
{ {
visitor = new ComponentFileKindVisitor(document, builder, syntaxTree.Options.FeatureFlags) visitor = new ComponentFileKindVisitor(document, builder, syntaxTree.Options.FeatureFlags)

View File

@ -1,6 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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 namespace Microsoft.AspNetCore.Razor.Language
{ {
public static class FileKinds public static class FileKinds
@ -8,5 +11,28 @@ namespace Microsoft.AspNetCore.Razor.Language
public static readonly string Component = "component"; public static readonly string Component = "component";
public static readonly string Legacy = "mvc"; 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;
}
}
} }
} }

View File

@ -1,11 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.Diagnostics;
using System.IO; using System.IO;
using Microsoft.AspNetCore.Razor.Language.Components;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
namespace Microsoft.AspNetCore.Razor.Language namespace Microsoft.AspNetCore.Razor.Language
{ {
@ -45,18 +42,7 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
get get
{ {
if (FilePath == null) return FilePath == null ? null : FileKinds.GetFileKindFromFilePath(FilePath);
{
return null;
}
else if (string.Equals(".razor", Path.GetExtension(FilePath), StringComparison.OrdinalIgnoreCase))
{
return FileKinds.Component;
}
else
{
return FileKinds.Legacy;
}
} }
} }