Skip to content

Commit 9406c00

Browse files
authored
refactor: replace esprima with acorn (Acode-Foundation#1849)
1 parent 4266e36 commit 9406c00

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

package-lock.json

Lines changed: 1 addition & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@
108108
"@xterm/addon-web-links": "^0.11.0",
109109
"@xterm/addon-webgl": "^0.18.0",
110110
"@xterm/xterm": "^5.5.0",
111+
"acorn": "^8.15.0",
111112
"autosize": "^6.0.1",
112113
"cordova": "13.0.0",
113114
"core-js": "^3.45.0",
114115
"crypto-js": "^4.2.0",
115116
"dompurify": "^3.2.6",
116117
"escape-string-regexp": "^5.0.0",
117-
"esprima": "^4.0.1",
118118
"filesize": "^11.0.2",
119119
"html-tag-js": "^2.4.15",
120120
"js-base64": "^3.7.7",

src/lib/console.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "core-js/stable";
22
import "html-tag-js/dist/polyfill";
3-
import * as esprima from "esprima";
3+
import { parse } from "acorn";
44
import css from "styles/console.m.scss";
55
import loadPolyFill from "utils/polyfill";
66

@@ -374,16 +374,21 @@ import loadPolyFill from "utils/polyfill";
374374
});
375375
}
376376

377+
/** @type {import("acorn").Options} */
378+
const acornOptions = {
379+
ecmaVersion: "latest",
380+
};
381+
377382
function parseFunction(data) {
378383
let parsed;
379384
let str;
380385

381386
try {
382-
parsed = esprima.parse(data.toString()).body[0];
387+
parsed = parse(data.toString(), acornOptions).body[0];
383388
} catch (error) {
384389
try {
385390
const fun = ("(" + data.toString() + ")").replace(/\{.*\}/, "{}");
386-
parsed = esprima.parse(fun).body[0];
391+
parsed = parse(fun, acornOptions).body[0];
387392
} catch (error) {
388393
return data
389394
.toString()
@@ -632,9 +637,7 @@ import loadPolyFill from "utils/polyfill";
632637
function execute(code) {
633638
let res = null;
634639
try {
635-
const parsed = esprima.parse(code, {
636-
range: true,
637-
}).body;
640+
const parsed = parse(code, acornOptions).body;
638641
res = execParsedCode(parsed);
639642
} catch (e) {
640643
res = execParsedCode([]);
@@ -648,8 +651,7 @@ import loadPolyFill from "utils/polyfill";
648651
if (st.type === "VariableDeclaration") {
649652
if (["const", "let"].indexOf(st.kind) < 0) return;
650653

651-
const range = st.range;
652-
const exCode = code.substring(range[0], range[1]) + ";";
654+
const exCode = code.substring(st.start, st.end) + ";";
653655
extra += exCode;
654656
}
655657
});

0 commit comments

Comments
 (0)