Skip to content

Commit c69e8ca

Browse files
committed
feat(compiler): support JSXComponent in JSXExpressionContainer
1 parent 1e84dfe commit c69e8ca

File tree

8 files changed

+55
-45
lines changed

8 files changed

+55
-45
lines changed

packages/compiler/src/generators/for.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {
77
} from '@babel/types'
88
import {
99
createSimpleExpression,
10-
isStaticNode,
1110
walkIdentifiers,
1211
type SimpleExpressionNode,
1312
} from '@vue/compiler-dom'
1413
import { extend, isGloballyAllowed } from '@vue/shared'
1514
import { walkAST } from 'ast-kit'
15+
import { isConstant } from '../utils'
1616
import type { CodegenContext } from '../generate'
1717
import type { BlockIRNode, ForIRNode, IREffect } from '../ir'
1818
import { genBlockContent } from './block'
@@ -478,8 +478,8 @@ function matchSelectorPattern(
478478
ast.test.type === 'BinaryExpression' &&
479479
ast.test.operator === '===' &&
480480
ast.test.left.type !== 'PrivateName' &&
481-
isStaticNode(ast.consequent) &&
482-
isStaticNode(ast.alternate)
481+
isConstant(ast.consequent) &&
482+
isConstant(ast.alternate)
483483
) {
484484
const left = ast.test.left
485485
const right = ast.test.right

packages/compiler/src/transform.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
defaultOnError,
33
defaultOnWarn,
4-
isConstantNode,
54
type TransformOptions as BaseTransformOptions,
65
type CommentNode,
76
type CompilerCompatOptions,
@@ -21,7 +20,13 @@ import {
2120
type SetEventIRNode,
2221
} from './ir'
2322
import { newBlock, newDynamic } from './transforms/utils'
24-
import { findProp, getText, isConstantExpression, isTemplate } from './utils'
23+
import {
24+
findProp,
25+
getText,
26+
isConstant,
27+
isConstantExpression,
28+
isTemplate,
29+
} from './utils'
2530
import type { JSXAttribute, JSXElement, JSXFragment } from '@babel/types'
2631

2732
export type NodeTransform = (
@@ -177,7 +182,7 @@ export class TransformContext<
177182
if (
178183
this.inVOnce ||
179184
expressions.length === 0 ||
180-
expressions.every((e) => e.ast && isConstantNode(e.ast, {}))
185+
expressions.every((e) => e.ast && isConstant(e.ast))
181186
) {
182187
return this.registerOperation(operations, getOperationIndex)
183188
}

packages/compiler/src/transforms/expression.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { isStaticNode } from '@vue/compiler-dom'
21
import { DynamicFlag, IRNodeTypes, type OperationNode } from '../ir'
32
import { transformNode, type TransformContext } from '../transform'
4-
import { resolveExpression } from '../utils'
3+
import { isConstant, resolveExpression } from '../utils'
54
import { createBranch } from './utils'
65
import type { ConditionalExpression, LogicalExpression } from '@babel/types'
76

@@ -20,7 +19,7 @@ export function processConditionalExpression(
2019
id,
2120
condition,
2221
positive: branch,
23-
once: context.inVOnce || isStaticNode(test),
22+
once: context.inVOnce || isConstant(test),
2423
}
2524

2625
return [

packages/compiler/src/transforms/vFor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
createCompilerError,
33
ErrorCodes,
4-
isConstantNode,
54
type SimpleExpressionNode,
65
} from '@vue/compiler-dom'
76
import { DynamicFlag, IRNodeTypes } from '../ir'
@@ -12,6 +11,7 @@ import {
1211
} from '../transform'
1312
import {
1413
findProp,
14+
isConstant,
1515
isJSXComponent,
1616
propToExpression,
1717
resolveExpression,
@@ -69,7 +69,7 @@ export function processFor(
6969
index,
7070
keyProp: keyProperty,
7171
render,
72-
once: context.inVOnce || !!(source.ast && isConstantNode(source.ast, {})),
72+
once: context.inVOnce || !!(source.ast && isConstant(source.ast)),
7373
component: isComponent,
7474
onlyChild: !!isOnlyChild,
7575
}

packages/compiler/src/transforms/vIf.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import {
22
createCompilerError,
33
createSimpleExpression,
44
ErrorCodes,
5-
isConstantNode,
65
} from '@vue/compiler-dom'
76
import { DynamicFlag, IRNodeTypes } from '../ir'
87
import {
98
createStructuralDirectiveTransform,
109
type NodeTransform,
1110
type TransformContext,
1211
} from '../transform'
13-
import { isEmptyText, resolveDirective, resolveLocation } from '../utils'
12+
import {
13+
isConstant,
14+
isEmptyText,
15+
resolveDirective,
16+
resolveLocation,
17+
} from '../utils'
1418
import { createBranch } from './utils'
1519
import type { JSXAttribute, JSXElement } from '@babel/types'
1620

@@ -49,7 +53,7 @@ export function processIf(
4953
id,
5054
condition: dir.exp!,
5155
positive: branch,
52-
once: context.inVOnce || isConstantNode(attribute.value!, {}),
56+
once: context.inVOnce || isConstant(attribute.value!),
5357
}
5458
}
5559
} else {

packages/compiler/src/utils.ts

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
import { parseExpression } from '@babel/parser'
2-
import {
3-
isLiteral,
4-
type BigIntLiteral,
5-
type Expression,
6-
type JSXAttribute,
7-
type JSXElement,
8-
type JSXFragment,
9-
type JSXText,
10-
type Node,
11-
type NumericLiteral,
12-
type SourceLocation,
13-
type StringLiteral,
14-
} from '@babel/types'
152
import {
163
createSimpleExpression,
4+
isConstantNode,
175
isLiteralWhitelisted,
186
NodeTypes,
197
unwrapTSNode,
@@ -24,6 +12,18 @@ import { isGloballyAllowed, isHTMLTag, isString, isSVGTag } from '@vue/shared'
2412
import { EMPTY_EXPRESSION } from './transforms/utils'
2513
import type { VaporDirectiveNode } from './ir'
2614
import type { TransformContext } from './transform'
15+
import type {
16+
BigIntLiteral,
17+
Expression,
18+
JSXAttribute,
19+
JSXElement,
20+
JSXFragment,
21+
JSXText,
22+
Node,
23+
NumericLiteral,
24+
SourceLocation,
25+
StringLiteral,
26+
} from '@babel/types'
2727

2828
export function propToExpression(
2929
prop: JSXAttribute,
@@ -68,24 +68,7 @@ export const isConstant = (node: Node | null | undefined): boolean => {
6868
if (node.type === 'Identifier') {
6969
return node.name === 'undefined' || isGloballyAllowed(node.name)
7070
}
71-
if (['JSXElement', 'JSXFragment', 'NullLiteral'].includes(node.type)) {
72-
return true
73-
}
74-
if (node.type === 'ArrayExpression') {
75-
const { elements } = node
76-
return elements.every((element) => element && isConstant(element))
77-
}
78-
if (node.type === 'ObjectExpression') {
79-
return node.properties.every((property) =>
80-
isConstant((property as any).value),
81-
)
82-
}
83-
if (
84-
node.type === 'TemplateLiteral' ? !node.expressions.length : isLiteral(node)
85-
) {
86-
return true
87-
}
88-
return false
71+
return isConstantNode(node, {})
8972
}
9073

9174
const EMPTY_TEXT_REGEX =

packages/compiler/test/transforms/__snapshots__/transformChildren.spec.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`compiler: children transform > JSXComponent in JSXExpressionContainer 1`] = `
4+
"
5+
const n0 = t0()
6+
const x0 = _child(n0)
7+
_setNodes(x0, () => (<Comp />))
8+
return n0
9+
"
10+
`;
11+
312
exports[`compiler: children transform > efficient find 1`] = `
413
"
514
const n1 = t0()

packages/compiler/test/transforms/transformChildren.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,14 @@ describe('compiler: children transform', () => {
150150
expect(code).contains(`const n0 = _nthChild(n1, 2)`)
151151
expect(code).toMatchSnapshot()
152152
})
153+
154+
test('JSXComponent in JSXExpressionContainer', () => {
155+
const { code } = compileWithElementTransform(
156+
`<div>
157+
{<Comp />}
158+
</div>`,
159+
)
160+
expect(code).contains(`_setNodes(x0, () => (<Comp />))`)
161+
expect(code).toMatchSnapshot()
162+
})
153163
})

0 commit comments

Comments
 (0)