This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
ModelDirective.cs
117 lines (98 loc) · 3.82 KB
/
ModelDirective.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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;
using Microsoft.AspNetCore.Razor.Evolution;
using Microsoft.AspNetCore.Razor.Evolution.Intermediate;
namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
public static class ModelDirective
{
public static readonly DirectiveDescriptor Directive = DirectiveDescriptorBuilder.Create("model").AddType().Build();
public static IRazorEngineBuilder Register(IRazorEngineBuilder builder)
{
builder.AddDirective(Directive);
builder.Features.Add(new Pass());
return builder;
}
public static string GetModelType(DocumentIRNode document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
var visitor = new Visitor();
return GetModelType(document, visitor);
}
private static string GetModelType(DocumentIRNode document, Visitor visitor)
{
visitor.Visit(document);
for (var i = visitor.ModelDirectives.Count - 1; i >= 0; i--)
{
var directive = visitor.ModelDirectives[i];
var tokens = directive.Tokens.ToArray();
if (tokens.Length >= 1)
{
return tokens[0].Content;
}
}
if (document.DocumentKind == RazorPageDocumentClassifierPass.RazorPageDocumentKind)
{
return visitor.Class.Name;
}
else
{
return "dynamic";
}
}
internal class Pass : RazorIRPassBase, IRazorDirectiveClassifierPass
{
// Runs after the @inherits directive
public override int Order => 5;
public override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIRNode irDocument)
{
var visitor = new Visitor();
var modelType = GetModelType(irDocument, visitor);
var baseType = visitor.Class?.BaseType?.Replace("<TModel>", "<" + modelType + ">");
for (var i = visitor.InheritsDirectives.Count - 1; i >= 0; i--)
{
var directive = visitor.InheritsDirectives[i];
var tokens = directive.Tokens.ToArray();
if (tokens.Length >= 1)
{
baseType = tokens[0].Content.Replace("<TModel>", "<" + modelType + ">");
tokens[0].Content = baseType;
break;
}
}
visitor.Class.BaseType = baseType;
}
}
private class Visitor : RazorIRNodeWalker
{
public ClassDeclarationIRNode Class { get; private set; }
public IList<DirectiveIRNode> InheritsDirectives { get; } = new List<DirectiveIRNode>();
public IList<DirectiveIRNode> ModelDirectives { get; } = new List<DirectiveIRNode>();
public override void VisitClass(ClassDeclarationIRNode node)
{
if (Class == null)
{
Class = node;
}
base.VisitClass(node);
}
public override void VisitDirective(DirectiveIRNode node)
{
if (node.Descriptor == Directive)
{
ModelDirectives.Add(node);
}
else if (node.Descriptor.Name == "inherits")
{
InheritsDirectives.Add(node);
}
}
}
}
}