@@ -43,13 +43,16 @@ type NodePath = {
43
43
*/
44
44
export default function ( { types : t , template, options} : PluginParams ) : Plugin {
45
45
46
- const PRECONDITION_NAME = 'pre' ;
47
- const POSTCONDITION_NAME = 'post' ;
48
- const INVARIANT_NAME = 'invariant' ;
49
- const ASSERT_NAME = 'assert' ;
50
- const RETURN_NAME = 'it' ;
51
- const OLD_VALUE_NAME = 'old' ;
52
- const returnId : Identifier = t . identifier ( RETURN_NAME ) ;
46
+ const defaultNames = {
47
+ assert : 'assert' ,
48
+ precondition : 'pre' ,
49
+ postcondition : 'post' ,
50
+ invariant : 'invariant' ,
51
+ return : 'it' ,
52
+ old : 'old'
53
+ } ;
54
+
55
+ let NAMES = Object . assign ( { } , defaultNames ) ;
53
56
54
57
const guard : ( ids : { [ key : string ] : Node } ) => Node = template ( `
55
58
if (!condition) {
@@ -147,7 +150,7 @@ export default function ({types: t, template, options}: PluginParams): Plugin {
147
150
CallExpression ( call : NodePath ) : void {
148
151
const callee : NodePath = call . get ( 'callee' ) ;
149
152
const args : NodePath [ ] = call . get ( 'arguments' ) ;
150
- if ( ! callee . isIdentifier ( ) || callee . node . name !== OLD_VALUE_NAME || call . scope . hasBinding ( OLD_VALUE_NAME ) || args . length === 0 ) {
153
+ if ( ! callee . isIdentifier ( ) || callee . node . name !== NAMES . old || call . scope . hasBinding ( NAMES . old ) || args . length === 0 ) {
151
154
return ;
152
155
}
153
156
const argument : NodePath = args [ 0 ] ;
@@ -180,7 +183,7 @@ export default function ({types: t, template, options}: PluginParams): Plugin {
180
183
fn . get ( 'body' ) . get ( 'body' ) [ 0 ] . insertBefore ( guardFn ( {
181
184
id,
182
185
conditions,
183
- it : returnId
186
+ it : t . identifier ( NAMES . return )
184
187
} ) ) ;
185
188
186
189
path . remove ( ) ;
@@ -296,7 +299,7 @@ export default function ({types: t, template, options}: PluginParams): Plugin {
296
299
path . parentPath . get ( 'body' ) [ 0 ] . insertBefore ( guardFn ( {
297
300
id,
298
301
conditions,
299
- it : returnId
302
+ it : t . identifier ( NAMES . return )
300
303
} ) ) ;
301
304
path . remove ( ) ;
302
305
return id ;
@@ -322,79 +325,86 @@ export default function ({types: t, template, options}: PluginParams): Plugin {
322
325
323
326
return {
324
327
visitor : {
325
- Function ( fn : NodePath , { opts} ) : void {
326
- if ( fn . isArrowFunctionExpression ( ) && ! fn . get ( 'body' ) . isBlockStatement ( ) ) {
327
- // Naked arrow functions cannot contain contracts.
328
- return ;
328
+ Program ( path : NodePath , { opts} : any ) {
329
+ if ( opts ! = null && opts . names !== undefined ) {
330
+ NAMES = Object . assign ( { } , defaultNames , opts . names ) ;
329
331
}
330
- fn . traverse ( {
331
- Function ( path : NodePath ) : void {
332
- // This will be handled by the outer visitor, so skip it.
333
- path. skip ( ) ;
334
- } ,
335
-
336
- LabeledStatement ( path : NodePath ) : void {
337
- const label : NodePath = path . get ( 'label' ) ;
338
- if ( opts . strip || ( opts . env && opts . env [ process . env . NODE_ENV ] && opts . env [ process . env . NODE_ENV ] . strip ) ) {
339
- if ( label . node . name === PRECONDITION_NAME || label . node . name === POSTCONDITION_NAME || label . node . name === INVARIANT_NAME || label . node . name === ASSERT_NAME ) {
340
- path . remove ( ) ;
341
- }
342
- return ;
343
- }
344
-
345
-
346
- let id : ?Identifier ;
347
- let children : ?NodePath [ ] ;
348
- let parent : NodePath = fn ;
349
- if ( label . node . name === PRECONDITION_NAME ) {
350
- assemblePrecondition ( path ) ;
332
+ return path . traverse ( {
333
+ Function ( fn : NodePath ) : void {
334
+ if ( fn . isArrowFunctionExpression ( ) && ! fn . get ( 'body' ) . isBlockStatement ( ) ) {
335
+ // Naked arrow functions cannot contain contracts.
351
336
return ;
352
337
}
353
- else if ( label . node . name === POSTCONDITION_NAME ) {
354
- id = assemblePostcondition ( path ) ;
355
- children = fn . get ( 'body' ) . get ( 'body' ) ;
356
- }
357
- else if ( label . node . name === ASSERT_NAME ) {
358
- assembleAssertion ( path ) ;
359
- return ;
360
- }
361
- else if ( label . node . name === INVARIANT_NAME ) {
362
- id = assembleInvariant ( path ) ;
363
- parent = path . findParent ( t . isBlockStatement ) ;
364
- children = parent . get ( 'body' ) ;
365
- const first : NodePath = children [ 0 ] ;
366
- first . insertAfter ( t . expressionStatement ( t . callExpression ( id , [ ] ) ) )
367
- }
368
- parent . traverse ( {
338
+ fn . traverse ( {
369
339
Function ( path : NodePath ) : void {
370
340
// This will be handled by the outer visitor, so skip it.
371
341
path. skip ( ) ;
372
342
} ,
373
- ReturnStatement ( statement : NodePath ) : void {
374
- statement . get ( 'argument' ) . replaceWith ( t . callExpression ( id , [ statement . node . argument ] ) ) ;
343
+
344
+ LabeledStatement ( path : NodePath ) : void {
345
+ const label : NodePath = path . get ( 'label' ) ;
346
+ if ( opts . strip || ( opts . env && opts . env [ process . env . NODE_ENV ] && opts . env [ process . env . NODE_ENV ] . strip ) ) {
347
+ if ( label . node . name === NAMES . precondition || label . node . name === NAMES . postcondition || label . node . name === NAMES . invariant || label . node . name === NAMES . assert ) {
348
+ path . remove ( ) ;
349
+ }
350
+ return ;
351
+ }
352
+
353
+
354
+ let id : ?Identifier ;
355
+ let children : ?NodePath [ ] ;
356
+ let parent : NodePath = fn ;
357
+ if ( label . node . name === NAMES . precondition ) {
358
+ assemblePrecondition ( path ) ;
359
+ return ;
360
+ }
361
+ else if ( label . node . name === NAMES . postcondition ) {
362
+ id = assemblePostcondition ( path ) ;
363
+ children = fn . get ( 'body' ) . get ( 'body' ) ;
364
+ }
365
+ else if ( label . node . name === NAMES . assert ) {
366
+ assembleAssertion ( path ) ;
367
+ return ;
368
+ }
369
+ else if ( label . node . name === NAMES . invariant ) {
370
+ id = assembleInvariant ( path ) ;
371
+ parent = path . findParent ( t . isBlockStatement ) ;
372
+ children = parent . get ( 'body' ) ;
373
+ const first : NodePath = children [ 0 ] ;
374
+ first . insertAfter ( t . expressionStatement ( t . callExpression ( id , [ ] ) ) )
375
+ }
376
+ parent . traverse ( {
377
+ Function ( path : NodePath ) : void {
378
+ // This will be handled by the outer visitor, so skip it.
379
+ path . skip ( ) ;
380
+ } ,
381
+ ReturnStatement ( statement : NodePath ) : void {
382
+ statement . get ( 'argument' ) . replaceWith ( t . callExpression ( id , [ statement . node . argument ] ) ) ;
383
+ }
384
+ } ) ;
385
+ const last : NodePath = children [ children . length - 1 ] ;
386
+ if ( ! last . isReturnStatement ( ) ) {
387
+ last . insertAfter ( t . expressionStatement ( t . callExpression ( id , [ ] ) ) ) ;
388
+ }
375
389
}
376
390
} ) ;
377
- const last : NodePath = children [ children . length - 1 ] ;
378
- if ( ! last . isReturnStatement ( ) ) {
379
- last . insertAfter ( t . expressionStatement ( t . callExpression ( id , [ ] ) ) ) ;
380
- }
381
- }
382
- } ) ;
383
- } ,
391
+ } ,
384
392
385
- LabeledStatement ( path : NodePath , { opts } ) : void {
386
- const label : NodePath = path . get ( 'label' ) ;
393
+ LabeledStatement ( path : NodePath ) : void {
394
+ const label : NodePath = path . get ( 'label' ) ;
387
395
388
- if ( label . node . name === ASSERT_NAME ) {
389
- if ( opts . strip || ( opts . env && opts . env [ process . env . NODE_ENV ] && opts . env [ process . env . NODE_ENV ] . strip ) ) {
390
- path . remove ( ) ;
391
- }
392
- else {
393
- assembleAssertion ( path ) ;
396
+ if ( label . node . name === NAMES . assert ) {
397
+ if ( opts . strip || ( opts . env && opts . env [ process . env . NODE_ENV ] && opts . env [ process . env . NODE_ENV ] . strip ) ) {
398
+ path . remove ( ) ;
399
+ }
400
+ else {
401
+ assembleAssertion ( path ) ;
402
+ }
403
+ return ;
404
+ }
394
405
}
395
- return ;
396
- }
406
+ } ) ;
397
407
}
398
408
}
399
- } ;
409
+ }
400
410
}
0 commit comments