Skip to content

Commit ff82ce0

Browse files
committed
refactor: replace esprima with acorn
1 parent 9ed27a6 commit ff82ce0

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

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)