Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void Process_WithFileKind_SetsOnCodeDocument()
var codeDocument = projectEngine.Process(RazorSourceDocument.ReadFrom(projectItem), "test", importSources: default, tagHelpers: null);

// Assert
var actual = codeDocument.GetFileKind();
var actual = codeDocument.FileKind;
Assert.Equal("test", actual);
}

Expand Down Expand Up @@ -201,7 +201,7 @@ public void Process_SetsInferredFileKindOnCodeDocument_MvcFile()
var codeDocument = projectEngine.Process(projectItem);

// Assert
var actual = codeDocument.GetFileKind();
var actual = codeDocument.FileKind;
Assert.Same(FileKinds.Legacy, actual);
}

Expand All @@ -217,7 +217,7 @@ public void Process_SetsInferredFileKindOnCodeDocument_Component()
var codeDocument = projectEngine.Process(projectItem);

// Assert
var actual = codeDocument.GetFileKind();
var actual = codeDocument.FileKind;
Assert.Same(FileKinds.Component, actual);
}

Expand Down Expand Up @@ -288,7 +288,7 @@ public void ProcessDesignTime_SetsInferredFileKindOnCodeDocument_MvcFile()
var codeDocument = projectEngine.ProcessDesignTime(projectItem);

// Assert
var actual = codeDocument.GetFileKind();
var actual = codeDocument.FileKind;
Assert.Same(FileKinds.Legacy, actual);
}

Expand All @@ -304,7 +304,7 @@ public void ProcessDesignTime_SetsInferredFileKindOnCodeDocument_Component()
var codeDocument = projectEngine.ProcessDesignTime(projectItem);

// Assert
var actual = codeDocument.GetFileKind();
var actual = codeDocument.FileKind;
Assert.Same(FileKinds.Component, actual);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,15 @@ public void TryComputeNamespace_RespectsNamespaceDirective()
filePath: "C:\\Hello\\Components\\Test.cshtml",
relativePath: "\\Components\\Test.cshtml");


var codeDocument = RazorCodeDocument.Create(
source,
parserOptions: RazorParserOptions.Default.WithDirectives(NamespaceDirective.Directive),
parserOptions: RazorParserOptions.Create(RazorLanguageVersion.Latest, FileKinds.Component, builder =>
{
builder.Directives = [NamespaceDirective.Directive];
}),
codeGenerationOptions: RazorCodeGenerationOptions.Default.WithRootNamespace("Hello.World"));

codeDocument.SetFileKind(FileKinds.Component);
codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(source, codeDocument.ParserOptions));

// Act
Expand All @@ -325,10 +328,12 @@ public void TryComputeNamespace_RespectsImportsNamespaceDirective()

var codeDocument = RazorCodeDocument.Create(
source,
parserOptions: RazorParserOptions.Default.WithDirectives(NamespaceDirective.Directive),
parserOptions: RazorParserOptions.Create(RazorLanguageVersion.Latest, FileKinds.Component, builder =>
{
builder.Directives = [NamespaceDirective.Directive];
}),
codeGenerationOptions: RazorCodeGenerationOptions.Default.WithRootNamespace("Hello.World"));

codeDocument.SetFileKind(FileKinds.Component);
codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(source, codeDocument.ParserOptions));

var importSource = TestRazorSourceDocument.Create(
Expand Down Expand Up @@ -356,10 +361,12 @@ public void TryComputeNamespace_RespectsImportsNamespaceDirective_SameFolder()

var codeDocument = RazorCodeDocument.Create(
source,
parserOptions: RazorParserOptions.Default.WithDirectives(NamespaceDirective.Directive),
parserOptions: RazorParserOptions.Create(RazorLanguageVersion.Latest, FileKinds.Component, builder =>
{
builder.Directives = [NamespaceDirective.Directive];
}),
codeGenerationOptions: RazorCodeGenerationOptions.Default.WithRootNamespace("Hello.World"));

codeDocument.SetFileKind(FileKinds.Component);
codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(source, codeDocument.ParserOptions));

var importSource = TestRazorSourceDocument.Create(
Expand Down Expand Up @@ -388,9 +395,11 @@ public void TryComputeNamespace_OverrideImportsNamespaceDirective()

var codeDocument = RazorCodeDocument.Create(
source,
parserOptions: RazorParserOptions.Default.WithDirectives(NamespaceDirective.Directive));
parserOptions: RazorParserOptions.Create(RazorLanguageVersion.Latest, FileKinds.Component, builder =>
{
builder.Directives = [NamespaceDirective.Directive];
}));

codeDocument.SetFileKind(FileKinds.Component);
codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(source, codeDocument.ParserOptions));

var importSource = TestRazorSourceDocument.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void SetCSharpLanguageVersion_ResolvesLatestCSharpLangVersions()
});

var features = projectEngine.Engine.GetFeatures<IConfigureRazorCodeGenerationOptionsFeature>().OrderByAsArray(static x => x.Order);
var builder = new RazorCodeGenerationOptions.Builder(RazorLanguageVersion.Latest, FileKinds.Legacy);
var builder = new RazorCodeGenerationOptions.Builder();

foreach (var feature in features)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ComponentDocumentClassifierPass(RazorLanguageVersion version)

protected override bool IsMatch(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
{
return FileKinds.IsComponent(codeDocument.GetFileKind());
return FileKinds.IsComponent(codeDocument.FileKind);
}

protected override CodeTarget CreateTarget(RazorCodeDocument codeDocument, RazorCodeGenerationOptions options)
Expand Down Expand Up @@ -95,7 +95,7 @@ protected override void OnDocumentStructureCreated(
@class.Modifiers.Add("public");
@class.Modifiers.Add("partial");

if (FileKinds.IsComponentImport(codeDocument.GetFileKind()))
if (FileKinds.IsComponentImport(codeDocument.FileKind))
{
// We don't want component imports to be considered as real component.
// But we still want to generate code for it so we can get diagnostics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentInte
for (var i = 0; i < directives.Count; i++)
{
var directive = directives[i];
if (FileKinds.IsComponentImport(codeDocument.GetFileKind()) || directive.Node.IsImported())
if (FileKinds.IsComponentImport(codeDocument.FileKind) || directive.Node.IsImported())
{
directive.Node.Diagnostics.Add(ComponentDiagnosticFactory.CreatePageDirective_CannotBeImported(directive.Node.Source.GetValueOrDefault()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class DefaultDirectiveSyntaxTreePass : RazorEngineFeatureBase, IRazorSy

public RazorSyntaxTree Execute(RazorCodeDocument codeDocument, RazorSyntaxTree syntaxTree)
{
if (FileKinds.IsComponent(codeDocument.GetFileKind()))
if (FileKinds.IsComponent(codeDocument.FileKind))
{
// Nothing to do here.
return syntaxTree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, Cancellation
// We need to decide up front if this document is a "component" file. This will affect how
// lowering behaves.
LoweringVisitor visitor;
if (FileKinds.IsComponentImport(codeDocument.GetFileKind()) &&
if (FileKinds.IsComponentImport(codeDocument.FileKind) &&
syntaxTree.Options.AllowComponentFileKind)
{
visitor = new ComponentImportFileKindVisitor(documentNode, builder, syntaxTree.Options)
Expand All @@ -53,7 +53,7 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, Cancellation

visitor.Visit(syntaxTree.Root);
}
else if (FileKinds.IsComponent(codeDocument.GetFileKind()) &&
else if (FileKinds.IsComponent(codeDocument.FileKind) &&
syntaxTree.Options.AllowComponentFileKind)
{
visitor = new ComponentFileKindVisitor(documentNode, builder, syntaxTree.Options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal static PooledDirectiveVisitor GetPooledVisitor(
out DirectiveVisitor visitor)
{
var useComponentDirectiveVisitor = codeDocument.ParserOptions.AllowComponentFileKind &&
FileKinds.IsComponent(codeDocument.GetFileKind());
FileKinds.IsComponent(codeDocument.FileKind);

if (useComponentDirectiveVisitor)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentInte
var directiveNodes = new List<IntermediateNodeReference>();
directiveNodes.AddRange(documentNode.FindDirectiveReferences(FunctionsDirective.Directive));

if (FileKinds.IsComponent(codeDocument.GetFileKind()))
if (FileKinds.IsComponent(codeDocument.FileKind))
{
directiveNodes.AddRange(documentNode.FindDirectiveReferences(ComponentCodeDirective.Directive));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public sealed class RazorCodeDocument
public RazorParserOptions ParserOptions { get; }
public RazorCodeGenerationOptions CodeGenerationOptions { get; }

public string FileKind => ParserOptions.FileKind;

private RazorCodeDocument(
RazorSourceDocument source,
ImmutableArray<RazorSourceDocument> imports,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,26 +202,6 @@ public static void SetCSharpDocument(this RazorCodeDocument document, RazorCShar
document.Items[typeof(RazorCSharpDocument)] = csharp;
}

public static string GetFileKind(this RazorCodeDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}

return (string)document.Items[typeof(FileKinds)];
}

public static void SetFileKind(this RazorCodeDocument document, string fileKind)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}

document.Items[typeof(FileKinds)] = fileKind;
}

public static string GetCssScope(this RazorCodeDocument document)
{
if (document == null)
Expand Down Expand Up @@ -360,7 +340,7 @@ bool TryComputeNamespaceCore(RazorCodeDocument codeDocument, bool fallbackToRoot
appendSuffix = true;

// Empty RootNamespace is allowed only in components.
if (!FileKinds.IsComponent(codeDocument.GetFileKind()) && string.IsNullOrEmpty(baseNamespace))
if (!FileKinds.IsComponent(codeDocument.FileKind) && string.IsNullOrEmpty(baseNamespace))
{
@namespace = null;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ public sealed partial class RazorCodeGenerationOptions
{
public sealed class Builder
{
public RazorLanguageVersion LanguageVersion { get; }
public string FileKind { get; }

private Flags _flags;

public int IndentSize { get; set; }
Expand All @@ -25,10 +22,8 @@ public sealed class Builder
/// </summary>
public string? SuppressUniqueIds { get; set; }

internal Builder(RazorLanguageVersion languageVersion, string fileKind)
internal Builder()
{
LanguageVersion = languageVersion ?? DefaultLanguageVersion;
FileKind = fileKind ?? DefaultFileKind;
IndentSize = DefaultIndentSize;
NewLine = DefaultNewLine;
}
Expand Down Expand Up @@ -159,6 +154,6 @@ public bool RemapLinePragmaPathsOnWindows
}

public RazorCodeGenerationOptions ToOptions()
=> new(LanguageVersion, FileKind, IndentSize, NewLine, RootNamespace, SuppressUniqueIds, _flags);
=> new(IndentSize, NewLine, RootNamespace, SuppressUniqueIds, _flags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,23 @@ namespace Microsoft.AspNetCore.Razor.Language;

public sealed partial class RazorCodeGenerationOptions
{
private static RazorLanguageVersion DefaultLanguageVersion => RazorLanguageVersion.Latest;
private static string DefaultFileKind => FileKinds.Legacy;
private static int DefaultIndentSize => 4;
private static string DefaultNewLine => Environment.NewLine;

public static RazorCodeGenerationOptions Default { get; } = new(
languageVersion: DefaultLanguageVersion,
fileKind: DefaultFileKind,
indentSize: DefaultIndentSize,
newLine: DefaultNewLine,
rootNamespace: null,
suppressUniqueIds: null,
flags: Flags.DefaultFlags);

public static RazorCodeGenerationOptions DesignTimeDefault { get; } = new(
languageVersion: DefaultLanguageVersion,
fileKind: DefaultFileKind,
indentSize: DefaultIndentSize,
newLine: DefaultNewLine,
rootNamespace: null,
suppressUniqueIds: null,
flags: Flags.DefaultDesignTimeFlags);

public RazorLanguageVersion LanguageVersion { get; }
internal string FileKind { get; }

public int IndentSize { get; }
public string NewLine { get; }

Expand All @@ -50,23 +41,27 @@ public sealed partial class RazorCodeGenerationOptions
private readonly Flags _flags;

private RazorCodeGenerationOptions(
RazorLanguageVersion languageVersion,
string fileKind,
int indentSize,
string newLine,
string? rootNamespace,
string? suppressUniqueIds,
Flags flags)
{
LanguageVersion = languageVersion ?? DefaultLanguageVersion;
FileKind = fileKind ?? DefaultFileKind;
IndentSize = indentSize;
NewLine = newLine;
RootNamespace = rootNamespace;
SuppressUniqueIds = suppressUniqueIds;
_flags = flags;
}

public static RazorCodeGenerationOptions Create(Action<Builder> configure)
{
var builder = new Builder();
configure?.Invoke(builder);

return builder.ToOptions();
}

public bool DesignTime
=> _flags.HasFlag(Flags.DesignTime);

Expand Down Expand Up @@ -159,22 +154,22 @@ public bool RemapLinePragmaPathsOnWindows
public RazorCodeGenerationOptions WithIndentSize(int value)
=> IndentSize == value
? this
: new(LanguageVersion, FileKind, value, NewLine, RootNamespace, SuppressUniqueIds, _flags);
: new(value, NewLine, RootNamespace, SuppressUniqueIds, _flags);

public RazorCodeGenerationOptions WithNewLine(string value)
=> NewLine == value
? this
: new(LanguageVersion, FileKind, IndentSize, value, RootNamespace, SuppressUniqueIds, _flags);
: new(IndentSize, value, RootNamespace, SuppressUniqueIds, _flags);

public RazorCodeGenerationOptions WithRootNamespace(string? value)
=> RootNamespace == value
? this
: new(LanguageVersion, FileKind, IndentSize, NewLine, value, SuppressUniqueIds, _flags);
: new(IndentSize, NewLine, value, SuppressUniqueIds, _flags);

public RazorCodeGenerationOptions WithSuppressUniqueIds(string? value)
=> RootNamespace == value
? this
: new(LanguageVersion, FileKind, IndentSize, NewLine, RootNamespace, value, _flags);
: new(IndentSize, NewLine, RootNamespace, value, _flags);

public RazorCodeGenerationOptions WithFlags(
Optional<bool> designTime = default,
Expand Down Expand Up @@ -254,6 +249,6 @@ public RazorCodeGenerationOptions WithFlags(

return flags == _flags
? this
: new(LanguageVersion, FileKind, IndentSize, NewLine, RootNamespace, SuppressUniqueIds, flags);
: new(IndentSize, NewLine, RootNamespace, SuppressUniqueIds, flags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ private RazorParserOptions(
_flags = flags;
}

public static RazorParserOptions Create(RazorLanguageVersion languageVersion, string fileKind, Action<Builder>? configure = null)
{
var builder = new Builder(languageVersion, fileKind);
configure?.Invoke(builder);

return builder.ToOptions();
}

public bool DesignTime
=> _flags.IsFlagSet(Flags.DesignTime);

Expand Down
Loading