-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as t from '@babel/types'; | ||
import _ from 'lodash'; | ||
import { hashNode } from '../utils/helpers'; | ||
import generate from '@babel/generator'; | ||
|
||
function matchLeftOrRight(node, check) { | ||
return check(node.left) || check(node.right); | ||
} | ||
|
||
function combineFromArray(arr) { | ||
// x === 'foo', 'foo' === x, x.y === 'foo', 'foo' === x.y | ||
const [match, noMatch] = _.partition(arr, item => { | ||
return ( | ||
t.isLogicalExpression(item, { operator: '&&' }) && | ||
t.isBinaryExpression(item.left, { operator: '===' }) && | ||
matchLeftOrRight(item.left, t.isStringLiteral) && | ||
(matchLeftOrRight(item.left, t.isMemberExpression) || | ||
matchLeftOrRight(item.left, t.isIdentifier)) | ||
); | ||
}); | ||
|
||
if (match.length === 0) { | ||
return arr; | ||
} | ||
|
||
// Make sure the string is on the right side of === | ||
// Makes the rest of the code simpler | ||
const rearranged = match.map(node => { | ||
const tempNode = { ...node }; | ||
if (t.isStringLiteral(tempNode.left.left)) { | ||
tempNode.left = t.binaryExpression('===', tempNode.left.right, tempNode.left.left); | ||
} | ||
return tempNode; | ||
}); | ||
|
||
// Group on whatever the strings are compared to | ||
const grouped = _.groupBy(rearranged, node => hashNode(node.left.left)); | ||
|
||
const newArgs = []; | ||
_.forOwn(grouped, item => { | ||
const result = item.reduce((acc, node) => { | ||
let key = node.left.right; | ||
|
||
// If possible, use a identifier as the key, saves 2 characters | ||
if (/[^A-Za-z]/.test(node.left.right.value) === false) { | ||
key = t.identifier(node.left.right.value); | ||
} | ||
|
||
acc.push(t.objectProperty(key, node.right)); | ||
return acc; | ||
}, []); | ||
|
||
const output = t.memberExpression(t.objectExpression(result), item[0].left.left, true); | ||
if (item.length > 1) { | ||
newArgs.push(output); | ||
return; | ||
} | ||
|
||
// If the size is the same, use the original | ||
const a = generate(item[0], { compact: true }).code; | ||
const b = generate(output, { compact: true }).code; | ||
newArgs.push(a.length <= b.length ? item[0] : output); | ||
}); | ||
|
||
return [...noMatch, ...newArgs]; | ||
} | ||
|
||
const arrayVisitor = { | ||
ArrayExpression(path) { | ||
path.node.elements = combineFromArray(path.node.elements); | ||
|
||
if (path.node.elements.length === 1) { | ||
path.replaceWith(path.node.elements[0]); | ||
} | ||
}, | ||
}; | ||
|
||
const visitor = { | ||
CallExpression(path) { | ||
const c = path.node.callee; | ||
if (!t.isIdentifier(c) || !this.options.functionNames.includes(c.name)) { | ||
return; | ||
} | ||
|
||
path.traverse(arrayVisitor); | ||
path.node.arguments = combineFromArray(path.node.arguments); | ||
}, | ||
}; | ||
|
||
export default (path, options) => { | ||
path.traverse(visitor, { options }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
clsx( | ||
text && [ | ||
classes.text, | ||
color === 'primary' && [classes.text, classes.textPrimary], | ||
color === 'secondary' && classes.textSecondary, | ||
{ | ||
primary: [classes.text, classes.textPrimary], | ||
secondary: classes.textSecondary, | ||
}[color], | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
clsx({ | ||
[foo.a]: bar === 'up', | ||
[foo.b]: bar === 'down', | ||
[foo.c]: baz === 'left', | ||
[foo.d]: baz === 'right', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
clsx( | ||
{ | ||
up: foo.a, | ||
down: foo.b, | ||
}[bar], | ||
{ | ||
left: foo.c, | ||
right: foo.d, | ||
}[baz], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
clsx({ | ||
[foo.a]: direction === 'up', | ||
[foo.b]: direction === 'down', | ||
[foo.c]: direction === 'left', | ||
[foo.d]: direction === 'right', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
clsx( | ||
{ | ||
up: foo.a, | ||
down: foo.b, | ||
left: foo.c, | ||
right: foo.d, | ||
}[direction], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
clsx(context.alignItems === 'flex-start' && classes.alignItemsFlexStart); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
clsx(context.alignItems === 'flex-start' && classes.alignItemsFlexStart); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
clsx( | ||
classes.cellHide, | ||
options.responsive === 'stacked' && classes.cellStacked, | ||
!print && 'datatables-noprint', | ||
{ | ||
stacked: classes.cellStacked, | ||
}[options.responsive], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
clsx( | ||
classes.root, | ||
options.responsive === 'stacked' && classes.cellStacked, | ||
!print && 'datatables-noprint', | ||
{ | ||
stacked: classes.cellStacked, | ||
}[options.responsive], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters