@@ -22,11 +22,22 @@ public DocumentCompiler(Lazy<BlockCompiler> blockCompiler)
22
22
23
23
public IDocumentCompilationResult Compile ( Compilation compilation , ClassDeclarationSyntax document )
24
24
{
25
- var methods = document . DescendantNodes ( )
26
- . OfType < MethodDeclarationSyntax > ( ) ;
27
- var policyDocument = new XElement ( "policies" ) ;
28
- DocumentCompilationContext context = new ( compilation , document , policyDocument ) ;
25
+ var semanticModel = compilation . GetSemanticModel ( document . SyntaxTree ) ;
26
+ var documentType = document . ExtractDocumentType ( semanticModel ) ;
27
+ var methods = document . DescendantNodes ( ) . OfType < MethodDeclarationSyntax > ( ) ;
28
+ var rootElement = new XElement ( documentType == DocumentType . Fragment ? "fragment" : "policies" ) ;
29
+ var context = new DocumentCompilationContext ( compilation , document , rootElement ) ;
29
30
31
+ if ( documentType == DocumentType . Fragment )
32
+ CompileFragment ( context , methods ) ;
33
+ else
34
+ CompilePolicy ( context , methods ) ;
35
+
36
+ return context ;
37
+ }
38
+
39
+ private void CompilePolicy ( DocumentCompilationContext context , IEnumerable < MethodDeclarationSyntax > methods )
40
+ {
30
41
foreach ( var method in methods )
31
42
{
32
43
var sectionName = method . Identifier . ValueText switch
@@ -45,12 +56,30 @@ public IDocumentCompilationResult Compile(Compilation compilation, ClassDeclarat
45
56
46
57
CompileSection ( context , sectionName , method ) ;
47
58
}
48
-
49
- return context ;
50
59
}
51
60
61
+ private void CompileFragment ( DocumentCompilationContext context , IEnumerable < MethodDeclarationSyntax > methods )
62
+ {
63
+ var fragmentMethod = methods . FirstOrDefault ( m => m . Identifier . ValueText == "Fragment" ) ;
64
+
65
+ if ( fragmentMethod != null && ValidateMethodBody ( fragmentMethod , context ) )
66
+ {
67
+ _blockCompiler . Value . Compile ( context , fragmentMethod . Body ! ) ;
68
+ }
69
+ }
52
70
53
71
private void CompileSection ( DocumentCompilationContext context , string section , MethodDeclarationSyntax method )
72
+ {
73
+ if ( ! ValidateMethodBody ( method , context ) )
74
+ return ;
75
+
76
+ var sectionElement = new XElement ( section ) ;
77
+ var sectionContext = new DocumentCompilationContext ( context , sectionElement ) ;
78
+ _blockCompiler . Value . Compile ( sectionContext , method . Body ! ) ;
79
+ context . AddPolicy ( sectionElement ) ;
80
+ }
81
+
82
+ private bool ValidateMethodBody ( MethodDeclarationSyntax method , DocumentCompilationContext context )
54
83
{
55
84
if ( method . Body is null )
56
85
{
@@ -59,12 +88,8 @@ private void CompileSection(DocumentCompilationContext context, string section,
59
88
method . GetLocation ( ) ,
60
89
method . Identifier . ValueText
61
90
) ) ;
62
- return ;
91
+ return false ;
63
92
}
64
-
65
- var sectionElement = new XElement ( section ) ;
66
- var sectionContext = new DocumentCompilationContext ( context , sectionElement ) ;
67
- _blockCompiler . Value . Compile ( sectionContext , method . Body ) ;
68
- context . AddPolicy ( sectionElement ) ;
93
+ return true ;
69
94
}
70
95
}
0 commit comments