Skip to content

Commit

Permalink
src: replace var for (let|const) in utilities module
Browse files Browse the repository at this point in the history
Update Utilities module to replace var for let or const

PR-URL: #18814
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Matheus Marchini <matheus@sthima.com>
  • Loading branch information
jvelezpo authored and MylesBorins committed Mar 6, 2018
1 parent 1d6b136 commit d7f01bf
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 37 deletions.
10 changes: 5 additions & 5 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function deprecate(fn, msg, code) {
if (code !== undefined && typeof code !== 'string')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'string');

var warned = false;
let warned = false;
function deprecated(...args) {
if (!warned) {
warned = true;
Expand Down Expand Up @@ -90,7 +90,7 @@ function assertCrypto() {
// Return undefined if there is no match.
function normalizeEncoding(enc) {
if (!enc) return 'utf8';
var retried;
let retried;
while (true) {
switch (enc) {
case 'utf8':
Expand Down Expand Up @@ -139,7 +139,7 @@ function filterDuplicateStrings(items, low) {
}

function cachedResult(fn) {
var result;
let result;
return () => {
if (result === undefined)
result = fn();
Expand Down Expand Up @@ -194,7 +194,7 @@ function convertToValidSignal(signal) {

function getConstructorOf(obj) {
while (obj) {
var descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
const descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
if (descriptor !== undefined &&
typeof descriptor.value === 'function' &&
descriptor.value.name !== '') {
Expand Down Expand Up @@ -273,7 +273,7 @@ promisify.custom = kCustomPromisifiedSymbol;

// The build-in Array#join is slower in v8 6.0
function join(output, separator) {
var str = '';
let str = '';
if (output.length !== 0) {
for (var i = 0; i < output.length - 1; i++) {
// It is faster not to use a template string here
Expand Down
136 changes: 104 additions & 32 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ const regExpToString = RegExp.prototype.toString;
const dateToISOString = Date.prototype.toISOString;
const errorToString = Error.prototype.toString;

var CIRCULAR_ERROR_MESSAGE;
var Debug;
let CIRCULAR_ERROR_MESSAGE;
let Debug;

/* eslint-disable */
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
Expand Down Expand Up @@ -113,8 +113,8 @@ function strEscape(str) {
return `'${str}'`;
if (str.length > 100)
return `'${str.replace(strEscapeSequencesReplacer, escapeFn)}'`;
var result = '';
var last = 0;
let result = '';
let last = 0;
for (var i = 0; i < str.length; i++) {
const point = str.charCodeAt(i);
if (point === 39 || point === 92 || point < 32) {
Expand Down Expand Up @@ -153,10 +153,10 @@ function tryStringify(arg) {
}

function format(f) {
var i, tempStr;
let i, tempStr;
if (typeof f !== 'string') {
if (arguments.length === 0) return '';
var res = '';
let res = '';
for (i = 0; i < arguments.length - 1; i++) {
res += inspect(arguments[i]);
res += ' ';
Expand All @@ -167,9 +167,9 @@ function format(f) {

if (arguments.length === 1) return f;

var str = '';
var a = 1;
var lastPos = 0;
let str = '';
let a = 1;
let lastPos = 0;
for (i = 0; i < f.length - 1; i++) {
if (f.charCodeAt(i) === 37) { // '%'
const nextChar = f.charCodeAt(++i);
Expand Down Expand Up @@ -244,9 +244,9 @@ function debuglog(set) {
set = set.toUpperCase();
if (!debugs[set]) {
if (debugEnvRegex.test(set)) {
var pid = process.pid;
const pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
const msg = exports.format.apply(exports, arguments);
console.error('%s %d: %s', set, pid, msg);
};
} else {
Expand Down Expand Up @@ -421,8 +421,8 @@ function formatValue(ctx, value, recurseTimes, ln) {
}
}

var keys;
var symbols = Object.getOwnPropertySymbols(value);
let keys;
let symbols = Object.getOwnPropertySymbols(value);

// Look up the keys of the object.
if (ctx.showHidden) {
Expand All @@ -434,15 +434,27 @@ function formatValue(ctx, value, recurseTimes, ln) {
}

const keyLength = keys.length + symbols.length;
<<<<<<< HEAD
const constructor = getConstructorOf(value);
const ctorName = constructor && constructor.name ?
`${constructor.name} ` : '';

var base = '';
var formatter = formatObject;
var braces;
var noIterator = true;
var raw;
=======

const { constructor, tag } = getIdentificationOf(value);
let prefix = '';
if (constructor && tag && constructor !== tag)
prefix = `${constructor} [${tag}] `;
else if (constructor)
prefix = `${constructor} `;
else if (tag)
prefix = `[${tag}] `;
>>>>>>> ca79fc5373... src: replace var for (let|const) in utilities module

let base = '';
let formatter = formatObject;
let braces;
let noIterator = true;
let raw;

// Iterators and the rest are split to reduce checks
if (value[Symbol.iterator]) {
Expand Down Expand Up @@ -602,8 +614,45 @@ function formatNumber(fn, value) {
return fn(`${value}`, 'number');
}
<<<<<<< HEAD
function formatPrimitive(fn, value) {
if (typeof value === 'string')
=======
function formatPrimitive(fn, value, ctx) {
if (typeof value === 'string') {
if (ctx.compact === false &&
value.length > MIN_LINE_LENGTH &&
ctx.indentationLvl + value.length > ctx.breakLength) {
// eslint-disable-next-line max-len
const minLineLength = Math.max(ctx.breakLength - ctx.indentationLvl, MIN_LINE_LENGTH);
// eslint-disable-next-line max-len
const averageLineLength = Math.ceil(value.length / Math.ceil(value.length / minLineLength));
const divisor = Math.max(averageLineLength, MIN_LINE_LENGTH);
let res = '';
if (readableRegExps[divisor] === undefined) {
// Build a new RegExp that naturally breaks text into multiple lines.
//
// Rules
// 1. Greedy match all text up the max line length that ends with a
// whitespace or the end of the string.
// 2. If none matches, non-greedy match any text up to a whitespace or
// the end of the string.
//
// eslint-disable-next-line max-len, node-core/no-unescaped-regexp-dot
readableRegExps[divisor] = new RegExp(`(.|\\n){1,${divisor}}(\\s|$)|(\\n|.)+?(\\s|$)`, 'gm');
}
const indent = ' '.repeat(ctx.indentationLvl);
const matches = value.match(readableRegExps[divisor]);
if (matches.length > 1) {
res += `${fn(strEscape(matches[0]), 'string')} +\n`;
for (var i = 1; i < matches.length - 1; i++) {
res += `${indent} ${fn(strEscape(matches[i]), 'string')} +\n`;
}
res += `${indent} ${fn(strEscape(matches[i]), 'string')}`;
return res;
}
}
>>>>>>> ca79fc5373... src: replace var for (let|const) in utilities module
return fn(strEscape(value), 'string');
if (typeof value === 'number')
return formatNumber(fn, value);
Expand Down Expand Up @@ -631,8 +680,8 @@ function formatObject(ctx, value, recurseTimes, keys) {
function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) {
const output = [];
const keyLen = keys.length;
var visibleLength = 0;
var i = 0;
let visibleLength = 0;
let i = 0;
if (keyLen !== 0 && numberRegExp.test(keys[0])) {
for (const key of keys) {
if (visibleLength === maxLength)
Expand Down Expand Up @@ -681,7 +730,7 @@ function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) {
} else if (keys[keyLen - 1] !== `${valLen - 1}`) {
const extra = [];
// Only handle special keys
var key;
let key;
for (i = keys.length - 1; i >= 0; i--) {
key = keys[i];
if (numberRegExp.test(key) && +key < 2 ** 32 - 1)
Expand Down Expand Up @@ -745,7 +794,7 @@ function formatTypedArray(ctx, value, recurseTimes, keys) {
function formatSet(ctx, value, recurseTimes, keys) {
const output = new Array(value.size + keys.length + (ctx.showHidden ? 1 : 0));
var i = 0;
let i = 0;
for (const v of value)
output[i++] = formatValue(ctx, v, recurseTimes);
// With `showHidden`, `length` will display as a hidden property for
Expand All @@ -761,7 +810,7 @@ function formatSet(ctx, value, recurseTimes, keys) {
function formatMap(ctx, value, recurseTimes, keys) {
const output = new Array(value.size + keys.length + (ctx.showHidden ? 1 : 0));
var i = 0;
let i = 0;
for (const [k, v] of value)
output[i++] = `${formatValue(ctx, k, recurseTimes)} => ` +
formatValue(ctx, v, recurseTimes);
Expand All @@ -774,10 +823,17 @@ function formatMap(ctx, value, recurseTimes, keys) {
return output;
}
<<<<<<< HEAD
function formatCollectionIterator(ctx, value, recurseTimes, keys) {
ensureDebugIsInitialized();
const mirror = Debug.MakeMirror(value, true);
const vals = mirror.preview();
=======
function formatCollectionIterator(preview, ctx, value, recurseTimes,
visibleKeys, keys) {
const nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
const vals = preview(value, 100);
>>>>>>> ca79fc5373... src: replace var for (let|const) in utilities module
const output = [];
for (const o of vals) {
output.push(formatValue(ctx, o, recurseTimes));
Expand All @@ -786,7 +842,7 @@ function formatCollectionIterator(ctx, value, recurseTimes, keys) {
}
function formatPromise(ctx, value, recurseTimes, keys) {
var output;
let output;
const [state, result] = getPromiseDetails(value);
if (state === kPending) {
output = ['<pending>'];
Expand All @@ -801,7 +857,7 @@ function formatPromise(ctx, value, recurseTimes, keys) {
}
function formatProperty(ctx, value, recurseTimes, key, array) {
var name, str;
let name, str;
const desc = Object.getOwnPropertyDescriptor(value, key) ||
{ value: value[key], enumerable: true };
if (desc.value !== undefined) {
Expand Down Expand Up @@ -838,9 +894,25 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
function reduceToSingleString(ctx, output, base, braces, addLn) {
const breakLength = ctx.breakLength;
<<<<<<< HEAD
if (output.length * 2 <= breakLength) {
var length = 0;
for (var i = 0; i < output.length && length <= breakLength; i++) {
=======
let i = 0;
if (ctx.compact === false) {
const indentation = ' '.repeat(ctx.indentationLvl);
let res = `${base ? `${base} ` : ''}${braces[0]}\n${indentation} `;
for (; i < output.length - 1; i++) {
res += `${output[i]},\n${indentation} `;
}
res += `${output[i]}\n${indentation}${braces[1]}`;
return res;
}
if (output.length * 2 <= breakLength) {
let length = 0;
for (; i < output.length && length <= breakLength; i++) {
>>>>>>> ca79fc5373... src: replace var for (let|const) in utilities module
if (ctx.colors) {
length += removeColors(output[i]).length + 1;
} else {
Expand Down Expand Up @@ -911,10 +983,10 @@ const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
// 26 Feb 16:19:34
function timestamp() {
var d = new Date();
var time = [pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
const d = new Date();
const time = [pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
return [d.getDate(), months[d.getMonth()], time].join(' ');
}
Expand Down Expand Up @@ -958,8 +1030,8 @@ function _extend(target, source) {
// Don't do anything if source isn't an object
if (source === null || typeof source !== 'object') return target;
var keys = Object.keys(source);
var i = keys.length;
const keys = Object.keys(source);
let i = keys.length;
while (i--) {
target[keys[i]] = source[keys[i]];
}
Expand Down

0 comments on commit d7f01bf

Please sign in to comment.