feat: ast

This commit is contained in:
msojocs 2022-09-07 21:32:42 +08:00
parent 66032edce7
commit bcc876553b
4 changed files with 70 additions and 7 deletions

View File

@ -1,6 +1,6 @@
module.exports = {
require: ["@babel/register"],
recursive: true,
spec: "test/**/*.test.js",
spec: "test/**/llw.test.js",
timeout: 20000,
}

21
test/runner/ast-helper.js Normal file
View 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
View 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
}

View File

@ -1,7 +1,7 @@
const assert = require("assert");
const node = require("../../runner/node");
const wine = require("../../runner/wine");
const CODE = require("../../runner/code");
const ASTHelper = require("../../runner/ast-helper");
const path = require("path");
const fs = require("fs");
@ -11,11 +11,10 @@ assert.deepEqual = function(){
try {
originDE.apply(this, arguments)
} catch (error) {
const dw = CODE.delVariables(JSON.stringify(arguments[0]))
const dn = CODE.delVariables(JSON.stringify(arguments[1]))
console.log('\tdeepEqual检测失败尝试检测语法测试中')
assert.equal(dw, dn);
console.log('\t默认deepEqual检测失败尝试检测语法')
const dw = ASTHelper.code2ast(JSON.stringify(arguments[0]))
const dn = ASTHelper.code2ast(JSON.stringify(arguments[1]))
originDE(dw, dn);
}
}