From 89d4f26846694344de5573836ba1cea028fda0a3 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 11 Oct 2016 06:51:46 -0700 Subject: [PATCH] Cleanup console and ErrorUtils Reviewed By: davidaurelio Differential Revision: D3981005 fbshipit-source-id: 3c95e62177f78785c7f971dd632126dbfb83746b --- .../src/Resolver/polyfills/console.js | 24 ++++------ .../src/Resolver/polyfills/error-guard.js | 45 +++++++++---------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/react-packager/src/Resolver/polyfills/console.js b/react-packager/src/Resolver/polyfills/console.js index 33b853b2..ed603997 100644 --- a/react-packager/src/Resolver/polyfills/console.js +++ b/react-packager/src/Resolver/polyfills/console.js @@ -16,7 +16,7 @@ /* eslint-disable */ -var inspect = (function() { +const inspect = (function() { // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -356,8 +356,8 @@ var inspect = (function() { })(); -var OBJECT_COLUMN_NAME = '(index)'; -var LOG_LEVELS = { +const OBJECT_COLUMN_NAME = '(index)'; +const LOG_LEVELS = { trace: 0, info: 1, warn: 2, @@ -371,7 +371,7 @@ function setupConsole(global) { function getNativeLogFunction(level) { return function() { - var str; + let str; if (arguments.length === 1 && typeof arguments[0] === 'string') { str = arguments[0]; } else { @@ -380,7 +380,7 @@ function setupConsole(global) { }).join(', '); } - var logLevel = level; + let logLevel = level; if (str.slice(0, 9) === 'Warning: ' && logLevel >= LOG_LEVELS.error) { // React warnings use console.error so that a stack trace is shown, // but we don't (currently) want these to show a redbox @@ -391,7 +391,7 @@ function setupConsole(global) { }; } - var repeat = function(element, n) { + function repeat(element, n) { return Array.apply(null, Array(n)).map(function() { return element; }); }; @@ -431,7 +431,7 @@ function setupConsole(global) { // Join all elements in the row into a single string with | separators // (appends extra spaces to each cell to make separators | alligned) - var joinRow = function(row, space) { + function joinRow(row, space) { var cells = row.map(function(cell, i) { var extraSpaces = repeat(' ', columnWidths[i] - cell.length).join(''); return cell + extraSpaces; @@ -465,7 +465,7 @@ function setupConsole(global) { Object.defineProperty(global, 'originalConsole', descriptor); } - var console = { + global.console = { error: getNativeLogFunction(LOG_LEVELS.error), info: getNativeLogFunction(LOG_LEVELS.info), log: getNativeLogFunction(LOG_LEVELS.info), @@ -475,14 +475,6 @@ function setupConsole(global) { table: consoleTablePolyfill }; - // don't reassign to the original descriptor. breaks on ios7 - Object.defineProperty(global, 'console', { - value: console, - configurable: descriptor ? descriptor.configurable : true, - enumerable: descriptor ? descriptor.enumerable : true, - writable: descriptor ? descriptor.writable : true, - }); - // If available, also call the original `console` method since that is // sometimes useful. Ex: on OS X, this will let you see rich output in // the Safari Web Inspector console. diff --git a/react-packager/src/Resolver/polyfills/error-guard.js b/react-packager/src/Resolver/polyfills/error-guard.js index eaeea1f3..8b9697b0 100644 --- a/react-packager/src/Resolver/polyfills/error-guard.js +++ b/react-packager/src/Resolver/polyfills/error-guard.js @@ -13,30 +13,40 @@ * before any of the modules, this ErrorUtils must be defined (and the handler * set) globally before requiring anything. */ + /* eslint strict:0 */ -var ErrorUtils = { - _inGuard: 0, - _globalHandler: null, +const _inGuard = 0; + +/** + * This is the error handler that is called when we encounter an exception + * when loading a module. This will report any errors encountered before + * ExceptionsManager is configured. + */ +const _globalHandler = function onError(e) { + throw e; +}; + +const ErrorUtils = { setGlobalHandler: function(fun) { - ErrorUtils._globalHandler = fun; + _globalHandler = fun; }, getGlobalHandler: function() { - return ErrorUtils._globalHandler; + return _globalHandler; }, reportError: function(error) { - ErrorUtils._globalHandler && ErrorUtils._globalHandler(error); + _globalHandler && _globalHandler(error); }, reportFatalError: function(error) { - ErrorUtils._globalHandler && ErrorUtils._globalHandler(error, true); + _globalHandler && _globalHandler(error, true); }, applyWithGuard: function(fun, context, args) { try { - ErrorUtils._inGuard++; + _inGuard++; return fun.apply(context, args); } catch (e) { ErrorUtils.reportError(e); } finally { - ErrorUtils._inGuard--; + _inGuard--; } }, applyWithGuardIfNeeded: function(fun, context, args) { @@ -47,7 +57,7 @@ var ErrorUtils = { } }, inGuard: function() { - return ErrorUtils._inGuard; + return _inGuard; }, guard: function(fun, name, context) { if (typeof fun !== 'function') { @@ -70,18 +80,5 @@ var ErrorUtils = { return guarded; } }; + global.ErrorUtils = ErrorUtils; - -/** - * This is the error handler that is called when we encounter an exception - * when loading a module. This will report any errors encountered before - * ExceptionsManager is configured. - */ -function setupErrorGuard() { - var onError = function(e) { - throw e; - }; - global.ErrorUtils.setGlobalHandler(onError); -} - -setupErrorGuard();