提交 a0f6d174 编写于 作者: Q qiang

feat(mp): v-text

上级 0127d67d
import { assert } from './testUtils'
describe('compiler: transform v-text', () => {
test('basic', () => {
assert(
`<view>{{text}}</view>`,
`<view>{{a}}</view>`,
`(_ctx, _cache) => {
return { a: _t(_ctx.text) }
}`
)
assert(
`<view v-text="text"></view>`,
`<view>{{a}}</view>`,
`(_ctx, _cache) => {
return { a: _t(_ctx.text) }
}`
)
assert(
`<view v-text="'text'"></view>`,
`<view>{{a}}</view>`,
`(_ctx, _cache) => {
return { a: _t('text') }
}`
)
})
test('self closing', () => {
assert(
`<view v-text="text"/>`,
`<view>{{a}}</view>`,
`(_ctx, _cache) => {
return { a: _t(_ctx.text) }
}`
)
})
})
......@@ -18,6 +18,7 @@ import { transformSlot } from './transforms/vSlot'
import { transformRoot } from './transforms/transformRoot'
import { transformTag } from './transforms/transformTag'
import { transformHtml } from './transforms/vHtml'
import { transformText } from './transforms/vText'
import { transformAttr } from './transforms/transformAttr'
export type TransformPreset = [
......@@ -38,6 +39,7 @@ export function getBaseTransformPreset({
transformAttr,
transformTag,
transformHtml,
transformText,
transformIf,
transformFor,
transformSlot,
......
import {
isElementNode,
} from '@dcloudio/uni-cli-shared'
import {
ElementTypes,
findDir,
NodeTypes,
} from '@vue/compiler-core'
import { NodeTransform } from '../transform'
export const transformText: NodeTransform = (node, _) => {
if (!isElementNode(node)) {
return
}
const dir = findDir(node, 'text')
if (!dir) {
return
}
// remove v-text
node.props.splice(node.props.indexOf(dir), 1)
if (node.tagType !== ElementTypes.ELEMENT) {
return
}
node.isSelfClosing = false
node.children = [{
type: NodeTypes.INTERPOLATION,
loc: dir.exp!.loc,
content: dir.exp!
}]
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册