11import timespec from 'timespec' ;
22import { visit } from './ast' ;
3- import { formatLocation , capitalizeClass , regexpEscape , capitalize } from './util' ;
3+ import {
4+ formatLocation ,
5+ capitalizeClass ,
6+ regexpEscape ,
7+ capitalize ,
8+ } from './util' ;
49
510const comparison = ( operator , left , right ) => {
611 if ( operator === '!=' || operator === '!~' ) {
@@ -9,19 +14,29 @@ const comparison = (operator, left, right) => {
914 return [ operator , left , right ] ;
1015} ;
1116
12- export default ( ast ) => {
17+ export default ast => {
1318 const mode = [ 'fact' ] ;
1419 return visit ( ast , {
1520 visitComparison ( path ) {
1621 this . traverse ( path ) ;
1722 // Function to handle negating comparisons
1823 if ( mode [ 0 ] === 'fact' ) {
19- return [ 'in' , 'certname' ,
20- [ 'extract' , 'certname' ,
21- [ 'select_fact_contents' ,
22- [ 'and' ,
24+ return [
25+ 'in' ,
26+ 'certname' ,
27+ [
28+ 'extract' ,
29+ 'certname' ,
30+ [
31+ 'select_fact_contents' ,
32+ [
33+ 'and' ,
2334 path . node . left ,
24- comparison ( path . node . operator , 'value' , path . node . right ) ] ] ] ] ;
35+ comparison ( path . node . operator , 'value' , path . node . right ) ,
36+ ] ,
37+ ] ,
38+ ] ,
39+ ] ;
2540 } else if ( mode [ 0 ] === 'subquery' ) {
2641 let left ;
2742 if ( path . node . left . length === 1 ) {
@@ -32,9 +47,17 @@ export default (ast) => {
3247 return comparison ( path . node . operator , left , path . node . right ) ;
3348 } else if ( mode [ 0 ] === 'resource' ) {
3449 if ( path . node . left [ 0 ] === 'tag' ) {
35- return comparison ( path . node . operator , path . node . left [ 0 ] , path . node . right ) ;
50+ return comparison (
51+ path . node . operator ,
52+ path . node . left [ 0 ] ,
53+ path . node . right ,
54+ ) ;
3655 }
37- return comparison ( path . node . operator , [ 'parameter' , path . node . left [ 0 ] ] , path . node . right ) ;
56+ return comparison (
57+ path . node . operator ,
58+ [ 'parameter' , path . node . left [ 0 ] ] ,
59+ path . node . right ,
60+ ) ;
3861 }
3962 throw Error ( `Unknown mode ${ mode } ` ) ;
4063 } ,
@@ -85,9 +108,15 @@ export default (ast) => {
85108 mode . unshift ( 'subquery' ) ;
86109 this . traverse ( path ) ;
87110 mode . shift ( ) ;
88- return [ 'in' , 'certname' ,
89- [ 'extract' , 'certname' ,
90- [ `select_${ path . node . endpoint } s` , path . node . expression ] ] ] ;
111+ return [
112+ 'in' ,
113+ 'certname' ,
114+ [
115+ 'extract' ,
116+ 'certname' ,
117+ [ `select_${ path . node . endpoint } s` , path . node . expression ] ,
118+ ] ,
119+ ] ;
91120 } ,
92121 visitRegexpNodeMatch ( path ) {
93122 mode . unshift ( 'regexp' ) ;
@@ -98,11 +127,7 @@ export default (ast) => {
98127 visitIdentifierPath ( path ) {
99128 this . traverse ( path ) ;
100129 if ( mode [ 0 ] === 'fact' ) {
101- return [
102- ( path . node . regexp ? '~>' : '=' ) ,
103- 'path' ,
104- path . node . components ,
105- ] ;
130+ return [ path . node . regexp ? '~>' : '=' , 'path' , path . node . components ] ;
106131 }
107132 return path . node . components ;
108133 } ,
@@ -116,24 +141,28 @@ export default (ast) => {
116141 return path . node . name ;
117142 } ,
118143 visitResource ( path ) {
119- const regexp = ( path . node . title . type === 'RegexpIdentifier' ) ;
144+ const regexp = path . node . title . type === 'RegexpIdentifier' ;
120145 mode . unshift ( 'resource' ) ;
121146 this . traverse ( path ) ;
122147 mode . shift ( ) ;
123148 let { title } = path . node ;
124149 if ( ! regexp && capitalize ( path . node . res_type ) === 'Class' ) {
125150 title = capitalizeClass ( title ) ;
126151 }
127- const andExpr = [ 'and' ,
128- [ '=' , 'type' , capitalizeClass ( path . node . res_type ) ] ,
129- [ ( regexp ? '~' : '=' ) , 'title' , title ] ,
130- [ '=' , 'exported' , path . node . exported ] ] ;
152+ const andExpr = [
153+ 'and' ,
154+ [ '=' , 'type' , capitalizeClass ( path . node . res_type ) ] ,
155+ [ regexp ? '~' : '=' , 'title' , title ] ,
156+ [ '=' , 'exported' , path . node . exported ] ,
157+ ] ;
131158 if ( path . node . parameters ) {
132159 andExpr . push ( path . node . parameters ) ;
133160 }
134- return [ 'in' , 'certname' ,
135- [ 'extract' , 'certname' ,
136- [ 'select_resources' , andExpr ] ] ] ;
161+ return [
162+ 'in' ,
163+ 'certname' ,
164+ [ 'extract' , 'certname' , [ 'select_resources' , andExpr ] ] ,
165+ ] ;
137166 } ,
138167 } ) ;
139168} ;
0 commit comments