@@ -19,13 +19,11 @@ export function genExpression(
19
19
context : CodegenContext ,
20
20
assignment ?: string ,
21
21
) : CodeFragment [ ] {
22
- const {
23
- options : { prefixIdentifiers } ,
24
- } = context
22
+ const { prefixIdentifiers } = context . options
23
+ const { content, ast, isStatic, loc } = node
25
24
26
- const { content : rawExpr , ast, isStatic, loc } = node
27
25
if ( isStatic ) {
28
- return [ [ JSON . stringify ( rawExpr ) , NewlineType . None , loc ] ]
26
+ return [ [ JSON . stringify ( content ) , NewlineType . None , loc ] ]
29
27
}
30
28
31
29
if (
@@ -36,16 +34,16 @@ export function genExpression(
36
34
ast === false ||
37
35
isConstantExpression ( node )
38
36
) {
39
- return [ [ rawExpr , NewlineType . None , loc ] , assignment && ` = ${ assignment } ` ]
37
+ return [ [ content , NewlineType . None , loc ] , assignment && ` = ${ assignment } ` ]
40
38
}
41
39
42
40
// the expression is a simple identifier
43
41
if ( ast === null ) {
44
- return genIdentifier ( rawExpr , context , loc , assignment )
42
+ return genIdentifier ( content , context , loc , assignment )
45
43
}
46
44
47
45
const ids : Identifier [ ] = [ ]
48
- const parentStackMap = new WeakMap < Identifier , Node [ ] > ( )
46
+ const parentStackMap = new Map < Identifier , Node [ ] > ( )
49
47
const parentStack : Node [ ] = [ ]
50
48
walkIdentifiers (
51
49
ast ! ,
@@ -56,55 +54,57 @@ export function genExpression(
56
54
false ,
57
55
parentStack ,
58
56
)
57
+
59
58
let hasMemberExpression = false
60
59
if ( ids . length ) {
61
- ids . sort ( ( a , b ) => a . start ! - b . start ! )
62
60
const [ frag , push ] = buildCodeFragment ( )
63
- ids . forEach ( ( id , i ) => {
64
- // range is offset by -1 due to the wrapping parens when parsed
65
- const start = id . start ! - 1
66
- const end = id . end ! - 1
67
- const last = ids [ i - 1 ]
68
-
69
- const leadingText = rawExpr . slice ( last ? last . end ! - 1 : 0 , start )
70
- if ( leadingText . length ) push ( [ leadingText , NewlineType . Unknown ] )
71
-
72
- const source = rawExpr . slice ( start , end )
73
- const parentStack = parentStackMap . get ( id ) !
74
- const parent = parentStack [ parentStack . length - 1 ]
75
-
76
- hasMemberExpression ||=
77
- parent &&
78
- ( parent . type === 'MemberExpression' ||
79
- parent . type === 'OptionalMemberExpression' )
80
-
81
- push (
82
- ...genIdentifier (
83
- source ,
84
- context ,
85
- {
86
- start : advancePositionWithClone ( node . loc . start , source , start ) ,
87
- end : advancePositionWithClone ( node . loc . start , source , end ) ,
61
+ ids
62
+ . sort ( ( a , b ) => a . start ! - b . start ! )
63
+ . forEach ( ( id , i ) => {
64
+ // range is offset by -1 due to the wrapping parens when parsed
65
+ const start = id . start ! - 1
66
+ const end = id . end ! - 1
67
+ const last = ids [ i - 1 ]
68
+
69
+ const leadingText = content . slice ( last ? last . end ! - 1 : 0 , start )
70
+ if ( leadingText . length ) push ( [ leadingText , NewlineType . Unknown ] )
71
+
72
+ const source = content . slice ( start , end )
73
+ const parentStack = parentStackMap . get ( id ) !
74
+ const parent = parentStack [ parentStack . length - 1 ]
75
+
76
+ hasMemberExpression ||=
77
+ parent &&
78
+ ( parent . type === 'MemberExpression' ||
79
+ parent . type === 'OptionalMemberExpression' )
80
+
81
+ push (
82
+ ...genIdentifier (
88
83
source ,
89
- } ,
90
- hasMemberExpression ? undefined : assignment ,
91
- id ,
92
- parent ,
93
- parentStack ,
94
- ) ,
95
- )
96
-
97
- if ( i === ids . length - 1 && end < rawExpr . length ) {
98
- push ( [ rawExpr . slice ( end ) , NewlineType . Unknown ] )
99
- }
100
- } )
84
+ context ,
85
+ {
86
+ start : advancePositionWithClone ( node . loc . start , source , start ) ,
87
+ end : advancePositionWithClone ( node . loc . start , source , end ) ,
88
+ source,
89
+ } ,
90
+ hasMemberExpression ? undefined : assignment ,
91
+ id ,
92
+ parent ,
93
+ parentStack ,
94
+ ) ,
95
+ )
96
+
97
+ if ( i === ids . length - 1 && end < content . length ) {
98
+ push ( [ content . slice ( end ) , NewlineType . Unknown ] )
99
+ }
100
+ } )
101
101
102
102
if ( assignment && hasMemberExpression ) {
103
103
push ( ` = ${ assignment } ` )
104
104
}
105
105
return frag
106
106
} else {
107
- return [ [ rawExpr , NewlineType . Unknown , loc ] ]
107
+ return [ [ content , NewlineType . Unknown , loc ] ]
108
108
}
109
109
}
110
110
0 commit comments