Upgrade Ava to v4

This commit is contained in:
Henry Mercer 2022-02-01 18:01:11 +00:00
parent 9a40cc5274
commit ce89f1b611
1153 changed files with 27264 additions and 95308 deletions

View file

@ -1,8 +1,8 @@
'use strict';
var util = require('util');
var fs = require('fs');
var path = require('path');
var fs = require('fs');
function camelCase(str) {
const isCamelCase = str !== str.toLowerCase() && str !== str.toUpperCase();
@ -125,6 +125,7 @@ class YargsParser {
key: undefined
}, options);
const args = tokenizeArgString(argsInput);
const inputIsString = typeof argsInput === 'string';
const aliases = combineAliases(Object.assign(Object.create(null), opts.alias));
const configuration = Object.assign({
'boolean-negation': true,
@ -272,7 +273,7 @@ class YargsParser {
i = eatNargs(i, m[1], args, m[2]);
}
else {
setArg(m[1], m[2]);
setArg(m[1], m[2], true);
}
}
}
@ -509,7 +510,7 @@ class YargsParser {
}
else {
if (!isUndefined(argAfterEqualSign)) {
argsToSet.push(processValue(key, argAfterEqualSign));
argsToSet.push(processValue(key, argAfterEqualSign, true));
}
for (let ii = i + 1; ii < args.length; ii++) {
if ((!configuration['greedy-arrays'] && argsToSet.length > 0) ||
@ -519,7 +520,7 @@ class YargsParser {
if (/^-/.test(next) && !negative.test(next) && !isUnknownOptionAsArg(next))
break;
i = ii;
argsToSet.push(processValue(key, next));
argsToSet.push(processValue(key, next, inputIsString));
}
}
if (typeof nargsCount === 'number' && ((nargsCount && argsToSet.length < nargsCount) ||
@ -529,14 +530,14 @@ class YargsParser {
setArg(key, argsToSet);
return i;
}
function setArg(key, val) {
function setArg(key, val, shouldStripQuotes = inputIsString) {
if (/-/.test(key) && configuration['camel-case-expansion']) {
const alias = key.split('.').map(function (prop) {
return camelCase(prop);
}).join('.');
addNewAlias(key, alias);
}
const value = processValue(key, val);
const value = processValue(key, val, shouldStripQuotes);
const splitKey = key.split('.');
setKey(argv, splitKey, value);
if (flags.aliases[key]) {
@ -580,11 +581,9 @@ class YargsParser {
addNewAlias(alias, key);
}
}
function processValue(key, val) {
if (typeof val === 'string' &&
(val[0] === "'" || val[0] === '"') &&
val[val.length - 1] === val[0]) {
val = val.substring(1, val.length - 1);
function processValue(key, val, shouldStripQuotes) {
if (shouldStripQuotes) {
val = stripQuotes(val);
}
if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
if (typeof val === 'string')
@ -997,10 +996,17 @@ function sanitizeKey(key) {
return '___proto___';
return key;
}
function stripQuotes(val) {
return (typeof val === 'string' &&
(val[0] === "'" || val[0] === '"') &&
val[val.length - 1] === val[0])
? val.substring(1, val.length - 1)
: val;
}
const minNodeVersion = (process && process.env && process.env.YARGS_MIN_NODE_VERSION)
? Number(process.env.YARGS_MIN_NODE_VERSION)
: 10;
: 12;
if (process && process.version) {
const major = Number(process.version.match(/v([^.]+)/)[1]);
if (major < minNodeVersion) {
@ -1021,7 +1027,7 @@ const parser = new YargsParser({
return require(path);
}
else if (path.match(/\.json$/)) {
return fs.readFileSync(path, 'utf8');
return JSON.parse(fs.readFileSync(path, 'utf8'));
}
else {
throw Error('only .json config files are supported in ESM');

View file

@ -7,15 +7,15 @@
* SPDX-License-Identifier: ISC
*/
import { format } from 'util';
import { readFileSync } from 'fs';
import { normalize, resolve } from 'path';
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js';
import { YargsParser } from './yargs-parser.js';
import { readFileSync } from 'fs';
// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our
// version support policy. The YARGS_MIN_NODE_VERSION is used for testing only.
const minNodeVersion = (process && process.env && process.env.YARGS_MIN_NODE_VERSION)
? Number(process.env.YARGS_MIN_NODE_VERSION)
: 10;
: 12;
if (process && process.version) {
const major = Number(process.version.match(/v([^.]+)/)[1]);
if (major < minNodeVersion) {
@ -39,7 +39,8 @@ const parser = new YargsParser({
return require(path);
}
else if (path.match(/\.json$/)) {
return readFileSync(path, 'utf8');
// Addresses: https://github.com/yargs/yargs/issues/2040
return JSON.parse(readFileSync(path, 'utf8'));
}
else {
throw Error('only .json config files are supported in ESM');

View file

@ -33,6 +33,9 @@ export class YargsParser {
// allow a string argument to be passed in rather
// than an argv array.
const args = tokenizeArgString(argsInput);
// tokenizeArgString adds extra quotes to args if argsInput is a string
// only strip those extra quotes in processValue if argsInput is a string
const inputIsString = typeof argsInput === 'string';
// aliases might have transitive relationships, normalize this.
const aliases = combineAliases(Object.assign(Object.create(null), opts.alias));
const configuration = Object.assign({
@ -200,7 +203,7 @@ export class YargsParser {
i = eatNargs(i, m[1], args, m[2]);
}
else {
setArg(m[1], m[2]);
setArg(m[1], m[2], true);
}
}
}
@ -474,7 +477,7 @@ export class YargsParser {
else {
// value in --option=value is eaten as is
if (!isUndefined(argAfterEqualSign)) {
argsToSet.push(processValue(key, argAfterEqualSign));
argsToSet.push(processValue(key, argAfterEqualSign, true));
}
for (let ii = i + 1; ii < args.length; ii++) {
if ((!configuration['greedy-arrays'] && argsToSet.length > 0) ||
@ -484,7 +487,7 @@ export class YargsParser {
if (/^-/.test(next) && !negative.test(next) && !isUnknownOptionAsArg(next))
break;
i = ii;
argsToSet.push(processValue(key, next));
argsToSet.push(processValue(key, next, inputIsString));
}
}
// If both array and nargs are configured, create an error if less than
@ -497,14 +500,14 @@ export class YargsParser {
setArg(key, argsToSet);
return i;
}
function setArg(key, val) {
function setArg(key, val, shouldStripQuotes = inputIsString) {
if (/-/.test(key) && configuration['camel-case-expansion']) {
const alias = key.split('.').map(function (prop) {
return camelCase(prop);
}).join('.');
addNewAlias(key, alias);
}
const value = processValue(key, val);
const value = processValue(key, val, shouldStripQuotes);
const splitKey = key.split('.');
setKey(argv, splitKey, value);
// handle populating aliases of the full key
@ -555,12 +558,10 @@ export class YargsParser {
addNewAlias(alias, key);
}
}
function processValue(key, val) {
function processValue(key, val, shouldStripQuotes) {
// strings may be quoted, clean this up as we assign values.
if (typeof val === 'string' &&
(val[0] === "'" || val[0] === '"') &&
val[val.length - 1] === val[0]) {
val = val.substring(1, val.length - 1);
if (shouldStripQuotes) {
val = stripQuotes(val);
}
// handle parsing boolean arguments --foo=true --bar false.
if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
@ -1035,3 +1036,10 @@ function sanitizeKey(key) {
return '___proto___';
return key;
}
function stripQuotes(val) {
return (typeof val === 'string' &&
(val[0] === "'" || val[0] === '"') &&
val[val.length - 1] === val[0])
? val.substring(1, val.length - 1)
: val;
}