@@ -188,27 +188,8 @@ Base.exports.PaperScript = function() {
188
188
code = code . substring ( 0 , start ) + str + code . substring ( end ) ;
189
189
}
190
190
191
- // Recursively walks the AST and replaces the code of certain nodes
192
- function walkAST ( node , parent ) {
193
- if ( ! node )
194
- return ;
195
- // The easiest way to walk through the whole AST is to simply loop
196
- // over each property of the node and filter out fields we don't
197
- // need to consider...
198
- for ( var key in node ) {
199
- if ( key === 'range' || key === 'loc' )
200
- continue ;
201
- var value = node [ key ] ;
202
- if ( Array . isArray ( value ) ) {
203
- for ( var i = 0 , l = value . length ; i < l ; i ++ )
204
- walkAST ( value [ i ] , node ) ;
205
- } else if ( value && typeof value === 'object' ) {
206
- // We cannot use Base.isPlainObject() for these since
207
- // Acorn.js uses its own internal prototypes now.
208
- walkAST ( value , node ) ;
209
- }
210
- }
211
- switch ( node . type ) {
191
+ function handleOverloading ( node , parent ) {
192
+ switch ( node . type ) {
212
193
case 'UnaryExpression' : // -a
213
194
if ( node . operator in unaryOperators
214
195
&& node . argument . type !== 'Literal' ) {
@@ -291,6 +272,11 @@ Base.exports.PaperScript = function() {
291
272
}
292
273
}
293
274
break ;
275
+ }
276
+ }
277
+
278
+ function handleExports ( node ) {
279
+ switch ( node . type ) {
294
280
case 'ExportDefaultDeclaration' :
295
281
// Convert `export default` to `module.exports = ` statements:
296
282
replaceCode ( {
@@ -328,6 +314,35 @@ Base.exports.PaperScript = function() {
328
314
}
329
315
}
330
316
317
+ // Recursively walks the AST and replaces the code of certain nodes
318
+ function walkAST ( node , parent , paperFeatures ) {
319
+ if ( node ) {
320
+ // The easiest way to walk through the whole AST is to simply
321
+ // loop over each property of the node and filter out fields we
322
+ // don't need to consider...
323
+ for ( var key in node ) {
324
+ if ( key !== 'range' && key !== 'loc' ) {
325
+ var value = node [ key ] ;
326
+ if ( Array . isArray ( value ) ) {
327
+ for ( var i = 0 , l = value . length ; i < l ; i ++ ) {
328
+ walkAST ( value [ i ] , node , paperFeatures ) ;
329
+ }
330
+ } else if ( value && typeof value === 'object' ) {
331
+ // Don't use Base.isPlainObject() for these since
332
+ // Acorn.js uses its own internal prototypes now.
333
+ walkAST ( value , node , paperFeatures ) ;
334
+ }
335
+ }
336
+ }
337
+ if ( paperFeatures . operatorOverloading !== false ) {
338
+ handleOverloading ( node , parent ) ;
339
+ }
340
+ if ( paperFeatures . moduleExports !== false ) {
341
+ handleExports ( node ) ;
342
+ }
343
+ }
344
+ }
345
+
331
346
// Source-map support:
332
347
// Encodes a Variable Length Quantity as a Base64 string.
333
348
// See: https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/
@@ -411,7 +426,7 @@ Base.exports.PaperScript = function() {
411
426
ranges : true ,
412
427
preserveParens : true ,
413
428
sourceType : 'module'
414
- } ) ) ;
429
+ } ) , null , paperFeatures ) ;
415
430
}
416
431
if ( map ) {
417
432
if ( offsetCode ) {
0 commit comments