@@ -8,71 +8,53 @@ import type { Transformer } from '../transform';
88// Note that the validator should complain if it see anything other than export default []
99// What is the relationship between the validator and the compiler?
1010
11- export type ExtendedProgram = NodePath < namedTypes . Program > & {
12- operations : Array < { line : number ; name : string ; order : number } > ;
13- } ;
11+ export type ExtendedProgram = NodePath <
12+ namedTypes . Program & {
13+ operations : Array < { line : number ; name : string ; order : number } > ;
14+ }
15+ > ;
1416
1517export type TopLevelOpsOptions = {
1618 // Wrap operations in a `(state) => op` wrapper
1719 wrap : boolean ; // TODO
1820} ;
1921
20- function visitor ( path : NodePath < namedTypes . CallExpression > ) {
21- const root = path . parent . parent . node ;
22- // Check if the node is a top level Operation
22+ function visitor ( programPath : ExtendedProgram ) {
23+ const operations : Array < { line : number ; name : string ; order : number } > = [ ] ;
24+ const children = programPath . node . body ;
25+ const rem = [ ] ;
26+
27+ const target = programPath . node . body . at ( - 1 ) ;
2328 if (
24- // Check this is a top level call expression
25- // ie, the parent must be an ExpressionStatement, and the statement's parent must be a Program
26- n . Program . check ( root ) &&
27- n . Statement . check ( path . parent . node ) &&
28- // An Operation could be an Identifier (fn()) or Member Expression (http.get())
29- ( n . Identifier . check ( path . node . callee ) ||
30- n . MemberExpression . check ( path . node . callee ) )
29+ n . ExportDefaultDeclaration . check ( target ) &&
30+ n . ArrayExpression . check ( target . declaration )
3131 ) {
32- appendOperationMetadata ( path , root as unknown as ExtendedProgram ) ;
33-
34- // Now Find the top level exports array
35- const target = root . body . at ( - 1 ) ;
36- if (
37- n . ExportDefaultDeclaration . check ( target ) &&
38- n . ArrayExpression . check ( target . declaration )
39- ) {
40- const statement = path . parent ;
41- target . declaration . elements . push ( path . node ) ;
42- // orphan the original expression statement parent
43- statement . prune ( ) ;
44- } else {
45- // error! there isn't an appropriate export statement
46- // What do we do?
32+ for ( const child of children ) {
33+ if (
34+ n . ExpressionStatement . check ( child ) &&
35+ n . CallExpression . check ( child . expression )
36+ ) {
37+ const order = operations . length + 1 ;
38+ // @ts -ignore
39+ const name = child . expression . callee . name ;
40+ const line = child . expression . loc ?. start . line ?? - 1 ;
41+ operations . push ( { name, line, order } ) ;
42+ target . declaration . elements . push ( child . expression as any ) ;
43+ } else rem . push ( child ) ;
4744 }
45+ programPath . node . body = rem ;
46+ } else {
47+ // error! there isn't an appropriate export statement
48+ // What do we do?
4849 }
50+ programPath . node . operations = operations ;
4951
5052 // if not (for now) we should cancel traversal
5153 return true ;
5254}
5355
54- // Add metadata to each operation for:
55- // - the order (do we know? We can guess from the state of the exports array
56- // - the name of the operation
57- // - the original line number (do we know?)
58- function appendOperationMetadata (
59- path : NodePath < namedTypes . CallExpression > ,
60- root : ExtendedProgram
61- ) {
62- if ( ! root . operations ) {
63- root . operations = [ ] ;
64- }
65-
66- const { operations } = root ;
67- const order = operations . length + 1 ;
68- const name = path . value . callee . name ;
69- const line = path . value . loc ?. start . line ?? - 1 ;
70-
71- operations . push ( { line, order, name } ) ;
72- }
73-
7456export default {
7557 id : 'top-level-operations' ,
76- types : [ 'CallExpression ' ] ,
58+ types : [ 'Program ' ] ,
7759 visitor,
78- } as Transformer ;
60+ } as unknown as Transformer ;
0 commit comments