mirror of
https://github.com/zebrajr/react.git
synced 2026-01-15 12:15:22 +00:00
refactor: scripts/error-codes (#11697)
Convert scripts/error-codes to use ES6 syntax
This commit is contained in:
committed by
Dan Abramov
parent
2ae4c62158
commit
b097a34eba
@@ -6,9 +6,9 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var invertObject = require('../invertObject');
|
||||
const invertObject = require('../invertObject');
|
||||
|
||||
var objectValues = target => Object.keys(target).map(key => target[key]);
|
||||
const objectValues = target => Object.keys(target).map(key => target[key]);
|
||||
|
||||
describe('invertObject', () => {
|
||||
it('should return an empty object for an empty input', () => {
|
||||
|
||||
@@ -17,11 +17,11 @@ function transform(input) {
|
||||
}
|
||||
|
||||
function compare(input, output) {
|
||||
var compiled = transform(input);
|
||||
const compiled = transform(input);
|
||||
expect(compiled).toEqual(output);
|
||||
}
|
||||
|
||||
var oldEnv;
|
||||
let oldEnv;
|
||||
|
||||
describe('error codes transform', () => {
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -35,8 +35,8 @@ module.exports = function(opts) {
|
||||
);
|
||||
}
|
||||
|
||||
var errorMapFilePath = opts.errorMapFilePath;
|
||||
var existingErrorMap;
|
||||
const errorMapFilePath = opts.errorMapFilePath;
|
||||
let existingErrorMap;
|
||||
try {
|
||||
// Using `fs.readFileSync` instead of `require` here, because `require()`
|
||||
// calls are cached, and the cache map is not properly invalidated after
|
||||
@@ -51,8 +51,8 @@ module.exports = function(opts) {
|
||||
existingErrorMap = {};
|
||||
}
|
||||
|
||||
var allErrorIDs = Object.keys(existingErrorMap);
|
||||
var currentID;
|
||||
const allErrorIDs = Object.keys(existingErrorMap);
|
||||
let currentID;
|
||||
|
||||
if (allErrorIDs.length === 0) {
|
||||
// Map is empty
|
||||
@@ -65,17 +65,17 @@ module.exports = function(opts) {
|
||||
existingErrorMap = invertObject(existingErrorMap);
|
||||
|
||||
function transform(source) {
|
||||
var ast = babylon.parse(source, babylonOptions);
|
||||
const ast = babylon.parse(source, babylonOptions);
|
||||
|
||||
traverse(ast, {
|
||||
CallExpression: {
|
||||
exit: function(astPath) {
|
||||
exit(astPath) {
|
||||
if (astPath.get('callee').isIdentifier({name: 'invariant'})) {
|
||||
var node = astPath.node;
|
||||
const node = astPath.node;
|
||||
|
||||
// error messages can be concatenated (`+`) at runtime, so here's a
|
||||
// trivial partial evaluator that interprets the literal value
|
||||
var errorMsgLiteral = evalToString(node.arguments[1]);
|
||||
const errorMsgLiteral = evalToString(node.arguments[1]);
|
||||
if (existingErrorMap.hasOwnProperty(errorMsgLiteral)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -17,12 +17,11 @@
|
||||
* { 0: 'MUCH ERROR', 1: 'SUCH WRONG' }
|
||||
*/
|
||||
function invertObject(targetObj /* : ErrorMap */) /* : ErrorMap */ {
|
||||
var result = {};
|
||||
var mapKeys = Object.keys(targetObj);
|
||||
const result = {};
|
||||
const mapKeys = Object.keys(targetObj);
|
||||
|
||||
for (let i = 0; i < mapKeys.length; i++) {
|
||||
var originalKey = mapKeys[i];
|
||||
var originalVal = targetObj[originalKey];
|
||||
for (const originalKey of mapKeys) {
|
||||
const originalVal = targetObj[originalKey];
|
||||
|
||||
result[originalVal] = originalKey;
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ var evalToString = require('../shared/evalToString');
|
||||
var invertObject = require('./invertObject');
|
||||
|
||||
module.exports = function(babel) {
|
||||
var t = babel.types;
|
||||
const t = babel.types;
|
||||
|
||||
var SEEN_SYMBOL = Symbol('replace-invariant-error-codes.seen');
|
||||
const SEEN_SYMBOL = Symbol('replace-invariant-error-codes.seen');
|
||||
|
||||
// Generate a hygienic identifier
|
||||
function getProdInvariantIdentifier(path, file, localState) {
|
||||
@@ -27,17 +27,17 @@ module.exports = function(babel) {
|
||||
return localState.prodInvariantIdentifier;
|
||||
}
|
||||
|
||||
var DEV_EXPRESSION = t.identifier('__DEV__');
|
||||
const DEV_EXPRESSION = t.identifier('__DEV__');
|
||||
|
||||
return {
|
||||
pre: function() {
|
||||
pre() {
|
||||
this.prodInvariantIdentifier = null;
|
||||
},
|
||||
|
||||
visitor: {
|
||||
CallExpression: {
|
||||
exit: function(path, file) {
|
||||
var node = path.node;
|
||||
exit(path, file) {
|
||||
const node = path.node;
|
||||
// Ignore if it's already been processed
|
||||
if (node[SEEN_SYMBOL]) {
|
||||
return;
|
||||
@@ -74,10 +74,10 @@ module.exports = function(babel) {
|
||||
// - `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.
|
||||
var condition = node.arguments[0];
|
||||
var errorMsgLiteral = evalToString(node.arguments[1]);
|
||||
const condition = node.arguments[0];
|
||||
const errorMsgLiteral = evalToString(node.arguments[1]);
|
||||
|
||||
var devInvariant = t.callExpression(
|
||||
const devInvariant = t.callExpression(
|
||||
node.callee,
|
||||
[
|
||||
t.booleanLiteral(false),
|
||||
@@ -88,14 +88,18 @@ module.exports = function(babel) {
|
||||
devInvariant[SEEN_SYMBOL] = true;
|
||||
|
||||
// Avoid caching because we write it as we go.
|
||||
var existingErrorMap = JSON.parse(
|
||||
const existingErrorMap = JSON.parse(
|
||||
fs.readFileSync(__dirname + '/codes.json', 'utf-8')
|
||||
);
|
||||
var errorMap = invertObject(existingErrorMap);
|
||||
const errorMap = invertObject(existingErrorMap);
|
||||
|
||||
var localInvariantId = getProdInvariantIdentifier(path, file, this);
|
||||
var prodErrorId = errorMap[errorMsgLiteral];
|
||||
var body = null;
|
||||
const localInvariantId = getProdInvariantIdentifier(
|
||||
path,
|
||||
file,
|
||||
this
|
||||
);
|
||||
const prodErrorId = errorMap[errorMsgLiteral];
|
||||
let body = null;
|
||||
|
||||
if (prodErrorId === undefined) {
|
||||
// The error wasn't found in the map.
|
||||
@@ -103,7 +107,7 @@ module.exports = function(babel) {
|
||||
// Keep the original invariant.
|
||||
body = t.expressionStatement(devInvariant);
|
||||
} else {
|
||||
var prodInvariant = t.callExpression(
|
||||
const prodInvariant = t.callExpression(
|
||||
localInvariantId,
|
||||
[t.stringLiteral(prodErrorId)].concat(node.arguments.slice(2))
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user