diff --git a/src/fast-path.js b/src/fast-path.js index 7bebc52ed..c4615fd93 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -295,6 +295,10 @@ FPp.needsParens = function() { } case "BinaryExpression": { + if (parent.type === "UpdateExpression") { + return true; + } + const isLeftOfAForStatement = node => { let i = 0; while (node) { diff --git a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap index 3f4fb5796..feaa30074 100644 --- a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap @@ -214,6 +214,85 @@ interface i2 { `; +exports[`decrementAndIncrementOperators.ts 1`] = ` +var x = 0; + +// errors +1 ++; + +(1)++; +(1)--; + +++(1); +--(1); + +(1 + 2)++; +(1 + 2)--; + +++(1 + 2); +--(1 + 2); + +(x + x)++; +(x + x)--; + +++(x + x); +--(x + x); + +//OK +x++; +x--; + +++x; +--x; + +(x)++; +--(x); + +((x))++; +((x))--; + +x[x++]++; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +var x = 0; + +// errors +1++; + +1++; +1--; + +++1; +--1; + +(1 + 2)++; +(1 + 2)--; + +++(1 + 2); +--(1 + 2); + +(x + x)++; +(x + x)--; + +++(x + x); +--(x + x); + +//OK +x++; +x--; + +++x; +--x; + +x++; +--x; + +x++; +x--; + +x[x++]++; + +`; + exports[`downlevelLetConst1.ts 1`] = ` const~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ const; diff --git a/tests/typescript/compiler/decrementAndIncrementOperators.ts b/tests/typescript/compiler/decrementAndIncrementOperators.ts new file mode 100644 index 000000000..a31e5243d --- /dev/null +++ b/tests/typescript/compiler/decrementAndIncrementOperators.ts @@ -0,0 +1,37 @@ +var x = 0; + +// errors +1 ++; + +(1)++; +(1)--; + +++(1); +--(1); + +(1 + 2)++; +(1 + 2)--; + +++(1 + 2); +--(1 + 2); + +(x + x)++; +(x + x)--; + +++(x + x); +--(x + x); + +//OK +x++; +x--; + +++x; +--x; + +(x)++; +--(x); + +((x))++; +((x))--; + +x[x++]++;