babel-plugin-optimize-clsx
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -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 @@ |
{ | ||
"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, | ||
}; |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
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
10798
60
0
3
13
196
2
+ Added@babel/generator@^7.4.4
+ Addedlodash@^4.17.11
+ Added@babel/generator@7.25.7(transitive)
+ Added@jridgewell/gen-mapping@0.3.5(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Addedjsesc@3.0.2(transitive)
+ Addedlodash@4.17.21(transitive)