-
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.
feat: remove unnecessary function calls
- Loading branch information
Showing
14 changed files
with
109 additions
and
1 deletion.
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,65 @@ | ||
const t = require('@babel/types'); | ||
|
||
const visitor = { | ||
CallExpression(path) { | ||
const c = path.node.callee; | ||
if (!t.isIdentifier(c) || !this.options.functionNames.includes(c.name)) { | ||
return; | ||
} | ||
|
||
const argLength = path.node.arguments.length; | ||
if (argLength > 0) { | ||
let arg = path.node.arguments[0]; | ||
if (argLength === 1) { | ||
if (t.isStringLiteral(arg)) { | ||
path.replaceWith(arg); | ||
} else if (isSafeConditional(arg)) { | ||
path.replaceWith(arg); | ||
} | ||
} else if (argLength === 2) { | ||
let arg2 = path.node.arguments[1]; | ||
if (isSafeConditional(arg) && isSafeConditional(arg2)) { | ||
const newCond = t.conditionalExpression( | ||
arg.test, | ||
t.stringLiteral(arg.consequent.value + ' '), | ||
t.stringLiteral(arg.alternate.value + ' '), | ||
); | ||
path.replaceWith(t.binaryExpression('+', newCond, arg2)); | ||
} else if ( | ||
(t.isStringLiteral(arg) || t.isStringLiteral(arg2)) && | ||
(isSafeConditional(arg) || isSafeConditional(arg2)) | ||
) { | ||
if (t.isStringLiteral(arg)) { | ||
path.replaceWith(t.binaryExpression('+', t.stringLiteral(arg.value + ' '), arg2)); | ||
} else { | ||
path.replaceWith(t.binaryExpression('+', t.stringLiteral(arg2.value + ' '), arg)); | ||
} | ||
} | ||
} | ||
} else if (argLength === 0) { | ||
path.replaceWith(t.stringLiteral('')); | ||
} | ||
|
||
function isSafeConditional(node) { | ||
return ( | ||
t.isConditionalExpression(node) && | ||
t.isStringLiteral(node.consequent) && | ||
t.isStringLiteral(node.alternate) && | ||
node.consequent.value.length > 0 && | ||
node.alternate.value.length > 0 | ||
); | ||
} | ||
}, | ||
}; | ||
|
||
module.exports = (path, options) => { | ||
if (!t.isProgram(path.node)) { | ||
throw new Error('Node has to be a program node'); | ||
} | ||
|
||
if (!options.removeUnnecessaryCalls) { | ||
return; | ||
} | ||
|
||
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,3 +1,4 @@ | ||
{ | ||
"functionNames": ["clsx"] | ||
"functionNames": ["clsx"], | ||
"removeUnnecessaryCalls": false | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/remove-unnecessary-calls/one-conditional-argument/code.js
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 @@ | ||
const baseClass = clsx(foo ? 'a' : 'b'); |
1 change: 1 addition & 0 deletions
1
test/fixtures/remove-unnecessary-calls/one-conditional-argument/output.js
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 @@ | ||
const baseClass = foo ? 'a' : 'b'; |
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,3 @@ | ||
{ | ||
"removeUnnecessaryCalls": true | ||
} |
2 changes: 2 additions & 0 deletions
2
test/fixtures/remove-unnecessary-calls/string-and-conditional-argument/code.js
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,2 @@ | ||
const classA = clsx('foo bar', foo ? 'a' : 'b'); | ||
const classB = clsx(foo ? 'a' : 'b', 'foo bar'); |
2 changes: 2 additions & 0 deletions
2
test/fixtures/remove-unnecessary-calls/string-and-conditional-argument/output.js
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,2 @@ | ||
const classA = 'foo bar ' + (foo ? 'a' : 'b'); | ||
const classB = 'foo bar ' + (foo ? 'a' : 'b'); |
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 @@ | ||
const baseClass = clsx('foo bar'); |
1 change: 1 addition & 0 deletions
1
test/fixtures/remove-unnecessary-calls/string-argument/output.js
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 @@ | ||
const baseClass = 'foo bar'; |
1 change: 1 addition & 0 deletions
1
test/fixtures/remove-unnecessary-calls/two-conditional-arguments/code.js
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 @@ | ||
const baseClass = clsx(foo ? 'a' : 'b', bar ? 'c' : 'd'); |
1 change: 1 addition & 0 deletions
1
test/fixtures/remove-unnecessary-calls/two-conditional-arguments/output.js
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 @@ | ||
const baseClass = (foo ? 'a ' : 'b ') + (bar ? 'c' : 'd'); |