Skip to content

Commit 91a4be2

Browse files
invariant: improve code that babel outputs (#2064)
1 parent dea6028 commit 91a4be2

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

resources/inline-invariant.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
module.exports = function inlineInvariant(context) {
1717
const replaceTemplate = context.template(`
18-
if (!%%cond%%) {
19-
invariant(0, %%args%%);
20-
}
18+
(%%cond%%) || invariant(0, %%args%%)
2119
`);
2220

2321
return {
@@ -39,9 +37,9 @@ module.exports = function inlineInvariant(context) {
3937

4038
function isAppropriateInvariantCall(node, parent) {
4139
return (
40+
parent.type === 'ExpressionStatement' &&
4241
node.callee.type === 'Identifier' &&
4342
node.callee.name === 'invariant' &&
44-
node.arguments.length > 0 &&
45-
parent.type === 'ExpressionStatement'
43+
node.arguments.length > 0
4644
);
4745
}

src/jsutils/invariant.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @flow strict
22

3-
export default function invariant(condition: mixed, message: string) {
3+
/* istanbul ignore file */
4+
export default function invariant(condition: mixed, message?: string): void {
45
const booleanCondition = Boolean(condition);
5-
/* istanbul ignore else */
66
if (!booleanCondition) {
7-
throw new Error(message);
7+
throw new Error(message || 'Unexpected invariant triggered');
88
}
99
}

0 commit comments

Comments
 (0)