-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast.js
162 lines (135 loc) · 5.04 KB
/
ast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict'
module.exports = {
createLiteral: function( value ) {
return { type: 'Literal', value: value }
},
createArray: function( elements ) {
return { type: 'ArrayExpression', elements: elements }
},
createReturn: function( argument ) {
return { type: 'ReturnStatement', argument: argument }
},
createBlockStatement: function( body ) {
return {
type: 'BlockStatement',
body: [].concat( body )
}
},
createFunction: function( body ) {
// a simple nullary function to scope the conf modules
return {
type: 'FunctionExpression',
id: null,
params: [],
defaults: [],
body: body,
rest: null,
generator: false,
expression: false
}
},
createProperty: function( key, value ) {
return {
type: 'Property',
key: { type: 'Identifier', name: key },
value: value,
kind: 'init'
}
},
createObject: function( properties ) {
return { type: 'ObjectExpression', properties: properties }
},
createSubmodule: function( hidden, returned ) {
var priv = [].concat( hidden || [] )
return this.createFunction(
this.createBlockStatement( priv.concat( this.createReturn( returned ) ) ) )
},
createModule: function( encapsulated, exports ) {
var usestrict = {
type: 'ExpressionStatement',
expression: { type: 'Literal', value: 'use strict', raw: '\'use strict\'' }
},
moduleExports = {
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'MemberExpression',
computed: false,
object: { type: 'Identifier', name: 'module' },
property: { type: 'Identifier', name: 'exports' }
},
right: exports
}
}
encapsulated = encapsulated || []
return {
type: 'Program',
body: [ usestrict ].concat( encapsulated, moduleExports )
}
},
isConfExport: function( node ) {
return node.type === 'ExpressionStatement' &&
node.expression.type === 'AssignmentExpression' &&
node.expression.left.property.name === 'exports'
},
getCombinedScopeExprs: function( programAst ) {
return this.astSelect( programAst, function( node ) {
return !( (node.type === 'ExpressionStatement' && node.expression.value === 'use strict') ||
this.isConfExport( node ) )
}.bind( this ), { depth: 1 } )
},
astSelect: function( node, test, opts, depth, selected ) {
if ( opts.depth ) { opts.minDepth = opts.maxDepth = opts.depth }
depth = depth || 0
selected = selected || []
var self = this
if ( ( !opts.minDepth || depth >= opts.minDepth ) && test( node ) ) {
selected.push( node )
}
if ( !opts.maxDepth || ( opts.maxDepth && depth < opts.maxDepth ) ) {
Object.keys( node ).forEach( function( key ) {
var val = node[ key ]
if ( Array.isArray( val ) ) {
val.forEach( function( child ) {
self.astSelect.call( self, child, test, opts, depth + 1, selected )
})
}
})
}
return selected
},
getConfSubtrees: function( programAst ) {
var confExport = this.astSelect( programAst, this.isConfExport, { depth: 1 } )[0]
.expression.right,
selectPropByName = function( tree, propName ) {
return this.astSelect( tree, function( node ) {
return node.type === 'Property' && node.key.name === propName
}, { depth: 1 } )
}.bind( this ),
selectExported = function( propName ) {
var selected = selectPropByName( confExport, propName )
return selected.length ? selected[0].value : undefined
},
globalsSubtree = selectExported( 'global' ),
machineSubtree = selectExported( 'machine' ),
viewerSubtree = selectExported( 'viewer' ),
repoSubtree = selectExported( 'repo' ),
globals
// move the globals into the separated subtrees (if there are globals)
if ( globalsSubtree ) {
globals = globalsSubtree.properties
machineSubtree.properties = globals.concat( machineSubtree.properties )
viewerSubtree.properties = globals.concat( viewerSubtree.properties )
if ( repoSubtree ) {
repoSubtree.properties = globals.concat( repoSubtree.properties )
}
}
return {
machine: machineSubtree,
viewer: viewerSubtree,
repo: repoSubtree
}
}
}