Replace use of var with const and let (#1161)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent 30368b8 · Browse files · 8 files·+71 added−70 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+71 added−70 removed
- tests:+3 added−3 removed
- other:+68 added−67 removed
Provenance
- Commit
3e5b05e- Tree
1987325- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
30368b8
8 files changed+71 added−70 removed
modified:examples/deploy+1 added-1 removed
12 unmodified lines13 .command('setup [env]')14 .description('run setup commands for all envs')15 .option('-s, --setup_mode [mode]', 'Which setup mode to use')16 .action(function(env, options) {17- var mode = options.setup_mode || 'normal';17+ const mode = options.setup_mode || 'normal';18 env = env || 'all';19 console.log('setup for %s env(s) with %s mode', env, mode);20 });21 16 unmodified linesmodified:examples/pizza+1 added-1 removed
16 unmodified lines17 if (program.peppers) console.log(' - peppers');18 if (program.pineapple) console.log(' - pineapple');19 if (program.bbq) console.log(' - bbq');20 21-var cheese = !program.cheese ? 'no' : program.cheese;21+const cheese = !program.cheese ? 'no' : program.cheese;22 23 console.log(' - %s cheese', cheese);24 console.log(program.args);modified:examples/pm-install+1 added-1 removed
5 unmodified lines6 program7 .option('-f, --force', 'force installation')8 .parse(process.argv);9 10-var pkgs = program.args;10+const pkgs = program.args;11 12 if (!pkgs.length) {13 console.error('packages required');14 process.exit(1);8 unmodified linesmodified:tests/fixtures/inspect.js+1 added-1 removed
12334567#!/usr/bin/env node
var program = require('../../');const program = require('../../');
program .command('sub', 'install one or more packages') .parse(process.argv);modified:tests/fixtures/pm-cache.js+1 added-1 removed
1123456var program = require('../../');const program = require('../../');
program .command('clear', 'clear the cache') .command('validate', 'validate the cache', { isDefault: true }) .parse(process.argv);modified:tests/command.executableSubcommand.signals.test.js+1 added-1 removed
18 unmodified lines1920212223232425262710 unmodified lines18 unmodified lines test(`when command killed with ${value} then executableSubcommand receieves ${value}`, (done) => { const pmPath = path.join(__dirname, './fixtures/pm');
// The child process writes to stdout. var proc = childProcess.spawn(pmPath, ['listen'], {}); const proc = childProcess.spawn(pmPath, ['listen'], {});
let processOutput = ''; proc.stdout.on('data', (data) => { if (processOutput.length === 0) {10 unmodified linesmodified:.eslintrc.json+1 added
1234567891011{ "extends": ["standard", "plugin:jest/recommended"], "plugins": ["jest"], "rules": { "one-var": "off", "no-var": "warn", "semi": ["error", "always"], "space-before-function-paren": ["error", "never"], "no-else-return": ["error", { "allowElseIf": false }] }}modified:index.js+64 added-64 removed
1 /**2 * Module dependencies.3 */4 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');9 10 class Option {11 /**12 * Initialize a new `Option` with the given `flags` and `description`.136 unmodified lines149 * @api public150 */151 152 command(nameAndArgs, actionOptsOrExecDesc, execOpts) {153- var desc = actionOptsOrExecDesc;154- var opts = execOpts;153+ let desc = actionOptsOrExecDesc;154+ let opts = execOpts;155 if (typeof desc === 'object' && desc !== null) {156 opts = desc;157 desc = null;158 }159 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());162 163 if (desc) {164 cmd.description(desc);165 cmd._executableHandler = true;107 unmodified lines273 */274 275 _parseExpectedArgs(args) {276 if (!args.length) return;277- var self = this;277+ const self = this;278 args.forEach(function(arg) {279- var argDetails = {279+ const argDetails = {280 required: false,281 name: '',282 variadic: false283 };77 unmodified lines361 * @api public362 */363 364 action(fn) {365- var self = this;366- var listener = function(args) {365+ const self = this;366+ const listener = function(args) {367 // 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);370 if (self._passCommandToAction) {371 actionArgs[expectedArgsCount] = self;372 } else {373 actionArgs[expectedArgsCount] = self.opts();27 unmodified lines401 * @api private402 */403 404 _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();409 option.mandatory = !!config.mandatory;410 411 // default as 3rd arg412 if (typeof fn !== 'function') {413 if (fn instanceof RegExp) {414 // This is a bit simplistic (especially no error messages), and probably better handled by caller using custom option processing.415 // No longer documented in README, but still present for backwards compatibility.416- var regex = fn;416+ const regex = fn;417 fn = function(val, def) {418- var m = regex.exec(val);418+ const m = regex.exec(val);419 return m ? m[0] : def;420 };421 } else {422 defaultValue = fn;443 unmodified lines866 */867 868 _checkForMissingMandatoryOptions() {869 // 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) {871 cmd.options.forEach((anOption) => {872 if (anOption.mandatory && (cmd._getOptionValue(anOption.attributeName()) === undefined)) {873 cmd.missingMandatoryOptionValue(anOption);874 }107 unmodified lines982 */983 opts() {984 if (this._storeOptionsAsProperties) {985 // Preserve original behaviour so backwards compatible when still using properties986- var result = {},987- len = this.options.length;986+ const result = {};987+ const len = this.options.length;988 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();991 result[key] = key === this._versionOptionName ? this._version : this[key];992 }993 return result;994 }105 unmodified lines1100 if (arguments.length === 0) return this._version;1101 this._version = str;1102 flags = flags || '-V, --version';1103 description = description || 'output the version number';1104- var versionOption = new Option(flags, description);1104+ const versionOption = new Option(flags, description);1105 this._versionOptionName = versionOption.long.substr(2) || 'version';1106 this.options.push(versionOption);1107- var self = this;1107+ const self = this;1108 this.on('option:' + this._versionOptionName, function() {1109 process.stdout.write(str + '\n');1110 self._exit(0, 'commander.version', str);1111 });24 unmodified lines1136 * @api public1137 */1138 1139 alias(alias) {1140- var command = this;1140+ let command = this;1141 if (this.commands.length !== 0) {1142 command = this.commands[this.commands.length - 1];1143 }1144 13 unmodified lines1158 * @api public1159 */1160 1161 usage(str) {1162- var args = this._args.map(function(arg) {1162+ const args = this._args.map(function(arg) {1163 return humanReadableArgName(arg);1164 });1165 1166- var usage = '[options]' +1166+ const usage = '[options]' +1167 (this.commands.length ? ' [command]' : '') +1168 (this._args.length ? ' ' + args.join(' ') : '');1169 1170 if (arguments.length === 0) return this._usage || usage;26 unmodified lines1197 prepareCommands() {1198 const commandDetails = this.commands.filter(function(cmd) {1199 return !cmd._noHelp;1200 }).map(function(cmd) {1201- var args = cmd._args.map(function(arg) {1201+ const args = cmd._args.map(function(arg) {1202 return humanReadableArgName(arg);1203 }).join(' ');1204 1205 return [18 unmodified lines1224 * @api private1225 */1226 1227 largestCommandLength() {1228- var commands = this.prepareCommands();1228+ const commands = this.prepareCommands();1229 return commands.reduce(function(max, command) {1230 return Math.max(max, command[0].length);1231 }, 0);1232 };5 unmodified lines1238 * @api private1239 */1240 1241 largestOptionLength() {1242- var options = [].slice.call(this.options);1242+ const options = [].slice.call(this.options);1243 options.push({1244 flags: this._helpFlags1245 });1246 22 unmodified lines1269 * @api private1270 */1271 1272 padWidth() {1273- var width = this.largestOptionLength();1273+ let width = this.largestOptionLength();1274 if (this._argsDescription && this._args.length) {1275 if (this.largestArgLength() > width) {1276 width = this.largestArgLength();1277 }15 unmodified lines1293 * @api private1294 */1295 1296 optionHelp() {1297- var width = this.padWidth();1297+ const width = this.padWidth();1298 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;1301 1302 // Append the help information1303 return this.options.map(function(option) {1304 const fullDesc = option.description +12 unmodified lines1317 1318 commandHelp() {1319 if (!this.commands.length && !this._lazyHasImplicitHelpCommand()) return '';1320 1321- var commands = this.prepareCommands();1322- var width = this.padWidth();1321+ const commands = this.prepareCommands();1322+ const width = this.padWidth();1323 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;1326 1327 return [1328 'Commands:',1329 commands.map(function(cmd) {1330- var desc = cmd[1] ? ' ' + cmd[1] : '';1330+ const desc = cmd[1] ? ' ' + cmd[1] : '';1331 return (desc ? pad(cmd[0], width) : cmd[0]) + optionalWrap(desc, descriptionWidth, width + 2);1332 }).join('\n').replace(/^/gm, ' '),1333 ''1334 ].join('\n');6 unmodified lines1341 * @api private1342 */1343 1344 helpInformation() {1345- var desc = [];1345+ let desc = [];1346 if (this._description) {1347 desc = [1348 this._description,1349 ''1350 ];1351 1352- var argsDescription = this._argsDescription;1352+ const argsDescription = this._argsDescription;1353 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;1357 desc.push('Arguments:');1358 desc.push('');1359 this._args.forEach(function(arg) {1360 desc.push(' ' + pad(arg.name, width) + ' ' + wrap(argsDescription[arg.name], descriptionWidth, width + 4));1 unmodified line1362 desc.push('');1363 }1364 }1365 1366- var cmdName = this._name;1366+ let cmdName = this._name;1367 if (this._alias) {1368 cmdName = cmdName + '|' + this._alias;1369 }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) {1372 parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;1373 }1374- var usage = [1374+ const usage = [1375 'Usage: ' + parentCmdNames + cmdName + ' ' + this.usage(),1376 ''1377 ];1378 1379- var cmds = [];1380- var commandHelp = this.commandHelp();1379+ let cmds = [];1380+ const commandHelp = this.commandHelp();1381 if (commandHelp) cmds = [commandHelp];1382 1383- var options = [1383+ const options = [1384 'Options:',1385 '' + this.optionHelp().replace(/^/gm, ' '),1386 ''1387 ];41 unmodified lines1429 helpOption(flags, description) {1430 this._helpFlags = flags || this._helpFlags;1431 this._helpDescription = description || this._helpDescription;1432 1433- var splitFlags = this._helpFlags.split(/[ ,|]+/);1433+ const splitFlags = this._helpFlags.split(/[ ,|]+/);1434 1435 if (splitFlags.length > 1) this._helpShortFlag = splitFlags.shift();1436 1437 this._helpLongFlag = splitFlags.shift();65 unmodified lines1503 * @api private1504 */1505 1506 function pad(str, width) {1507- var len = Math.max(0, width - str.length);1507+ const len = Math.max(0, width - str.length);1508 return str + Array(len + 1).join(' ');1509 }1510 1511 /**6 unmodified lines1518 * @return {String}1519 * @api private1520 */1521 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) || [];1524 return lines.map(function(line, i) {1525 if (line.slice(-1) === '\n') {1526 line = line.slice(0, line.length - 1);1527 }48 unmodified lines1576 * @api private1577 */1578 1579 function humanReadableArgName(arg) {1580- var nameOutput = arg.name + (arg.variadic === true ? '...' : '');1580+ const nameOutput = arg.name + (arg.variadic === true ? '...' : '');1581 1582 return arg.required1583 ? '<' + nameOutput + '>'1584 : '[' + nameOutput + ']';12 unmodified lines1597 // --inspect[=[host:]port]1598 // --inspect-brk[=[host:]port]1599 // --inspect-port=[host:]port1600 return args.map((arg) => {1601- var result = arg;1601+ let result = arg;1602 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;1607 if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {1608 // e.g. --inspect1609 debugOption = match[1];1610 } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {22 unmodified lines