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 1.1.0 to 1.1.1

8

CHANGELOG.md

@@ -5,2 +5,10 @@ # Change Log

## [1.1.1](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v1.1.0...v1.1.1) (2019-05-07)
### Bug Fixes
- **combine:** don't assume jagged array has dimension [x][1] ([3b308a1](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/3b308a1))
- **combine:** skip if no node appears more than once ([f888818](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/f888818))
- use lodash to compare nodes ([b10733e](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/b10733e))
# [1.1.0](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v1.0.1...v1.1.0) (2019-05-05)

@@ -7,0 +15,0 @@

11

package.json
{
"name": "babel-plugin-optimize-clsx",
"version": "1.1.0",
"version": "1.1.1",
"main": "src/index.js",

@@ -20,3 +20,4 @@ "files": [

"test": "jest",
"release": "standard-version"
"release": "standard-version",
"benchmark": "node benchmark/index.js"
},

@@ -30,2 +31,4 @@ "devDependencies": {

"babel-jest": "^24.7.1",
"benchmark": "^2.1.4",
"clsx": "^1.0.4",
"husky": "^2.2.0",

@@ -38,3 +41,5 @@ "jest": "^24.7.1",

"dependencies": {
"@babel/types": "^7.4.4"
"@babel/generator": "^7.4.4",
"@babel/types": "^7.4.4",
"lodash": "^4.17.11"
},

@@ -41,0 +46,0 @@ "husky": {

@@ -56,1 +56,5 @@ # babel-plugin-optimize-clsx

```
## Benchmarks
Benchmarks can be found in the [`benchmark`](/benchmark) directory

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

const _ = require('lodash');
const t = require('@babel/types');

@@ -6,5 +7,6 @@ const compareNodes = require('./utils/compareNodes');

module.exports = args => {
const [match, noMatch] = helpers.filterArray(args, item => {
return t.isLogicalExpression(item) && helpers.isAllLogicalAndOperators(item);
});
const [match, noMatch] = _.partition(
args,
item => t.isLogicalExpression(item) && helpers.isAllLogicalAndOperators(item),
);

@@ -17,2 +19,7 @@ // Not enough items to optimize

const node = helpers.getMostFrequentNode(operators);
// No nodes appear more than once
if (node === null) {
return args;
}
const rootNode = combineOperators(operators, node);

@@ -40,7 +47,3 @@

const r = convertToAST(node.next);
if (r.push !== undefined) {
result.push(...r);
} else {
result.push(r);
}
_.isArray(r) ? result.push(...r) : result.push(r);
}

@@ -69,4 +72,10 @@

newNode.next = checkSub(newNode.next);
newNode.child = checkSub(newNode.child);
const child = checkSub(newNode.child);
if (_.isArray(child)) {
newNode.child = child.length === 1 ? child[0] : t.arrayExpression(child);
} else {
newNode.child = child;
}
return newNode;

@@ -76,25 +85,22 @@

if (items.length === 0) return undefined;
if (items.length === 1) {
const item = items[0];
if (item.length === 1) {
return item[0];
if (items.length > 1) {
const nextCheck = helpers.getMostFrequentNode(items);
if (nextCheck !== null) {
return combineOperators(items, nextCheck);
}
}
let result = t.logicalExpression('&&', item.shift(), item.shift());
while (item.length > 0) {
result = t.logicalExpression('&&', result, item.shift());
return items.map(e => {
if (e.length === 1) return e[0];
let result = t.logicalExpression('&&', e.shift(), e.shift());
while (e.length > 0) {
result = t.logicalExpression('&&', result, e.shift());
}
return result;
}
const nextCheck = helpers.getMostFrequentNode(items);
if (nextCheck !== null) {
return combineOperators(items, nextCheck);
}
return t.arrayExpression(items.map(e => e[0]));
});
}
}
};

@@ -7,3 +7,3 @@ const t = require('@babel/types');

for (const argument of args) {
if (argument.type === 'ObjectExpression') {
if (t.isObjectExpression(argument)) {
for (const p of argument.properties) {

@@ -10,0 +10,0 @@ newArguments.push(t.LogicalExpression('&&', p.value, p.key));

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

const t = require('@babel/types');
const extractArguments = require('./extractArguments');

@@ -10,3 +11,3 @@ const combineArguments = require('./combineArguments');

const { callee: c } = node;
if (c.type === 'Identifier' && (c.name === 'clsx' || c.name === 'classNames')) {
if (t.isIdentifier(c) && (c.name === 'clsx' || c.name === 'classNames')) {
try {

@@ -13,0 +14,0 @@ let args = node.arguments;

@@ -1,40 +0,7 @@

module.exports = function compareNodes(a, b) {
if (typeof a !== typeof b) {
return false;
}
const isEqualWith = require('lodash/isEqualWith');
if (typeof a !== 'object') {
return a === b;
}
for (const key in a) {
if (a.hasOwnProperty(key)) {
// Ignore location data
if (key === 'start' || key === 'end' || key === 'loc') {
continue;
}
if (b.hasOwnProperty(key) === false) {
return false;
}
if (typeof a[key] === 'object') {
if (typeof b[key] !== 'object') {
return false;
}
if (compareNodes(a[key], b[key]) === false) {
return false;
}
continue;
}
if (a[key] !== b[key]) {
return false;
}
}
}
return true;
module.exports = (obj1, obj2) => {
return isEqualWith(obj1, obj2, (v1, v2, key) => {
return key === 'start' || key === 'end' || key === 'loc' ? true : undefined;
});
};
const t = require('@babel/types');
const fs = require('fs');
const path = require('path');
const compareNodes = require('./compareNodes');
const generate = require('@babel/generator');

@@ -24,17 +27,2 @@ function flattenLogicalOperator(node) {

function filterArray(array, callback) {
const match = [];
const noMatch = [];
for (const item of array) {
if (callback(item) === true) {
match.push(item);
} else {
noMatch.push(item);
}
}
return [match, noMatch];
}
function getMostFrequentNode(operators) {

@@ -79,8 +67,27 @@ let maxNode = null;

// Used during testing and debugging,
const counts = new Map();
const rootPath = path.join(__dirname, '../../dumps');
function dumpData(obj, name = 'dump', generateCode = false) {
const data = generateCode ? generate.default(obj).code : stringify(obj);
const count = counts.get(name) || 0;
counts.set(name, count + 1);
if (!fs.existsSync(rootPath)) {
fs.mkdirSync(rootPath);
}
fs.writeFileSync(
path.join(rootPath, name + '_' + count + (generateCode ? '.js' : '.json')),
data,
);
}
module.exports = {
dumpData,
flattenLogicalOperator,
isAllLogicalAndOperators,
filterArray,
getMostFrequentNode,
stringify,
};
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