@@ -4,6 +4,10 @@ var falafel = require('falafel');
4
4
var escodegen = require ( 'escodegen' ) ;
5
5
var _ = require ( 'underscore' ) ;
6
6
7
+ function clone ( obj ) {
8
+ return JSON . parse ( JSON . stringify ( obj ) ) ;
9
+ }
10
+
7
11
function unwrapRewriteNode ( node ) {
8
12
if ( node . type == 'Program' && node . body . length > 0 ) {
9
13
node = unwrapRewriteNode ( node . body [ 0 ] ) ;
@@ -13,8 +17,8 @@ function unwrapRewriteNode(node) {
13
17
return node ;
14
18
}
15
19
16
- function isWildcard ( name ) {
17
- return / \b [ a - z ] \b / g. test ( name ) ;
20
+ function isWildcard ( node ) {
21
+ return node . type == "Identifier" && / \b [ a - z ] \b / g. test ( node . name ) ;
18
22
}
19
23
20
24
function partial ( wildcards , patterns , nodes ) {
@@ -57,7 +61,7 @@ function match(wildcards, pattern, node) {
57
61
return false ;
58
62
}
59
63
60
- if ( wildcards != null && pattern . type == 'Identifier' && isWildcard ( pattern . name ) ) {
64
+ if ( wildcards != null && isWildcard ( pattern ) ) {
61
65
if ( pattern . name in wildcards ) {
62
66
return match ( null , wildcards [ pattern . name ] , node ) ;
63
67
}
@@ -164,7 +168,7 @@ function match(wildcards, pattern, node) {
164
168
function replaceWildcards ( wildcards , replacement ) {
165
169
switch ( replacement . type ) {
166
170
case 'Identifier' :
167
- if ( wildcards != null && isWildcard ( replacement . name ) ) {
171
+ if ( wildcards != null && isWildcard ( replacement ) ) {
168
172
if ( replacement . name in wildcards ) {
169
173
replacement = wildcards [ replacement . name ] ;
170
174
}
@@ -266,7 +270,7 @@ exports.rewrite = function(js, rewriteRule) {
266
270
return falafel ( js , parseOptions , function ( node ) {
267
271
var wildcards = { } ;
268
272
if ( match ( wildcards , pattern , node ) ) {
269
- node . update ( escodegen . generate ( replaceWildcards ( wildcards , _ . clone ( replacement ) ) ) ) ;
273
+ node . update ( escodegen . generate ( replaceWildcards ( wildcards , clone ( replacement ) ) ) ) ;
270
274
}
271
275
} ) ;
272
276
}
0 commit comments