Skip to content

Commit 98a24d6

Browse files
committed
feat(compiler): support v-text directive
1 parent 3dbd90b commit 98a24d6

File tree

4 files changed

+12
-22
lines changed

4 files changed

+12
-22
lines changed

docs/features/directives.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Vue built-in directives for JSX.
88
| `v-slot`, `v-slots` | :white_check_mark: | :white_check_mark: |
99
| `v-for` | :white_check_mark: | :white_check_mark: |
1010
| `v-model` | :white_check_mark: | :white_check_mark: |
11-
| `v-html` | :white_check_mark: | / |
11+
| `v-html`, `v-text` | :white_check_mark: | / |
1212
| `v-once` | :white_check_mark: | / |
1313

1414
## Dynamic Arguments

packages/compiler/src/compile.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { transformVOnce } from './transforms/vOnce'
3131
import { transformVShow } from './transforms/vShow'
3232
import { transformVSlot } from './transforms/vSlot'
3333
import { transformVSlots } from './transforms/vSlots'
34+
import { transformVText } from './transforms/vText'
3435
import type { ExpressionStatement, JSXElement, JSXFragment } from '@babel/types'
3536
import type { CompilerOptions as BaseCompilerOptions } from '@vue/compiler-dom'
3637

@@ -123,7 +124,6 @@ export function getBaseTransformPreset(): TransformPreset {
123124
transformTemplateRef,
124125
transformText,
125126
transformElement,
126-
transformVSlots,
127127
transformVSlot,
128128
transformChildren,
129129
],
@@ -133,6 +133,8 @@ export function getBaseTransformPreset(): TransformPreset {
133133
model: transformVModel,
134134
show: transformVShow,
135135
html: transformVHtml,
136+
text: transformVText,
137+
slots: transformVSlots,
136138
},
137139
]
138140
}

packages/compiler/src/transforms/vSlots.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,24 @@
11
import { createCompilerError, ErrorCodes } from '@vue/compiler-dom'
22
import { IRSlotType } from '../ir'
33
import { isJSXComponent, resolveExpression, resolveLocation } from '../utils'
4-
import type { NodeTransform } from '../transform'
5-
import type { JSXAttribute } from '@babel/types'
4+
import type { DirectiveTransform } from '../transform'
65

7-
export const transformVSlots: NodeTransform = (node, context) => {
8-
if (node.type !== 'JSXElement' || !isJSXComponent(node)) return
6+
export const transformVSlots: DirectiveTransform = (dir, node, context) => {
7+
if (!isJSXComponent(node)) return
98

10-
const {
11-
openingElement: { attributes },
12-
children,
13-
} = node
14-
15-
const vSlotsIndex = attributes.findIndex(
16-
(attr) =>
17-
attr.type === 'JSXAttribute' && attr.name.name.toString() === 'v-slots',
18-
)
19-
const vSlotsDir = attributes[vSlotsIndex] as JSXAttribute
20-
if (vSlotsDir && vSlotsDir.value?.type === 'JSXExpressionContainer') {
21-
attributes.splice(vSlotsIndex, 1)
9+
if (dir.value?.type === 'JSXExpressionContainer') {
2210
context.slots = [
2311
{
2412
slotType: IRSlotType.EXPRESSION,
25-
slots: resolveExpression(vSlotsDir.value.expression, context),
13+
slots: resolveExpression(dir.value.expression, context),
2614
},
2715
]
2816

29-
if (children.length) {
17+
if (node.children.length) {
3018
context.options.onError(
3119
createCompilerError(
3220
ErrorCodes.X_V_SLOT_MIXED_SLOT_USAGE,
33-
resolveLocation(children[0].loc, context),
21+
resolveLocation(node.children[0].loc, context),
3422
),
3523
)
3624
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const compileWithSlots = makeCompile({
1515
nodeTransforms: [
1616
transformText,
1717
transformElement,
18-
transformVSlots,
1918
transformVSlot,
2019
transformChildren,
2120
],
2221
directiveTransforms: {
2322
bind: transformVBind,
2423
on: transformVOn,
24+
slots: transformVSlots,
2525
},
2626
})
2727

0 commit comments

Comments
 (0)