1
1
import { declare } from "@babel/helper-plugin-utils" ;
2
2
import { types as t } from "@babel/core" ;
3
+ import escapeRegExp from "lodash/escapeRegExp" ;
4
+
5
+ function checkNameMatchesSelectiveLoose ( name , selectiveLoose ) {
6
+ for ( let i = 0 ; i < selectiveLoose . length ; i ++ ) {
7
+ if ( selectiveLoose [ i ] . test ( name ) ) {
8
+ return true ;
9
+ }
10
+ }
11
+ return false ;
12
+ }
3
13
4
14
export default declare ( ( api , options ) => {
5
15
api . assertVersion ( 7 ) ;
6
16
7
- const { loose = false , useBuiltIns = false } = options ;
17
+ const {
18
+ loose = false ,
19
+ useBuiltIns = false ,
20
+ selectiveLoose = false ,
21
+ } = options ;
8
22
9
23
if ( typeof loose !== "boolean" ) {
10
24
throw new Error ( `.loose must be a boolean or undefined` ) ;
11
25
}
12
26
27
+ // Format selectiveLoose entries to RegExp expressions that can
28
+ // match generated babel names, i.e. `_x`, `_x2`, `_x3`, etc.
29
+ if ( selectiveLoose ) {
30
+ if ( selectiveLoose instanceof Array ) {
31
+ for ( let i = 0 ; i < selectiveLoose . length ; i ++ ) {
32
+ selectiveLoose [ i ] = new RegExp (
33
+ `^_${ escapeRegExp ( selectiveLoose [ i ] ) } \\d*$` ,
34
+ ) ;
35
+ }
36
+ } else {
37
+ throw new Error ( `.selectiveLoose must be an array or undefined` ) ;
38
+ }
39
+ }
40
+
13
41
const arrayOnlySpread = loose ;
14
42
15
43
function getExtendsHelper ( file ) {
@@ -73,6 +101,7 @@ export default declare((api, options) => {
73
101
this . kind = opts . kind ;
74
102
this . arrayOnlySpread = opts . arrayOnlySpread ;
75
103
this . addHelper = opts . addHelper ;
104
+ this . selectiveLoose = opts . selectiveLoose ;
76
105
}
77
106
78
107
buildVariableAssignment ( id , init ) {
@@ -210,8 +239,12 @@ export default declare((api, options) => {
210
239
) ;
211
240
}
212
241
242
+ const isLoose =
243
+ ( this . selectiveLoose &&
244
+ checkNameMatchesSelectiveLoose ( objRef . name , this . selectiveLoose ) ) ||
245
+ loose ;
213
246
value = t . callExpression (
214
- this . addHelper ( `objectWithoutProperties${ loose ? "Loose" : "" } ` ) ,
247
+ this . addHelper ( `objectWithoutProperties${ isLoose ? "Loose" : "" } ` ) ,
215
248
[ t . cloneNode ( objRef ) , keyExpression ] ,
216
249
) ;
217
250
}
@@ -479,6 +512,7 @@ export default declare((api, options) => {
479
512
scope : scope ,
480
513
nodes : nodes ,
481
514
arrayOnlySpread,
515
+ selectiveLoose,
482
516
addHelper : name => this . addHelper ( name ) ,
483
517
} ) ;
484
518
@@ -504,6 +538,7 @@ export default declare((api, options) => {
504
538
scope : scope ,
505
539
nodes : nodes ,
506
540
arrayOnlySpread,
541
+ selectiveLoose,
507
542
addHelper : name => this . addHelper ( name ) ,
508
543
} ) ;
509
544
destructuring . init ( pattern , ref ) ;
@@ -522,6 +557,7 @@ export default declare((api, options) => {
522
557
scope : scope ,
523
558
nodes : nodes ,
524
559
arrayOnlySpread,
560
+ selectiveLoose,
525
561
addHelper : name => this . addHelper ( name ) ,
526
562
} ) ;
527
563
@@ -579,6 +615,7 @@ export default declare((api, options) => {
579
615
scope : scope ,
580
616
kind : node . kind ,
581
617
arrayOnlySpread,
618
+ selectiveLoose,
582
619
addHelper : name => this . addHelper ( name ) ,
583
620
} ) ;
584
621
0 commit comments