@samitouri / QOS-React / commits / b097a34eba

refactor: scripts/error-codes (#11697)

Convert scripts/error-codes to use ES6 syntax

Gabriel Kalani committed Nov 28, 2017 at 23:13 UTC b097a34ebaced196b6c4a3bedd6f6bf0e8c0936d
5 files changed +35 -32
scripts/error-codes/__tests__/invertObject-test.js
+2 -2
@@ -6,9 +6,9 @@
6 */
7 'use strict';
8
9 -var invertObject = require('../invertObject');
9 +const invertObject = require('../invertObject');
10
11 -var objectValues = target => Object.keys(target).map(key => target[key]);
11 +const objectValues = target => Object.keys(target).map(key => target[key]);
12
13 describe('invertObject', () => {
14 it('should return an empty object for an empty input', () => {
scripts/error-codes/__tests__/replace-invariant-error-codes-test.js
+2 -2
@@ -17,11 +17,11 @@ function transform(input) {
17 }
18
19 function compare(input, output) {
20 - var compiled = transform(input);
20 + const compiled = transform(input);
21 expect(compiled).toEqual(output);
22 }
23
24 -var oldEnv;
24 +let oldEnv;
25
26 describe('error codes transform', () => {
27 beforeEach(() => {
scripts/error-codes/extract-errors.js
+8 -8
@@ -35,8 +35,8 @@ module.exports = function(opts) {
35 );
36 }
37
38 - var errorMapFilePath = opts.errorMapFilePath;
39 - var existingErrorMap;
38 + const errorMapFilePath = opts.errorMapFilePath;
39 + let existingErrorMap;
40 try {
41 // Using `fs.readFileSync` instead of `require` here, because `require()`
42 // calls are cached, and the cache map is not properly invalidated after
@@ -51,8 +51,8 @@ module.exports = function(opts) {
51 existingErrorMap = {};
52 }
53
54 - var allErrorIDs = Object.keys(existingErrorMap);
55 - var currentID;
54 + const allErrorIDs = Object.keys(existingErrorMap);
55 + let currentID;
56
57 if (allErrorIDs.length === 0) {
58 // Map is empty
@@ -65,17 +65,17 @@ module.exports = function(opts) {
65 existingErrorMap = invertObject(existingErrorMap);
66
67 function transform(source) {
68 - var ast = babylon.parse(source, babylonOptions);
68 + const ast = babylon.parse(source, babylonOptions);
69
70 traverse(ast, {
71 CallExpression: {
72 - exit: function(astPath) {
72 + exit(astPath) {
73 if (astPath.get('callee').isIdentifier({name: 'invariant'})) {
74 - var node = astPath.node;
74 + const node = astPath.node;
75
76 // error messages can be concatenated (`+`) at runtime, so here's a
77 // trivial partial evaluator that interprets the literal value
78 - var errorMsgLiteral = evalToString(node.arguments[1]);
78 + const errorMsgLiteral = evalToString(node.arguments[1]);
79 if (existingErrorMap.hasOwnProperty(errorMsgLiteral)) {
80 return;
81 }
scripts/error-codes/invertObject.js
+4 -5
@@ -17,12 +17,11 @@
17 * { 0: 'MUCH ERROR', 1: 'SUCH WRONG' }
18 */
19 function invertObject(targetObj /* : ErrorMap */) /* : ErrorMap */ {
20 - var result = {};
21 - var mapKeys = Object.keys(targetObj);
20 + const result = {};
21 + const mapKeys = Object.keys(targetObj);
22
23 - for (let i = 0; i < mapKeys.length; i++) {
24 - var originalKey = mapKeys[i];
25 - var originalVal = targetObj[originalKey];
23 + for (const originalKey of mapKeys) {
24 + const originalVal = targetObj[originalKey];
25
26 result[originalVal] = originalKey;
27 }
scripts/error-codes/replace-invariant-error-codes.js
+19 -15
@@ -11,9 +11,9 @@ var evalToString = require('../shared/evalToString');
11 var invertObject = require('./invertObject');
12
13 module.exports = function(babel) {
14 - var t = babel.types;
14 + const t = babel.types;
15
16 - var SEEN_SYMBOL = Symbol('replace-invariant-error-codes.seen');
16 + const SEEN_SYMBOL = Symbol('replace-invariant-error-codes.seen');
17
18 // Generate a hygienic identifier
19 function getProdInvariantIdentifier(path, file, localState) {
@@ -27,17 +27,17 @@ module.exports = function(babel) {
27 return localState.prodInvariantIdentifier;
28 }
29
30 - var DEV_EXPRESSION = t.identifier('__DEV__');
30 + const DEV_EXPRESSION = t.identifier('__DEV__');
31
32 return {
33 - pre: function() {
33 + pre() {
34 this.prodInvariantIdentifier = null;
35 },
36
37 visitor: {
38 CallExpression: {
39 - exit: function(path, file) {
40 - var node = path.node;
39 + exit(path, file) {
40 + const node = path.node;
41 // Ignore if it's already been processed
42 if (node[SEEN_SYMBOL]) {
43 return;
@@ -74,10 +74,10 @@ module.exports = function(babel) {
74 // - `reactProdInvariant` is always renamed to avoid shadowing
75 // The generated code is longer than the original code but will dead
76 // code removal in a minifier will strip that out.
77 - var condition = node.arguments[0];
78 - var errorMsgLiteral = evalToString(node.arguments[1]);
77 + const condition = node.arguments[0];
78 + const errorMsgLiteral = evalToString(node.arguments[1]);
79
80 - var devInvariant = t.callExpression(
80 + const devInvariant = t.callExpression(
81 node.callee,
82 [
83 t.booleanLiteral(false),
@@ -88,14 +88,18 @@ module.exports = function(babel) {
88 devInvariant[SEEN_SYMBOL] = true;
89
90 // Avoid caching because we write it as we go.
91 - var existingErrorMap = JSON.parse(
91 + const existingErrorMap = JSON.parse(
92 fs.readFileSync(__dirname + '/codes.json', 'utf-8')
93 );
94 - var errorMap = invertObject(existingErrorMap);
94 + const errorMap = invertObject(existingErrorMap);
95
96 - var localInvariantId = getProdInvariantIdentifier(path, file, this);
97 - var prodErrorId = errorMap[errorMsgLiteral];
98 - var body = null;
96 + const localInvariantId = getProdInvariantIdentifier(
97 + path,
98 + file,
99 + this
100 + );
101 + const prodErrorId = errorMap[errorMsgLiteral];
102 + let body = null;
103
104 if (prodErrorId === undefined) {
105 // The error wasn't found in the map.
@@ -103,7 +107,7 @@ module.exports = function(babel) {
107 // Keep the original invariant.
108 body = t.expressionStatement(devInvariant);
109 } else {
106 - var prodInvariant = t.callExpression(
110 + const prodInvariant = t.callExpression(
111 localInvariantId,
112 [t.stringLiteral(prodErrorId)].concat(node.arguments.slice(2))
113 );