-
Notifications
You must be signed in to change notification settings - Fork 47.9k
/
Copy pathreplace-invariant-error-codes.js
136 lines (123 loc) · 4.8 KB
/
replace-invariant-error-codes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const fs = require('fs');
const {basename} = require('path');
const evalToString = require('../shared/evalToString');
const invertObject = require('./invertObject');
module.exports = function(babel) {
const t = babel.types;
const SEEN_SYMBOL = Symbol('replace-invariant-error-codes.seen');
// Generate a hygienic identifier
function getProdInvariantIdentifier(path, file, localState) {
if (!localState.prodInvariantIdentifier) {
localState.prodInvariantIdentifier = file.addImport(
'shared/reactProdInvariant',
'default',
'prodInvariant'
);
}
return localState.prodInvariantIdentifier;
}
const DEV_EXPRESSION = t.identifier('__DEV__');
return {
pre() {
this.prodInvariantIdentifier = null;
},
visitor: {
CallExpression: {
exit(path, file) {
const node = path.node;
// Ignore if it's already been processed
if (node[SEEN_SYMBOL]) {
return;
}
// Insert `import PROD_INVARIANT from 'reactProdInvariant';`
// before all `invariant()` calls.
if (path.get('callee').isIdentifier({name: 'invariant'})) {
// Turns this code:
//
// invariant(condition, argument, 'foo', 'bar');
//
// into this:
//
// if (!condition) {
// if ("production" !== process.env.NODE_ENV) {
// invariant(false, argument, 'foo', 'bar');
// } else {
// PROD_INVARIANT('XYZ', 'foo', 'bar');
// }
// }
//
// where
// - `XYZ` is an error code: a unique identifier (a number string)
// that references a verbose error message.
// The mapping is stored in `scripts/error-codes/codes.json`.
// - `PROD_INVARIANT` is the `reactProdInvariant` function that always throws with an error URL like
// http://reactjs.org/docs/error-decoder.html?invariant=XYZ&args[]=foo&args[]=bar
//
// Specifically this does 3 things:
// 1. Checks the condition first, preventing an extra function call.
// 2. Adds an environment check so that verbose error messages aren't
// shipped to production.
// 3. Rewrites the call to `invariant` in production to `reactProdInvariant`
// - `reactProdInvariant` is always renamed to avoid shadowing
// The generated code is longer than the original code but will dead
// code removal in a minifier will strip that out.
const condition = node.arguments[0];
const errorMsgLiteral = evalToString(node.arguments[1]);
const devInvariant = t.callExpression(
node.callee,
[
t.booleanLiteral(false),
t.stringLiteral(errorMsgLiteral),
].concat(node.arguments.slice(2))
);
devInvariant[SEEN_SYMBOL] = true;
// Avoid caching because we write it as we go.
const existingErrorMap = JSON.parse(
fs.readFileSync(__dirname + '/codes.json', 'utf-8')
);
const errorMap = invertObject(existingErrorMap);
const localInvariantId = getProdInvariantIdentifier(
path,
file,
this
);
const prodErrorId = errorMap[errorMsgLiteral];
let body = null;
if (prodErrorId === undefined) {
// The error wasn't found in the map.
// This is only expected to occur on master since we extract codes before releases.
// Keep the original invariant.
body = t.expressionStatement(devInvariant);
} else {
const prodInvariant = t.callExpression(
localInvariantId,
[t.stringLiteral(prodErrorId)].concat(node.arguments.slice(2))
);
prodInvariant[SEEN_SYMBOL] = true;
// The error was found in the map.
// Switch between development and production versions depending on the env.
body = t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([t.expressionStatement(devInvariant)]),
t.blockStatement([t.expressionStatement(prodInvariant)])
);
}
path.replaceWith(
t.ifStatement(
t.unaryExpression('!', condition),
t.blockStatement([body])
)
);
}
},
},
},
};
};