Skip to content

Commit 4fd695d

Browse files
authored
feat: optimize expression, flatten i18n json, add validations for lite card (#69)
* fix: add judgement for whether the attr is an expression Signed-off-by: lileirjyb <lileirjyb@vivo.com> * fix: modify entry and pages fields required validation for card project Signed-off-by: lileirjyb <lileirjyb@vivo.com> * feat: optimize expression result in prefix way and mark keys with $ sign in template and actions Signed-off-by: lileirjyb <lileirjyb@vivo.com> * fix: move styleObjectId into template node Signed-off-by: lileirjyb <lileirjyb@vivo.com> * feat: flattten i18n json for lite card Signed-off-by: lileirjyb <lileirjyb@vivo.com> * feat: add actions validator Signed-off-by: lileirjyb <lileirjyb@vivo.com> * fix: add dynamic component validation Signed-off-by: lileirjyb <lileirjyb@vivo.com> * fix: modify simple expression result Signed-off-by: lileirjyb <lileirjyb@vivo.com> * fix: update expresssion npm lib version Signed-off-by: lileirjyb <lileirjyb@vivo.com> --------- Signed-off-by: lileirjyb <lileirjyb@vivo.com>
1 parent eb55140 commit 4fd695d

File tree

16 files changed

+572
-49
lines changed

16 files changed

+572
-49
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"rules": {
1010
"camelcase": 0,
1111
"no-debugger": 1,
12-
"no-unused-vars": 1
12+
"no-unused-vars": 1,
13+
"no-control-regex": 1
1314
}
1415
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2021-present, the hapjs-platform Project Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { checkParams } from './validator'
7+
8+
function parse(actionObj) {
9+
/**
10+
* "actionObj": {
11+
"jumpWithParams": {
12+
"type": "router",
13+
"url": "{{jumpParamsUrl}}",
14+
"params": {
15+
"type": "{{jumpParam}}"
16+
}
17+
},
18+
}
19+
*/
20+
if (actionObj && Object.prototype.toString.call(actionObj) !== '[object Object]') {
21+
throw new Error(`<data> 事件 actions 必须为 Object 对象`)
22+
}
23+
24+
// 检查params参数合法性
25+
Object.keys(actionObj).forEach((key) => {
26+
checkParams(actionObj[key].params, 1)
27+
})
28+
29+
return {
30+
jsonAction: actionObj
31+
}
32+
}
33+
34+
export default {
35+
parse
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2021-present, the hapjs-platform Project Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import exp from '../template/exp'
7+
8+
function isReserved(str) {
9+
// $开头
10+
const c = (str + '').charCodeAt(0)
11+
return c === 0x24
12+
}
13+
14+
function checkParams(paramsObj, dep) {
15+
if (!paramsObj || Object.prototype.toString.call(paramsObj) !== '[object Object]') return
16+
17+
Object.keys(paramsObj).forEach((key) => {
18+
if (isReserved(key)) {
19+
throw new Error(`<data> 事件 actions 中 params 参数名不能以 “$” 开头`)
20+
}
21+
22+
if (exp.isExpr(paramsObj[key]) && dep > 1) {
23+
throw new Error(`<data> 事件 actions 中 params 参数值只支持一级结构中绑定变量`)
24+
}
25+
26+
checkParams(paramsObj[key], dep + 1)
27+
})
28+
}
29+
30+
export { checkParams }

packages/hap-compiler/src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
'use strict'
77
import parse5 from 'parse5'
88
import templater from './template'
9+
import actioner from './actions'
910
import styler from './style'
1011
import scripter from './script'
1112
import { serialize } from './utils'
1213

13-
export { scripter, styler, templater }
14+
export { scripter, styler, templater, actioner }
1415
export * from './style'
1516

1617
/**
@@ -156,5 +157,16 @@ function parseScript(source) {
156157
return { parsed: parsed }
157158
}
158159

159-
export { parseFragmentsWithCache, parseTemplate, parseStyle, parseScript, serialize }
160+
/**
161+
* 解析actions
162+
* @param {String} jsonObj - actions对象
163+
* @returns {Object}
164+
*/
165+
function parseActions(jsonObj) {
166+
const { jsonAction } = actioner.parse(jsonObj)
167+
const parsed = JSON.stringify(jsonAction)
168+
return { parsed }
169+
}
170+
171+
export { parseFragmentsWithCache, parseTemplate, parseStyle, parseScript, parseActions, serialize }
160172
export { ENTRY_TYPE, FRAG_TYPE, isEmptyObject } from './utils'

packages/hap-compiler/src/template/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function traverse(node, output, previousNode, conditionList, options) {
174174
output.log.push({
175175
line: node.__location.line,
176176
column: node.__location.col,
177-
reason: `Warn: 组件 ${node.tagName} 不支持文本内容作为字节点`
177+
reason: `Warn: 组件 ${node.tagName} 不支持文本内容作为子节点`
178178
})
179179
}
180180
}

packages/hap-compiler/src/template/validator.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ const tagNatives = {
261261
}
262262
},
263263
component: {
264+
supportCard: true,
264265
excludeRoot: true,
265266
attrs: {
266267
extendCommon: false, // 不支持通用属性
@@ -1827,15 +1828,17 @@ function checkCustomDirective(name, value, output, node) {
18271828
function checkAttr(name, value, output, tagName, locationInfo, options) {
18281829
if (name && isValidValue(value)) {
18291830
if (shouldConvertPath(name, value, tagName)) {
1830-
// 判断路径下资源是否存在
1831-
const hasFile = fileExists(value, options.filePath)
1832-
if (!hasFile) {
1833-
output.log.push({
1834-
line: locationInfo.line,
1835-
column: locationInfo.column,
1836-
reason:
1837-
'WARN: ' + tagName + ' 属性 ' + name + ' 的值 ' + value + ' 下不存在对应的文件资源'
1838-
})
1831+
if (!exp.isExpr(value)) {
1832+
// 若路径不包含表达式,判断路径下资源是否存在
1833+
const hasFile = fileExists(value, options.filePath)
1834+
if (!hasFile) {
1835+
output.log.push({
1836+
line: locationInfo.line,
1837+
column: locationInfo.column,
1838+
reason:
1839+
'WARN: ' + tagName + ' 属性 ' + name + ' 的值 ' + value + ' 下不存在对应的文件资源'
1840+
})
1841+
}
18391842
}
18401843
// 转换为以项目源码为根的绝对路径
18411844
value = resolvePath(value, options.filePath)
@@ -2017,5 +2020,6 @@ export default {
20172020
getTagChildren,
20182021
isSupportedSelfClosing,
20192022
isEmptyElement,
2020-
isNotTextContentAtomic
2023+
isNotTextContentAtomic,
2024+
isExpr: exp.isExpr
20212025
}

packages/hap-dsl-xvm/src/loaders/action-loader.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
* Copyright (c) 2024-present, the hapjs-platform Project Contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
import { parseActions } from '@hap-toolkit/compiler'
56

67
export default function actionLoader(source) {
7-
let jsonObj = {}
8+
let actionStr = ''
89
try {
910
const obj = JSON.parse(source)
10-
jsonObj = obj.actions || {}
11+
const jsonObj = obj.actions || {}
12+
const { parsed } = parseActions(jsonObj)
13+
actionStr = parsed
1114
} catch (e) {
12-
throw new Error(`Invalid <data> in ${this.resourcePath}:: ${e}`)
15+
throw new Error(`Invalid <data> in ${this.resourcePath}\n${e}`)
1316
}
14-
return `module.exports = ${JSON.stringify(jsonObj)}`
17+
return `module.exports = ${actionStr}`
1518
}

packages/hap-packager/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"babel.tree.config.js"
3030
],
3131
"dependencies": {
32+
"@aiot-toolkit/card-expression": "^1.0.8",
3233
"@babel/core": "^7.9.6",
3334
"@babel/generator": "^7.9.6",
3435
"@babel/parser": "^7.9.6",
@@ -41,6 +42,7 @@
4142
"@hap-toolkit/shared-utils": "1.9.14",
4243
"@jayfate/path": "^0.0.13",
4344
"babel-loader": "^8.1.0",
45+
"flat": "^5.0.2",
4446
"fs-extra": "^10.0.0",
4547
"glob": "^7.1.6",
4648
"hash-sum": "^1.0.2",

packages/hap-packager/src/common/constant.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ const LOADER_PATH_STYLE = {
3737
type: 'styles'
3838
}
3939

40+
const LOADER_PATH_TEMPLATE = {
41+
path: '/template-loader.js',
42+
type: 'template'
43+
}
44+
4045
/**
4146
* loader path
4247
*/
4348
const LOADER_INFO_LIST = [
4449
LOADER_PATH_UX,
45-
{
46-
path: '/template-loader.js',
47-
type: 'template'
48-
},
4950
LOADER_PATH_STYLE,
51+
LOADER_PATH_TEMPLATE,
5052
{
5153
path: '/script-loader.js',
5254
type: 'script'
@@ -75,5 +77,6 @@ export {
7577
BUILD_INFO_FILE,
7678
LOADER_PATH_UX,
7779
LOADER_PATH_STYLE,
80+
LOADER_PATH_TEMPLATE,
7881
LOADER_INFO_LIST
7982
}

packages/hap-packager/src/common/utils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,6 @@ export function genPriorities(manifest, skeletonConf) {
204204
const entry = manifest.router.entry
205205
const entrySkFiles = getEntrySkeleton(skeletonConf, entry)
206206
priorities.splice(5, 0, ...entrySkFiles, new RegExp(`^${entry}/$`), new RegExp(`^${entry}/.+`))
207-
} else {
208-
colorconsole.error(`manifest.json 中未配置入口页面 router.entry`)
209207
}
210208
return priorities
211209
}

0 commit comments

Comments
 (0)