Split options into ParserOptions and CodeGenerationOptions

This commit is contained in:
Ryan Nowak 2017-05-17 16:07:17 -07:00
parent 5a1090f324
commit e391ac7a3c
70 changed files with 408 additions and 149 deletions

View File

@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
internal RazorSourceDocument SourceDocument => CodeDocument?.Source;
internal RazorParserOptions Options { get; set; }
internal RazorCodeGenerationOptions Options { get; set; }
internal TagHelperRenderingContext TagHelperRenderingContext { get; set; }

View File

@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
public abstract class CodeTarget
{
public static CodeTarget CreateDefault(RazorCodeDocument codeDocument, RazorParserOptions options)
public static CodeTarget CreateDefault(RazorCodeDocument codeDocument, RazorCodeGenerationOptions options)
{
if (codeDocument == null)
{
@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public static CodeTarget CreateDefault(
RazorCodeDocument codeDocument,
RazorParserOptions options,
RazorCodeGenerationOptions options,
Action<ICodeTargetBuilder> configure)
{
if (codeDocument == null)
@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
var builder = new DefaultCodeTargetBuilder(codeDocument, options);
if (builder.Options.DesignTimeMode)
if (builder.Options.DesignTime)
{
AddDesignTimeDefaults(builder);
}
@ -58,7 +58,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public static CodeTarget CreateEmpty(
RazorCodeDocument codeDocument,
RazorParserOptions options,
RazorCodeGenerationOptions options,
Action<ICodeTargetBuilder> configure)
{
if (codeDocument == null)

View File

@ -1,7 +1,6 @@
// 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.Collections.Generic;
using System.Linq;
@ -9,9 +8,9 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
internal class DefaultCodeTarget : CodeTarget
{
private readonly RazorParserOptions _options;
private readonly RazorCodeGenerationOptions _options;
public DefaultCodeTarget(RazorParserOptions options, IEnumerable<ICodeTargetExtension> extensions)
public DefaultCodeTarget(RazorCodeGenerationOptions options, IEnumerable<ICodeTargetExtension> extensions)
{
_options = options;
Extensions = extensions.ToArray();

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
internal class DefaultCodeTargetBuilder : ICodeTargetBuilder
{
public DefaultCodeTargetBuilder(RazorCodeDocument codeDocument, RazorParserOptions options)
public DefaultCodeTargetBuilder(RazorCodeDocument codeDocument, RazorCodeGenerationOptions options)
{
CodeDocument = codeDocument;
Options = options;
@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public RazorCodeDocument CodeDocument { get; }
public RazorParserOptions Options { get; }
public RazorCodeGenerationOptions Options { get; }
public ICollection<ICodeTargetExtension> TargetExtensions { get; }

View File

@ -38,8 +38,8 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
_context.RenderChildren = visitor.RenderChildren;
_context.RenderNode = visitor.Visit;
_context.BasicWriter = _context.Options.DesignTimeMode ? (BasicWriter)new DesignTimeBasicWriter() : new RuntimeBasicWriter();
_context.TagHelperWriter = _context.Options.DesignTimeMode ? (TagHelperWriter)new DesignTimeTagHelperWriter() : new RuntimeTagHelperWriter();
_context.BasicWriter = _context.Options.DesignTime ? (BasicWriter)new DesignTimeBasicWriter() : new RuntimeBasicWriter();
_context.TagHelperWriter = _context.Options.DesignTime ? (TagHelperWriter)new DesignTimeTagHelperWriter() : new RuntimeTagHelperWriter();
visitor.VisitDocument(node);
_context.RenderChildren = null;

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
RazorCodeDocument CodeDocument { get; }
RazorParserOptions Options { get; }
RazorCodeGenerationOptions Options { get; }
ICollection<ICodeTargetExtension> TargetExtensions { get; }

View File

@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Razor.Language
{
var parserOptions = irDocument.Options;
var designTime = parserOptions.DesignTimeMode;
var designTime = parserOptions.DesignTime;
var walker = new DirectiveWalker();
walker.VisitDocument(irDocument);

View File

@ -11,9 +11,11 @@ namespace Microsoft.AspNetCore.Razor.Language
private readonly string _generatedCode;
private readonly RazorDiagnostic[] _diagnostics;
private readonly LineMapping[] _lineMappings;
private readonly RazorCodeGenerationOptions _options;
public DefaultRazorCSharpDocument(
string generatedCode,
RazorCodeGenerationOptions options,
RazorDiagnostic[] diagnostics,
LineMapping[] lineMappings)
{
@ -22,7 +24,14 @@ namespace Microsoft.AspNetCore.Razor.Language
throw new ArgumentNullException(nameof(generatedCode));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
_generatedCode = generatedCode;
_options = options;
_diagnostics = diagnostics ?? Array.Empty<RazorDiagnostic>();
_lineMappings = lineMappings ?? Array.Empty<LineMapping>();
}
@ -32,5 +41,7 @@ namespace Microsoft.AspNetCore.Razor.Language
public override string GeneratedCode => _generatedCode;
public override IReadOnlyList<LineMapping> LineMappings => _lineMappings;
public override RazorCodeGenerationOptions Options => _options;
}
}

View File

@ -70,6 +70,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var csharpDocument = RazorCSharpDocument.Create(
renderingContext.Writer.GenerateCode(),
irDocument.Options,
diagnostics,
renderingContext.LineMappings);
codeDocument.SetCSharpDocument(csharpDocument);

View File

@ -0,0 +1,23 @@
// 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;
namespace Microsoft.AspNetCore.Razor.Language
{
internal class DefaultRazorCodeGenerationOptions : RazorCodeGenerationOptions
{
public DefaultRazorCodeGenerationOptions(bool indentWithTabs, int indentSize, bool designTime)
{
IndentWithTabs = indentWithTabs;
IndentSize = indentSize;
DesignTime = designTime;
}
public override bool DesignTime { get; }
public override bool IndentWithTabs { get; }
public override int IndentSize { get; }
}
}

View File

@ -0,0 +1,19 @@
// 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.
namespace Microsoft.AspNetCore.Razor.Language
{
internal class DefaultRazorCodeGenerationOptionsBuilder : RazorCodeGenerationOptionsBuilder
{
public override bool DesignTime { get; set; }
public override int IndentSize { get; set; } = 4;
public override bool IndentWithTabs { get; set; }
public override RazorCodeGenerationOptions Build()
{
return new DefaultRazorCodeGenerationOptions(IndentWithTabs, IndentSize, DesignTime);
}
}
}

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Razor.Language
public int Order => 100;
void IRazorParserOptionsFeature.Configure(RazorParserOptions options)
void IRazorParserOptionsFeature.Configure(RazorParserOptionsBuilder options)
{
if (options == null)
{

View File

@ -13,6 +13,13 @@ namespace Microsoft.AspNetCore.Razor.Language
{
internal class DefaultRazorIRLoweringPhase : RazorEnginePhaseBase, IRazorIRLoweringPhase
{
private IRazorCodeGenerationOptionsFeature[] _optionsCallbacks;
protected override void OnIntialized()
{
_optionsCallbacks = Engine.Features.OfType<IRazorCodeGenerationOptionsFeature>().OrderBy(f => f.Order).ToArray();
}
protected override void ExecuteCore(RazorCodeDocument codeDocument)
{
var syntaxTree = codeDocument.GetSyntaxTree();
@ -24,7 +31,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var document = new DocumentIRNode();
var builder = RazorIRBuilder.Create(document);
document.Options = syntaxTree.Options;
document.Options = CreateCodeGenerationOptions();
var checksum = ChecksumIRNode.Create(codeDocument.Source);
builder.Insert(0, checksum);
@ -72,6 +79,17 @@ namespace Microsoft.AspNetCore.Razor.Language
codeDocument.SetIRDocument(document);
}
private RazorCodeGenerationOptions CreateCodeGenerationOptions()
{
var builder = new DefaultRazorCodeGenerationOptionsBuilder();
for (var i = 0; i < _optionsCallbacks.Length; i++)
{
_optionsCallbacks[i].Configure(builder);
}
return builder.Build();
}
private class LoweringVisitor : ParserVisitor
{
protected readonly RazorIRBuilder _builder;

View File

@ -0,0 +1,29 @@
// 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.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Language
{
internal class DefaultRazorParserOptions : RazorParserOptions
{
public DefaultRazorParserOptions(DirectiveDescriptor[] directives, bool designTime, bool parseOnlyLeadingDirectives)
{
if (directives == null)
{
throw new ArgumentNullException(nameof(directives));
}
Directives = directives;
DesignTime = designTime;
ParseOnlyLeadingDirectives = parseOnlyLeadingDirectives;
}
public override bool DesignTime { get; }
public override IReadOnlyCollection<DirectiveDescriptor> Directives { get; }
public override bool ParseOnlyLeadingDirectives { get; }
}
}

View File

@ -0,0 +1,22 @@
// 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.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Language
{
internal class DefaultRazorParserOptionsBuilder : RazorParserOptionsBuilder
{
public override bool DesignTime { get; set; }
public override ICollection<DirectiveDescriptor> Directives { get; } = new List<DirectiveDescriptor>();
public override bool ParseOnlyLeadingDirectives { get; set; }
public override RazorParserOptions Build()
{
return new DefaultRazorParserOptions(Directives.ToArray(), DesignTime, ParseOnlyLeadingDirectives);
}
}
}

View File

@ -16,12 +16,14 @@ namespace Microsoft.AspNetCore.Razor.Language
protected override void ExecuteCore(RazorCodeDocument codeDocument)
{
var options = RazorParserOptions.CreateDefaultOptions();
var builder = new DefaultRazorParserOptionsBuilder();
for (var i = 0; i < _parserOptionsCallbacks.Length; i++)
{
_parserOptionsCallbacks[i].Configure(options);
_parserOptionsCallbacks[i].Configure(builder);
}
var options = builder.Build();
var syntaxTree = RazorSyntaxTree.Parse(codeDocument.Source, options);
codeDocument.SetSyntaxTree(syntaxTree);

View File

@ -0,0 +1,32 @@
// 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;
namespace Microsoft.AspNetCore.Razor.Language
{
internal class DesignTimeOptionsFeature : RazorEngineFeatureBase, IRazorParserOptionsFeature, IRazorCodeGenerationOptionsFeature
{
public int Order { get; set; }
public void Configure(RazorParserOptionsBuilder options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
options.DesignTime = true;
}
public void Configure(RazorCodeGenerationOptionsBuilder options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
options.DesignTime = true;
}
}
}

View File

@ -1,15 +0,0 @@
// 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.
namespace Microsoft.AspNetCore.Razor.Language
{
internal class DesignTimeParserOptionsFeature : RazorEngineFeatureBase, IRazorParserOptionsFeature
{
public int Order { get; set; }
public void Configure(RazorParserOptions options)
{
options.DesignTimeMode = true;
}
}
}

View File

@ -19,7 +19,6 @@ namespace Microsoft.AspNetCore.Razor.Language
protected override void OnInitialized()
{
var feature = Engine.Features.OfType<IRazorTargetExtensionFeature>();
TargetExtensions = feature.FirstOrDefault()?.TargetExtensions.ToArray() ?? EmptyExtensionArray;
}
@ -82,7 +81,7 @@ namespace Microsoft.AspNetCore.Razor.Language
protected abstract bool IsMatch(RazorCodeDocument codeDocument, DocumentIRNode irDocument);
private CodeTarget CreateTarget(RazorCodeDocument codeDocument, RazorParserOptions options)
private CodeTarget CreateTarget(RazorCodeDocument codeDocument, RazorCodeGenerationOptions options)
{
return CodeTarget.CreateDefault(codeDocument, options, (builder) =>
{

View File

@ -28,14 +28,14 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: TemplateWriterName))
{
if (!context.Options.DesignTimeMode)
if (!context.Options.DesignTime)
{
context.Writer.WriteMethodInvocation(PushWriterMethod, TemplateWriterName);
}
context.RenderChildren(node);
if (!context.Options.DesignTimeMode)
if (!context.Options.DesignTime)
{
context.Writer.WriteMethodInvocation(PopWriterMethod);
}

View File

@ -0,0 +1,12 @@
// 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.
namespace Microsoft.AspNetCore.Razor.Language
{
public interface IRazorCodeGenerationOptionsFeature : IRazorEngineFeature
{
int Order { get; }
void Configure(RazorCodeGenerationOptionsBuilder options);
}
}

View File

@ -7,6 +7,6 @@ namespace Microsoft.AspNetCore.Razor.Language
{
int Order { get; }
void Configure(RazorParserOptions options);
void Configure(RazorParserOptionsBuilder options);
}
}

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class AddTagHelperHtmlAttributeIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = new List<RazorIRNode>();

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class CSharpAttributeValueIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = new List<RazorIRNode>();

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class ChecksumIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children => EmptyArray;

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class CreateTagHelperIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = EmptyArray;

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class DeclareTagHelperFieldsIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = EmptyArray;

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class DirectiveTokenIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = EmptyArray;

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
public string DocumentKind { get; set; }
public RazorParserOptions Options { get; set; }
public RazorCodeGenerationOptions Options { get; set; }
public override RazorIRNode Parent { get; set; }

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class ExecuteTagHelpersIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = new List<RazorIRNode>();

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class HtmlAttributeIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = new List<RazorIRNode>();

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class HtmlAttributeValueIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = EmptyArray;

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class HtmlContentIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = new List<RazorIRNode>();

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class InitializeTagHelperStructureIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = new List<RazorIRNode>();

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class RazorIRToken : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children => RazorIRNode.EmptyArray;

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class SetTagHelperPropertyIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = new List<RazorIRNode>();

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
public sealed class UsingStatementIRNode : RazorIRNode
{
public override ItemCollection Annotations => ReadonlyItemCollection.Empty;
public override ItemCollection Annotations => ReadOnlyItemCollection.Empty;
public override IList<RazorIRNode> Children { get; } = EmptyArray;

View File

@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
}
public CSharpCodeParser(IEnumerable<DirectiveDescriptor> directiveDescriptors, ParserContext context)
: base(context.StopParsingAfterFirstDirective ? FirstDirectiveCSharpLanguageCharacteristics.Instance : CSharpLanguageCharacteristics.Instance, context)
: base(context.ParseOnlyLeadingDirectives ? FirstDirectiveCSharpLanguageCharacteristics.Instance : CSharpLanguageCharacteristics.Instance, context)
{
Keywords = new HashSet<string>();
SetUpKeywords();

View File

@ -76,17 +76,17 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
var basePadding = CalculatePadding();
var resolvedPadding = Math.Max(basePadding - offset, 0);
if (context.Options.IsIndentingWithTabs)
if (context.Options.IndentWithTabs)
{
// Avoid writing directly to the StringBuilder here, that will throw off the manual indexing
// done by the base class.
var tabs = resolvedPadding / context.Options.TabSize;
var tabs = resolvedPadding / context.Options.IndentSize;
for (var i = 0; i < tabs; i++)
{
Write("\t");
}
var spaces = resolvedPadding % context.Options.TabSize;
var spaces = resolvedPadding % context.Options.IndentSize;
for (var i = 0; i < spaces; i++)
{
Write(" ");
@ -114,7 +114,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
}
else if (@char == '\t')
{
spaceCount += context.Options.TabSize;
spaceCount += context.Options.IndentSize;
}
else
{

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
};
public HtmlMarkupParser(ParserContext context)
: base(context.StopParsingAfterFirstDirective ? FirstDirectiveHtmlLanguageCharacteristics.Instance : HtmlLanguageCharacteristics.Instance, context)
: base(context.ParseOnlyLeadingDirectives ? FirstDirectiveHtmlLanguageCharacteristics.Instance : HtmlLanguageCharacteristics.Instance, context)
{
}

View File

@ -17,8 +17,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
}
Source = source;
DesignTimeMode = options.DesignTimeMode;
StopParsingAfterFirstDirective = options.StopParsingAfterFirstDirective;
DesignTimeMode = options.DesignTime;
ParseOnlyLeadingDirectives = options.ParseOnlyLeadingDirectives;
Builder = new SyntaxTreeBuilder();
ErrorSink = new ErrorSink();
}
@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
public bool DesignTimeMode { get; }
public bool StopParsingAfterFirstDirective { get; }
public bool ParseOnlyLeadingDirectives { get; }
public bool WhiteSpaceIsSignificantToAncestorBlock { get; set; }

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
internal class RazorParser
{
public RazorParser()
: this(RazorParserOptions.CreateDefaultOptions())
: this(RazorParserOptions.CreateDefault())
{
}

View File

@ -15,23 +15,31 @@ namespace Microsoft.AspNetCore.Razor.Language
public abstract IReadOnlyList<RazorDiagnostic> Diagnostics { get; }
public static RazorCSharpDocument Create(string generatedCode, IEnumerable<RazorDiagnostic> diagnostics)
public abstract RazorCodeGenerationOptions Options { get; }
public static RazorCSharpDocument Create(string generatedCode, RazorCodeGenerationOptions options, IEnumerable<RazorDiagnostic> diagnostics)
{
if (generatedCode == null)
{
throw new ArgumentNullException(nameof(generatedCode));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (diagnostics == null)
{
throw new ArgumentNullException(nameof(diagnostics));
}
return new DefaultRazorCSharpDocument(generatedCode, diagnostics.ToArray(), lineMappings: null);
return new DefaultRazorCSharpDocument(generatedCode, options, diagnostics.ToArray(), lineMappings: null);
}
public static RazorCSharpDocument Create(
string generatedCode,
RazorCodeGenerationOptions options,
IEnumerable<RazorDiagnostic> diagnostics,
IEnumerable<LineMapping> lineMappings)
{
@ -40,6 +48,11 @@ namespace Microsoft.AspNetCore.Razor.Language
throw new ArgumentNullException(nameof(generatedCode));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (diagnostics == null)
{
throw new ArgumentNullException(nameof(diagnostics));
@ -50,7 +63,7 @@ namespace Microsoft.AspNetCore.Razor.Language
throw new ArgumentNullException(nameof(lineMappings));
}
return new DefaultRazorCSharpDocument(generatedCode, diagnostics.ToArray(), lineMappings.ToArray());
return new DefaultRazorCSharpDocument(generatedCode, options, diagnostics.ToArray(), lineMappings.ToArray());
}
}
}

View File

@ -0,0 +1,24 @@
// 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.
namespace Microsoft.AspNetCore.Razor.Language
{
public abstract class RazorCodeGenerationOptions
{
public static RazorCodeGenerationOptions Create(bool indentWithTabs, int indentSize, bool designTime)
{
return new DefaultRazorCodeGenerationOptions(indentWithTabs, indentSize, designTime);
}
public static RazorCodeGenerationOptions CreateDefault()
{
return new DefaultRazorCodeGenerationOptions(indentWithTabs: false, indentSize: 4, designTime: false);
}
public abstract bool DesignTime { get; }
public abstract bool IndentWithTabs { get; }
public abstract int IndentSize { get; }
}
}

View File

@ -0,0 +1,16 @@
// 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.
namespace Microsoft.AspNetCore.Razor.Language
{
public abstract class RazorCodeGenerationOptionsBuilder
{
public abstract bool DesignTime { get; set; }
public abstract int IndentSize { get; set; }
public abstract bool IndentWithTabs { get; set; }
public abstract RazorCodeGenerationOptions Build();
}
}

View File

@ -108,7 +108,7 @@ namespace Microsoft.AspNetCore.Razor.Language
internal static void AddDesignTimeDefaults(IRazorEngineBuilder builder)
{
// Configure options
builder.Features.Add(new DesignTimeParserOptionsFeature());
builder.Features.Add(new DesignTimeOptionsFeature());
// IR Passes
builder.Features.Add(new RazorDesignTimeIRPass());

View File

@ -1,30 +1,43 @@
// 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.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Language
{
public sealed class RazorParserOptions
public abstract class RazorParserOptions
{
public static RazorParserOptions CreateDefaultOptions()
public static RazorParserOptions Create(IEnumerable<DirectiveDescriptor> directives, bool designTime)
{
return new RazorParserOptions();
if (directives == null)
{
throw new ArgumentNullException(nameof(directives));
}
return new DefaultRazorParserOptions(directives.ToArray(), designTime, parseOnlyLeadingDirectives: false);
}
private RazorParserOptions()
public static RazorParserOptions Create(IEnumerable<DirectiveDescriptor> directives, bool designTime, bool parseOnlyLeadingDirectives)
{
Directives = new List<DirectiveDescriptor>();
if (directives == null)
{
throw new ArgumentNullException(nameof(directives));
}
return new DefaultRazorParserOptions(directives.ToArray(), designTime, parseOnlyLeadingDirectives);
}
public bool DesignTimeMode { get; set; }
public static RazorParserOptions CreateDefault()
{
return new DefaultRazorParserOptions(Array.Empty<DirectiveDescriptor>(), designTime: false, parseOnlyLeadingDirectives: false);
}
public int TabSize { get; set; } = 4;
public abstract bool DesignTime { get; }
public bool IsIndentingWithTabs { get; set; }
public abstract IReadOnlyCollection<DirectiveDescriptor> Directives { get; }
public bool StopParsingAfterFirstDirective { get; set; }
public ICollection<DirectiveDescriptor> Directives { get; }
public abstract bool ParseOnlyLeadingDirectives { get; }
}
}

View File

@ -0,0 +1,18 @@
// 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.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Language
{
public abstract class RazorParserOptionsBuilder
{
public abstract bool DesignTime { get; set; }
public abstract ICollection<DirectiveDescriptor> Directives { get; }
public abstract bool ParseOnlyLeadingDirectives { get; set; }
public abstract RazorParserOptions Build();
}
}

View File

@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.Razor.Language
throw new ArgumentNullException(nameof(source));
}
var parser = new RazorParser(options ?? RazorParserOptions.CreateDefaultOptions());
var parser = new RazorParser(options ?? RazorParserOptions.CreateDefault());
return parser.Parse(source);
}

View File

@ -5,9 +5,9 @@ using System;
namespace Microsoft.AspNetCore.Razor.Language
{
internal class ReadonlyItemCollection : ItemCollection
internal class ReadOnlyItemCollection : ItemCollection
{
public static readonly ItemCollection Empty = new ReadonlyItemCollection();
public static readonly ItemCollection Empty = new ReadOnlyItemCollection();
public override object this[object key]
{

View File

@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
// Act
var target = CodeTarget.CreateDefault(codeDocument, options);
@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
Action<ICodeTargetBuilder> @delegate = (b) => { wasCalled = true; };
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
// Act
CodeTarget.CreateDefault(codeDocument, options, @delegate);
@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
// Act
CodeTarget.CreateDefault(codeDocument, options, configure: null);
@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
// Act
CodeTarget.CreateDefault(codeDocument, options, configure: null);

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var builder = new DefaultCodeTargetBuilder(codeDocument, options);

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public void Constructor_CreatesDefensiveCopy()
{
// Arrange
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var extensions = new ICodeTargetExtension[]
{
@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public void CreateWriter_CreatesDefaultDocumentWriter()
{
// Arrange
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var target = new DefaultCodeTarget(options, Enumerable.Empty<ICodeTargetExtension>());
@ -46,7 +46,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public void HasExtension_ReturnsTrue_WhenExtensionFound()
{
// Arrange
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var extensions = new ICodeTargetExtension[]
{
@ -67,7 +67,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public void HasExtension_ReturnsFalse_WhenExtensionNotFound()
{
// Arrange
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var extensions = new ICodeTargetExtension[]
{
@ -88,7 +88,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public void GetExtension_ReturnsExtension_WhenExtensionFound()
{
// Arrange
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var extensions = new ICodeTargetExtension[]
{
@ -109,7 +109,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public void GetExtension_ReturnsFirstMatch_WhenExtensionFound()
{
// Arrange
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var extensions = new ICodeTargetExtension[]
{
@ -133,7 +133,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public void GetExtension_ReturnsNull_WhenExtensionNotFound()
{
// Arrange
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var extensions = new ICodeTargetExtension[]
{

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var target = CodeTarget.CreateDefault(codeDocument, options);
var context = new CSharpRenderingContext()
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var target = CodeTarget.CreateDefault(codeDocument, options);
var context = new CSharpRenderingContext()
@ -91,7 +91,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var target = CodeTarget.CreateDefault(codeDocument, options);
var context = new CSharpRenderingContext()
@ -132,7 +132,7 @@ internal virtual async string TestMethod()
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var target = CodeTarget.CreateDefault(codeDocument, options);
var context = new CSharpRenderingContext()
@ -169,7 +169,7 @@ internal virtual async string TestMethod()
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var target = CodeTarget.CreateDefault(codeDocument, options);
var context = new CSharpRenderingContext()

View File

@ -115,7 +115,7 @@ using System;
var context = new CSharpRenderingContext()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
Writer = new Legacy.CSharpCodeWriter(),
};
@ -194,7 +194,7 @@ __o = i++;
var context = new CSharpRenderingContext()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
CodeDocument = RazorCodeDocument.Create(sourceDocument),
Writer = new Legacy.CSharpCodeWriter(),
};
@ -270,7 +270,7 @@ __o = i++;
var context = new CSharpRenderingContext()
{
Writer = new Legacy.CSharpCodeWriter(),
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var node = new CSharpStatementIRNode()
@ -336,7 +336,7 @@ __o = i++;
var context = new CSharpRenderingContext()
{
Writer = new Legacy.CSharpCodeWriter(),
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var node = new CSharpStatementIRNode()
@ -375,7 +375,7 @@ if (true) { }
var context = new CSharpRenderingContext()
{
Writer = new Legacy.CSharpCodeWriter(),
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var node = new CSharpStatementIRNode()

View File

@ -195,7 +195,7 @@ __InputTagHelper.FooProp[""bound""] = 42;
private static CSharpRenderingContext GetCSharpRenderingContext(TagHelperWriter writer, RazorCodeDocument codeDocument = null)
{
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var codeWriter = new Legacy.CSharpCodeWriter();
var context = new CSharpRenderingContext()
{

View File

@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
var context = new CSharpRenderingContext()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
Writer = new Legacy.CSharpCodeWriter(),
};

View File

@ -169,7 +169,7 @@ using System;
var context = new CSharpRenderingContext()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
Writer = new Legacy.CSharpCodeWriter(),
};
@ -254,7 +254,7 @@ Test(i++);
var context = new CSharpRenderingContext()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
CodeDocument = RazorCodeDocument.Create(sourceDocument),
Writer = new Legacy.CSharpCodeWriter(),
};
@ -361,7 +361,7 @@ Test(i++);
var context = new CSharpRenderingContext()
{
Writer = new Legacy.CSharpCodeWriter(),
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var node = new CSharpStatementIRNode()
@ -400,7 +400,7 @@ if (true) { }
var context = new CSharpRenderingContext()
{
Writer = new Legacy.CSharpCodeWriter(),
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var node = new CSharpStatementIRNode()
@ -437,7 +437,7 @@ if (true) { }
var context = new CSharpRenderingContext()
{
Writer = new Legacy.CSharpCodeWriter(),
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var node = new HtmlContentIRNode();
@ -467,7 +467,7 @@ if (true) { }
var context = new CSharpRenderingContext()
{
Writer = new Legacy.CSharpCodeWriter(),
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
@ -601,7 +601,7 @@ WriteAttributeValue("" "", 27, false, 28, 6, false);
private static CSharpRenderingContext GetCSharpRenderingContext(BasicWriter writer)
{
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var codeWriter = new Legacy.CSharpCodeWriter();
var context = new CSharpRenderingContext()
{

View File

@ -430,7 +430,7 @@ __tagHelperExecutionContext.AddTagHelperAttribute(""foo-bound"", __InputTagHelpe
private static CSharpRenderingContext GetCSharpRenderingContext(TagHelperWriter writer, RazorCodeDocument codeDocument = null)
{
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var codeWriter = new Legacy.CSharpCodeWriter();
var context = new CSharpRenderingContext()
{

View File

@ -92,7 +92,7 @@ AddHtmlAttributeValue("" "", 27, false, 28, 6, false);
private static CSharpRenderingContext GetCSharpRenderingContext(BasicWriter writer)
{
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var codeWriter = new Legacy.CSharpCodeWriter();
var context = new CSharpRenderingContext()
{

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var irDocument = new DocumentIRNode()
{
DocumentKind = "ignore",
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var pass = new DefaultDocumentClassifierPass();
@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var pass = new DefaultDocumentClassifierPass();

View File

@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var engine = RazorEngine.CreateEmpty(b => b.Phases.Add(phase));
var codeDocument = TestRazorCodeDocument.Create("<p class=@(");
codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source));
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var irDocument = new DocumentIRNode()
{
DocumentKind = "test",
@ -116,7 +116,7 @@ namespace Microsoft.AspNetCore.Razor.Language
RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("@ ")),
RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("<p @(")),
});
var options = RazorParserOptions.CreateDefaultOptions();
var options = RazorCodeGenerationOptions.CreateDefault();
var irDocument = new DocumentIRNode()
{
DocumentKind = "test",

View File

@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Xunit;
using static Microsoft.AspNetCore.Razor.Language.Intermediate.RazorIRAssert;
using Moq;
namespace Microsoft.AspNetCore.Razor.Language
{
@ -27,7 +28,7 @@ namespace Microsoft.AspNetCore.Razor.Language
}
[Fact]
public void Lower_SetsOptions()
public void Lower_SetsOptions_Defaults()
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
@ -37,7 +38,38 @@ namespace Microsoft.AspNetCore.Razor.Language
// Assert
Assert.NotNull(irDocument.Options);
Assert.Same(codeDocument.GetSyntaxTree().Options, irDocument.Options);
Assert.False(irDocument.Options.DesignTime);
Assert.Equal(4, irDocument.Options.IndentSize);
Assert.False(irDocument.Options.IndentWithTabs);
}
[Fact]
public void Lower_SetsOptions_RunsConfigureCallbacks()
{
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var callback = new Mock<IRazorCodeGenerationOptionsFeature>();
callback
.Setup(c => c.Configure(It.IsAny<RazorCodeGenerationOptionsBuilder>()))
.Callback<RazorCodeGenerationOptionsBuilder>(o =>
{
o.DesignTime = true;
o.IndentSize = 17;
o.IndentWithTabs = true;
});
// Act
var irDocument = Lower(codeDocument, builder: b =>
{
b.Features.Add(callback.Object);
});
// Assert
Assert.NotNull(irDocument.Options);
Assert.True(irDocument.Options.DesignTime);
Assert.Equal(17, irDocument.Options.IndentSize);
Assert.True(irDocument.Options.IndentWithTabs);
}
[Fact]

View File

@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Razor.Language
{
public int Order { get; }
public void Configure(RazorParserOptions options)
public void Configure(RazorParserOptionsBuilder options)
{
options.Directives.Add(DirectiveDescriptor.CreateDirective("test", DirectiveKind.SingleLine));
}

View File

@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var irDocument = new DocumentIRNode()
{
DocumentKind = "ignore",
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var pass = new TestDocumentClassifierPass();
@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var pass = new TestDocumentClassifierPass()
@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var expected = new ICodeTargetExtension[]
@ -98,7 +98,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var pass = new TestDocumentClassifierPass();
@ -123,7 +123,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var builder = RazorIRBuilder.Create(irDocument);
@ -148,7 +148,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var builder = RazorIRBuilder.Create(irDocument);
@ -174,7 +174,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var builder = RazorIRBuilder.Create(irDocument);
@ -201,7 +201,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var builder = RazorIRBuilder.Create(irDocument);
@ -230,7 +230,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var builder = RazorIRBuilder.Create(irDocument);
@ -265,7 +265,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var irDocument = new DocumentIRNode()
{
Options = RazorParserOptions.CreateDefaultOptions(),
Options = RazorCodeGenerationOptions.CreateDefault(),
};
var builder = RazorIRBuilder.Create(irDocument);

View File

@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
BasicWriter = new RuntimeBasicWriter(),
TagHelperWriter = new RuntimeTagHelperWriter(),
Writer = new CSharpCodeWriter(),
Options = RazorParserOptions.CreateDefaultOptions()
Options = RazorCodeGenerationOptions.CreateDefault(),
};
context.RenderChildren = (n) =>

View File

@ -37,8 +37,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
var source = TestRazorSourceDocument.Create(document);
var reader = new SeekableTextReader(document, filePath: null);
var options = RazorParserOptions.CreateDefaultOptions();
options.DesignTimeMode = designTime;
var options = RazorParserOptions.Create(Array.Empty<DirectiveDescriptor>(), designTime);
var context = new ParserContext(reader, options);
var codeParser = new CSharpCodeParser(context);
@ -69,8 +68,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
using (var reader = new SeekableTextReader(document, filePath: null))
{
var options = RazorParserOptions.CreateDefaultOptions();
options.DesignTimeMode = designTime;
var options = RazorParserOptions.Create(Array.Empty<DirectiveDescriptor>(), designTime);
var context = new ParserContext(reader, options);
var parser = new HtmlMarkupParser(context);
@ -102,8 +100,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
using (var reader = new SeekableTextReader(document, filePath: null))
{
var options = RazorParserOptions.CreateDefaultOptions();
options.DesignTimeMode = designTime;
var options = RazorParserOptions.Create(descriptors, designTime);
var context = new ParserContext(reader, options);
var parser = new CSharpCodeParser(descriptors, context);
@ -117,12 +114,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
var root = context.Builder.Build();
var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error));
options.Directives.Clear();
foreach (var directive in descriptors)
{
options.Directives.Add(directive);
}
return RazorSyntaxTree.Create(root, source, diagnostics, options);
}
}

View File

@ -93,7 +93,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var expected = RazorCSharpDocument.Create("", Array.Empty<RazorDiagnostic>());
var expected = RazorCSharpDocument.Create("", RazorCodeGenerationOptions.CreateDefault(), Array.Empty<RazorDiagnostic>());
codeDocument.Items[typeof(RazorCSharpDocument)] = expected;
// Act
@ -109,7 +109,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var expected = RazorCSharpDocument.Create("", Array.Empty<RazorDiagnostic>());
var expected = RazorCSharpDocument.Create("", RazorCodeGenerationOptions.CreateDefault(), Array.Empty<RazorDiagnostic>());
// Act
codeDocument.SetCSharpDocument(expected);

View File

@ -182,7 +182,7 @@ namespace Microsoft.AspNetCore.Razor.Language
feature => Assert.IsType<DefaultDirectiveIRPass>(feature),
feature => Assert.IsType<DirectiveRemovalIROptimizationPass>(feature),
feature => Assert.IsType<DefaultDocumentClassifierPassFeature>(feature),
feature => Assert.IsType<DesignTimeParserOptionsFeature>(feature),
feature => Assert.IsType<DesignTimeOptionsFeature>(feature),
feature => Assert.IsType<RazorDesignTimeIRPass>(feature));
}

View File

@ -1,6 +1,7 @@
// 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.Collections.Generic;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Xunit;
@ -62,8 +63,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Test
{
// Arrange
var source = TestRazorSourceDocument.Create("\r\n \r\n @*SomeComment*@ \r\n @tagHelperPrefix \"SomePrefix\"\r\n<html>\r\n@if (true) {\r\n @if(false) { <div>@something.</div> } \r\n}");
var options = RazorParserOptions.CreateDefaultOptions();
options.StopParsingAfterFirstDirective = true;
var options = RazorParserOptions.Create(Array.Empty<DirectiveDescriptor>(), designTime: false, parseOnlyLeadingDirectives: true);
// Act
var syntaxTree = RazorSyntaxTree.Parse(source, options);