mirror of
https://github.com/msojocs/wx-compiler.git
synced 2025-07-19 00:00:04 +08:00
feat: 简单的语法检测
This commit is contained in:
parent
43211e8a46
commit
f7c5af4498
48
test/runner/code.js
Normal file
48
test/runner/code.js
Normal 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
|
||||||
|
}
|
@ -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(){
|
||||||
|
@ -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(){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user