Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invariant: improve code that babel outputs #2064

Merged
merged 1 commit into from Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions resources/inline-invariant.js
Expand Up @@ -15,9 +15,7 @@
*/
module.exports = function inlineInvariant(context) {
const replaceTemplate = context.template(`
if (!%%cond%%) {
invariant(0, %%args%%);
}
(%%cond%%) || invariant(0, %%args%%)
`);

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

function isAppropriateInvariantCall(node, parent) {
return (
parent.type === 'ExpressionStatement' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'invariant' &&
node.arguments.length > 0 &&
parent.type === 'ExpressionStatement'
node.arguments.length > 0
);
}
6 changes: 3 additions & 3 deletions src/jsutils/invariant.js
@@ -1,9 +1,9 @@
// @flow strict

export default function invariant(condition: mixed, message: string) {
/* istanbul ignore file */
export default function invariant(condition: mixed, message?: string): void {
const booleanCondition = Boolean(condition);
/* istanbul ignore else */
if (!booleanCondition) {
throw new Error(message);
throw new Error(message || 'Unexpected invariant triggered');
}
}