@@ -23,14 +23,17 @@ import {
23
23
makeMap ,
24
24
babelParserDefaultPlugins ,
25
25
hasOwn ,
26
- isString
26
+ isString ,
27
+ isReferencedIdentifier ,
28
+ isInDestructureAssignment ,
29
+ isStaticProperty ,
30
+ isStaticPropertyKey ,
31
+ isFunctionType
27
32
} from '@vue/shared'
28
33
import { createCompilerError , ErrorCodes } from '../errors'
29
34
import {
30
35
Node ,
31
- Function ,
32
36
Identifier ,
33
- ObjectProperty ,
34
37
AssignmentExpression ,
35
38
UpdateExpression
36
39
} from '@babel/types'
@@ -279,7 +282,7 @@ export function processExpression(
279
282
ids . push ( node )
280
283
}
281
284
}
282
- } else if ( isFunction ( node ) ) {
285
+ } else if ( isFunctionType ( node ) ) {
283
286
// walk function expressions and add its arguments to known identifiers
284
287
// so that we don't prefix them
285
288
node . params . forEach ( p =>
@@ -372,97 +375,16 @@ export function processExpression(
372
375
return ret
373
376
}
374
377
375
- const isFunction = ( node : Node ) : node is Function => {
376
- return / F u n c t i o n (?: E x p r e s s i o n | D e c l a r a t i o n ) $ | M e t h o d $ / . test ( node . type )
377
- }
378
-
379
- const isStaticProperty = ( node : Node ) : node is ObjectProperty =>
380
- node &&
381
- ( node . type === 'ObjectProperty' || node . type === 'ObjectMethod' ) &&
382
- ! node . computed
383
-
384
- const isStaticPropertyKey = ( node : Node , parent : Node ) =>
385
- isStaticProperty ( parent ) && parent . key === node
386
-
387
378
function shouldPrefix ( id : Identifier , parent : Node , parentStack : Node [ ] ) {
388
- // declaration id
389
- if (
390
- ( parent . type === 'VariableDeclarator' ||
391
- parent . type === 'ClassDeclaration' ) &&
392
- parent . id === id
393
- ) {
394
- return false
395
- }
396
-
397
- if ( isFunction ( parent ) ) {
398
- // function decalration/expression id
399
- if ( ( parent as any ) . id === id ) {
400
- return false
401
- }
402
- // params list
403
- if ( parent . params . includes ( id ) ) {
404
- return false
405
- }
406
- }
407
-
408
- // property key
409
- // this also covers object destructure pattern
410
- if ( isStaticPropertyKey ( id , parent ) ) {
411
- return false
412
- }
413
-
414
- // non-assignment array destructure pattern
415
- if (
416
- parent . type === 'ArrayPattern' &&
417
- ! isInDestructureAssignment ( parent , parentStack )
418
- ) {
419
- return false
420
- }
421
-
422
- // member expression property
423
- if (
424
- ( parent . type === 'MemberExpression' ||
425
- parent . type === 'OptionalMemberExpression' ) &&
426
- parent . property === id &&
427
- ! parent . computed
428
- ) {
429
- return false
430
- }
431
-
432
- // is a special keyword but parsed as identifier
433
- if ( id . name === 'arguments' ) {
434
- return false
435
- }
436
-
437
379
// skip whitelisted globals
438
380
if ( isGloballyWhitelisted ( id . name ) ) {
439
381
return false
440
382
}
441
-
442
383
// special case for webpack compilation
443
384
if ( id . name === 'require' ) {
444
385
return false
445
386
}
446
-
447
- return true
448
- }
449
-
450
- function isInDestructureAssignment ( parent : Node , parentStack : Node [ ] ) : boolean {
451
- if (
452
- parent &&
453
- ( parent . type === 'ObjectProperty' || parent . type === 'ArrayPattern' )
454
- ) {
455
- let i = parentStack . length
456
- while ( i -- ) {
457
- const p = parentStack [ i ]
458
- if ( p . type === 'AssignmentExpression' ) {
459
- return true
460
- } else if ( p . type !== 'ObjectProperty' && ! p . type . endsWith ( 'Pattern' ) ) {
461
- break
462
- }
463
- }
464
- }
465
- return false
387
+ return isReferencedIdentifier ( id , parent , parentStack )
466
388
}
467
389
468
390
function stringifyExpression ( exp : ExpressionNode | string ) : string {
0 commit comments