Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
D-GSQ
uni-app
提交
8c7b3e0c
U
uni-app
项目概览
D-GSQ
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8c7b3e0c
编写于
2月 21, 2023
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: native component v-model with v-for
上级
0455765d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
52 addition
and
31 deletion
+52
-31
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
...es/uni-template-compiler/__tests__/compiler-extra.spec.js
+34
-24
packages/uni-template-compiler/lib/script/traverse/data/event.js
...s/uni-template-compiler/lib/script/traverse/data/event.js
+11
-3
packages/uni-template-compiler/lib/script/traverse/data/model.js
...s/uni-template-compiler/lib/script/traverse/data/model.js
+5
-4
packages/uni-template-compiler/lib/script/traverse/visitor.js
...ages/uni-template-compiler/lib/script/traverse/visitor.js
+2
-0
未找到文件。
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
浏览文件 @
8c7b3e0c
...
...
@@ -21,22 +21,22 @@ describe('mp:compiler-extra', () => {
'
<view @touchmove="a.touchmove">{{t.a}}{{t[
\'
a
\'
]}}{{t.a(b)}}{{t[
\'
a
\'
](b)}}{{u.t.a(b)}}{{u.t.a}}</view>
'
,
'
<view bindtouchmove="{{a.touchmove}}">{{t.a+t[
\'
a
\'
]+t.a(b)+t[
\'
a
\'
](b)+$root.g0+u.t.a}}</view>
'
,
'
with(this){var g0=u.t.a(b);$mp.data=Object.assign({},{$root:{g0:g0}})}
'
,
{
filterModules
:
{
t
:
{},
a
:
{}
filterModules
:
{
t
:
{},
a
:
{}
}
}
}
)
assertCodegen
(
/* eslint-disable no-template-curly-in-string */
'
<view v-for="(item, index) in a.test()" :key="index">{{item}}</view>
'
,
'
<block wx:for="{{a.test()}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view>{{item}}</view></block>
'
,
'
with(this){}
'
,
{
filterModules
:
{
t
:
{},
a
:
{}
filterModules
:
{
t
:
{},
a
:
{}
}
}
}
)
})
...
...
@@ -45,43 +45,43 @@ describe('mp:compiler-extra', () => {
'
<view></view>
'
,
'
<view class="data-v-1"></view>
'
,
undefined
,
{
scopeId
:
'
data-v-1
'
}
scopeId
:
'
data-v-1
'
}
)
assertCodegen
(
'
<view class="view"></view>
'
,
'
<view class="view data-v-2"></view>
'
,
undefined
,
{
scopeId
:
'
data-v-2
'
}
scopeId
:
'
data-v-2
'
}
)
assertCodegen
(
'
<view :class="[{ active: isActive }, errorClass]"></view>
'
,
'
<view class="{{[
\'
data-v-3
\'
,[(isActive)?
\'
active
\'
:
\'\'
],errorClass]}}"></view>
'
,
undefined
,
{
scopeId
:
'
data-v-3
'
}
scopeId
:
'
data-v-3
'
}
)
assertCodegen
(
'
<view class="static" :class="[{ active: isActive }, errorClass]"></view>
'
,
'
<view class="{{[
\'
static
\'
,
\'
data-v-4
\'
,[(isActive)?
\'
active
\'
:
\'\'
],errorClass]}}"></view>
'
,
undefined
,
{
scopeId
:
'
data-v-4
'
}
scopeId
:
'
data-v-4
'
}
)
assertCodegen
(
'
<view ref="ref" :class="[{ active: isActive }, errorClass]"></view>
'
,
'
<view data-ref="ref" class="{{[
\'
data-v-5
\'
,
\'
vue-ref
\'
,[(isActive)?
\'
active
\'
:
\'\'
],errorClass]}}"></view>
'
,
undefined
,
{
scopeId
:
'
data-v-5
'
}
scopeId
:
'
data-v-5
'
}
)
assertCodegen
(
'
<view :class="view"></view>
'
,
'
<view class="{{[
\'
data-v-6
\'
,view]}}"></view>
'
,
undefined
,
{
scopeId
:
'
data-v-6
'
}
scopeId
:
'
data-v-6
'
}
)
// assertCodegen(
// '<view :class="view"></view>',
...
...
@@ -94,8 +94,8 @@ describe('mp:compiler-extra', () => {
'
<view :class="view" class="view"></view>
'
,
'
<view class="{{[
\'
view
\'
,
\'
data-v-7
\'
,view]}}"></view>
'
,
undefined
,
{
scopeId
:
'
data-v-7
'
}
scopeId
:
'
data-v-7
'
}
)
// assertCodegen(
// '<view :class="view" class="view"></view>',
...
...
@@ -124,8 +124,8 @@ describe('mp:compiler-extra', () => {
'
<view style="height:100rpx;width:100rpx;">text</view>
'
,
undefined
,
undefined
,
{
transformPx
:
true
}
transformPx
:
true
}
)
})
...
...
@@ -826,6 +826,16 @@ describe('mp:compiler-extra', () => {
'
<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index"><view><view data-event-opts="{{[[
\'
tap
\'
,[[
\'
e0
\'
,[
\'
$event
\'
]]]]]}}" data-event-params="{{({item:item.$orig})}}" bindtap="__e">{{item.m0}}</view></view></block>
'
,
'
with(this){var l0=__map(array(),function(item,index){var $orig=__get_orig(item);var m0=get(item);return{$orig:$orig,m0:m0}});if(!_isMounted){e0=function($event,item){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;return test(item)}}$mp.data=Object.assign({},{$root:{l0:l0}})}
'
)
assertCodegen
(
'
<template v-for="item in [1,2]"><input v-model="values[item]" /></template>
'
,
'
<block wx:for="{{[1,2]}}" wx:for-item="item" wx:for-index="__i0__"><input data-event-opts="{{[[
\'
input
\'
,[[
\'
e0
\'
,[
\'
$event
\'
]]]]]}}" data-event-params="{{({item})}}" value="{{values[item]}}" bindinput="__e"/></block>
'
,
'
with(this){if(!_isMounted){e0=function($event,item){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;$event=$event.target.value;return __set_model(values,item,$event,[])}}}
'
)
assertCodegen
(
'
<template v-for="item in [1,2]"><custom-input v-model="values[item]" /></template>
'
,
'
<block wx:for="{{[1,2]}}" wx:for-item="item" wx:for-index="__i0__"><custom-input bind:input="__e" vue-id="{{
\'
551070e6-1-
\'
+__i0__}}" value="{{values[item]}}" data-event-opts="{{[[
\'
^input
\'
,[[
\'
e0
\'
]]]]}}" data-event-params="{{({item})}}" bind:__l="__l"></custom-input></block>
'
,
'
with(this){if(!_isMounted){e0=function($event,item){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;return __set_model(values,item,$event,[])}}}
'
)
})
it
(
'
generate bool attr
'
,
()
=>
{
assertCodegen
(
...
...
packages/uni-template-compiler/lib/script/traverse/data/event.js
浏览文件 @
8c7b3e0c
const
t
=
require
(
'
@babel/types
'
)
const
parser
=
require
(
'
@babel/parser
'
)
const
template
=
require
(
'
@babel/template
'
).
default
const
{
IDENTIFIER_EVENT
,
...
...
@@ -7,7 +7,8 @@ const {
INTERNAL_EVENT_PROXY
,
ATTR_DATA_EVENT_OPTS
,
ATTR_DATA_EVENT_PARAMS
,
INTERNAL_SET_SYNC
INTERNAL_SET_SYNC
,
INTERNAL_SET_MODEL
}
=
require
(
'
../../../constants
'
)
const
{
...
...
@@ -387,6 +388,13 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
funcPath
.
node
.
params
.
push
(
t
.
identifier
(
name
))
})
if
(
params
.
length
)
{
if
(
!
isCustom
)
{
const
bodyStatements
=
funcPath
.
get
(
'
body.body
'
)
const
returnStatement
=
bodyStatements
[
0
]
if
(
t
.
isReturnStatement
(
returnStatement
)
&&
t
.
isCallExpression
(
returnStatement
.
node
.
argument
)
&&
returnStatement
.
node
.
argument
.
callee
.
name
===
INTERNAL_SET_MODEL
)
{
funcPath
.
node
.
body
.
body
.
unshift
(
template
(
'
$event=$event.target.value
'
)())
}
}
let
argumentsName
=
'
arguments
'
if
(
funcPath
.
isArrowFunctionExpression
())
{
argumentsName
=
'
args
'
...
...
@@ -396,7 +404,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
const
paramsUid
=
funcPath
.
scope
.
generateDeclaredUidIdentifier
().
name
const
dataset
=
ATTR_DATA_EVENT_PARAMS
.
substring
(
5
)
const
code
=
`var
${
datasetUid
}
=
${
argumentsName
}
[
${
argumentsName
}
.length-1].currentTarget.dataset,
${
paramsUid
}
=
${
datasetUid
}
.
${
dataset
.
replace
(
/-
([
a-z
])
/
,
(
_
,
str
)
=>
str
.
toUpperCase
())}
||
${
datasetUid
}
['
${
dataset
}
'],
${
params
.
map
(
item
=>
`
${
item
}
=
${
paramsUid
}
.
${
item
}
`
).
join
(
'
,
'
)}
`
funcPath
.
node
.
body
.
body
.
unshift
(
parser
.
parse
(
code
).
program
.
body
[
0
]
)
funcPath
.
node
.
body
.
body
.
unshift
(
template
(
code
)()
)
}
methods
.
push
(
addEventExpressionStatement
(
funcPath
,
state
,
isComponent
,
isNativeOn
))
}
...
...
packages/uni-template-compiler/lib/script/traverse/data/model.js
浏览文件 @
8c7b3e0c
...
...
@@ -7,13 +7,14 @@ const {
module
.
exports
=
function
processRef
(
paths
,
path
,
state
)
{
const
modelPath
=
paths
.
model
if
(
modelPath
)
{
const
callbackProperty
=
modelPath
.
node
.
value
.
properties
.
find
(
property
=>
{
const
properties
=
modelPath
.
node
.
value
.
properties
const
[
callbackProperty
]
=
properties
.
splice
(
properties
.
findIndex
(
property
=>
{
return
property
.
key
.
name
===
'
callback
'
})
const
valueProperty
=
modelPath
.
node
.
value
.
properties
.
find
(
})
,
1
)
const
valueProperty
=
properties
.
find
(
property
=>
property
.
key
.
name
===
'
value
'
)
const
exprProperty
=
modelPath
.
node
.
value
.
properties
.
find
(
const
exprProperty
=
properties
.
find
(
property
=>
property
.
key
.
name
===
'
expression
'
)
...
...
packages/uni-template-compiler/lib/script/traverse/visitor.js
浏览文件 @
8c7b3e0c
...
...
@@ -230,6 +230,8 @@ module.exports = {
path
=>
path
.
isObjectProperty
()
&&
[
'
on
'
,
'
nativeOn
'
].
includes
(
path
.
node
.
key
.
name
)
)
// path is model.callback
// || path.findParent(path => path.isObjectProperty() && path.node.key.name === 'callback' && t.isFunctionExpression(path.node.value) && t.isObjectProperty(path.parentPath.parentPath) && path.parentPath.parentPath.node.key.name === 'model')
)
{
// event
return
path
.
skip
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录