mirror of
https://github.com/aphrody-labs/oxc.git
synced 2026-10-17 12:56:09 +00:00
fix(packages/codegen): fix printing negative BigIntLiteral after - (#27377)
Fix printing a negative `BigIntLiteral` after `-`. ```javascript // Original x = -(-1n); // Printed - syntax error x = --1n; ``` This bug could not be produced by an AST which has come from parser, because parser would parse the input as equivalent to `x = -(-(1n));`. But it could be produced when printing a hand-built AST.
This commit is contained in:
1 parent
80840acdb9
commit
5461c1ade4
2 files changed
+18
-8
No files matched your search
@@ -190,17 +190,25 @@ function printRegExpLiteral(node: ESTree.RegExpLiteral, state: State): void {
|
||||
/**
|
||||
* Print a bigint literal, parenthesizing a negative one where the position needs it,
|
||||
* same as `printNumericLiteral` does.
|
||||
*
|
||||
* Also like `printNumericLiteral`, a negative one is spaced off a preceding `-` or `--`
|
||||
* the same way a unary minus is, so `-(-1n)` prints as `- -1n`, not `--1n`.
|
||||
*/
|
||||
function printBigIntLiteral(node: ESTree.BigIntLiteral, state: State, precedence: number): void {
|
||||
printSpaceBeforeIdentifier(state);
|
||||
|
||||
const value = node.bigint;
|
||||
if (value.startsWith("-") && precedence >= PREC_PREFIX) {
|
||||
writeWithMapNoLast(state, "(", node.start, node.end, node);
|
||||
writeNoLast(state, value);
|
||||
write(state, "n)", CAT_CLOSE_BRACKET);
|
||||
if (value.startsWith("-")) {
|
||||
if (precedence >= PREC_PREFIX) {
|
||||
writeWithMapNoLast(state, "(", node.start, node.end, node);
|
||||
writeNoLast(state, value);
|
||||
write(state, "n)", CAT_CLOSE_BRACKET);
|
||||
return;
|
||||
}
|
||||
|
||||
printSpaceBeforeOperator(state, CAT_OP_UN_NEG);
|
||||
} else {
|
||||
writeWithMapNoLast(state, value, node.start, node.end, node);
|
||||
writeIdent(state, "n");
|
||||
printSpaceBeforeIdentifier(state);
|
||||
}
|
||||
|
||||
writeWithMapNoLast(state, value, node.start, node.end, node);
|
||||
writeIdent(state, "n");
|
||||
}
|
||||
@@ -352,6 +352,8 @@ describe("unary and update expressions", () => {
|
||||
["delete-infinity", e(unary("delete", num(Infinity))), "delete (0, Infinity);\n"],
|
||||
["minus-negative-literal", e(unary("-", num(-1))), "- -1;\n"],
|
||||
["plus-negative-literal", e(unary("+", num(-1))), "+-1;\n"],
|
||||
["minus-negative-bigint", e(unary("-", big("-1"))), "- -1n;\n"],
|
||||
["plus-negative-bigint", e(unary("+", big("-1"))), "+-1n;\n"],
|
||||
["preinc", e(update("++", id("x"))), "++x;\n"],
|
||||
["postinc", e(update("++", id("x"), false)), "x++;\n"],
|
||||
["predec", e(update("--", id("x"))), "--x;\n"],
|
||||
|
||||
Reference in new issue
Block a user