Socket
Socket
Sign inDemoInstall

babel-plugin-optimize-clsx

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-optimize-clsx - npm Package Compare versions

Comparing version 2.4.0 to 2.4.1

2

CHANGELOG.md

@@ -5,2 +5,4 @@ # Changelog

### [2.4.1](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v2.4.0...v2.4.1) (2019-08-18)
## [2.4.0](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v2.3.0...v2.4.0) (2019-08-18)

@@ -7,0 +9,0 @@

2

dist/index.js

@@ -1,2 +0,2 @@

"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=e(require("lodash/forOwn")),r=require("@babel/types"),n=e(require("lodash/isEqualWith")),i=e(require("fs")),s=e(require("path")),o=e(require("@babel/generator")),l=e(require("object-hash")),a=e(require("lodash/partition")),u=e(require("lodash/fp/values")),c=e(require("lodash/fp/groupBy")),p=e(require("lodash/fp/map")),f=e(require("lodash/fp/flow")),h=e(require("lodash/fp/partition")),g=e(require("lodash/flattenDeep")),m=e(require("lodash/fp/uniqBy"));var d=(e,n)=>{function i(t,r){n.functionNames.push(r),e.functionCounters={...e.functionCounters||{},[r]:t.scope.getBinding(r).references},e.importPaths={...e.importPaths||{},[r]:t}}0===n.libraries.length||n._removeUnusedImports&&!n.removeUnnecessaryCalls||(n._removeUnusedImports&&n.removeUnnecessaryCalls?t(e.functionCounters,(t,r)=>{0===t&&e.importPaths[r].remove()}):e.get("body").forEach(e=>{const t=e.node;r.isImportDeclaration(t)&&n.libraries.includes(t.source.value)&&1===t.specifiers.length&&(r.isImportDefaultSpecifier(t.specifiers[0])||r.isImportNamespaceSpecifier(t.specifiers[0]))?i(e,t.specifiers[0].local.name):r.isVariableDeclaration(t)&&e.get("declarations").forEach(e=>{const t=e.node;r.isVariableDeclarator(t)&&r.isCallExpression(t.init)&&r.isIdentifier(t.init.callee,{name:"require"})&&1===t.init.arguments.length&&r.isLiteral(t.init.arguments[0])&&n.libraries.includes(t.init.arguments[0].value)&&i(e,t.id.name)})}))},x={CallExpression(e){const t=e.node.arguments,n=[];for(const e of t)if(r.isObjectExpression(e))for(const t of e.properties)n.push(r.LogicalExpression("&&",t.value,t.computed?t.key:r.isStringLiteral(t.key)?t.key:r.stringLiteral(t.key.name)));else n.push(e);e.node.arguments=n}};function v(e){return r.isStringLiteral(e)||r.isTemplateLiteral(e)}function y(e,t){if(E(e)&&E(t))return r.stringLiteral("");if(r.isStringLiteral(e)&&r.isStringLiteral(t))return r.stringLiteral(e.value+" "+t.value);if(r.isTemplateLiteral(e)&&r.isTemplateLiteral(t)){const r=[...e.expressions,...t.expressions],n=[...e.quasis];return n[n.length-1]=b(n[n.length-1].value.raw+" "+t.quasis[0].value.raw),n.push(...t.quasis.slice(1)),L(n,r)}if(r.isTemplateLiteral(e)&&r.isStringLiteral(t)){const r=[...e.expressions],n=[...e.quasis],i=n.length-1;return n[i]=b(n[i].value.raw+" "+t.value,!0),L(n,r)}if(r.isStringLiteral(e)&&r.isTemplateLiteral(t)){const r=[...t.expressions],n=[...t.quasis],i=0;return n[i]=b(e.value+" "+n[i].value.raw,!0),L(n,r)}throw new Error("Unable to handle that input")}function E(e){return r.isStringLiteral(e)?0===e.value.length:!!r.isTemplateLiteral(e)&&(0===e.expressions.length&&e.quasis.every(e=>0===e.value.raw.length))}function L(e,t){return 0===t.length?r.stringLiteral(e[0].value.raw):r.templateLiteral(e,t)}function b(e,t=!1){return{type:"TemplateElement",value:{raw:e},tail:t}}function q(e){const t=[];return function e(n){r.isLogicalExpression(n)?(e(n.left),t.push(n.right)):t.push(n)}(e),t}function C(e){if(!r.isLogicalExpression(e,{operator:"&&"}))return!1;let t=e.left;for(;r.isLogicalExpression(t);){if("&&"!==t.operator)return!1;t=t.left}return!0}function I(e){let t=null,r=0,n=e.length;for(let i=0,s=n-1;i<s;i++)for(let s=0,o=e[i],l=o.length-1;s<l;s++){let l=o[s],a=0;for(let t=i+1;t<n;t++)for(let r=0,n=e[t];r<n.length-1;r++)A(l,n[r])&&(a+=1);a>r&&(t=l,r=a)}return t}s.join(__dirname,"../../dumps");function A(e,t){if(e.type!==t.type)return!1;switch(e.type){case"Identifier":return e.name===t.name;case"MemberExpression":return A(e.object,t.object)&&A(e.property,t.property);case"BinaryExpression":return e.operator===t.operator&&A(e.left,t.left)&&A(e.right,t.right);default:return r.isLiteral(e)?e.value===t.value:n(e,t,(e,t,r)=>"start"===r||"end"===r||"loc"===r||void 0)}}function S(e){return l(e,{excludeKeys:e=>"start"===e||"end"===e||"loc"===e||"extra"===e})}function j(e){if(!r.isConditionalExpression(e))return!1;const{consequent:t,alternate:n}=e;return!(!v(t)||!v(n))||!!(v(t)&&j(n)||v(n)&&j(t))}function B(e){return e.reduce((e,t)=>r.logicalExpression("&&",e,t))}function w(e){return E(e)||r.isNullLiteral(e)||r.isIdentifier(e,{name:"undefined"})||r.isBooleanLiteral(e,{value:!1})||r.isNumericLiteral(e,{value:0})}const k={BinaryExpression(e){const t=e.node;if(r.isBinaryExpression(t)&&("==="===t.operator||"!=="===t.operator)&&(r.isStringLiteral(t.right)||r.isStringLiteral(t.left))){const e=this.types.find(e=>e.matches.some(r=>(e.isRequired||r.hasDefaultValue)&&(A(t.left,r)||A(t.right,r))));if(void 0===e)return;const n=e.optionA.length<e.optionB.length?e.optionA:e.optionB,i=r.isStringLiteral(t.right)?"right":"left";t[i].value===n||t[i].value!==e.optionA&&t[i].value!==e.optionB||(t[i]=r.stringLiteral(n),t.operator="!=="===t.operator?"===":"!==")}}},N={"FunctionExpression|FunctionDeclaration"(e){const t=e.node;for(const i of this.propTypes)if(r.isFunction(t)&&r.isIdentifier(t.id,{name:i.name})&&0!==t.params.length){function n(e,s){if(Array.isArray(e))for(const t of e)n(t,s);else{if(r.isBlockStatement(e))return n(e.body,s);if(r.isVariableDeclaration(e))return n(e.declarations,s);if(r.isObjectPattern(e))return n(e.properties,null);if(r.isObjectProperty(e)&&r.isIdentifier(e.key)){let t=null;if(r.isIdentifier(e.value))t=e.value;else{if(!r.isAssignmentPattern(e.value))return;(t=e.value.left).hasDefaultValue=!0}i.values=i.values.map(r=>r.name!==e.key.name?r:(void 0===r.matches&&(r.matches=[]),{...r,matches:[...r.matches,t]}))}else{if(r.isRestElement(e)&&r.isIdentifier(e.argument))return n(e.argument,null);if(r.isIdentifier(e))return i.values=i.values.map(t=>(void 0===t.matches&&(t.matches=[]),{...t,matches:[...t.matches,r.memberExpression(e,r.identifier(t.name))]})),n(t.body,e);if(r.isVariableDeclarator(e))if(r.isMemberExpression(e.init)&&A(e.init.object,s)&&r.isIdentifier(e.init.property)&&r.isIdentifier(e.id))i.values=i.values.map(t=>t.name!==e.init.property.name?t:(void 0===t.matches&&(t.matches=[]),{...t,matches:[...t.matches,e.id]}));else if(r.isIdentifier(e.init)&&A(e.init,s)&&r.isObjectPattern(e.id))return n(e.id,null)}}}n(t.params[0],null),e.traverse(k,{types:i.values}),e.skip()}}};function D(e){const t=function(e){for(const t of e)if(r.isImportDeclaration(t)&&"prop-types"===t.source.value&&1===t.specifiers.length){const e=t.specifiers[0];if(r.isImportDefaultSpecifier(e))return e.local.name}}(e);if(void 0===t)return[];const n=[];for(const s of e)if(r.isExpressionStatement(s)){const e=s.expression;if(r.isAssignmentExpression(e,{operator:"="})&&r.isMemberExpression(e.left)&&r.isIdentifier(e.left.property,{name:"propTypes"})&&r.isIdentifier(e.left.object)){let s={name:e.left.object.name,values:[]};if(r.isObjectExpression(e.right))for(const n of e.right.properties){function i(e,o){if(r.isMemberExpression(e)&&r.isIdentifier(e.property,{name:"isRequired"}))i(e.object,!0);else if(r.isCallExpression(e)&&r.isMemberExpression(e.callee)&&r.isIdentifier(e.callee.object,{name:t})&&r.isIdentifier(e.callee.property,{name:"oneOf"})&&1===e.arguments.length){const t=e.arguments[0];r.isArrayExpression(t)&&2===t.elements.length&&r.isStringLiteral(t.elements[0])&&r.isStringLiteral(t.elements[1])&&s.values.push({name:n.key.name,isRequired:o,optionA:t.elements[0].value,optionB:t.elements[1].value})}}i(n.value,!1)}0!==s.values.length&&n.push(s)}}return n}function T(e){if(e.length<2)return e;const[t,r]=a(e,v);return t.length<2?e:[t.reduce(y),...r]}const P={ArrayExpression(e){const{node:t}=e;t.elements=T(t.elements),1===t.elements.length&&e.replaceWith(t.elements[0])}};const U=[function(e){if(0===e.length)return r.stringLiteral("")},function(e){if(1===e.length&&v(e[0]))return e[0]},function(e){if(1===e.length&&j(e[0]))return e[0]},function(e){if(2!==e.length)return;const[t,n]=e;if(e.every(j)){const e=r.conditionalExpression(t.test,y(t.consequent,r.stringLiteral("")),y(t.alternate,r.stringLiteral("")));return r.binaryExpression("+",e,n)}},function(e){if(2!==e.length)return;const[t,n]=e;if(e.some(v)&&e.some(j)){const e=v(t)?t:n,i=v(n)?t:n;return E(i.consequent)||E(i.alternate)?r.binaryExpression("+",e,r.conditionalExpression(i.test,y(r.stringLiteral(""),i.consequent),y(r.stringLiteral(""),i.alternate))):r.binaryExpression("+",y(e,r.stringLiteral("")),i)}},function(e){if(1!==e.length)return;const[t]=e;return r.isLogicalExpression(t,{operator:"&&"})&&(v(t.right)||j(t.right)||r.isBinaryExpression(t.right,{operator:"+"})&&v(t.right.left)&&j(t.right.right))?r.conditionalExpression(t.left,t.right,r.stringLiteral("")):r.isLogicalExpression(t,{operator:"||"})&&v(t.right)?t:void 0},function(e){if(2!==e.length)return;const[t,n]=e;return v(t)&&r.isLogicalExpression(n,{operator:"&&"})&&v(n.right)?r.binaryExpression("+",t,r.conditionalExpression(n.left,y(r.stringLiteral(""),n.right),r.stringLiteral(""))):void 0}],O={ArrayExpression(e){for(const t of U){const r=t(e.node.elements);if(void 0!==r){e.replaceWith(r);break}}}};function W(e,t){return t(e.left)||t(e.right)}function _(e){const[t,n]=h(e=>{return function e(t){if(C(t))return e(t.left);if(r.isBinaryExpression(t,{operator:"==="})&&W(t,r.isStringLiteral)&&(W(t,r.isMemberExpression)||W(t,r.isIdentifier)))return!0;return!1}(e)},e);return 0===t.length?e:[...n,...f(p(q),p(e=>{let t={...e[0]};return r.isStringLiteral(t.left)&&(t=r.binaryExpression("===",t.right,t.left)),[t,...e.slice(1)]}),c(e=>S(e[0].left)),u,p(e=>{const t=e.reduce((e,t)=>{let n=t[0].right;return!1===/[^A-Za-z]/.test(t[0].right.value)&&(n=r.identifier(t[0].right.value)),e.push(r.objectProperty(n,B(t.slice(1)))),e},[]),n=r.memberExpression(r.objectExpression(t),e[0][0].left,!0);if(e.length>1)return n;const i=B(e[0]),s=o(i,{compact:!0}).code,l=o(n,{compact:!0}).code;return s.length<=l.length?i:n}))(t)]}const M={ArrayExpression(e){e.node.elements=_(e.node.elements),1===e.node.elements.length&&e.replaceWith(e.node.elements[0])}};let V=null,R=0;function F(e){return f(m(S),p(e=>(r.isBinaryExpression(e)&&("!=="===e.operator||"==="===e.operator)&&r.isNumericLiteral(e.right,{value:1})&&r.isBinaryExpression(e.left,{operator:"%"})&&r.isNumericLiteral(e.left.right,{value:2})&&(e.right=r.numericLiteral(0),e.operator="==="===e.operator?"!==":"==="),e)))(e)}const $=function(e){return e.reduce((e,t)=>("object"==typeof t?e.length&&Array.isArray(e[e.length-1])?e[e.length-1].push(t):e.push([t]):e.push(t),e),[]).map(e=>{if(!Array.isArray(e))return e;const n={};let i={};for(const s of e)t(s,(e,t)=>{void 0===n[t]&&(n[t]=[]),n[t].push(e),void 0===i[t]&&(i={...i,[t](e,...i){let s=null;if("CallExpression"===t){const t=e.node.callee;if(!r.isIdentifier(t)||!this.options.functionNames.includes(t.name))return;s=t.name}for(const r of n[t])if(r.call(this,e,...i),e.shouldSkip||e.type!==t)return void(null!==s&&"CallExpression"===t&&"CallExpression"!==e.type&&void 0!==this.functionCounters&&this.functionCounters[s]&&(this.functionCounters[s]-=1))}})});return(e,t)=>{e.traverse(i,{options:t,functionCounters:e.functionCounters})}})}([{CallExpression(e){if(this.options.collectCalls){if(null===V){const e=s.join(__dirname,"log.js");V=i.createWriteStream(e,{flags:"w"}),console.log("Writing calls to "+e)}V.write(`const x${++R} = ${o(e.node).code};\n\n`)}}},{CallExpression(e){e.node.arguments=g(function e(t){return t.map(t=>r.isArrayExpression(t)?e(t.elements):t)}(e.node.arguments))}},x,(e,t)=>{const r=D(e.node.body);0!==r.length&&e.traverse(N,{options:t,propTypes:r})},{CallExpression(e){e.node.arguments=e.node.arguments.map(e=>(r.isConditionalExpression(e)&&(w(e.alternate)?e=r.logicalExpression("&&",e.test,e.consequent):w(e.consequent)&&(e=r.logicalExpression("&&",r.unaryExpression("!",e.test),e.alternate))),C(e)&&(e=f(q,F,B)(e)),e))}},{CallExpression(e){const[t,n]=a(e.node.arguments,C),i=t.map(q).map(e=>e.filter((t,n)=>!(r.isBooleanLiteral(t,{value:!0})||n!==e.length-1&&r.isNumericLiteral(t)&&0!==t.value||n!==e.length-1&&v(t)&&!E(t)))).filter(e=>!(0===e.length||e.some(w))).map(B),s=n.filter(e=>!w(e));e.node.arguments=[...s,...i]}},{CallExpression(e){if(e.node.arguments.length<2)return;const[t,n]=a(e.node.arguments,C);if(t.length<2)return;const i=t.map(q),s=I(i);if(null===s)return;const o=function e(t){if("rootNode"!==t.type)return t;const n=[];if("rootNode"===t.child.type){let i=e(t.child);i=1===i.length?i[0]:r.arrayExpression(i),n.push(r.logicalExpression("&&",t.node,i))}else n.push(r.logicalExpression("&&",t.node,t.child));if(void 0!==t.next){const r=e(t.next);Array.isArray(r)?n.push(...r):n.push(r)}return n}(function e(t,n){const i={type:"rootNode",node:n,child:[],next:[]};t.forEach(e=>{const t=e.filter(e=>!A(e,n));t.length===e.length?i.next.push(e):i.child.push(t)});i.next=o(i.next);const s=o(i.child);Array.isArray(s)?i.child=1===s.length?s[0]:r.arrayExpression(s):i.child=s;return i;function o(t){if(0!==t.length){if(t.length>1){const r=I(t);if(null!==r)return e(t,r)}return t.map(B)}}}(i,s));return void(e.node.arguments=[...n,...o])}},{CallExpression(e){e.node.arguments=T(e.node.arguments),e.traverse(P)}},{CallExpression(e){e.node.arguments=function e(t){const[n,i]=a(t,C);if(0===n.length)return i;let s=n.map(q).map(t=>{let n=t.pop();return r.isArrayExpression(n)&&(n.elements=e(n.elements),1===n.elements.length&&(n=n.elements[0])),t.push(n),t});const o=i;for(let e=0;e<s.length;e++){const t=s[e];function l(){for(let n=0;n<t.length-1;n++){const i=t[n],l=r.isUnaryExpression(i,{operator:"!"}),a=r.isBinaryExpression(i,{operator:"!=="});if(l||a)for(let u=0;u<s.length;u++){const c=s[u];for(let p=0;p<c.length-1;p++){const f=c[p];if(e===u&&n===p)continue;if(l&&!A(i.argument,f))continue;if(a&&(!r.isBinaryExpression(f,{operator:"==="})||!(A(i.left,f.left)&&A(i.right,f.right)||A(i.left,f.right)&&A(i.right,f.left))))continue;const h=B(c.filter((e,t)=>t!==p)),g=B(t.filter((e,t)=>t!==n));return o.push(r.conditionalExpression(f,h,g)),s=s.filter((t,r)=>r!==e&&r!==u),!0}}}return!1}l()&&(e-=1)}return[...o,...s.map(B)]}(e.node.arguments)}},{CallExpression(e){if(this.options.removeUnnecessaryCalls){e.traverse(O);for(const t of U){const r=t(e.node.arguments);if(void 0!==r){e.replaceWith(r),e.skip();break}}}}},{CallExpression(e){e.traverse(M),e.node.arguments=_(e.node.arguments)}},(e,t)=>d(e,{...t,_removeUnusedImports:!0})]);module.exports=()=>({visitor:{Program(e,t){const r=function(e){return{libraries:["clsx","classnames"],functionNames:[],removeUnnecessaryCalls:!0,collectCalls:!1,...e}}(t.opts);if(d(e,r),0!==r.functionNames.length)try{for(const t of $)t(e,r)}catch(e){throw e}}}});
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=e(require("lodash/forOwn")),r=require("@babel/types"),n=e(require("lodash/isEqualWith")),i=e(require("fs")),s=e(require("path")),o=e(require("@babel/generator")),l=e(require("object-hash")),a=e(require("lodash/partition")),u=e(require("lodash/fp/values")),c=e(require("lodash/fp/groupBy")),p=e(require("lodash/fp/map")),f=e(require("lodash/fp/flow")),h=e(require("lodash/fp/partition")),g=e(require("lodash/flattenDeep")),m=e(require("lodash/fp/uniqBy"));function d(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function y(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function x(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?y(r,!0).forEach(function(t){d(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):y(r).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}var v=(e,n)=>{function i(t,r){n.functionNames.push(r),e.functionCounters=x({},e.functionCounters||{},{[r]:t.scope.getBinding(r).references}),e.importPaths=x({},e.importPaths||{},{[r]:t})}0===n.libraries.length||n._removeUnusedImports&&!n.removeUnnecessaryCalls||(n._removeUnusedImports&&n.removeUnnecessaryCalls?t(e.functionCounters,(t,r)=>{0===t&&e.importPaths[r].remove()}):e.get("body").forEach(e=>{const t=e.node;r.isImportDeclaration(t)&&n.libraries.includes(t.source.value)&&1===t.specifiers.length&&(r.isImportDefaultSpecifier(t.specifiers[0])||r.isImportNamespaceSpecifier(t.specifiers[0]))?i(e,t.specifiers[0].local.name):r.isVariableDeclaration(t)&&e.get("declarations").forEach(e=>{const t=e.node;r.isVariableDeclarator(t)&&r.isCallExpression(t.init)&&r.isIdentifier(t.init.callee,{name:"require"})&&1===t.init.arguments.length&&r.isLiteral(t.init.arguments[0])&&n.libraries.includes(t.init.arguments[0].value)&&i(e,t.id.name)})}))},E={CallExpression(e){const t=e.node.arguments,n=[];for(const e of t)if(r.isObjectExpression(e))for(const t of e.properties)n.push(r.LogicalExpression("&&",t.value,t.computed?t.key:r.isStringLiteral(t.key)?t.key:r.stringLiteral(t.key.name)));else n.push(e);e.node.arguments=n}};function b(e){return r.isStringLiteral(e)||r.isTemplateLiteral(e)}function L(e,t){if(q(e)&&q(t))return r.stringLiteral("");if(r.isStringLiteral(e)&&r.isStringLiteral(t))return r.stringLiteral(e.value+" "+t.value);if(r.isTemplateLiteral(e)&&r.isTemplateLiteral(t)){const r=[...e.expressions,...t.expressions],n=[...e.quasis];return n[n.length-1]=j(n[n.length-1].value.raw+" "+t.quasis[0].value.raw),n.push(...t.quasis.slice(1)),C(n,r)}if(r.isTemplateLiteral(e)&&r.isStringLiteral(t)){const r=[...e.expressions],n=[...e.quasis],i=n.length-1;return n[i]=j(n[i].value.raw+" "+t.value,!0),C(n,r)}if(r.isStringLiteral(e)&&r.isTemplateLiteral(t)){const r=[...t.expressions],n=[...t.quasis],i=0;return n[i]=j(e.value+" "+n[i].value.raw,!0),C(n,r)}throw new Error("Unable to handle that input")}function q(e){return r.isStringLiteral(e)?0===e.value.length:!!r.isTemplateLiteral(e)&&(0===e.expressions.length&&e.quasis.every(e=>0===e.value.raw.length))}function C(e,t){return 0===t.length?r.stringLiteral(e[0].value.raw):r.templateLiteral(e,t)}function j(e,t=!1){return{type:"TemplateElement",value:{raw:e},tail:t}}function I(e){const t=[];return function e(n){r.isLogicalExpression(n)?(e(n.left),t.push(n.right)):t.push(n)}(e),t}function A(e){if(!r.isLogicalExpression(e,{operator:"&&"}))return!1;let t=e.left;for(;r.isLogicalExpression(t);){if("&&"!==t.operator)return!1;t=t.left}return!0}function O(e){let t=null,r=0,n=e.length;for(let i=0,s=n-1;i<s;i++)for(let s=0,o=e[i],l=o.length-1;s<l;s++){let l=o[s],a=0;for(let t=i+1;t<n;t++)for(let r=0,n=e[t];r<n.length-1;r++)S(l,n[r])&&(a+=1);a>r&&(t=l,r=a)}return t}s.join(__dirname,"../../dumps");function S(e,t){if(e.type!==t.type)return!1;switch(e.type){case"Identifier":return e.name===t.name;case"MemberExpression":return S(e.object,t.object)&&S(e.property,t.property);case"BinaryExpression":return e.operator===t.operator&&S(e.left,t.left)&&S(e.right,t.right);default:return r.isLiteral(e)?e.value===t.value:n(e,t,(e,t,r)=>"start"===r||"end"===r||"loc"===r||void 0)}}function w(e){return l(e,{excludeKeys:e=>"start"===e||"end"===e||"loc"===e||"extra"===e})}function B(e){if(!r.isConditionalExpression(e))return!1;const{consequent:t,alternate:n}=e;return!(!b(t)||!b(n))||!!(b(t)&&B(n)||b(n)&&B(t))}function P(e){return e.reduce((e,t)=>r.logicalExpression("&&",e,t))}function D(e){return q(e)||r.isNullLiteral(e)||r.isIdentifier(e,{name:"undefined"})||r.isBooleanLiteral(e,{value:!1})||r.isNumericLiteral(e,{value:0})}const k={BinaryExpression(e){const t=e.node;if(r.isBinaryExpression(t)&&("==="===t.operator||"!=="===t.operator)&&(r.isStringLiteral(t.right)||r.isStringLiteral(t.left))){const e=this.types.find(e=>e.matches.some(r=>(e.isRequired||r.hasDefaultValue)&&(S(t.left,r)||S(t.right,r))));if(void 0===e)return;const n=e.optionA.length<e.optionB.length?e.optionA:e.optionB,i=r.isStringLiteral(t.right)?"right":"left";t[i].value===n||t[i].value!==e.optionA&&t[i].value!==e.optionB||(t[i]=r.stringLiteral(n),t.operator="!=="===t.operator?"===":"!==")}}},N={"FunctionExpression|FunctionDeclaration"(e){const t=e.node;for(const i of this.propTypes)if(r.isFunction(t)&&r.isIdentifier(t.id,{name:i.name})&&0!==t.params.length){function n(e,s){if(Array.isArray(e))for(const t of e)n(t,s);else{if(r.isBlockStatement(e))return n(e.body,s);if(r.isVariableDeclaration(e))return n(e.declarations,s);if(r.isObjectPattern(e))return n(e.properties,null);if(r.isObjectProperty(e)&&r.isIdentifier(e.key)){let t=null;if(r.isIdentifier(e.value))t=e.value;else{if(!r.isAssignmentPattern(e.value))return;(t=e.value.left).hasDefaultValue=!0}i.values=i.values.map(r=>r.name!==e.key.name?r:(void 0===r.matches&&(r.matches=[]),x({},r,{matches:[...r.matches,t]})))}else{if(r.isRestElement(e)&&r.isIdentifier(e.argument))return n(e.argument,null);if(r.isIdentifier(e))return i.values=i.values.map(t=>(void 0===t.matches&&(t.matches=[]),x({},t,{matches:[...t.matches,r.memberExpression(e,r.identifier(t.name))]}))),n(t.body,e);if(r.isVariableDeclarator(e))if(r.isMemberExpression(e.init)&&S(e.init.object,s)&&r.isIdentifier(e.init.property)&&r.isIdentifier(e.id))i.values=i.values.map(t=>t.name!==e.init.property.name?t:(void 0===t.matches&&(t.matches=[]),x({},t,{matches:[...t.matches,e.id]})));else if(r.isIdentifier(e.init)&&S(e.init,s)&&r.isObjectPattern(e.id))return n(e.id,null)}}}n(t.params[0],null),e.traverse(k,{types:i.values}),e.skip()}}};function T(e){const t=function(e){for(const t of e)if(r.isImportDeclaration(t)&&"prop-types"===t.source.value&&1===t.specifiers.length){const e=t.specifiers[0];if(r.isImportDefaultSpecifier(e))return e.local.name}}(e);if(void 0===t)return[];const n=[];for(const s of e)if(r.isExpressionStatement(s)){const e=s.expression;if(r.isAssignmentExpression(e,{operator:"="})&&r.isMemberExpression(e.left)&&r.isIdentifier(e.left.property,{name:"propTypes"})&&r.isIdentifier(e.left.object)){let s={name:e.left.object.name,values:[]};if(r.isObjectExpression(e.right))for(const n of e.right.properties){function i(e,o){if(r.isMemberExpression(e)&&r.isIdentifier(e.property,{name:"isRequired"}))i(e.object,!0);else if(r.isCallExpression(e)&&r.isMemberExpression(e.callee)&&r.isIdentifier(e.callee.object,{name:t})&&r.isIdentifier(e.callee.property,{name:"oneOf"})&&1===e.arguments.length){const t=e.arguments[0];r.isArrayExpression(t)&&2===t.elements.length&&r.isStringLiteral(t.elements[0])&&r.isStringLiteral(t.elements[1])&&s.values.push({name:n.key.name,isRequired:o,optionA:t.elements[0].value,optionB:t.elements[1].value})}}i(n.value,!1)}0!==s.values.length&&n.push(s)}}return n}function U(e){if(e.length<2)return e;const[t,r]=a(e,b);return t.length<2?e:[t.reduce(L),...r]}const W={ArrayExpression(e){const{node:t}=e;t.elements=U(t.elements),1===t.elements.length&&e.replaceWith(t.elements[0])}};const _=[function(e){if(0===e.length)return r.stringLiteral("")},function(e){if(1===e.length&&b(e[0]))return e[0]},function(e){if(1===e.length&&B(e[0]))return e[0]},function(e){if(2!==e.length)return;const[t,n]=e;if(e.every(B)){const e=r.conditionalExpression(t.test,L(t.consequent,r.stringLiteral("")),L(t.alternate,r.stringLiteral("")));return r.binaryExpression("+",e,n)}},function(e){if(2!==e.length)return;const[t,n]=e;if(e.some(b)&&e.some(B)){const e=b(t)?t:n,i=b(n)?t:n;return q(i.consequent)||q(i.alternate)?r.binaryExpression("+",e,r.conditionalExpression(i.test,L(r.stringLiteral(""),i.consequent),L(r.stringLiteral(""),i.alternate))):r.binaryExpression("+",L(e,r.stringLiteral("")),i)}},function(e){if(1!==e.length)return;const[t]=e;return r.isLogicalExpression(t,{operator:"&&"})&&(b(t.right)||B(t.right)||r.isBinaryExpression(t.right,{operator:"+"})&&b(t.right.left)&&B(t.right.right))?r.conditionalExpression(t.left,t.right,r.stringLiteral("")):r.isLogicalExpression(t,{operator:"||"})&&b(t.right)?t:void 0},function(e){if(2!==e.length)return;const[t,n]=e;return b(t)&&r.isLogicalExpression(n,{operator:"&&"})&&b(n.right)?r.binaryExpression("+",t,r.conditionalExpression(n.left,L(r.stringLiteral(""),n.right),r.stringLiteral(""))):void 0}],M={ArrayExpression(e){for(const t of _){const r=t(e.node.elements);if(void 0!==r){e.replaceWith(r);break}}}};function V(e,t){return t(e.left)||t(e.right)}function R(e){const[t,n]=h(e=>{return function e(t){if(A(t))return e(t.left);if(r.isBinaryExpression(t,{operator:"==="})&&V(t,r.isStringLiteral)&&(V(t,r.isMemberExpression)||V(t,r.isIdentifier)))return!0;return!1}(e)},e);return 0===t.length?e:[...n,...f(p(I),p(e=>{let t=x({},e[0]);return r.isStringLiteral(t.left)&&(t=r.binaryExpression("===",t.right,t.left)),[t,...e.slice(1)]}),c(e=>w(e[0].left)),u,p(e=>{const t=e.reduce((e,t)=>{let n=t[0].right;return!1===/[^A-Za-z]/.test(t[0].right.value)&&(n=r.identifier(t[0].right.value)),e.push(r.objectProperty(n,P(t.slice(1)))),e},[]),n=r.memberExpression(r.objectExpression(t),e[0][0].left,!0);if(e.length>1)return n;const i=P(e[0]),s=o(i,{compact:!0}).code,l=o(n,{compact:!0}).code;return s.length<=l.length?i:n}))(t)]}const F={ArrayExpression(e){e.node.elements=R(e.node.elements),1===e.node.elements.length&&e.replaceWith(e.node.elements[0])}};let $=null,z=0;function K(e){return f(m(w),p(e=>(r.isBinaryExpression(e)&&("!=="===e.operator||"==="===e.operator)&&r.isNumericLiteral(e.right,{value:1})&&r.isBinaryExpression(e.left,{operator:"%"})&&r.isNumericLiteral(e.left.right,{value:2})&&(e.right=r.numericLiteral(0),e.operator="==="===e.operator?"!==":"==="),e)))(e)}const Z=function(e){return e.reduce((e,t)=>("object"==typeof t?e.length&&Array.isArray(e[e.length-1])?e[e.length-1].push(t):e.push([t]):e.push(t),e),[]).map(e=>{if(!Array.isArray(e))return e;const n={};let i={};for(const s of e)t(s,(e,t)=>{void 0===n[t]&&(n[t]=[]),n[t].push(e),void 0===i[t]&&(i=x({},i,{[t](e,...i){let s=null;if("CallExpression"===t){const t=e.node.callee;if(!r.isIdentifier(t)||!this.options.functionNames.includes(t.name))return;s=t.name}for(const r of n[t])if(r.call(this,e,...i),e.shouldSkip||e.type!==t)return void(null!==s&&"CallExpression"===t&&"CallExpression"!==e.type&&void 0!==this.functionCounters&&this.functionCounters[s]&&(this.functionCounters[s]-=1))}}))});return(e,t)=>{e.traverse(i,{options:t,functionCounters:e.functionCounters})}})}([{CallExpression(e){if(this.options.collectCalls){if(null===$){const e=s.join(__dirname,"log.js");$=i.createWriteStream(e,{flags:"w"}),console.log("Writing calls to "+e)}$.write(`const x${++z} = ${o(e.node).code};\n\n`)}}},{CallExpression(e){e.node.arguments=g(function e(t){return t.map(t=>r.isArrayExpression(t)?e(t.elements):t)}(e.node.arguments))}},E,(e,t)=>{const r=T(e.node.body);0!==r.length&&e.traverse(N,{options:t,propTypes:r})},{CallExpression(e){e.node.arguments=e.node.arguments.map(e=>(r.isConditionalExpression(e)&&(D(e.alternate)?e=r.logicalExpression("&&",e.test,e.consequent):D(e.consequent)&&(e=r.logicalExpression("&&",r.unaryExpression("!",e.test),e.alternate))),A(e)&&(e=f(I,K,P)(e)),e))}},{CallExpression(e){const[t,n]=a(e.node.arguments,A),i=t.map(I).map(e=>e.filter((t,n)=>!(r.isBooleanLiteral(t,{value:!0})||n!==e.length-1&&r.isNumericLiteral(t)&&0!==t.value||n!==e.length-1&&b(t)&&!q(t)))).filter(e=>!(0===e.length||e.some(D))).map(P),s=n.filter(e=>!D(e));e.node.arguments=[...s,...i]}},{CallExpression(e){if(e.node.arguments.length<2)return;const[t,n]=a(e.node.arguments,A);if(t.length<2)return;const i=t.map(I),s=O(i);if(null===s)return;const o=function e(t){if("rootNode"!==t.type)return t;const n=[];if("rootNode"===t.child.type){let i=e(t.child);i=1===i.length?i[0]:r.arrayExpression(i),n.push(r.logicalExpression("&&",t.node,i))}else n.push(r.logicalExpression("&&",t.node,t.child));if(void 0!==t.next){const r=e(t.next);Array.isArray(r)?n.push(...r):n.push(r)}return n}(function e(t,n){const i={type:"rootNode",node:n,child:[],next:[]};t.forEach(e=>{const t=e.filter(e=>!S(e,n));t.length===e.length?i.next.push(e):i.child.push(t)});i.next=o(i.next);const s=o(i.child);Array.isArray(s)?i.child=1===s.length?s[0]:r.arrayExpression(s):i.child=s;return i;function o(t){if(0!==t.length){if(t.length>1){const r=O(t);if(null!==r)return e(t,r)}return t.map(P)}}}(i,s));return void(e.node.arguments=[...n,...o])}},{CallExpression(e){e.node.arguments=U(e.node.arguments),e.traverse(W)}},{CallExpression(e){e.node.arguments=function e(t){const[n,i]=a(t,A);if(0===n.length)return i;let s=n.map(I).map(t=>{let n=t.pop();return r.isArrayExpression(n)&&(n.elements=e(n.elements),1===n.elements.length&&(n=n.elements[0])),t.push(n),t});const o=i;for(let e=0;e<s.length;e++){const t=s[e];function l(){for(let n=0;n<t.length-1;n++){const i=t[n],l=r.isUnaryExpression(i,{operator:"!"}),a=r.isBinaryExpression(i,{operator:"!=="});if(l||a)for(let u=0;u<s.length;u++){const c=s[u];for(let p=0;p<c.length-1;p++){const f=c[p];if(e===u&&n===p)continue;if(l&&!S(i.argument,f))continue;if(a&&(!r.isBinaryExpression(f,{operator:"==="})||!(S(i.left,f.left)&&S(i.right,f.right)||S(i.left,f.right)&&S(i.right,f.left))))continue;const h=P(c.filter((e,t)=>t!==p)),g=P(t.filter((e,t)=>t!==n));return o.push(r.conditionalExpression(f,h,g)),s=s.filter((t,r)=>r!==e&&r!==u),!0}}}return!1}l()&&(e-=1)}return[...o,...s.map(P)]}(e.node.arguments)}},{CallExpression(e){if(this.options.removeUnnecessaryCalls){e.traverse(M);for(const t of _){const r=t(e.node.arguments);if(void 0!==r){e.replaceWith(r),e.skip();break}}}}},{CallExpression(e){e.traverse(F),e.node.arguments=R(e.node.arguments)}},(e,t)=>v(e,x({},t,{_removeUnusedImports:!0}))]);module.exports=()=>({visitor:{Program(e,t){const r=function(e){return x({},{libraries:["clsx","classnames"],functionNames:[],removeUnnecessaryCalls:!0,collectCalls:!1},{},e)}(t.opts);if(v(e,r),0!==r.functionNames.length)try{for(const t of Z)t(e,r)}catch(e){throw e}}}});
//# sourceMappingURL=index.js.map
{
"name": "babel-plugin-optimize-clsx",
"version": "2.4.0",
"version": "2.4.1",
"main": "dist/index.js",

@@ -5,0 +5,0 @@ "files": [

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc