Skip to content

Commit

Permalink
fix(parser): fix trailing comma on objects when parsing expression
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 85636bd6ae7f4d996fc483bc7d5d779d1ab44407
  • Loading branch information
IcaroG authored and actions-user committed Feb 14, 2025
1 parent e185253 commit 30f8daf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
13 changes: 13 additions & 0 deletions platform/wab/src/wab/shared/eval/expression-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ describe("renameObjectKey", function () {
);
expect(newCode).toEqual("$ctx.a + $state.new.yek + $ctx.b");
});

it("should rename variable in object", () => {
const newCode = renameObjectKey(
"{ code: $props.test }",
"$props",
"$props",
"test",
"newTest"
);

// Removing spaces to make it easier to compare, since ast formats the code
expect(newCode.replace(/\s+/g, " ")).toEqual("{ code: $props.newTest }");
});
});

describe("replaceVarWithProp", function () {
Expand Down
2 changes: 1 addition & 1 deletion platform/wab/src/wab/shared/eval/expression-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export function mergeParsedExprInfos(infos: ParsedExprInfo[]): ParsedExprInfo {
}

function generateCode(ast: ast.Program): string {
const newCode = writeJs(ast);
const newCode = writeJs(ast, { semicolons: false });

// Remove trailing semicolon in case the generator added
return newCode.endsWith(";") ? newCode.slice(0, -1) : newCode;
Expand Down
6 changes: 5 additions & 1 deletion platform/wab/src/wab/shared/parser-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ export function parseJsCode(code: string) {
});
}

export function writeJs(ast: ast.Program, opts?: { indentLevel?: number }) {
export function writeJs(
ast: ast.Program,
opts?: { semicolons?: boolean; indentLevel?: number }
) {
return generate(ast, {
format: {
indent: {
style: " ",
adjustMultilineComment: true,
...(opts?.indentLevel ? { base: opts.indentLevel } : {}),
},
...(opts?.semicolons != null ? { semicolons: opts?.semicolons } : {}),
quotes: "double",
},
});
Expand Down

0 comments on commit 30f8daf

Please sign in to comment.