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.1 to 2.5.0

38

CHANGELOG.md

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

## [2.5.0](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v2.4.1...v2.5.0) (2019-10-07)
### Bug Fixes
- **combinestrings:** set cooked value for templateelement ([5e25580](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/5e25580)), closes [#15](https://github.com/merceyz/babel-plugin-optimize-clsx/issues/15)
- **extract:** handle spread and method properties ([51c8ced](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/51c8ced))
- **optimize:** handle ConditionalExpression in LogicalExpression ([fa05e72](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/fa05e72))
### Build System
- **deps:** upgrade dependencies ([cd02789](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/cd02789))
- improve terser output ([11ca28b](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/11ca28b))
- remove rollup-plugin-commonjs ([b994f88](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/b994f88))
### Features
- **collectcalls:** include the location of the code ([1ab1eab](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/1ab1eab))
### [2.4.1](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v2.4.0...v2.4.1) (2019-08-18)
### Build System
- **babel:** target node 8 ([a888c69](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/a888c69))
## [2.4.0](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v2.3.0...v2.4.0) (2019-08-18)

@@ -17,2 +39,9 @@

### Build System
- **deps:** add babel-plugin-lodash ([cd37d10](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/cd37d10))
- **deps-dev:** bump husky from 3.0.0 to 3.0.2 ([#3](https://github.com/merceyz/babel-plugin-optimize-clsx/issues/3)) ([de0ff50](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/de0ff50))
- **deps-dev:** bump rollup from 1.17.0 to 1.19.4 ([#12](https://github.com/merceyz/babel-plugin-optimize-clsx/issues/12)) ([0cb7b7e](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/0cb7b7e))
- **deps-dev:** bump standard-version from 6.0.1 to 7.0.0 ([#6](https://github.com/merceyz/babel-plugin-optimize-clsx/issues/6)) ([e699d1d](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/e699d1d))
### Features

@@ -24,2 +53,11 @@

### Performance Improvements
- **helper:** avoid unnecessary compares ([a5f1312](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/a5f1312))
- **helper:** compare type of nodes directly ([41ff13f](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/41ff13f))
- **helper:** use switch statement over if else chain ([b2874ad](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/b2874ad))
- track removed calls to avoid crawling the AST ([28188a8](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/28188a8))
- **proptypes:** skip traversing child nodes ([ec883af](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/ec883af))
- combine visitors ([#8](https://github.com/merceyz/babel-plugin-optimize-clsx/issues/8)) ([d6a9a62](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/d6a9a62))
## [2.3.0](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v2.2.0...v2.3.0) (2019-07-18)

@@ -26,0 +64,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"));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}}}});
"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/flatMap")),i=e(require("lodash/isEqualWith")),s=e(require("fs")),o=e(require("path")),a=e(require("@babel/generator")),l=e(require("object-hash")),u=e(require("@babel/template")),c=e(require("lodash/partition")),p=e(require("lodash/fp/values")),f=e(require("lodash/fp/groupBy")),g=e(require("lodash/fp/map")),h=e(require("lodash/fp/flow")),m=e(require("lodash/fp/partition")),d=e(require("lodash/flattenDeep")),y=e(require("lodash/fp/uniqBy"));function x(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function v(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 E(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?v(r,!0).forEach((function(t){x(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):v(r).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var b=(e,n)=>{function i(t,r){n.functionNames.push(r),e.functionCounters=E({},e.functionCounters||{},{[r]:t.scope.getBinding(r).references}),e.importPaths=E({},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)})}))},L={CallExpression(e){e.node.arguments=n(e.node.arguments,e=>r.isObjectExpression(e)?e.properties.map(e=>{function t(){return e.computed?e.key:r.isStringLiteral(e.key)?e.key:r.stringLiteral(e.key.name)}return r.isSpreadElement(e)?e.argument:r.isObjectMethod(e)?t():r.logicalExpression("&&",e.value,t())}):e)}};function q(e){return r.isStringLiteral(e)||r.isTemplateLiteral(e)}function C(e,t){if(j(e)&&j(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]=S(n[n.length-1].value.raw+" "+t.quasis[0].value.raw),n.push(...t.quasis.slice(1)),I(n,r)}if(r.isTemplateLiteral(e)&&r.isStringLiteral(t)){const r=[...e.expressions],n=[...e.quasis],i=n.length-1;return n[i]=S(n[i].value.raw+" "+t.value,!0),I(n,r)}if(r.isStringLiteral(e)&&r.isTemplateLiteral(t)){const r=[...t.expressions],n=[...t.quasis],i=0;return n[i]=S(e.value+" "+n[i].value.raw,!0),I(n,r)}throw new Error("Unable to handle that input")}function j(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 I(e,t){return 0===t.length?r.stringLiteral(e[0].value.raw):r.templateLiteral(e,t)}function S(e,t=!1){const n=u.ast(`\`${e}\``).expression.quasis[0].value;return r.templateElement(n,t)}function O(e){const t=[];return function e(n){r.isLogicalExpression(n)?(e(n.left),t.push(n.right)):t.push(n)}(e),t}function w(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 A(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],a=o.length-1;s<a;s++){let a=o[s],l=0;for(let t=i+1;t<n;t++)for(let r=0,n=e[t];r<n.length-1;r++)B(a,n[r])&&(l+=1);l>r&&(t=a,r=l)}return t}__dirname;function B(e,t){if(e.type!==t.type)return!1;switch(e.type){case"Identifier":return e.name===t.name;case"MemberExpression":return B(e.object,t.object)&&B(e.property,t.property);case"BinaryExpression":return e.operator===t.operator&&B(e.left,t.left)&&B(e.right,t.right);default:return r.isLiteral(e)?e.value===t.value:i(e,t,(e,t,r)=>"start"===r||"end"===r||"loc"===r||void 0)}}function D(e){return l(e,{excludeKeys:e=>"start"===e||"end"===e||"loc"===e||"extra"===e})}function P(e){if(!r.isConditionalExpression(e))return!1;const{consequent:t,alternate:n}=e;return!(!q(t)||!q(n))||!!(q(t)&&P(n)||q(n)&&P(t))}function k(e){return e.reduce((e,t)=>r.logicalExpression("&&",e,t))}function N(e){return j(e)||r.isNullLiteral(e)||r.isIdentifier(e,{name:"undefined"})||r.isBooleanLiteral(e,{value:!1})||r.isNumericLiteral(e,{value:0})}const T={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)&&(B(t.left,r)||B(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?"===":"!==")}}},U={"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=[]),E({},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=[]),E({},t,{matches:[...t.matches,r.memberExpression(e,r.identifier(t.name))]}))),n(t.body,e);if(r.isVariableDeclarator(e))if(r.isMemberExpression(e.init)&&B(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=[]),E({},t,{matches:[...t.matches,e.id]})));else if(r.isIdentifier(e.init)&&B(e.init,s)&&r.isObjectPattern(e.id))return n(e.id,null)}}}n(t.params[0],null),e.traverse(T,{types:i.values}),e.skip()}}};function M(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 W(e){if(e.length<2)return e;const[t,r]=c(e,q);return t.length<2?e:[t.reduce(C),...r]}const $={ArrayExpression(e){const{node:t}=e;t.elements=W(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&&q(e[0]))return e[0]},function(e){if(1===e.length&&P(e[0]))return e[0]},function(e){if(2!==e.length)return;const[t,n]=e;if(e.every(P)){const e=r.conditionalExpression(t.test,C(t.consequent,r.stringLiteral("")),C(t.alternate,r.stringLiteral("")));return r.binaryExpression("+",e,n)}},function(e){if(2!==e.length)return;const[t,n]=e;if(e.some(q)&&e.some(P)){const e=q(t)?t:n,i=q(n)?t:n;return j(i.consequent)||j(i.alternate)?r.binaryExpression("+",e,r.conditionalExpression(i.test,C(r.stringLiteral(""),i.consequent),C(r.stringLiteral(""),i.alternate))):r.binaryExpression("+",C(e,r.stringLiteral("")),i)}},function(e){if(1!==e.length)return;const[t]=e;return r.isLogicalExpression(t,{operator:"&&"})&&(q(t.right)||P(t.right)||r.isBinaryExpression(t.right,{operator:"+"})&&q(t.right.left)&&P(t.right.right))?r.conditionalExpression(t.left,t.right,r.stringLiteral("")):r.isLogicalExpression(t,{operator:"||"})&&q(t.right)?t:void 0},function(e){if(2!==e.length)return;const[t,n]=e;return q(t)&&r.isLogicalExpression(n,{operator:"&&"})&&q(n.right)?r.binaryExpression("+",t,r.conditionalExpression(n.left,C(r.stringLiteral(""),n.right),r.stringLiteral(""))):void 0}],V={ArrayExpression(e){for(const t of _){const r=t(e.node.elements);if(void 0!==r){e.replaceWith(r);break}}}};function R(e,t){return t(e.left)||t(e.right)}function F(e){const[t,n]=m(e=>{return function e(t){if(w(t))return e(t.left);if(r.isBinaryExpression(t,{operator:"==="})&&R(t,r.isStringLiteral)&&(R(t,r.isMemberExpression)||R(t,r.isIdentifier)))return!0;return!1}(e)},e);return 0===t.length?e:[...n,...h(g(O),g(e=>{let t=E({},e[0]);return r.isStringLiteral(t.left)&&(t=r.binaryExpression("===",t.right,t.left)),[t,...e.slice(1)]}),f(e=>D(e[0].left)),p,g(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,k(t.slice(1)))),e},[]),n=r.memberExpression(r.objectExpression(t),e[0][0].left,!0);if(e.length>1)return n;const i=k(e[0]),s=a(i,{compact:!0}).code,o=a(n,{compact:!0}).code;return s.length<=o.length?i:n}))(t)]}const z={ArrayExpression(e){e.node.elements=F(e.node.elements),1===e.node.elements.length&&e.replaceWith(e.node.elements[0])}};let K=null,Z=0;const G={ConditionalExpression:e=>N(e.alternate)?r.logicalExpression("&&",e.test,e.consequent):N(e.consequent)?r.logicalExpression("&&",r.unaryExpression("!",e.test),e.alternate):void 0,BinaryExpression(e){if(("!=="===e.operator||"==="===e.operator)&&r.isNumericLiteral(e.right,{value:1})&&r.isBinaryExpression(e.left,{operator:"%"})&&r.isNumericLiteral(e.left.right,{value:2}))return E({},e,{right:r.numericLiteral(0),operator:"==="===e.operator?"!==":"==="})},LogicalExpression(e){if(w(e))return h(O,y(D),g(H),k)(e)}};function H(e){if(e.type in G){let t=G[e.type](e);if(t)return t.type!==e.type?H(t):t}return e}const J=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=E({},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,r)=>{e.traverse(i,{state:r,options:t,functionCounters:e.functionCounters})}})}([{CallExpression(e){if(!this.options.collectCalls)return;if(null===K){const e=o.join(__dirname,`log-${new Date(Date.now()).toISOString().replace(/:/g,".")}.js`);K=s.createWriteStream(e,{flags:"w"}),console.log("Writing calls to "+e)}const t=e.node.loc.start;K.write(`// ${this.state.filename}:${t.line}:${t.column}\nconst x${++Z} = ${a(e.node).code};\n\n`)}},{CallExpression(e){e.node.arguments=d(function e(t){return t.map(t=>r.isArrayExpression(t)?e(t.elements):t)}(e.node.arguments))}},L,(e,t)=>{const r=M(e.node.body);0!==r.length&&e.traverse(U,{options:t,propTypes:r})},{CallExpression({node:e}){e.arguments=e.arguments.map(H)}},{CallExpression(e){const[t,n]=c(e.node.arguments,w),i=t.map(O).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&&q(t)&&!j(t)))).filter(e=>!(0===e.length||e.some(N))).map(k),s=n.filter(e=>!N(e));e.node.arguments=[...s,...i]}},{CallExpression(e){if(e.node.arguments.length<2)return;const[t,n]=c(e.node.arguments,w);if(t.length<2)return;const i=t.map(O),s=A(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=>!B(e,n));t.length===e.length?i.next.push(e):i.child.push(t)});i.next=o(i.next);const s=o(i.child);i.child=Array.isArray(s)?1===s.length?s[0]:r.arrayExpression(s):s;return i;function o(t){if(0!==t.length){if(t.length>1){const r=A(t);if(null!==r)return e(t,r)}return t.map(k)}}}(i,s));return void(e.node.arguments=[...n,...o])}},{CallExpression(e){e.node.arguments=W(e.node.arguments),e.traverse($)}},{CallExpression(e){e.node.arguments=function e(t){const[n,i]=c(t,w);if(0===n.length)return i;let s=n.map(O).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 a(){for(let n=0;n<t.length-1;n++){const i=t[n],a=r.isUnaryExpression(i,{operator:"!"}),l=r.isBinaryExpression(i,{operator:"!=="});if(a||l)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(a&&!B(i.argument,f))continue;if(l&&(!r.isBinaryExpression(f,{operator:"==="})||!(B(i.left,f.left)&&B(i.right,f.right)||B(i.left,f.right)&&B(i.right,f.left))))continue;const g=k(c.filter((e,t)=>t!==p)),h=k(t.filter((e,t)=>t!==n));return o.push(r.conditionalExpression(f,g,h)),s=s.filter((t,r)=>r!==e&&r!==u),!0}}}return!1}a()&&(e-=1)}return[...o,...s.map(k)]}(e.node.arguments)}},{CallExpression(e){if(this.options.removeUnnecessaryCalls){e.traverse(V);for(const t of _){const r=t(e.node.arguments);if(void 0!==r){e.replaceWith(r),e.skip();break}}}}},{CallExpression(e){e.traverse(z),e.node.arguments=F(e.node.arguments)}},(e,t)=>b(e,E({},t,{_removeUnusedImports:!0}))]);module.exports=()=>({visitor:{Program(e,t){const r=function(e){return E({},{libraries:["clsx","classnames"],functionNames:[],removeUnnecessaryCalls:!0,collectCalls:!1},{},e)}(t.opts);if(b(e,r),0!==r.functionNames.length)try{for(const n of J)n(e,r,t)}catch(e){throw e}}}});
//# sourceMappingURL=index.js.map
{
"name": "babel-plugin-optimize-clsx",
"version": "2.4.1",
"version": "2.5.0",
"main": "dist/index.js",

@@ -28,28 +28,30 @@ "files": [

"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"@types/jest": "^24.0.15",
"babel-jest": "^24.7.1",
"@babel/core": "^7.6.2",
"@babel/plugin-transform-template-literals": "^7.4.4",
"@babel/preset-env": "^7.6.2",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@types/jest": "^24.0.18",
"babel-jest": "^24.9.0",
"babel-plugin-lodash": "^3.3.4",
"babel-plugin-tester": "^6.4.0",
"babel-plugin-tester": "^7.0.1",
"benchmark": "^2.1.4",
"classnames": "^2.2.6",
"clsx": "^1.0.4",
"cross-env": "^5.2.0",
"husky": "^3.0.0",
"jest": "^24.7.1",
"cross-env": "^6.0.3",
"husky": "^3.0.8",
"jest": "^24.9.0",
"prettier": "^1.18.2",
"pretty-quick": "^1.11.1",
"rimraf": "^2.6.3",
"rollup": "^1.19.4",
"rimraf": "^3.0.0",
"rollup": "^1.23.1",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-terser": "^5.1.1",
"rollup-plugin-terser": "^5.1.2",
"standard-version": "^7.0.0"
},
"dependencies": {
"@babel/generator": "^7.5.5",
"@babel/types": "^7.5.5",
"lodash": "^4.17.14",
"@babel/generator": "^7.6.2",
"@babel/template": "^7.6.0",
"@babel/types": "^7.6.1",
"lodash": "^4.17.15",
"object-hash": "^1.3.1"

@@ -56,0 +58,0 @@ },

@@ -13,2 +13,9 @@ # babel-plugin-optimize-clsx

## Requirements
| Name | Version |
| ----- | ------- |
| Babel | ^7.0.0 |
| Node | >=8 |
## Examples

@@ -15,0 +22,0 @@

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