File tree Expand file tree Collapse file tree 4 files changed +70
-30
lines changed Expand file tree Collapse file tree 4 files changed +70
-30
lines changed Original file line number Diff line number Diff line change 41
41
"./lib/config/files/index.js" : " ./lib/config/files/index-browser.js" ,
42
42
"./lib/config/resolve-targets.js" : " ./lib/config/resolve-targets-browser.js" ,
43
43
"./lib/transform-file.js" : " ./lib/transform-file-browser.js" ,
44
- "./lib/transformation/util/clone-deep.js" : " ./lib/transformation/util/clone-deep-browser.js" ,
45
44
"./src/config/files/index.ts" : " ./src/config/files/index-browser.ts" ,
46
45
"./src/config/resolve-targets.ts" : " ./src/config/resolve-targets-browser.ts" ,
47
- "./src/transform-file.ts" : " ./src/transform-file-browser.ts" ,
48
- "./src/transformation/util/clone-deep.ts" : " ./src/transformation/util/clone-deep-browser.ts"
46
+ "./src/transform-file.ts" : " ./src/transform-file-browser.ts"
49
47
},
50
48
"dependencies" : {
51
49
"@ampproject/remapping" : " ^2.1.0" ,
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- import v8 from "v8" ;
2
- import cloneDeep from "./clone-deep-browser" ;
3
-
4
- export default function ( value ) {
5
- if ( v8 . deserialize && v8 . serialize ) {
6
- return v8 . deserialize ( v8 . serialize ( value ) ) ;
1
+ //https://github.com/babel/babel/pull/14583#discussion_r882828856
2
+ function deepClone ( value : any , cache : Map < any , any > ) : any {
3
+ if ( value !== null ) {
4
+ if ( cache . has ( value ) ) return cache . get ( value ) ;
5
+ let cloned : any ;
6
+ if ( Array . isArray ( value ) ) {
7
+ cloned = new Array ( value . length ) ;
8
+ for ( let i = 0 ; i < value . length ; i ++ ) {
9
+ cloned [ i ] =
10
+ typeof value [ i ] !== "object" ? value [ i ] : deepClone ( value [ i ] , cache ) ;
11
+ }
12
+ } else {
13
+ cloned = { } ;
14
+ const keys = Object . keys ( value ) ;
15
+ for ( let i = 0 ; i < keys . length ; i ++ ) {
16
+ const key = keys [ i ] ;
17
+ cloned [ key ] =
18
+ typeof value [ key ] !== "object"
19
+ ? value [ key ]
20
+ : deepClone ( value [ key ] , cache ) ;
21
+ }
22
+ }
23
+ cache . set ( value , cloned ) ;
24
+ return cloned ;
7
25
}
8
- return cloneDeep ( value ) ;
26
+ return value ;
27
+ }
28
+
29
+ export default function < T > ( value : T ) : T {
30
+ if ( typeof value !== "object" ) return value ;
31
+ return deepClone ( value , new Map ( ) ) ;
9
32
}
Original file line number Diff line number Diff line change @@ -261,7 +261,45 @@ describe("api", function () {
261
261
} ,
262
262
) ;
263
263
264
- it ( "transformFromAstSync should not mutate the AST" , function ( ) {
264
+ it ( "transformFromAst should generate same code with different cloneInputAst" , function ( ) {
265
+ const program = `//test1
266
+ /*test2*/var/*test3*/ a = 1/*test4*/;//test5
267
+ //test6
268
+ var b;
269
+ ` ;
270
+ const node = parseSync ( program ) ;
271
+ const { code } = transformFromAstSync ( node , program , {
272
+ plugins : [
273
+ function ( ) {
274
+ return {
275
+ visitor : {
276
+ Identifier : function ( path ) {
277
+ path . node . name = "replaced" ;
278
+ } ,
279
+ } ,
280
+ } ;
281
+ } ,
282
+ ] ,
283
+ } ) ;
284
+ const { code : code2 } = transformFromAstSync ( node , program , {
285
+ cloneInputAst : false ,
286
+ plugins : [
287
+ function ( ) {
288
+ return {
289
+ visitor : {
290
+ Identifier : function ( path ) {
291
+ path . node . name = "replaced" ;
292
+ } ,
293
+ } ,
294
+ } ;
295
+ } ,
296
+ ] ,
297
+ } ) ;
298
+
299
+ expect ( code ) . toBe ( code2 ) ;
300
+ } ) ;
301
+
302
+ it ( "transformFromAst should not mutate the AST" , function ( ) {
265
303
const program = "const identifier = 1" ;
266
304
const node = parseSync ( program ) ;
267
305
const { code } = transformFromAstSync ( node , program , {
You can’t perform that action at this time.
0 commit comments