Reputation: 21891
I am writing some syntactical analyzer that disallows changing a primitive.
So I know that one can modify primitives these ways:
let p // a primitive
p = 1
p += 1
p -= 1
p %= 1
p *= 1
Is it really a full, exhaustive list of ways to change a primitive? Or did I forget something...
P.S. The analyzer is for my peculiar library. It disallows to change a passed argument to a function that returns an object literal of test closures, i.e. each closure must not change their common primitive.
Upvotes: 1
Views: 68
Reputation: 138307
Here you go:
// assignment operators
p = 1;
p += 1;
p -= 1;
p *= 1;
p /= 1;
p %= 1;
p <<= 1;
p >>= 1;
p >>>= 1;
p &= 1;
p ^= 1;
p |= 1;
p **= 1;
// decrement / increment operators
p++;
++p;
p--;
--p;
// destructuring
({ p } = { p: 1 });
({ a: p } = { a: 1 });
([p] = [1]);
Note that all of the above could also occur in a parsed string:
(new Function("p = 1"))();
eval("p = 1");
The analyzer is for my peculiar library. It disallows to change a passed argument.
Then I guess the easiest is to just parse:
function toTest(p) { /* body */ }
to this and execute it:
const p = 1;
try {
eval(/* body */);
} catch(e) {
//...
}
If an error occurs, someone tried to mutate the const.
Upvotes: 1