feat: 简单的语法检测

This commit is contained in:
msojocs 2022-09-04 08:22:20 +08:00
parent 43211e8a46
commit f7c5af4498
3 changed files with 78 additions and 0 deletions

48
test/runner/code.js Normal file
View File

@ -0,0 +1,48 @@
const keyword = `abstract|arguments|boolean|break|byte|case|catch|char|class|const|continue|debugger|default|delete|do|double|else|enum|eval|export|extends|false|final|finally|float|for|function|goto|if|implements|import|in|instanceof|int|interface|let|long|native|new|null|package|private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|var|void|volatile|while|with|yield|Array|Date|eval|function|hasOwnProperty|Infinity|isFinite|isNaN|isPrototypeOf|length|Math|NaN|name|Number|Object|prototype|String|toString|undefined|valueOf`;
const keywords = keyword.split("|");
function delVariables(code) {
code = " " + code + " ";
let pos1 = 0,
pos2 = 0;
let len = code.length;
let isVariables = false;
let ret = "";
while (pos1 < len) {
pos2++;
if (isVariables) {
if (
code
.substring(pos2, pos2 + 2)
.replace(/[0-9a-zA-Z_][^a-zA-Z_0-9]/g, "") == ""
) {
isVariables = false;
const vv = code.substring(pos1, pos2 + 1);
if (keywords.includes(vv)) {
ret += vv;
//System.out.println("vv="+vv);
}
pos1 = pos2 + 1;
}
} else {
if (
code
.substring(pos2, pos2 + 2)
.replace(/[^\._a-zA-Z][_a-zA-Z]/, "") == ""
) {
isVariables = true;
ret += code.substring(pos1, pos2 + 1);
//System.out.println(code.substring(pos1,pos2+1));
pos1 = pos2 + 1;
}
}
if (pos2 == len) break;
}
return ret.toString().trim();
//return code.replaceAll("(?<=([^\\._a-zA-Z]))[a-zA-Z_]+[0-9_a-zA-Z]*(?=([^a-zA-Z_]))", "");
}
export{
delVariables
}

View File

@ -1,9 +1,24 @@
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 path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const originDE = assert.deepEqual
assert.deepEqual = function(){
// console.log(arguments[0])
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);
}
}
describe("wcc - lla", function () { describe("wcc - lla", function () {
describe("lla: node output should deep equal with wine", function () { describe("lla: node output should deep equal with wine", function () {
// afterEach(function(){ // afterEach(function(){

View File

@ -1,9 +1,24 @@
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 path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const originDE = assert.deepEqual
assert.deepEqual = function(){
// console.log(arguments[0])
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);
}
}
describe("wcc - llw", function () { describe("wcc - llw", function () {
describe("llw: node output should deep equal with wine", function () { describe("llw: node output should deep equal with wine", function () {
// afterEach(function(){ // afterEach(function(){