@@ -161,9 +161,9 @@ export function processExpression(
161
161
if ( ! isDuplicate ( node ) ) {
162
162
const needPrefix = shouldPrefix ( node , parent )
163
163
if ( ! knownIds [ node . name ] && needPrefix ) {
164
- if ( isPropertyShorthand ( node , parent ) ) {
165
- // property shorthand like { foo }, we need to add the key since we
166
- // rewrite the value
164
+ if ( isStaticProperty ( parent ) && parent . shorthand ) {
165
+ // property shorthand like { foo }, we need to add the key since
166
+ // we rewrite the value
167
167
node . prefix = `${ node . name } : `
168
168
}
169
169
node . name = prefix ( node . name )
@@ -278,46 +278,65 @@ const isStaticProperty = (node: Node): node is ObjectProperty =>
278
278
( node . type === 'ObjectProperty' || node . type === 'ObjectMethod' ) &&
279
279
! node . computed
280
280
281
- const isPropertyShorthand = ( node : Node , parent : Node ) => {
282
- return (
283
- isStaticProperty ( parent ) &&
284
- parent . value === node &&
285
- parent . key . type === 'Identifier' &&
286
- parent . key . name === ( node as Identifier ) . name &&
287
- parent . key . start === node . start
288
- )
289
- }
290
-
291
281
const isStaticPropertyKey = ( node : Node , parent : Node ) =>
292
282
isStaticProperty ( parent ) && parent . key === node
293
283
294
- function shouldPrefix ( identifier : Identifier , parent : Node ) {
284
+ function shouldPrefix ( id : Identifier , parent : Node ) {
285
+ // declaration id
295
286
if (
296
- ! (
297
- isFunction ( parent ) &&
298
- // not id of a FunctionDeclaration
299
- ( ( parent as any ) . id === identifier ||
300
- // not a params of a function
301
- parent . params . includes ( identifier ) )
302
- ) &&
303
- // not a key of Property
304
- ! isStaticPropertyKey ( identifier , parent ) &&
305
- // not a property of a MemberExpression
306
- ! (
307
- ( parent . type === 'MemberExpression' ||
308
- parent . type === 'OptionalMemberExpression' ) &&
309
- parent . property === identifier &&
310
- ! parent . computed
311
- ) &&
312
- // not in an Array destructure pattern
313
- ! ( parent . type === 'ArrayPattern' ) &&
314
- // skip whitelisted globals
315
- ! isGloballyWhitelisted ( identifier . name ) &&
316
- // special case for webpack compilation
317
- identifier . name !== `require` &&
318
- // is a special keyword but parsed as identifier
319
- identifier . name !== `arguments`
287
+ ( parent . type === 'VariableDeclarator' ||
288
+ parent . type === 'ClassDeclaration' ) &&
289
+ parent . id === id
320
290
) {
321
- return true
291
+ return false
292
+ }
293
+
294
+ if ( isFunction ( parent ) ) {
295
+ // function decalration/expression id
296
+ if ( ( parent as any ) . id === id ) {
297
+ return false
298
+ }
299
+ // params list
300
+ if ( parent . params . includes ( id ) ) {
301
+ return false
302
+ }
322
303
}
304
+
305
+ // property key
306
+ // this also covers object destructure pattern
307
+ if ( isStaticPropertyKey ( id , parent ) ) {
308
+ return false
309
+ }
310
+
311
+ // array destructure pattern
312
+ if ( parent . type === 'ArrayPattern' ) {
313
+ return false
314
+ }
315
+
316
+ // member expression property
317
+ if (
318
+ ( parent . type === 'MemberExpression' ||
319
+ parent . type === 'OptionalMemberExpression' ) &&
320
+ parent . property === id &&
321
+ ! parent . computed
322
+ ) {
323
+ return false
324
+ }
325
+
326
+ // is a special keyword but parsed as identifier
327
+ if ( id . name === 'arguments' ) {
328
+ return false
329
+ }
330
+
331
+ // skip whitelisted globals
332
+ if ( isGloballyWhitelisted ( id . name ) ) {
333
+ return false
334
+ }
335
+
336
+ // special case for webpack compilation
337
+ if ( id . name === 'require' ) {
338
+ return false
339
+ }
340
+
341
+ return true
323
342
}
0 commit comments