Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert scripts/error-codes to use ES6 syntax #11697

Merged
merged 1 commit into from Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/error-codes/__tests__/invertObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
16 changes: 8 additions & 8 deletions scripts/error-codes/extract-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions scripts/error-codes/invertObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
34 changes: 19 additions & 15 deletions scripts/error-codes/replace-invariant-error-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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),
Expand All @@ -88,22 +88,26 @@ 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.
// This is only expected to occur on master since we extract codes before releases.
// 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))
);
Expand Down