@@ -9,7 +9,7 @@ import go
9
9
*
10
10
* For an AstNode to be printed, it always requires `shouldPrintFile(f)` to hold
11
11
* for its containing file `f`, and additionally requires `shouldPrintFunction(fun)`
12
- * if it is, or falls within , function `fun`.
12
+ * to hold if it is, or is a child of , function `fun`.
13
13
*/
14
14
class PrintAstConfiguration extends string {
15
15
/**
@@ -28,6 +28,12 @@ class PrintAstConfiguration extends string {
28
28
* files.
29
29
*/
30
30
predicate shouldPrintFile ( File file ) { any ( ) }
31
+
32
+ /**
33
+ * Holds if the AST for `file` should include comments. By default, holds for all
34
+ * files.
35
+ */
36
+ predicate shouldPrintComments ( File file ) { any ( ) }
31
37
}
32
38
33
39
private predicate shouldPrintFunction ( FuncDef func ) {
@@ -38,6 +44,10 @@ private predicate shouldPrintFile(File file) {
38
44
exists ( PrintAstConfiguration config | config .shouldPrintFile ( file ) )
39
45
}
40
46
47
+ private predicate shouldPrintComments ( File file ) {
48
+ exists ( PrintAstConfiguration config | config .shouldPrintComments ( file ) )
49
+ }
50
+
41
51
private FuncDef getEnclosingFunction ( AstNode n ) {
42
52
result = n or
43
53
result = n .getEnclosingFunction ( )
@@ -49,7 +59,13 @@ private FuncDef getEnclosingFunction(AstNode n) {
49
59
private newtype TPrintAstNode =
50
60
TAstNode ( AstNode ast ) {
51
61
shouldPrintFile ( ast .getFile ( ) ) and
52
- forall ( FuncDef f | f = getEnclosingFunction ( ast ) | shouldPrintFunction ( f ) )
62
+ // Do print ast nodes without an enclosing function, e.g. file headers, that are not otherwise excluded
63
+ forall ( FuncDef f | f = getEnclosingFunction ( ast ) | shouldPrintFunction ( f ) ) and
64
+ (
65
+ shouldPrintComments ( ast .getFile ( ) )
66
+ or
67
+ not ast instanceof Comment and not ast instanceof CommentGroup
68
+ )
53
69
}
54
70
55
71
/**
0 commit comments