diff --git a/scripts/error-codes/__tests__/invertObject-test.js b/scripts/error-codes/__tests__/invertObject-test.js index 6c060001ef6cd..0b6a9d904b042 100644 --- a/scripts/error-codes/__tests__/invertObject-test.js +++ b/scripts/error-codes/__tests__/invertObject-test.js @@ -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', () => { diff --git a/scripts/error-codes/__tests__/replace-invariant-error-codes-test.js b/scripts/error-codes/__tests__/replace-invariant-error-codes-test.js index 1681dec9faf71..2a7a4a310fcd1 100644 --- a/scripts/error-codes/__tests__/replace-invariant-error-codes-test.js +++ b/scripts/error-codes/__tests__/replace-invariant-error-codes-test.js @@ -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(() => { diff --git a/scripts/error-codes/extract-errors.js b/scripts/error-codes/extract-errors.js index fcf72e2285762..9e45a04260eeb 100644 --- a/scripts/error-codes/extract-errors.js +++ b/scripts/error-codes/extract-errors.js @@ -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; } diff --git a/scripts/error-codes/invertObject.js b/scripts/error-codes/invertObject.js index 92539b9e3704e..2efe7ff6eb6bd 100644 --- a/scripts/error-codes/invertObject.js +++ b/scripts/error-codes/invertObject.js @@ -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; } diff --git a/scripts/error-codes/replace-invariant-error-codes.js b/scripts/error-codes/replace-invariant-error-codes.js index f0edbc447ca60..faf61e35e5ed6 100644 --- a/scripts/error-codes/replace-invariant-error-codes.js +++ b/scripts/error-codes/replace-invariant-error-codes.js @@ -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)) );