Repository navigation
Conversation
The jsep assignment plugin parses `+=`, `-=`, `*=`, `??=` and the other compound operators, but the safe evaluator ignored `ast.operator` and always did a plain `=`. So `x = 30, x -= @.price` set x to the price instead of subtracting it, with no error, and only gave the right answer for `+=` when x started at 0. `x++` / `--x` (UpdateExpression) failed with "Unexpected expression". Compound operators now reuse the binary operator table (`||=`, `&&=` and `??=` still short-circuit their right side), and `++` / `--` follow JS for prefix and postfix forms. Like in JS, using an undefined variable on the left now throws a ReferenceError instead of silently treating it as a fresh variable. Property operands are still refused.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR description
Fixes #297.
The jsep assignment plugin parses all compound assignment operators and
++/--, but the safe evaluator always did a plain=(it never readast.operator), and had no case forUpdateExpression. So with the defaulteval: "safe",x = 30, x -= @.vsetxto@.v, silently, andx++threwUnexpected expression.What changed in
src/Safe-Script.js:x op= ynow runsBINOPS[op](x, () => y), so it reuses the existing operator table.||=,&&=and??=keep short-circuiting the right side.evalUpdateExpressionfor prefix and postfix++/--. It lets JS do the update on a local copy, so number conversion matches JS ("4"++gives 5).ReferenceError(q is not defined). Before,q += 1quietly setqto 1.@.v += 1,@.v++) are still refused, same as@.v = 1today.I checked every compound operator and both update forms against plain JS with 7 start values (number, numeric string, 0, null, undefined, negative). All match on this branch; main differed on 117 of 137 cases. (
2nBigInt literals are not parsed by jsep, so they are out of scope.)Checklist
npm test, ensuring linting passes