Skip to content

Commit

Permalink
feat: strip literals
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed May 22, 2019
1 parent 300b006 commit f206424
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const visitors = [
require('./visitors/findFunctionNames'),
require('./visitors/extractObjectProperties'),
require('./visitors/propTypes'),
require('./visitors/stripLiterals'),
require('./visitors/combineArguments'),
require('./visitors/createConditionalExpression'),
];
Expand Down
58 changes: 58 additions & 0 deletions src/visitors/stripLiterals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const t = require('@babel/types');
const helpers = require('../utils/helpers');
const _ = require('lodash');

const visitor = {
CallExpression(path) {
const c = path.node.callee;
if (!t.isIdentifier(c) || !this.options.functionNames.includes(c.name)) {
return;
}

const [match, noMatch] = _.partition(
path.node.arguments,
item => t.isLogicalExpression(item) && helpers.isAllLogicalAndOperators(item),
);

if (match.length === 0) return;

const result = match
.map(helpers.flattenLogicalOperator)
.map(expression =>
expression.filter((item, index) => {
if (t.isBooleanLiteral(item, { value: true })) {
return false;
}
if (index !== expression.length - 1 && t.isStringLiteral(item) && item.value.length > 0) {
return false;
}
return true;
}),
)
.map(expression =>
expression.some(item => {
if (t.isBooleanLiteral(item, { value: false })) {
return true;
}
if (t.isStringLiteral(item) && item.value.length === 0) {
return true;
}
return false;
})
? []
: expression,
)
.filter(expression => expression.length !== 0)
.map(expression => expression.reduce((prev, curr) => t.logicalExpression('&&', prev, curr)));

path.node.arguments = [...noMatch, ...result];
},
};

module.exports = (path, options) => {
if (!t.isProgram(path.node)) {
throw new Error('Node has to be a program node');
}

path.traverse(visitor, { options });
};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
clsx(foo && bar, true && 'baz');
clsx('baz', foo && bar);
Original file line number Diff line number Diff line change
@@ -1 +1 @@
clsx(true && 'foo', false && 'bar', 'hello' && '--foobar');
clsx('foo', '--foobar');
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import myCustomFunction from 'my-custom-library';

myCustomFunction(true && 'foo');
myCustomFunction('foo');
2 changes: 1 addition & 1 deletion test/fixtures/find-function-names/using-import/output.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import fooFunction from 'clsx';

fooFunction(true && 'foo');
fooFunction('foo');
2 changes: 1 addition & 1 deletion test/fixtures/find-function-names/using-require/output.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const fooFunction = require('clsx');

fooFunction(true && 'foo');
fooFunction('foo');
6 changes: 6 additions & 0 deletions test/fixtures/strip-literals/boolean-literal/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
clsx({
[classes.root]: false,
[classes.cellHide]: true,
[classes.cellStacked]: options.responsive === 'stacked',
'datatables-noprint': !print,
});
5 changes: 5 additions & 0 deletions test/fixtures/strip-literals/boolean-literal/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
clsx(
classes.cellHide,
options.responsive === 'stacked' && classes.cellStacked,
!print && 'datatables-noprint',
);
6 changes: 6 additions & 0 deletions test/fixtures/strip-literals/string-literal/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
clsx({
[classes.root]: 'true',
[classes.cellHide]: '',
[classes.cellStacked]: options.responsive === 'stacked',
'datatables-noprint': !print,
});
6 changes: 6 additions & 0 deletions test/fixtures/strip-literals/string-literal/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
clsx(
classes.root,

options.responsive === 'stacked' && classes.cellStacked,
!print && 'datatables-noprint',
);

0 comments on commit f206424

Please sign in to comment.