Skip to content

Commit 63cf2ee

Browse files
committed
refactor: adjust isConstantNode
1 parent 222ced2 commit 63cf2ee

File tree

7 files changed

+38
-33
lines changed

7 files changed

+38
-33
lines changed

packages/compiler-core/src/babelUtils.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
Program,
1313
} from '@babel/types'
1414
import { 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

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformTemplateRef.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function render(_ctx) {
2323
const n2 = t0()
2424
_setTemplateRef(n2, "foo", void 0, true)
2525
return n2
26-
})
26+
}, null, null, true)
2727
return n0
2828
}"
2929
`;

packages/compiler-vapor/src/transform.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ export class TransformContext<T extends AllNode = AllNode> {
143143
if (
144144
this.inVOnce ||
145145
expressions.length === 0 ||
146-
isStaticExpression(this.root, expressions)
146+
expressions.every(e =>
147+
isStaticExpression(e, this.root.options.bindingMetadata),
148+
)
147149
) {
148150
return this.registerOperation(...operations)
149151
}

packages/compiler-vapor/src/transforms/transformText.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ function processTextLike(context: TransformContext<InterpolationNode>) {
5959
const nonConstantExps = values.filter(v => !isConstantExpression(v))
6060
const isStatic =
6161
!nonConstantExps.length ||
62-
isStaticExpression(context, nonConstantExps) ||
62+
nonConstantExps.every(e =>
63+
isStaticExpression(e, context.options.bindingMetadata),
64+
) ||
6365
context.inVOnce
6466

6567
context.registerOperation({

packages/compiler-vapor/src/transforms/vFor.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
IRNodeTypes,
1717
type VaporDirectiveNode,
1818
} from '../ir'
19-
import { findProp, propToExpression } from '../utils'
19+
import { findProp, isStaticExpression, propToExpression } from '../utils'
2020
import { newBlock, wrapTemplate } from './utils'
2121

2222
export const transformVFor: NodeTransform = createStructuralDirectiveTransform(
@@ -76,7 +76,12 @@ export function processFor(
7676
index: index as SimpleExpressionNode | undefined,
7777
keyProp: keyProperty,
7878
render,
79-
once: context.inVOnce,
79+
once:
80+
context.inVOnce ||
81+
isStaticExpression(
82+
source as SimpleExpressionNode,
83+
context.options.bindingMetadata,
84+
),
8085
component: isComponent,
8186
onlyChild: !!isOnlyChild,
8287
})

packages/compiler-vapor/src/transforms/vIf.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { extend } from '@vue/shared'
1919
import { newBlock, wrapTemplate } from './utils'
2020
import { getSiblingIf } from './transformComment'
21+
import { isStaticExpression } from '../utils'
2122

2223
export const transformVIf: NodeTransform = createStructuralDirectiveTransform(
2324
['if', 'else', 'else-if'],
@@ -50,7 +51,9 @@ export function processIf(
5051
id,
5152
condition: dir.exp!,
5253
positive: branch,
53-
once: context.inVOnce,
54+
once:
55+
context.inVOnce ||
56+
isStaticExpression(dir.exp!, context.options.bindingMetadata),
5457
})
5558
}
5659
} else {

packages/compiler-vapor/src/utils.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { BigIntLiteral, NumericLiteral, StringLiteral } from '@babel/types'
22
import { isGloballyAllowed } from '@vue/shared'
33
import {
44
type AttributeNode,
5+
type BindingMetadata,
56
BindingTypes,
67
type ElementNode,
78
NodeTypes,
@@ -14,7 +15,6 @@ import {
1415
} from '@vue/compiler-dom'
1516
import type { VaporDirectiveNode } from './ir'
1617
import { EMPTY_EXPRESSION } from './transforms/utils'
17-
import type { TransformContext } from './transform'
1818

1919
export const findProp = _findProp as (
2020
node: ElementNode,
@@ -49,21 +49,16 @@ export function isConstantExpression(exp: SimpleExpressionNode): boolean {
4949
}
5050

5151
export function isStaticExpression(
52-
context: TransformContext,
53-
expressions: SimpleExpressionNode[],
52+
node: SimpleExpressionNode,
53+
bindings: BindingMetadata,
5454
): boolean {
55-
const {
56-
options: { bindingMetadata },
57-
} = context
58-
const isLiteralConst = (name: string) =>
59-
bindingMetadata[name] === BindingTypes.LITERAL_CONST
60-
return expressions.every(node => {
61-
if (node.ast) {
62-
return isConstantNode(node.ast, isLiteralConst)
63-
} else if (node.ast === null) {
64-
return isLiteralConst(node.content)
65-
}
66-
})
55+
if (node.ast) {
56+
return isConstantNode(node.ast, bindings)
57+
} else if (node.ast === null) {
58+
const type = bindings[node.content]
59+
return type === BindingTypes.LITERAL_CONST
60+
}
61+
return false
6762
}
6863

6964
export function resolveExpression(

0 commit comments

Comments
 (0)