@@ -8,7 +8,6 @@ import { AstItem, RootAstItem } from '../astViewer';
88 * A class that wraps a tree of QL results from a query that
99 * has an @kind of graph
1010 */
11- // RENAME to ASTParser / ASTCreator
1211export default class AstBuilder {
1312
1413 private roots : RootAstItem [ ] | undefined ;
@@ -30,10 +29,11 @@ export default class AstBuilder {
3029 }
3130
3231 private async parseRoots ( ) : Promise < RootAstItem [ ] > {
32+ const options = { entities : [ 'id' , 'url' , 'string' ] } ;
3333 const [ nodeTuples , edgeTuples , graphProperties ] = await Promise . all ( [
34- await this . cli . bqrsDecode ( this . bqrsPath , 'nodes' ) ,
35- await this . cli . bqrsDecode ( this . bqrsPath , 'edges' ) ,
36- await this . cli . bqrsDecode ( this . bqrsPath , 'graphProperties' ) ,
34+ await this . cli . bqrsDecode ( this . bqrsPath , 'nodes' , options ) ,
35+ await this . cli . bqrsDecode ( this . bqrsPath , 'edges' , options ) ,
36+ await this . cli . bqrsDecode ( this . bqrsPath , 'graphProperties' , options ) ,
3737 ] ) ;
3838
3939 if ( ! this . isValidGraph ( graphProperties ) ) {
@@ -48,61 +48,75 @@ export default class AstBuilder {
4848
4949 // Build up the parent-child relationships
5050 edgeTuples . tuples . forEach ( tuple => {
51- const from = tuple [ 0 ] as EntityValue ;
52- const to = tuple [ 1 ] as EntityValue ;
53- const toId = to . id ! ;
54- const fromId = from . id ! ;
55-
56- if ( tuple [ 2 ] === 'semmle.order' ) {
57- astOrder . set ( toId , Number ( tuple [ 3 ] ) ) ;
58- } else if ( tuple [ 2 ] === 'semmle.label' ) {
59- childToParent . set ( toId , fromId ) ;
60- let children = parentToChildren . get ( fromId ) ;
61- if ( ! children ) {
62- parentToChildren . set ( fromId , children = [ ] ) ;
51+ const [ source , target , tupleType , orderValue ] = tuple as [ EntityValue , EntityValue , string , string ] ;
52+ const sourceId = source . id ! ;
53+ const targetId = target . id ! ;
54+
55+ switch ( tupleType ) {
56+ case 'semmle.order' :
57+ astOrder . set ( targetId , Number ( orderValue ) ) ;
58+ break ;
59+
60+ case 'semmle.label' : {
61+ childToParent . set ( targetId , sourceId ) ;
62+ let children = parentToChildren . get ( sourceId ) ;
63+ if ( ! children ) {
64+ parentToChildren . set ( sourceId , children = [ ] ) ;
65+ }
66+ children . push ( targetId ) ;
67+ break ;
6368 }
64- children . push ( toId ) ;
69+
70+ default :
71+ // ignore other tupleTypes since they are not needed by the ast viewer
6572 }
6673 } ) ;
6774
6875 // populate parents and children
6976 nodeTuples . tuples . forEach ( tuple => {
70- const entity = tuple [ 0 ] as EntityValue ;
77+ const [ entity , tupleType , orderValue ] = tuple as [ EntityValue , string , string ] ;
7178 const id = entity . id ! ;
7279
73- if ( tuple [ 1 ] === 'semmle.order' ) {
74- astOrder . set ( id , Number ( tuple [ 2 ] ) ) ;
75-
76- } else if ( tuple [ 1 ] === 'semmle.label' ) {
77- const item = {
78- id,
79- label : entity . label ,
80- location : entity . url ,
81- children : [ ] as AstItem [ ] ,
82- order : Number . MAX_SAFE_INTEGER
83- } ;
84-
85- idToItem . set ( id , item as RootAstItem ) ;
86- const parent = idToItem . get ( childToParent . get ( id ) || - 1 ) ;
87-
88- if ( parent ) {
89- const astItem = item as AstItem ;
90- astItem . parent = parent ;
91- parent . children . push ( astItem ) ;
92- }
93- const children = parentToChildren . get ( id ) || [ ] ;
94- children . forEach ( childId => {
95- const child = idToItem . get ( childId ) as AstItem | undefined ;
96- if ( child ) {
97- child . parent = item ;
98- item . children . push ( child ) ;
80+ switch ( tupleType ) {
81+ case 'semmle.order' :
82+ astOrder . set ( id , Number ( orderValue ) ) ;
83+ break ;
84+
85+ case 'semmle.label' : {
86+ const item = {
87+ id,
88+ label : entity . label ,
89+ location : entity . url ,
90+ children : [ ] as AstItem [ ] ,
91+ order : Number . MAX_SAFE_INTEGER
92+ } ;
93+
94+ idToItem . set ( id , item as RootAstItem ) ;
95+ const parent = idToItem . get ( childToParent . get ( id ) || - 1 ) ;
96+
97+ if ( parent ) {
98+ const astItem = item as AstItem ;
99+ astItem . parent = parent ;
100+ parent . children . push ( astItem ) ;
99101 }
100- } ) ;
102+ const children = parentToChildren . get ( id ) || [ ] ;
103+ children . forEach ( childId => {
104+ const child = idToItem . get ( childId ) as AstItem | undefined ;
105+ if ( child ) {
106+ child . parent = item ;
107+ item . children . push ( child ) ;
108+ }
109+ } ) ;
110+ break ;
111+ }
112+
113+ default :
114+ // ignore other tupleTypes since they are not needed by the ast viewer
101115 }
102116 } ) ;
103117
104118 // find the roots and add the order
105- for ( const [ , item ] of idToItem ) {
119+ for ( const [ , item ] of idToItem ) {
106120 item . order = astOrder . has ( item . id )
107121 ? astOrder . get ( item . id ) !
108122 : Number . MAX_SAFE_INTEGER ;
0 commit comments