Skip to content

Commit cabfd34

Browse files
inline-invariant: use template instead of AST (#1876)
1 parent 8263c96 commit cabfd34

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

resources/inline-invariant.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
* !<cond> ? invariant(0, ...) : undefined;
2222
*/
2323
module.exports = function inlineInvariant(context) {
24-
const t = context.types;
24+
const replaceTemplate = context.template(`
25+
if (!%%cond%%) {
26+
invariant(0, %%args%%);
27+
}
28+
`);
2529

2630
return {
2731
visitor: {
@@ -33,17 +37,8 @@ module.exports = function inlineInvariant(context) {
3337
return;
3438
}
3539

36-
const args = node.arguments.slice(0);
37-
args[0] = t.numericLiteral(0);
38-
39-
path.replaceWith(
40-
t.ifStatement(
41-
t.unaryExpression('!', node.arguments[0]),
42-
t.expressionStatement(
43-
t.callExpression(t.identifier(node.callee.name), args)
44-
)
45-
)
46-
);
40+
const [cond, args] = node.arguments;
41+
path.replaceWith(replaceTemplate({ cond, args }));
4742
},
4843
},
4944
};

0 commit comments

Comments
 (0)