@@ -12,6 +12,7 @@ import type {
1212 Program ,
1313} from '@babel/types'
1414import { walk } from 'estree-walker'
15+ import { type BindingMetadata , BindingTypes } from './options'
1516
1617/**
1718 * Return value indicates whether the AST walked can be a constant
@@ -543,34 +544,31 @@ export function isStaticNode(node: Node): boolean {
543544 case 'BooleanLiteral' :
544545 case 'NullLiteral' :
545546 case 'BigIntLiteral' :
547+ case 'RegExpLiteral' :
546548 return true
547549 }
548550 return false
549551}
550552
551- export function isConstantNode (
552- node : Node ,
553- onIdentifier : ( name : string ) => boolean ,
554- ) : boolean {
553+ export function isConstantNode ( node : Node , bindings : BindingMetadata ) : boolean {
555554 if ( isStaticNode ( node ) ) return true
556555
557556 node = unwrapTSNode ( node )
558557 switch ( node . type ) {
559558 case 'Identifier' :
560- return onIdentifier ( node . name )
561- case 'RegExpLiteral' :
562- return true
559+ const type = bindings [ node . name ]
560+ return type === BindingTypes . LITERAL_CONST
563561 case 'ObjectExpression' :
564562 return node . properties . every ( prop => {
565563 // { bar() {} } object methods are not considered static nodes
566564 if ( prop . type === 'ObjectMethod' ) return false
567565 // { ...{ foo: 1 } }
568566 if ( prop . type === 'SpreadElement' )
569- return isConstantNode ( prop . argument , onIdentifier )
567+ return isConstantNode ( prop . argument , bindings )
570568 // { foo: 1 }
571569 return (
572- ( ! prop . computed || isConstantNode ( prop . key , onIdentifier ) ) &&
573- isConstantNode ( prop . value , onIdentifier )
570+ ( ! prop . computed || isConstantNode ( prop . key , bindings ) ) &&
571+ isConstantNode ( prop . value , bindings )
574572 )
575573 } )
576574 case 'ArrayExpression' :
@@ -579,9 +577,9 @@ export function isConstantNode(
579577 if ( element === null ) return true
580578 // [1, ...[2, 3]]
581579 if ( element . type === 'SpreadElement' )
582- return isConstantNode ( element . argument , onIdentifier )
580+ return isConstantNode ( element . argument , bindings )
583581 // [1, 2]
584- return isConstantNode ( element , onIdentifier )
582+ return isConstantNode ( element , bindings )
585583 } )
586584 }
587585 return false
0 commit comments