Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
223eaa5b
U
uni-app
项目概览
DCloud
/
uni-app
3 个月 前同步成功
通知
720
Star
38705
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
7
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
7
Issue
7
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
223eaa5b
编写于
8月 02, 2023
作者:
DCloud-WZF
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wip(uts): vBind
上级
35436978
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
24 addition
and
38 deletion
+24
-38
packages/uni-app-uts/__tests__/transforms/vBind.spec.ts
packages/uni-app-uts/__tests__/transforms/vBind.spec.ts
+13
-3
packages/uni-app-uts/src/plugins/uvue/compiler/transforms/vBind.ts
...uni-app-uts/src/plugins/uvue/compiler/transforms/vBind.ts
+4
-4
packages/uni-app-uts/src/plugins/uvue/compiler/utils.ts
packages/uni-app-uts/src/plugins/uvue/compiler/utils.ts
+7
-31
未找到文件。
packages/uni-app-uts/__tests__/transforms/vBind.spec.ts
浏览文件 @
223eaa5b
...
...
@@ -157,11 +157,21 @@ describe('compiler: v-bind', () => {
]), null, 8 /* PROPS */, [\"onClick\"])`
)
})
test
(
'
style with object complex
expression
'
,
()
=>
{
test
(
'
object value width all number
expression
'
,
()
=>
{
assert
(
`<view
:style="{'opcity': 1 - (scrollTop*3>100?100:scrollTop*3)/100}"></view
>`
,
`<view
class="search" @click="toSearchPage" :style="{'opacity': 1 + 1}" /
>`
,
`createElementVNode(\"view\", new Map<string, any | null>([
[\"style\", new Map<string, any | null>([['opcity', '1 - (_ctx.scrollTop*3>100?100:_ctx.scrollTop*3)/100']])]
[\"class\", \"search\"],
[\"onClick\", _ctx.toSearchPage],
[\"style\", new Map<string, any | null>([['opacity', 1 + 1]])]
]), null, 8 /* PROPS */, [\"onClick\"])`
)
})
test
(
'
object value width expression (with data)
'
,
()
=>
{
assert
(
`<view :style="{'opacity': count > 0.3 ? 1 : count * 3, 'color': 'red'}" />`
,
`createElementVNode(\"view\", new Map<string, any | null>([
[\"style\", new Map<string, any | null>([['opacity', _ctx.count > 0.3 ? 1 : _ctx.count * 3], ['color', 'red']])]
]))`
)
})
...
...
packages/uni-app-uts/src/plugins/uvue/compiler/transforms/vBind.ts
浏览文件 @
223eaa5b
...
...
@@ -8,7 +8,7 @@ import {
import
{
createCompilerError
,
ErrorCodes
}
from
'
../errors
'
import
{
camelize
}
from
'
@vue/shared
'
import
{
CAMELIZE
}
from
'
@vue/compiler-core
'
import
{
objectExp
,
objectStringToMapString
}
from
'
../utils
'
import
{
OBJECT_EXP
,
objectStringToMapString
}
from
'
../utils
'
import
{
isString
}
from
'
@vue/shared
'
// v-bind without arg is handled directly in ./transformElements.ts due to it affecting
...
...
@@ -58,10 +58,10 @@ export const transformBind: DirectiveTransform = (dir, _node, context) => {
}
}
if
((
exp
as
any
).
content
&&
objectExp
.
test
((
exp
as
any
).
content
))
{
if
((
exp
as
any
).
content
&&
OBJECT_EXP
.
test
((
exp
as
any
).
content
))
{
;(
exp
as
any
).
content
=
objectStringToMapString
((
exp
as
any
).
content
)
}
else
if
((
exp
as
any
).
children
)
{
// {
'opcity': 1 - (scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100
} to map
// {
'opacity': count > 0.3 ? 1: count * 3, 'color': 'red'
} to map
// TODO: 考虑更多边缘情况
if
(
isString
((
exp
as
any
).
children
[
0
])
&&
...
...
@@ -83,7 +83,7 @@ export const transformBind: DirectiveTransform = (dir, _node, context) => {
}
;(
exp
as
any
).
children
.
forEach
(
(
child
:
ExpressionNode
|
string
,
index
:
number
)
=>
{
if
(
isString
(
child
)
&&
objectExp
.
test
(
child
))
{
if
(
isString
(
child
)
&&
OBJECT_EXP
.
test
(
child
))
{
;(
exp
as
any
).
children
[
index
]
=
objectStringToMapString
(
child
)
}
}
...
...
packages/uni-app-uts/src/plugins/uvue/compiler/utils.ts
浏览文件 @
223eaa5b
import
{
CompilerOptions
}
from
'
./options
'
import
{
isObject
,
isString
}
from
'
@vue/shared
'
import
{
isObject
}
from
'
@vue/shared
'
export
function
genRenderFunctionDecl
({
targetLanguage
,
...
...
@@ -10,10 +10,10 @@ export function genRenderFunctionDecl({
}
function
${
filename
}
Render(): VNode | null`
}
export
const
objectExp
=
/
\{[\s\S]
*
\}
/g
export
const
OBJECT_EXP
=
/
\{[\s\S]
*
\}
/g
export
function
objectStringToMapString
(
content
:
string
,
wrap
=
true
):
string
{
content
=
content
.
replace
(
/
\n
/g
,
''
)
const
matched
=
content
.
match
(
objectExp
)
!
[
0
]
const
matched
=
content
.
match
(
OBJECT_EXP
)
!
[
0
]
const
matchedObj
=
stringToData
(
matched
)
as
Record
<
any
,
any
>
const
mapConstructor
=
convertObjectToMapString
(
matchedObj
)
return
content
.
replace
(
...
...
@@ -65,18 +65,14 @@ function findObjectValueInString(
num
--
}
if
(
char
===
'
,
'
&&
num
===
0
)
{
const
value
=
removeExtraQuotationMarks
(
str
.
substring
(
startIndex
,
i
).
trim
()
)
const
value
=
str
.
substring
(
startIndex
,
i
).
trim
()
return
{
value
:
isComplexExpressionString
(
value
)
?
stringToData
(
value
)
:
value
,
endIndex
:
i
,
}
}
if
(
i
===
str
.
length
-
1
&&
num
===
0
)
{
const
value
=
removeExtraQuotationMarks
(
str
.
substring
(
startIndex
,
str
.
length
).
trim
()
)
const
value
=
str
.
substring
(
startIndex
,
str
.
length
).
trim
()
return
{
value
:
isComplexExpressionString
(
value
)
?
stringToData
(
value
)
:
value
,
endIndex
:
i
,
...
...
@@ -100,16 +96,12 @@ function stringToArray(str: string): any[] {
num
--
}
if
(
char
===
'
,
'
&&
num
===
0
)
{
const
item
=
removeExtraQuotationMarks
(
str
.
substring
(
preIndex
+
1
,
i
).
trim
()
)
const
item
=
str
.
substring
(
preIndex
+
1
,
i
).
trim
()
result
.
push
(
isComplexExpressionString
(
item
)
?
stringToData
(
item
)
:
item
)
preIndex
=
i
}
if
(
i
===
str
.
length
-
1
&&
num
===
0
)
{
const
item
=
removeExtraQuotationMarks
(
str
.
substring
(
preIndex
+
1
,
str
.
length
).
trim
()
)
const
item
=
str
.
substring
(
preIndex
+
1
,
str
.
length
).
trim
()
item
&&
result
.
push
(
isComplexExpressionString
(
item
)
?
stringToData
(
item
)
:
item
)
}
...
...
@@ -131,11 +123,6 @@ function getKeyValueString(key: string, value: any): string {
}
function
getValueString
(
value
:
any
):
string
{
if
(
isString
(
value
))
{
return
hasExtraQuotationMarks
(
value
)
||
isBooleanString
(
value
)
?
`
${
value
}
`
:
`'
${
value
}
'`
}
if
(
Array
.
isArray
(
value
))
{
return
`[
${
value
.
map
((
item
)
=>
getValueString
(
item
)).
join
(
'
,
'
)}
]`
}
...
...
@@ -156,21 +143,10 @@ function hasExtraQuotationMarks(str: string): boolean {
return
str
.
startsWith
(
"
'
"
)
||
str
.
endsWith
(
"
'
"
)
}
function
hasExtraStartQuotationMarks
(
str
:
string
):
boolean
{
return
/^
[
'"
]
.*
[
'"
]
$/
.
test
(
str
)
}
function
removeStartAndEndChar
(
str
:
string
):
string
{
return
str
.
substring
(
1
,
str
.
length
-
1
)
}
function
removeExtraQuotationMarks
(
str
:
string
):
string
{
if
(
hasExtraStartQuotationMarks
(
str
))
{
return
removeStartAndEndChar
(
str
)
}
return
str
}
function
isComplexExpressionString
(
str
:
string
):
boolean
{
return
/
[
{[
]
/
.
test
(
str
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录