@jsep-plugin/ternary
Advanced tools
Comparing version 1.0.2 to 1.1.0-beta.1
@@ -35,90 +35,20 @@ 'use strict'; | ||
}; | ||
} | ||
// if binary operator is custom-added (i.e. object plugin), then correct it to a ternary node: | ||
// Note: BinaryExpressions can be stacked (similar to 1 + 1 + 1), so we have to collapse the stack | ||
// Only do one level at a time so we can unroll as we pop the ternary stack | ||
else if (test.operator === ':') { | ||
// this happens when the alternate is a ternary | ||
if (!consequent.right) { | ||
this.throwError('Expected :'); | ||
// check for operators of higher priority than ternary (i.e. assignment) | ||
// jsep sets || at 1, and assignment at 0.9, and conditional should be between them | ||
if (test.operator && jsep.binary_ops[test.operator] <= 0.9) { | ||
let newTest = test; | ||
while (newTest.right.operator && jsep.binary_ops[newTest.right.operator] <= 0.9) { | ||
newTest = newTest.right; | ||
} | ||
env.node.test = newTest.right; | ||
newTest.right = env.node; | ||
env.node = test; | ||
} | ||
const node = findLastBinaryNode(consequent); | ||
test.right = { | ||
type: CONDITIONAL_EXP, | ||
test: test.right, | ||
consequent: node.left, | ||
alternate: node === consequent ? node.right : { | ||
// temporary values because we still have to wait to pop the consequent... | ||
operator: ':', | ||
left: node.right, | ||
right: consequent.right, | ||
}, | ||
}; | ||
env.node = test; | ||
} | ||
else if (consequent.operator === ':') { | ||
convertBinaryToConditional(findLastBinaryNode(consequent), test); | ||
env.node = consequent; | ||
} | ||
else if (consequent.alternate) { | ||
// cleanup the temporary placeholder we made, now that we have the consequent | ||
let alternate = consequent.alternate; | ||
while (alternate.alternate) { | ||
alternate = alternate.alternate; | ||
} | ||
env.node = { | ||
type: CONDITIONAL_EXP, | ||
test, | ||
consequent, | ||
alternate: alternate.right, | ||
}; | ||
delete alternate.operator; | ||
delete alternate.right; | ||
Object.assign(alternate, alternate.left); | ||
} | ||
else { | ||
this.throwError('Expected :'); | ||
} | ||
// ? and : precedence are before '||' (which defaults to 1) | ||
// object plugin sets : precedence to 0.95, so check for less than that | ||
// (which would capture assignment operators, which the plugin sets at 0.9) | ||
if (env.node.test && env.node.test.operator && jsep.binary_ops[env.node.test.operator] < 0.95) { | ||
const node = env.node; | ||
env.node = node.test; | ||
env.node.right = { | ||
type: CONDITIONAL_EXP, | ||
test: node.test.right, | ||
consequent: node.consequent, | ||
alternate: node.alternate, | ||
}; | ||
} | ||
} | ||
}); | ||
/** | ||
* @param {jsep.Expression} node | ||
* @returns {jsep.Expression} | ||
*/ | ||
function findLastBinaryNode(node) { | ||
while (node.left && node.left.operator === ':') { | ||
node = node.left; | ||
} | ||
return node; | ||
} | ||
/** | ||
* @param {jsep.BinaryExpression} node | ||
* @param {jsep.Expression} test | ||
* @returns {jsep.ConditionalExpression} | ||
*/ | ||
function convertBinaryToConditional(node, test) { | ||
node.type = CONDITIONAL_EXP; | ||
node.test = test; | ||
node.consequent = node.left; | ||
node.alternate = node.right; | ||
delete node.operator; | ||
delete node.left; | ||
delete node.right; | ||
} | ||
}, | ||
@@ -125,0 +55,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
"use strict";const e="ConditionalExpression";var t={name:"ternary",init(t){function o(e){for(;e.left&&":"===e.left.operator;)e=e.left;return e}t.hooks.add("after-expression",(function(r){if(r.node&&this.code===t.QUMARK_CODE){this.index++;const n=r.node,s=this.gobbleExpression();if(s||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===t.COLON_CODE){this.index++;const e=this.gobbleExpression();e||this.throwError("Expected expression"),r.node={type:"ConditionalExpression",test:n,consequent:s,alternate:e}}else if(":"===n.operator){s.right||this.throwError("Expected :");const e=o(s);n.right={type:"ConditionalExpression",test:n.right,consequent:e.left,alternate:e===s?e.right:{operator:":",left:e.right,right:s.right}},r.node=n}else if(":"===s.operator)!function(t,o){t.type=e,t.test=o,t.consequent=t.left,t.alternate=t.right,delete t.operator,delete t.left,delete t.right}(o(s),n),r.node=s;else if(s.alternate){let t=s.alternate;for(;t.alternate;)t=t.alternate;r.node={type:e,test:n,consequent:s,alternate:t.right},delete t.operator,delete t.right,Object.assign(t,t.left)}else this.throwError("Expected :");if(r.node.test&&r.node.test.operator&&t.binary_ops[r.node.test.operator]<.95){const t=r.node;r.node=t.test,r.node.right={type:e,test:t.test.right,consequent:t.consequent,alternate:t.alternate}}}}))}};module.exports=t; | ||
"use strict";var e={name:"ternary",init(e){e.hooks.add("after-expression",(function(o){if(o.node&&this.code===e.QUMARK_CODE){this.index++;const t=o.node,r=this.gobbleExpression();if(r||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const s=this.gobbleExpression();if(s||this.throwError("Expected expression"),o.node={type:"ConditionalExpression",test:t,consequent:r,alternate:s},t.operator&&e.binary_ops[t.operator]<=.9){let r=t;for(;r.right.operator&&e.binary_ops[r.right.operator]<=.9;)r=r.right;o.node.test=r.right,r.right=o.node,o.node=t}}else this.throwError("Expected :")}}))}};module.exports=e; | ||
//# sourceMappingURL=index.cjs.min.js.map |
@@ -36,90 +36,20 @@ var index = (function () { | ||
}; | ||
} | ||
// if binary operator is custom-added (i.e. object plugin), then correct it to a ternary node: | ||
// Note: BinaryExpressions can be stacked (similar to 1 + 1 + 1), so we have to collapse the stack | ||
// Only do one level at a time so we can unroll as we pop the ternary stack | ||
else if (test.operator === ':') { | ||
// this happens when the alternate is a ternary | ||
if (!consequent.right) { | ||
this.throwError('Expected :'); | ||
// check for operators of higher priority than ternary (i.e. assignment) | ||
// jsep sets || at 1, and assignment at 0.9, and conditional should be between them | ||
if (test.operator && jsep.binary_ops[test.operator] <= 0.9) { | ||
let newTest = test; | ||
while (newTest.right.operator && jsep.binary_ops[newTest.right.operator] <= 0.9) { | ||
newTest = newTest.right; | ||
} | ||
env.node.test = newTest.right; | ||
newTest.right = env.node; | ||
env.node = test; | ||
} | ||
const node = findLastBinaryNode(consequent); | ||
test.right = { | ||
type: CONDITIONAL_EXP, | ||
test: test.right, | ||
consequent: node.left, | ||
alternate: node === consequent ? node.right : { | ||
// temporary values because we still have to wait to pop the consequent... | ||
operator: ':', | ||
left: node.right, | ||
right: consequent.right, | ||
}, | ||
}; | ||
env.node = test; | ||
} | ||
else if (consequent.operator === ':') { | ||
convertBinaryToConditional(findLastBinaryNode(consequent), test); | ||
env.node = consequent; | ||
} | ||
else if (consequent.alternate) { | ||
// cleanup the temporary placeholder we made, now that we have the consequent | ||
let alternate = consequent.alternate; | ||
while (alternate.alternate) { | ||
alternate = alternate.alternate; | ||
} | ||
env.node = { | ||
type: CONDITIONAL_EXP, | ||
test, | ||
consequent, | ||
alternate: alternate.right, | ||
}; | ||
delete alternate.operator; | ||
delete alternate.right; | ||
Object.assign(alternate, alternate.left); | ||
} | ||
else { | ||
this.throwError('Expected :'); | ||
} | ||
// ? and : precedence are before '||' (which defaults to 1) | ||
// object plugin sets : precedence to 0.95, so check for less than that | ||
// (which would capture assignment operators, which the plugin sets at 0.9) | ||
if (env.node.test && env.node.test.operator && jsep.binary_ops[env.node.test.operator] < 0.95) { | ||
const node = env.node; | ||
env.node = node.test; | ||
env.node.right = { | ||
type: CONDITIONAL_EXP, | ||
test: node.test.right, | ||
consequent: node.consequent, | ||
alternate: node.alternate, | ||
}; | ||
} | ||
} | ||
}); | ||
/** | ||
* @param {jsep.Expression} node | ||
* @returns {jsep.Expression} | ||
*/ | ||
function findLastBinaryNode(node) { | ||
while (node.left && node.left.operator === ':') { | ||
node = node.left; | ||
} | ||
return node; | ||
} | ||
/** | ||
* @param {jsep.BinaryExpression} node | ||
* @param {jsep.Expression} test | ||
* @returns {jsep.ConditionalExpression} | ||
*/ | ||
function convertBinaryToConditional(node, test) { | ||
node.type = CONDITIONAL_EXP; | ||
node.test = test; | ||
node.consequent = node.left; | ||
node.alternate = node.right; | ||
delete node.operator; | ||
delete node.left; | ||
delete node.right; | ||
} | ||
}, | ||
@@ -126,0 +56,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
var index=function(){"use strict";const e="ConditionalExpression";return{name:"ternary",init(t){function r(e){for(;e.left&&":"===e.left.operator;)e=e.left;return e}t.hooks.add("after-expression",(function(o){if(o.node&&this.code===t.QUMARK_CODE){this.index++;const n=o.node,s=this.gobbleExpression();if(s||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===t.COLON_CODE){this.index++;const t=this.gobbleExpression();t||this.throwError("Expected expression"),o.node={type:e,test:n,consequent:s,alternate:t}}else if(":"===n.operator){s.right||this.throwError("Expected :");const t=r(s);n.right={type:e,test:n.right,consequent:t.left,alternate:t===s?t.right:{operator:":",left:t.right,right:s.right}},o.node=n}else if(":"===s.operator)!function(t,r){t.type=e,t.test=r,t.consequent=t.left,t.alternate=t.right,delete t.operator,delete t.left,delete t.right}(r(s),n),o.node=s;else if(s.alternate){let t=s.alternate;for(;t.alternate;)t=t.alternate;o.node={type:e,test:n,consequent:s,alternate:t.right},delete t.operator,delete t.right,Object.assign(t,t.left)}else this.throwError("Expected :");if(o.node.test&&o.node.test.operator&&t.binary_ops[o.node.test.operator]<.95){const t=o.node;o.node=t.test,o.node.right={type:e,test:t.test.right,consequent:t.consequent,alternate:t.alternate}}}}))}}}(); | ||
var index=function(){"use strict";return{name:"ternary",init(e){e.hooks.add("after-expression",(function(t){if(t.node&&this.code===e.QUMARK_CODE){this.index++;const o=t.node,r=this.gobbleExpression();if(r||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const i=this.gobbleExpression();if(i||this.throwError("Expected expression"),t.node={type:"ConditionalExpression",test:o,consequent:r,alternate:i},o.operator&&e.binary_ops[o.operator]<=.9){let r=o;for(;r.right.operator&&e.binary_ops[r.right.operator]<=.9;)r=r.right;t.node.test=r.right,r.right=t.node,t.node=o}}else this.throwError("Expected :")}}))}}}(); | ||
//# sourceMappingURL=index.iife.min.js.map |
@@ -33,90 +33,20 @@ const CONDITIONAL_EXP = 'ConditionalExpression'; | ||
}; | ||
} | ||
// if binary operator is custom-added (i.e. object plugin), then correct it to a ternary node: | ||
// Note: BinaryExpressions can be stacked (similar to 1 + 1 + 1), so we have to collapse the stack | ||
// Only do one level at a time so we can unroll as we pop the ternary stack | ||
else if (test.operator === ':') { | ||
// this happens when the alternate is a ternary | ||
if (!consequent.right) { | ||
this.throwError('Expected :'); | ||
// check for operators of higher priority than ternary (i.e. assignment) | ||
// jsep sets || at 1, and assignment at 0.9, and conditional should be between them | ||
if (test.operator && jsep.binary_ops[test.operator] <= 0.9) { | ||
let newTest = test; | ||
while (newTest.right.operator && jsep.binary_ops[newTest.right.operator] <= 0.9) { | ||
newTest = newTest.right; | ||
} | ||
env.node.test = newTest.right; | ||
newTest.right = env.node; | ||
env.node = test; | ||
} | ||
const node = findLastBinaryNode(consequent); | ||
test.right = { | ||
type: CONDITIONAL_EXP, | ||
test: test.right, | ||
consequent: node.left, | ||
alternate: node === consequent ? node.right : { | ||
// temporary values because we still have to wait to pop the consequent... | ||
operator: ':', | ||
left: node.right, | ||
right: consequent.right, | ||
}, | ||
}; | ||
env.node = test; | ||
} | ||
else if (consequent.operator === ':') { | ||
convertBinaryToConditional(findLastBinaryNode(consequent), test); | ||
env.node = consequent; | ||
} | ||
else if (consequent.alternate) { | ||
// cleanup the temporary placeholder we made, now that we have the consequent | ||
let alternate = consequent.alternate; | ||
while (alternate.alternate) { | ||
alternate = alternate.alternate; | ||
} | ||
env.node = { | ||
type: CONDITIONAL_EXP, | ||
test, | ||
consequent, | ||
alternate: alternate.right, | ||
}; | ||
delete alternate.operator; | ||
delete alternate.right; | ||
Object.assign(alternate, alternate.left); | ||
} | ||
else { | ||
this.throwError('Expected :'); | ||
} | ||
// ? and : precedence are before '||' (which defaults to 1) | ||
// object plugin sets : precedence to 0.95, so check for less than that | ||
// (which would capture assignment operators, which the plugin sets at 0.9) | ||
if (env.node.test && env.node.test.operator && jsep.binary_ops[env.node.test.operator] < 0.95) { | ||
const node = env.node; | ||
env.node = node.test; | ||
env.node.right = { | ||
type: CONDITIONAL_EXP, | ||
test: node.test.right, | ||
consequent: node.consequent, | ||
alternate: node.alternate, | ||
}; | ||
} | ||
} | ||
}); | ||
/** | ||
* @param {jsep.Expression} node | ||
* @returns {jsep.Expression} | ||
*/ | ||
function findLastBinaryNode(node) { | ||
while (node.left && node.left.operator === ':') { | ||
node = node.left; | ||
} | ||
return node; | ||
} | ||
/** | ||
* @param {jsep.BinaryExpression} node | ||
* @param {jsep.Expression} test | ||
* @returns {jsep.ConditionalExpression} | ||
*/ | ||
function convertBinaryToConditional(node, test) { | ||
node.type = CONDITIONAL_EXP; | ||
node.test = test; | ||
node.consequent = node.left; | ||
node.alternate = node.right; | ||
delete node.operator; | ||
delete node.left; | ||
delete node.right; | ||
} | ||
}, | ||
@@ -123,0 +53,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
const e="ConditionalExpression";var t={name:"ternary",init(t){function o(e){for(;e.left&&":"===e.left.operator;)e=e.left;return e}t.hooks.add("after-expression",(function(r){if(r.node&&this.code===t.QUMARK_CODE){this.index++;const n=r.node,s=this.gobbleExpression();if(s||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===t.COLON_CODE){this.index++;const e=this.gobbleExpression();e||this.throwError("Expected expression"),r.node={type:"ConditionalExpression",test:n,consequent:s,alternate:e}}else if(":"===n.operator){s.right||this.throwError("Expected :");const e=o(s);n.right={type:"ConditionalExpression",test:n.right,consequent:e.left,alternate:e===s?e.right:{operator:":",left:e.right,right:s.right}},r.node=n}else if(":"===s.operator)!function(t,o){t.type=e,t.test=o,t.consequent=t.left,t.alternate=t.right,delete t.operator,delete t.left,delete t.right}(o(s),n),r.node=s;else if(s.alternate){let t=s.alternate;for(;t.alternate;)t=t.alternate;r.node={type:e,test:n,consequent:s,alternate:t.right},delete t.operator,delete t.right,Object.assign(t,t.left)}else this.throwError("Expected :");if(r.node.test&&r.node.test.operator&&t.binary_ops[r.node.test.operator]<.95){const t=r.node;r.node=t.test,r.node.right={type:e,test:t.test.right,consequent:t.consequent,alternate:t.alternate}}}}))}};export{t as default}; | ||
var e={name:"ternary",init(e){e.hooks.add("after-expression",(function(o){if(o.node&&this.code===e.QUMARK_CODE){this.index++;const t=o.node,r=this.gobbleExpression();if(r||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const i=this.gobbleExpression();if(i||this.throwError("Expected expression"),o.node={type:"ConditionalExpression",test:t,consequent:r,alternate:i},t.operator&&e.binary_ops[t.operator]<=.9){let r=t;for(;r.right.operator&&e.binary_ops[r.right.operator]<=.9;)r=r.right;o.node.test=r.right,r.right=o.node,o.node=t}}else this.throwError("Expected :")}}))}};export{e as default}; | ||
//# sourceMappingURL=index.min.js.map |
{ | ||
"name": "@jsep-plugin/ternary", | ||
"version": "1.0.2", | ||
"version": "1.1.0-beta.1", | ||
"description": "Adds ternary expression support", | ||
@@ -5,0 +5,0 @@ "author": "Shelly (https://github.com/6utt3rfly)", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22234
159
2
1