0.0.1 • Published 2 years ago
@dylanarmstrong/babel-plugin-transform-chain-comparisons v0.0.1
babel-plugin-transform-chain-operators
Add chained comparison operators like Python
Setup
In your babel.config.json
, place the following:
"plugins": [
"@dylanarmstrong/babel-plugin-transform-chain-comparisons"
]
Examples:
// Original
if (a < b < c) {}
// Transform
if (a < b && b < c) {}
// Original
if (a != b != c) {}
// Transform
if (a != b && b != c) {}
// Original
if (a < f() < g()) {}
// Transform
const ref1 = f();
if (a < ref1 && ref1 < g()) {}
// Original
if (f() < g() < h()) {}
// Transform
const ref1 = g();
if (f() < ref1 && ref1 < h()) {}
Caveats:
a != b != c
does not implya != c
- await operators are finicky,
a < await f() < b
will callawait f()
twice at the moment. This is intended to be fixed. - Functions cannot change a value that is relied on in a subsequent statement
f(10) < g() < c
withc = 2
,b = 1; g = () => b;
, andf = (n) => { b = n; return 0; }
. This is because the expressiong()
is cached before any BinaryExpression is called. This is done intentionally to avoid double calling theg()
method.
0.0.1
2 years ago