mirror of
https://github.com/msojocs/wx-compiler.git
synced 2025-07-19 00:00:04 +08:00
feat: ast
This commit is contained in:
parent
66032edce7
commit
bcc876553b
@ -1,6 +1,6 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
require: ["@babel/register"],
|
require: ["@babel/register"],
|
||||||
recursive: true,
|
recursive: true,
|
||||||
spec: "test/**/*.test.js",
|
spec: "test/**/llw.test.js",
|
||||||
timeout: 20000,
|
timeout: 20000,
|
||||||
}
|
}
|
21
test/runner/ast-helper.js
Normal file
21
test/runner/ast-helper.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
const parser = require("@babel/parser");
|
||||||
|
const tool = require('./tool')
|
||||||
|
|
||||||
|
|
||||||
|
const code2ast = (str)=>{
|
||||||
|
|
||||||
|
const nodeData = JSON.parse(str)
|
||||||
|
|
||||||
|
for(let key in nodeData.generateFunctionContent){
|
||||||
|
const nData = nodeData.generateFunctionContent[key]
|
||||||
|
|
||||||
|
let nodeAST = JSON.parse(JSON.stringify(parser.parse(nData)))
|
||||||
|
nodeAST = tool.removeVariables(nodeAST)
|
||||||
|
nodeData.generateFunctionContent[key] = nodeAST
|
||||||
|
}
|
||||||
|
return nodeData
|
||||||
|
}
|
||||||
|
module.exports = {
|
||||||
|
code2ast
|
||||||
|
}
|
43
test/runner/tool.js
Normal file
43
test/runner/tool.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
|
||||||
|
const clearNum = ["start", "end", "line", "column", "index", 'parenStart']
|
||||||
|
|
||||||
|
const FUNC = {
|
||||||
|
'[object Object]': (ast)=>{
|
||||||
|
for(let key in ast){
|
||||||
|
const type = Object.prototype.toString.call(ast[key])
|
||||||
|
// console.log(key, ' - ', type)
|
||||||
|
if(clearNum.includes(key))ast[key] = 0
|
||||||
|
else if(FUNC[type]){
|
||||||
|
ast[key] = FUNC[type](ast[key])
|
||||||
|
}else{
|
||||||
|
// console.log('无法识别的类型:', type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(ast.type && ast.type === 'Identifier'){
|
||||||
|
ast.name = ''
|
||||||
|
ast.loc.identifierName = ''
|
||||||
|
}
|
||||||
|
return ast
|
||||||
|
},
|
||||||
|
'[object Array]': (arr)=>{
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
|
const ele = arr[i];
|
||||||
|
const type = Object.prototype.toString.call(ele)
|
||||||
|
arr[i] = FUNC[type](ele)
|
||||||
|
}
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
const removeVariables = (ast)=>{
|
||||||
|
const type = Object.prototype.toString.call(ast)
|
||||||
|
// console.log(type)
|
||||||
|
if(FUNC[type]){
|
||||||
|
return FUNC[type](ast)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
removeVariables
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
const assert = require("assert");
|
const assert = require("assert");
|
||||||
const node = require("../../runner/node");
|
const node = require("../../runner/node");
|
||||||
const wine = require("../../runner/wine");
|
const wine = require("../../runner/wine");
|
||||||
const CODE = require("../../runner/code");
|
const ASTHelper = require("../../runner/ast-helper");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
|
||||||
@ -11,11 +11,10 @@ assert.deepEqual = function(){
|
|||||||
try {
|
try {
|
||||||
originDE.apply(this, arguments)
|
originDE.apply(this, arguments)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log('\t默认deepEqual检测失败,尝试检测语法')
|
||||||
const dw = CODE.delVariables(JSON.stringify(arguments[0]))
|
const dw = ASTHelper.code2ast(JSON.stringify(arguments[0]))
|
||||||
const dn = CODE.delVariables(JSON.stringify(arguments[1]))
|
const dn = ASTHelper.code2ast(JSON.stringify(arguments[1]))
|
||||||
console.log('\tdeepEqual检测失败,尝试检测语法(测试中)')
|
originDE(dw, dn);
|
||||||
assert.equal(dw, dn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user