@imgcook/dsl-helper
Helper for davinci dsl developer.
Usage
printer
代码打印
const partsJson = [
{
type: 'line',
parts: ["'use strict';"]
},
{
type: 'line',
parts: ['']
},
{
type: 'line',
parts: ["import styles from './lib/style.js';"]
}
];
helper.printer(partsJson);
('use strict');
import styles from './lib/style.js';
parser
代码解析
'use strict';
import styles from './lib/style.js';
helper.parser(fileString);
[
{
type: 'line',
parts: ["'use strict';"]
},
{
type: 'line',
parts: ['']
},
{
type: 'line',
parts: ["import styles from './lib/style.js';"]
}
];
utils
代码打印工具函数
helper.utils.isArray([1,2,3]);
helper.utils.line(['hello');
helper.utils.string(['hello']);
helper.utils.indentTab();
helper.utils.setIndentTab();
helper.utils.countIndent(' hello');
weexProcessor
weex 下的样式处理器
let styles = helper.weexProcessor.filter({
width: '100px',
left: '0px',
lines: 0,
'margin-bottom': '0px',
hehehee: 'hehehe'
});
console.log(styles);
webProcessor
web 下的样式处理器
let styles = helper.weexProcessor.filter({
width: '100px',
left: '0px',
lines: 0,
'margin-bottom': '0px',
hehehee: 'hehehe'
});
console.log(styles);
processLayoutData
对 layout 的样式整体过滤一次
let layoutData = {
style: {
'width': '100px',
'left': '0px',
'lines': 0,
'margin-bottom': '0px',
'hehehee': 'hehehe',
},
children: [
{
style: {
'width': '100px',
'left': '0px',
'lines': 0,
'margin-bottom': '0px',
'hehehee': 'hehehe',
},
},
{
style: {
'width': '100px',
'left': '0px',
'lines': 0,
'margin-bottom': '0px',
'hehehee': 'hehehe',
},
}
]
};
let styles = helper.processLayoutData(layoutData, helper.weexProcessor);
console.log(styles);
{
"style":{
"width":"100px",
"left":"0px",
"lines":0
},
"children":[
{
"style":{
"width":"100px",
"left":"0px",
"lines":0
}
},{
"style":{
"width":"100px",
"left":"0px",
"lines":0
}
}
]
}
syntactic
分析函数,优先使用 acorn 分析,如果异常则使用原来的正则分析。
DSL 引擎,后端,前端都可使用。
helper.syntactic.parseFunction( `/**
* @author 卡狸
* @version 1.0
* @param
* @return
*/
export default function(args1, args2 = {}){
let text = 'hello world';
return {
arg1,
args2: text,
}
};
` );
{
"success": true,
"message": "",
"data": {
"parser": "Acorn",
"args": [
"args1",
"args2"
],
"params": [
{
"argsName": "args1",
"argsContent": "args1",
"argsType": "Identifier"
},
{
"argsName": "args2",
"argsContent": "args2 = {}",
"argsType": "AssignmentPattern"
}
],
"content": "\tlet text = 'hello world';\n\treturn {\n\t\targ1,\n\t\targs2: text,\n\t}",
"genContent": " let text = 'hello world';\n return {\n arg1,\n args2: text\n };",
"return": [
{
"key": "arg1",
"valueType": "Identifier"
},
{
"key": "args2",
"valueType": "Identifier"
}
]
}
}
Unit Test
mocha ./test/index.js