/
parse.js
214 lines (183 loc) · 4.84 KB
/
parse.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*jslint plusplus: false, strict: false */
/*global define: false */
/**
* An override for the jslib/parse.js to convert stealjs calls into
* require/define calls.
*/
define(['../../jslib/parse'], function (baseParse) {
var parse = baseParse,
allowedCalls = {
plugins: true,
views: true
},
viewStringRegExp = /^\/\//;
/**
* Finds a steal node in a nested, backwards AST tree structure.
*/
function getStealCall(node) {
if (!baseParse.isArray(node)) {
return false;
}
if (node[0] === 'name' && node[1] === 'steal' && !node.isRequireJSParsed) {
return node;
} else if (node[0] === 'call') {
if (node[1][0] === 'name' && node[1][1] === 'steal') {
return getStealCall(node[1]);
} else if (node[1][0] === 'dot') {
return getStealCall(node[1][1]);
}
}
return null;
}
/**
* Mark the steal node tree as processed. Need to do this given the
* backwards structure of the AST.
*/
function markStealTreeProcessed(node) {
getStealCall(node).isRequireJSParsed = true;
}
/**
* Transform a .views depdencency to an ejs! plugin loaded depdendency
* @param {String} value the .views string name.
* @returns {String} an 'ejs!' string
*/
function viewTransform(value) {
return 'ejs!' + value.replace(viewStringRegExp, '');
}
function addStringsToArray(node, array, transform) {
var i, item, matches = [];
for (i = 0; i < node.length; i++) {
item = node[i];
if (item && baseParse.isArray(item) && item[0] === 'string') {
matches.push((transform ? transform(item[1]) : item[1]));
}
}
if (matches.length) {
//Build up arguments to splice, since we need to put these
//matches before other matches, given the backwards nature of
//the call traversal in the AST.
matches.unshift(0);
matches.unshift(0);
array.splice.apply(array, matches);
}
}
function generateRequireCall(node, array) {
if (!baseParse.isArray(node)) {
return;
}
var args, previous, call;
if (node[0] === 'call' && node[1][0] === 'name' && node[1][1] === 'steal') {
//A simple steal() call.
addStringsToArray(node[2], array);
} else {
//A chained call
//Need to unwind the call since the dot access shows up "backwards"
//in the AST.
args = node[node.length - 1];
previous = node[node.length - 2];
call = previous[previous.length - 1];
if (typeof call === 'string' && allowedCalls[call]) {
if (call === 'plugins') {
addStringsToArray(args, array);
} else if (call === 'views') {
addStringsToArray(args, array, viewTransform);
}
//Find out if there are any other chained calls.
previous = previous[previous.length - 2];
generateRequireCall(previous, array);
}
}
}
parse.oldParseNode = parse.parseNode;
parse.parseNode = function (node) {
var value;
if (!this.isArray(node)) {
return null;
}
//Allow files with regular define/require calls to be co-mingled
//with StealJS APIs.
value = this.oldParseNode(node);
if (value) {
return value;
}
if (getStealCall(node)) {
value = [];
generateRequireCall(node, value);
if (value.length) {
markStealTreeProcessed(node);
return "require(" + JSON.stringify(value) + ");";
} else {
return '';
}
}
return null;
};
/*
use console.log(JSON.stringify(node, null, ' ')) to print out AST
Using this:
steal.plugins('foo','bar').views('//abc/init.ejs').then(function(){})
Has this for one of the nodes.
[
"call",
[
"dot",
[
"call",
[
"dot",
[
"name",
"steal"
],
"plugins"
],
[
[
"string",
"foo"
],
[
"string",
"bar"
]
]
],
"views"
],
[
[
"string",
"//abc/init.ejs"
]
]
]
**************************
steal('one', 'two')
[
"toplevel",
[
[
"stat",
[
"call",
[
"name",
"steal"
],
[
[
"string",
"one"
],
[
"string",
"two"
]
]
]
]
]
]
*/
return parse;
});