@@ -83,6 +83,49 @@ function reduceSet(a, b) {
8383 return a ;
8484}
8585
86+ function getPathInAst ( ast , node ) {
87+ if ( ast === node ) {
88+ return [ ] ;
89+ }
90+ const nr = node . range ;
91+ var i ;
92+ if ( Array . isArray ( ast ) ) {
93+ for ( i = 0 ; i < ast . length ; i ++ ) {
94+ const enterResult = enterNode ( ast [ i ] ) ;
95+ if ( typeof enterResult !== "undefined" )
96+ return enterResult ;
97+ }
98+ } else if ( ast && typeof ast === "object" ) {
99+ const keys = Object . keys ( ast ) ;
100+ for ( i = 0 ; i < keys . length ; i ++ ) {
101+ const value = ast [ keys [ i ] ] ;
102+ if ( Array . isArray ( value ) ) {
103+ const pathResult = getPathInAst ( value , node ) ;
104+ if ( typeof pathResult !== "undefined" )
105+ return pathResult ;
106+ } else if ( value && typeof value === "object" ) {
107+ const enterResult = enterNode ( value ) ;
108+ if ( typeof enterResult !== "undefined" )
109+ return enterResult ;
110+ }
111+ }
112+ }
113+
114+ function enterNode ( n ) {
115+ const r = n . range ;
116+ if ( r ) {
117+ if ( r [ 0 ] <= nr [ 0 ] && r [ 1 ] >= nr [ 1 ] ) {
118+ const path = getPathInAst ( n , node ) ;
119+ if ( path ) {
120+ path . push ( n ) ;
121+ return path ;
122+ }
123+ }
124+ }
125+ return undefined ;
126+ }
127+ }
128+
86129class ConcatenatedModule extends Module {
87130 constructor ( rootModule , modules ) {
88131 super ( ) ;
@@ -190,6 +233,7 @@ class ConcatenatedModule extends Module {
190233 return {
191234 module : m ,
192235 index : idx ,
236+ ast : undefined ,
193237 source : undefined ,
194238 globalScope : undefined ,
195239 moduleScope : undefined ,
@@ -259,6 +303,7 @@ class ConcatenatedModule extends Module {
259303 const globalScope = scopeManager . acquire ( ast ) ;
260304 const moduleScope = globalScope . childScopes [ 0 ] ;
261305 const resultSource = new ReplaceSource ( source ) ;
306+ info . ast = ast ;
262307 info . source = resultSource ;
263308 info . globalScope = globalScope ;
264309 info . moduleScope = moduleScope ;
@@ -298,7 +343,12 @@ class ConcatenatedModule extends Module {
298343 const allIdentifiers = new Set ( references . map ( r => r . identifier ) . concat ( variable . identifiers ) ) ;
299344 for ( const identifier of allIdentifiers ) {
300345 const r = identifier . range ;
301- source . replace ( r [ 0 ] , r [ 1 ] - 1 , newName ) ;
346+ const path = getPathInAst ( info . ast , identifier ) ;
347+ if ( path && path . length > 1 && path [ 1 ] . type === "Property" && path [ 1 ] . shorthand ) {
348+ source . insert ( r [ 1 ] , `: ${ newName } ` ) ;
349+ } else {
350+ source . replace ( r [ 0 ] , r [ 1 ] - 1 , newName ) ;
351+ }
302352 }
303353 } else {
304354 allUsedNames . add ( name ) ;
0 commit comments