Skip to content

Commit

Permalink
use rest parameters instead of arguments (GoogleChrome#1524)
Browse files Browse the repository at this point in the history
* use rest parameters instead of arguments

now possible by dropping support for node older than v6

* fix status event payload

also reduces rest/spread repetitions
  • Loading branch information
brendankenny authored and paulirish committed Jan 27, 2017
1 parent 1ca6a6b commit c4b73eb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 36 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ module.exports = {
"valid-jsdoc": 0,
"comma-dangle": 0,
"arrow-parens": 0,
// Compat: support for rest params is behind a flag for node v5.x
"prefer-rest-params": 0,
},
"parserOptions": {
"ecmaVersion": 6,
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-cli/test/global-mocha-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Object.keys(assert)
.filter(key => typeof assert[key] === 'function')
.forEach(key => {
const _origFn = assert[key];
assert[key] = function() {
assert[key] = function(...args) {
if (currTest) {
currTest._assertions++;
}
return _origFn.apply(this, arguments);
return _origFn.apply(this, args);
};
}
);
Expand Down
6 changes: 2 additions & 4 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,9 @@ function captureJSCallUsage(funcRef, set) {
const originalFunc = funcRef;
const originalPrepareStackTrace = __nativeError.prepareStackTrace;

return function() {
return function(...args) {
// Note: this function runs in the context of the page that is being audited.

const args = [...arguments]; // callee's arguments.

// See v8's Stack Trace API https://github.com/v8/v8/wiki/Stack-Trace-API#customizing-stack-traces
__nativeError.prepareStackTrace = function(error, structStackTrace) {
// First frame is the function we injected (the one that just threw).
Expand Down Expand Up @@ -850,7 +848,7 @@ function captureJSCallUsage(funcRef, set) {
__nativeError.prepareStackTrace = originalPrepareStackTrace;

// eslint-disable-next-line no-invalid-this
return originalFunc.apply(this, arguments);
return originalFunc.apply(this, args);
};
}

Expand Down
12 changes: 6 additions & 6 deletions lighthouse-core/lib/console-quieter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class ConsoleQuieter {
static mute(opts) {
ConsoleQuieter._logs = ConsoleQuieter._logs || [];

console.log = function() {
ConsoleQuieter._logs.push({type: 'log', args: arguments, prefix: opts.prefix});
console.log = function(...args) {
ConsoleQuieter._logs.push({type: 'log', args, prefix: opts.prefix});
};
console.warn = function() {
ConsoleQuieter._logs.push({type: 'warn', args: arguments, prefix: opts.prefix});
console.warn = function(...args) {
ConsoleQuieter._logs.push({type: 'warn', args, prefix: opts.prefix});
};
console.error = function() {
ConsoleQuieter._logs.push({type: 'error', args: arguments, prefix: opts.prefix});
console.error = function(...args) {
ConsoleQuieter._logs.push({type: 'error', args, prefix: opts.prefix});
};
}

Expand Down
36 changes: 19 additions & 17 deletions lighthouse-core/lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ class Emitter extends EventEmitter {
* Fires off all status updates. Listen with
* `require('lib/log').events.addListener('status', callback)`
* @param {string} title
* @param {!Array<*>} argsArray
*/
issueStatus(title, args) {
issueStatus(title, argsArray) {
if (title === 'status' || title === 'statusEnd') {
this.emit(title, args);
this.emit(title, [title, ...argsArray]);
}
}

/**
* Fires off all warnings. Listen with
* `require('lib/log').events.addListener('warning', callback)`
* @param {string} title
* @param {!Array<*>} argsArray
*/
issueWarning(args) {
this.emit('warning', args);
issueWarning(title, argsArray) {
this.emit('warning', [title, ...argsArray]);
}
}

Expand All @@ -62,9 +65,8 @@ const loggingBufferColumns = 25;
class Log {

static _logToStdErr(title, argsArray) {
const args = [...argsArray];
const log = Log.loggerfn(title);
log(...args);
log(...argsArray);
}

static loggerfn(title) {
Expand Down Expand Up @@ -113,23 +115,23 @@ class Log {
Log._logToStdErr(`${prefix}:${level || ''}`, [data.method, snippet]);
}

static log(title) {
Log.events.issueStatus(title, arguments);
return Log._logToStdErr(title, Array.from(arguments).slice(1));
static log(title, ...args) {
Log.events.issueStatus(title, args);
return Log._logToStdErr(title, args);
}

static warn(title) {
Log.events.issueWarning(arguments);
return Log._logToStdErr(`${title}:warn`, Array.from(arguments).slice(1));
static warn(title, ...args) {
Log.events.issueWarning(title, args);
return Log._logToStdErr(`${title}:warn`, args);
}

static error(title) {
return Log._logToStdErr(`${title}:error`, Array.from(arguments).slice(1));
static error(title, ...args) {
return Log._logToStdErr(`${title}:error`, args);
}

static verbose(title) {
Log.events.issueStatus(title);
return Log._logToStdErr(`${title}:verbose`, Array.from(arguments).slice(1));
static verbose(title, ...args) {
Log.events.issueStatus(title, args);
return Log._logToStdErr(`${title}:verbose`, args);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lighthouse-core/report/report-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ class ReportGenerator {
});

// arg1 && arg2 && ... && argn
Handlebars.registerHelper('and', function() {
Handlebars.registerHelper('and', function(...args) {
let arg = false;
for (let i = 0, n = arguments.length - 1; i < n; i++) {
arg = arguments[i];
for (let i = 0, n = args.length - 1; i < n; i++) {
arg = args[i];
if (!arg) {
break;
}
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-core/test/global-mocha-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ Object.keys(assert)
.filter(key => typeof assert[key] === 'function')
.forEach(key => {
const _origFn = assert[key];
assert[key] = function() {
assert[key] = function(...args) {
if (currTest) {
currTest._assertions++;
}
return _origFn.apply(this, arguments);
return _origFn.apply(this, args);
};
}
);
Expand Down

0 comments on commit c4b73eb

Please sign in to comment.