Skip to content

Commit

Permalink
fix(serialization): properly deserialize arrow functions
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Dvorak <[email protected]>
  • Loading branch information
Tomas2D committed Oct 10, 2024
1 parent 60f8953 commit 25234df
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/serializer/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,29 +450,40 @@ Serializer.register(Function, {
}

const toParsableForm = (): string => {
if (value.fn.match(/^\s*function.*?\(/)) {
return value.fn;
let fn = value.fn;

if (fn.match(/^\s*function.*?\(/)) {
return fn;
}

const arrowStart = value.fn.indexOf("=>");
const bracketStart = value.fn.indexOf("(");
const [a, b, c] = [fn.indexOf("=>"), fn.indexOf("("), fn.indexOf("{")];
if (a > -1) {
if (b === -1 || b > a || (c === -1 && c > a)) {
const [p, p2] = halveString(fn, "=>", false);
fn = `(${p.replace("async", "").replace("*", "").trim()})=>${p2}`;
fn = [p.includes("async") && "async ", p.includes("*") && "*", fn]
.filter(Boolean)
.join(" ");
}
}

const arrowStart = fn.indexOf("=>");
const bracketStart = fn.indexOf("(");

const [fnPrefix, fnContent = ""] =
bracketStart === -1 || bracketStart > arrowStart
? halveString(value.fn, "{", true)
: halveString(value.fn, "(", true);

const reservedSymbols = new Set(["async", "*"]);
? halveString(fn, "{", true)
: halveString(fn, "(", true);

const nonReservedSymbols = fnPrefix
.trim()
.split(" ")
.map((x) => x.trim())
.filter(Boolean)
.every((content) => reservedSymbols.has(content));
.every((content) => ["async", "*"].includes(content));

if (nonReservedSymbols) {
return value.fn;
return fn;
}

const name = value.name && fnPrefix.includes(value.name) ? value.name : "";
Expand Down

0 comments on commit 25234df

Please sign in to comment.