Use simple invariant in case of missing error codes (#11350)
Dan Abramov committed
Oct 24, 2017 at 19:11 UTC
793a28d69895c88d08ac1165e403125502df1f62
1 file changed
+16
-17
scripts/error-codes/replace-invariant-error-codes.js
+16
-17
@@ -102,34 +102,33 @@ module.exports = function(babel) {
102
devInvariant[SEEN_SYMBOL] = true;
103
104
var localInvariantId = getProdInvariantIdentifier(path, this);
105
-
105
var prodErrorId = errorMap[errorMsgLiteral];
107
- var prodInvariant;
106
+ var body = null;
107
+
108
if (prodErrorId === undefined) {
109
- // The error cannot be found in the map.
110
- // (This case isn't expected to occur.)
111
- // Even if it does, it's best to transform the invariant to a ternary,
112
- // So we don't risk executing any slow code unnecessarily
113
- // (eg generating an invariant message we don't actually need).
114
- prodInvariant = path.node.arguments[1];
109
+ // The error wasn't found in the map.
110
+ // This is only expected to occur on master since we extract codes before releases.
111
+ // Keep the original invariant.
112
+ body = t.expressionStatement(devInvariant);
113
} else {
116
- prodInvariant = t.callExpression(
114
+ var prodInvariant = t.callExpression(
115
localInvariantId,
116
[t.stringLiteral(prodErrorId)].concat(node.arguments.slice(2))
117
);
118
+ prodInvariant[SEEN_SYMBOL] = true;
119
+ // The error was found in the map.
120
+ // Switch between development and production versions depending on the env.
121
+ body = t.ifStatement(
122
+ DEV_EXPRESSION,
123
+ t.blockStatement([t.expressionStatement(devInvariant)]),
124
+ t.blockStatement([t.expressionStatement(prodInvariant)])
125
+ );
126
}
127
122
- prodInvariant[SEEN_SYMBOL] = true;
128
path.replaceWith(
129
t.ifStatement(
130
t.unaryExpression('!', condition),
126
- t.blockStatement([
127
- t.ifStatement(
128
- DEV_EXPRESSION,
129
- t.blockStatement([t.expressionStatement(devInvariant)]),
130
- t.blockStatement([t.expressionStatement(prodInvariant)])
131
- ),
132
- ])
131
+ t.blockStatement([body])
132
)
133
);
134
}