Skip to content

Commit 3e5b05e

Browse files
authored
Replace use of var with const and let (#1161)
1 parent 30368b8 commit 3e5b05e

File tree

8 files changed

+71
-70
lines changed

8 files changed

+71
-70
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"plugins": ["jest"],
44
"rules": {
55
"one-var": "off",
6+
"no-var": "warn",
67
"semi": ["error", "always"],
78
"space-before-function-paren": ["error", "never"],
89
"no-else-return": ["error", { "allowElseIf": false }]

examples/deploy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ program
1414
.description('run setup commands for all envs')
1515
.option('-s, --setup_mode [mode]', 'Which setup mode to use')
1616
.action(function(env, options) {
17-
var mode = options.setup_mode || 'normal';
17+
const mode = options.setup_mode || 'normal';
1818
env = env || 'all';
1919
console.log('setup for %s env(s) with %s mode', env, mode);
2020
});

examples/pizza

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if (program.peppers) console.log(' - peppers');
1818
if (program.pineapple) console.log(' - pineapple');
1919
if (program.bbq) console.log(' - bbq');
2020

21-
var cheese = !program.cheese ? 'no' : program.cheese;
21+
const cheese = !program.cheese ? 'no' : program.cheese;
2222

2323
console.log(' - %s cheese', cheese);
2424
console.log(program.args);

examples/pm-install

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ program
77
.option('-f, --force', 'force installation')
88
.parse(process.argv);
99

10-
var pkgs = program.args;
10+
const pkgs = program.args;
1111

1212
if (!pkgs.length) {
1313
console.error('packages required');

index.js

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* Module dependencies.
33
*/
44

5-
var EventEmitter = require('events').EventEmitter;
6-
var spawn = require('child_process').spawn;
7-
var path = require('path');
8-
var fs = require('fs');
5+
const EventEmitter = require('events').EventEmitter;
6+
const spawn = require('child_process').spawn;
7+
const path = require('path');
8+
const fs = require('fs');
99

1010
class Option {
1111
/**
@@ -150,15 +150,15 @@ class Command extends EventEmitter {
150150
*/
151151

152152
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
153-
var desc = actionOptsOrExecDesc;
154-
var opts = execOpts;
153+
let desc = actionOptsOrExecDesc;
154+
let opts = execOpts;
155155
if (typeof desc === 'object' && desc !== null) {
156156
opts = desc;
157157
desc = null;
158158
}
159159
opts = opts || {};
160-
var args = nameAndArgs.split(/ +/);
161-
var cmd = new Command(args.shift());
160+
const args = nameAndArgs.split(/ +/);
161+
const cmd = new Command(args.shift());
162162

163163
if (desc) {
164164
cmd.description(desc);
@@ -274,9 +274,9 @@ class Command extends EventEmitter {
274274

275275
_parseExpectedArgs(args) {
276276
if (!args.length) return;
277-
var self = this;
277+
const self = this;
278278
args.forEach(function(arg) {
279-
var argDetails = {
279+
const argDetails = {
280280
required: false,
281281
name: '',
282282
variadic: false
@@ -362,11 +362,11 @@ class Command extends EventEmitter {
362362
*/
363363

364364
action(fn) {
365-
var self = this;
366-
var listener = function(args) {
365+
const self = this;
366+
const listener = function(args) {
367367
// The .action callback takes an extra parameter which is the command or options.
368-
var expectedArgsCount = self._args.length;
369-
var actionArgs = args.slice(0, expectedArgsCount);
368+
const expectedArgsCount = self._args.length;
369+
const actionArgs = args.slice(0, expectedArgsCount);
370370
if (self._passCommandToAction) {
371371
actionArgs[expectedArgsCount] = self;
372372
} else {
@@ -402,20 +402,20 @@ class Command extends EventEmitter {
402402
*/
403403

404404
_optionEx(config, flags, description, fn, defaultValue) {
405-
var self = this,
406-
option = new Option(flags, description),
407-
oname = option.name(),
408-
name = option.attributeName();
405+
const self = this;
406+
const option = new Option(flags, description);
407+
const oname = option.name();
408+
const name = option.attributeName();
409409
option.mandatory = !!config.mandatory;
410410

411411
// default as 3rd arg
412412
if (typeof fn !== 'function') {
413413
if (fn instanceof RegExp) {
414414
// This is a bit simplistic (especially no error messages), and probably better handled by caller using custom option processing.
415415
// No longer documented in README, but still present for backwards compatibility.
416-
var regex = fn;
416+
const regex = fn;
417417
fn = function(val, def) {
418-
var m = regex.exec(val);
418+
const m = regex.exec(val);
419419
return m ? m[0] : def;
420420
};
421421
} else {
@@ -867,7 +867,7 @@ class Command extends EventEmitter {
867867

868868
_checkForMissingMandatoryOptions() {
869869
// Walk up hierarchy so can call in subcommand after checking for displaying help.
870-
for (var cmd = this; cmd; cmd = cmd.parent) {
870+
for (let cmd = this; cmd; cmd = cmd.parent) {
871871
cmd.options.forEach((anOption) => {
872872
if (anOption.mandatory && (cmd._getOptionValue(anOption.attributeName()) === undefined)) {
873873
cmd.missingMandatoryOptionValue(anOption);
@@ -983,11 +983,11 @@ class Command extends EventEmitter {
983983
opts() {
984984
if (this._storeOptionsAsProperties) {
985985
// Preserve original behaviour so backwards compatible when still using properties
986-
var result = {},
987-
len = this.options.length;
986+
const result = {};
987+
const len = this.options.length;
988988

989-
for (var i = 0; i < len; i++) {
990-
var key = this.options[i].attributeName();
989+
for (let i = 0; i < len; i++) {
990+
const key = this.options[i].attributeName();
991991
result[key] = key === this._versionOptionName ? this._version : this[key];
992992
}
993993
return result;
@@ -1101,10 +1101,10 @@ class Command extends EventEmitter {
11011101
this._version = str;
11021102
flags = flags || '-V, --version';
11031103
description = description || 'output the version number';
1104-
var versionOption = new Option(flags, description);
1104+
const versionOption = new Option(flags, description);
11051105
this._versionOptionName = versionOption.long.substr(2) || 'version';
11061106
this.options.push(versionOption);
1107-
var self = this;
1107+
const self = this;
11081108
this.on('option:' + this._versionOptionName, function() {
11091109
process.stdout.write(str + '\n');
11101110
self._exit(0, 'commander.version', str);
@@ -1137,7 +1137,7 @@ class Command extends EventEmitter {
11371137
*/
11381138

11391139
alias(alias) {
1140-
var command = this;
1140+
let command = this;
11411141
if (this.commands.length !== 0) {
11421142
command = this.commands[this.commands.length - 1];
11431143
}
@@ -1159,11 +1159,11 @@ class Command extends EventEmitter {
11591159
*/
11601160

11611161
usage(str) {
1162-
var args = this._args.map(function(arg) {
1162+
const args = this._args.map(function(arg) {
11631163
return humanReadableArgName(arg);
11641164
});
11651165

1166-
var usage = '[options]' +
1166+
const usage = '[options]' +
11671167
(this.commands.length ? ' [command]' : '') +
11681168
(this._args.length ? ' ' + args.join(' ') : '');
11691169

@@ -1198,7 +1198,7 @@ class Command extends EventEmitter {
11981198
const commandDetails = this.commands.filter(function(cmd) {
11991199
return !cmd._noHelp;
12001200
}).map(function(cmd) {
1201-
var args = cmd._args.map(function(arg) {
1201+
const args = cmd._args.map(function(arg) {
12021202
return humanReadableArgName(arg);
12031203
}).join(' ');
12041204

@@ -1225,7 +1225,7 @@ class Command extends EventEmitter {
12251225
*/
12261226

12271227
largestCommandLength() {
1228-
var commands = this.prepareCommands();
1228+
const commands = this.prepareCommands();
12291229
return commands.reduce(function(max, command) {
12301230
return Math.max(max, command[0].length);
12311231
}, 0);
@@ -1239,7 +1239,7 @@ class Command extends EventEmitter {
12391239
*/
12401240

12411241
largestOptionLength() {
1242-
var options = [].slice.call(this.options);
1242+
const options = [].slice.call(this.options);
12431243
options.push({
12441244
flags: this._helpFlags
12451245
});
@@ -1270,7 +1270,7 @@ class Command extends EventEmitter {
12701270
*/
12711271

12721272
padWidth() {
1273-
var width = this.largestOptionLength();
1273+
let width = this.largestOptionLength();
12741274
if (this._argsDescription && this._args.length) {
12751275
if (this.largestArgLength() > width) {
12761276
width = this.largestArgLength();
@@ -1294,10 +1294,10 @@ class Command extends EventEmitter {
12941294
*/
12951295

12961296
optionHelp() {
1297-
var width = this.padWidth();
1297+
const width = this.padWidth();
12981298

1299-
var columns = process.stdout.columns || 80;
1300-
var descriptionWidth = columns - width - 4;
1299+
const columns = process.stdout.columns || 80;
1300+
const descriptionWidth = columns - width - 4;
13011301

13021302
// Append the help information
13031303
return this.options.map(function(option) {
@@ -1318,16 +1318,16 @@ class Command extends EventEmitter {
13181318
commandHelp() {
13191319
if (!this.commands.length && !this._lazyHasImplicitHelpCommand()) return '';
13201320

1321-
var commands = this.prepareCommands();
1322-
var width = this.padWidth();
1321+
const commands = this.prepareCommands();
1322+
const width = this.padWidth();
13231323

1324-
var columns = process.stdout.columns || 80;
1325-
var descriptionWidth = columns - width - 4;
1324+
const columns = process.stdout.columns || 80;
1325+
const descriptionWidth = columns - width - 4;
13261326

13271327
return [
13281328
'Commands:',
13291329
commands.map(function(cmd) {
1330-
var desc = cmd[1] ? ' ' + cmd[1] : '';
1330+
const desc = cmd[1] ? ' ' + cmd[1] : '';
13311331
return (desc ? pad(cmd[0], width) : cmd[0]) + optionalWrap(desc, descriptionWidth, width + 2);
13321332
}).join('\n').replace(/^/gm, ' '),
13331333
''
@@ -1342,18 +1342,18 @@ class Command extends EventEmitter {
13421342
*/
13431343

13441344
helpInformation() {
1345-
var desc = [];
1345+
let desc = [];
13461346
if (this._description) {
13471347
desc = [
13481348
this._description,
13491349
''
13501350
];
13511351

1352-
var argsDescription = this._argsDescription;
1352+
const argsDescription = this._argsDescription;
13531353
if (argsDescription && this._args.length) {
1354-
var width = this.padWidth();
1355-
var columns = process.stdout.columns || 80;
1356-
var descriptionWidth = columns - width - 5;
1354+
const width = this.padWidth();
1355+
const columns = process.stdout.columns || 80;
1356+
const descriptionWidth = columns - width - 5;
13571357
desc.push('Arguments:');
13581358
desc.push('');
13591359
this._args.forEach(function(arg) {
@@ -1363,24 +1363,24 @@ class Command extends EventEmitter {
13631363
}
13641364
}
13651365

1366-
var cmdName = this._name;
1366+
let cmdName = this._name;
13671367
if (this._alias) {
13681368
cmdName = cmdName + '|' + this._alias;
13691369
}
1370-
var parentCmdNames = '';
1371-
for (var parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
1370+
let parentCmdNames = '';
1371+
for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
13721372
parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
13731373
}
1374-
var usage = [
1374+
const usage = [
13751375
'Usage: ' + parentCmdNames + cmdName + ' ' + this.usage(),
13761376
''
13771377
];
13781378

1379-
var cmds = [];
1380-
var commandHelp = this.commandHelp();
1379+
let cmds = [];
1380+
const commandHelp = this.commandHelp();
13811381
if (commandHelp) cmds = [commandHelp];
13821382

1383-
var options = [
1383+
const options = [
13841384
'Options:',
13851385
'' + this.optionHelp().replace(/^/gm, ' '),
13861386
''
@@ -1430,7 +1430,7 @@ class Command extends EventEmitter {
14301430
this._helpFlags = flags || this._helpFlags;
14311431
this._helpDescription = description || this._helpDescription;
14321432

1433-
var splitFlags = this._helpFlags.split(/[ ,|]+/);
1433+
const splitFlags = this._helpFlags.split(/[ ,|]+/);
14341434

14351435
if (splitFlags.length > 1) this._helpShortFlag = splitFlags.shift();
14361436

@@ -1504,7 +1504,7 @@ function camelcase(flag) {
15041504
*/
15051505

15061506
function pad(str, width) {
1507-
var len = Math.max(0, width - str.length);
1507+
const len = Math.max(0, width - str.length);
15081508
return str + Array(len + 1).join(' ');
15091509
}
15101510

@@ -1519,8 +1519,8 @@ function pad(str, width) {
15191519
* @api private
15201520
*/
15211521
function wrap(str, width, indent) {
1522-
var regex = new RegExp('.{1,' + (width - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');
1523-
var lines = str.match(regex) || [];
1522+
const regex = new RegExp('.{1,' + (width - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');
1523+
const lines = str.match(regex) || [];
15241524
return lines.map(function(line, i) {
15251525
if (line.slice(-1) === '\n') {
15261526
line = line.slice(0, line.length - 1);
@@ -1577,7 +1577,7 @@ function outputHelpIfRequested(cmd, args) {
15771577
*/
15781578

15791579
function humanReadableArgName(arg) {
1580-
var nameOutput = arg.name + (arg.variadic === true ? '...' : '');
1580+
const nameOutput = arg.name + (arg.variadic === true ? '...' : '');
15811581

15821582
return arg.required
15831583
? '<' + nameOutput + '>'
@@ -1598,12 +1598,12 @@ function incrementNodeInspectorPort(args) {
15981598
// --inspect-brk[=[host:]port]
15991599
// --inspect-port=[host:]port
16001600
return args.map((arg) => {
1601-
var result = arg;
1601+
let result = arg;
16021602
if (arg.indexOf('--inspect') === 0) {
1603-
var debugOption;
1604-
var debugHost = '127.0.0.1';
1605-
var debugPort = '9229';
1606-
var match;
1603+
let debugOption;
1604+
let debugHost = '127.0.0.1';
1605+
let debugPort = '9229';
1606+
let match;
16071607
if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {
16081608
// e.g. --inspect
16091609
debugOption = match[1];

tests/command.executableSubcommand.signals.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describeOrSkipOnWindows.each([['SIGINT'], ['SIGHUP'], ['SIGTERM'], ['SIGUSR1'],
2020
const pmPath = path.join(__dirname, './fixtures/pm');
2121

2222
// The child process writes to stdout.
23-
var proc = childProcess.spawn(pmPath, ['listen'], {});
23+
const proc = childProcess.spawn(pmPath, ['listen'], {});
2424

2525
let processOutput = '';
2626
proc.stdout.on('data', (data) => {

tests/fixtures/inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
var program = require('../../');
3+
const program = require('../../');
44

55
program
66
.command('sub', 'install one or more packages')

tests/fixtures/pm-cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var program = require('../../');
1+
const program = require('../../');
22

33
program
44
.command('clear', 'clear the cache')

0 commit comments

Comments
 (0)