Skip to content

Commit

Permalink
inline-invariant: use template instead of AST (#1876)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 19, 2019
1 parent 8263c96 commit cabfd34
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions resources/inline-invariant.js
Expand Up @@ -21,7 +21,11 @@
* !<cond> ? invariant(0, ...) : undefined;
*/
module.exports = function inlineInvariant(context) {
const t = context.types;
const replaceTemplate = context.template(`
if (!%%cond%%) {
invariant(0, %%args%%);
}
`);

return {
visitor: {
Expand All @@ -33,17 +37,8 @@ module.exports = function inlineInvariant(context) {
return;
}

const args = node.arguments.slice(0);
args[0] = t.numericLiteral(0);

path.replaceWith(
t.ifStatement(
t.unaryExpression('!', node.arguments[0]),
t.expressionStatement(
t.callExpression(t.identifier(node.callee.name), args)
)
)
);
const [cond, args] = node.arguments;
path.replaceWith(replaceTemplate({ cond, args }));
},
},
};
Expand Down

0 comments on commit cabfd34

Please sign in to comment.