Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

29 changes: 0 additions & 29 deletions .eslintrc.json

This file was deleted.

4 changes: 2 additions & 2 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"html",
"text"
],
"lines": 99.5,
"lines": 99.4,
"branches": "97",
"statements": "99.5"
"statements": "99.4"
}
13 changes: 13 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
build/
coverage/
test/tscc/
test/fixtures/
deno.ts
test/deno/cliui
.nycrc
# Ignore JSON, Markdown, and YAML files for now, keep focus on code.
# (Especially CHANGELOG.md, which is maintained by release-please.)
*.json
*.md
*.yaml
*.yml
3 changes: 0 additions & 3 deletions .prettierrc.cjs

This file was deleted.

6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bracketSpacing": false,
"singleQuote": true,
"trailingComma": "all",
"arrowParens": "avoid"
}
63 changes: 63 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import js from '@eslint/js';
import {defineConfig, globalIgnores} from 'eslint/config';
import prettierConfig from 'eslint-config-prettier/flat';
import n from 'eslint-plugin-n';
import tseslint from 'typescript-eslint';

export default defineConfig([
globalIgnores([
'build/',
'coverage/',
'test/tscc/',
'deno.ts',
'test/deno/cliui',
]),

js.configs.recommended,
n.configs['flat/recommended'],
// package.json has "type": "module", so treat .cjs files as CommonJS explicitly.
{files: ['**/*.cjs'], ...n.configs['flat/recommended-script']},

// Rules previously supplied by gts (formatting rules dropped: Prettier owns those).
{
rules: {
'block-scoped-var': 'error',
eqeqeq: 'error',
'no-var': 'error',
'prefer-const': 'error',
'prefer-arrow-callback': 'error',
'no-restricted-properties': [
'error',
{object: 'describe', property: 'only'},
{object: 'it', property: 'only'},
],
},
},

{
files: ['**/*.ts'],
extends: [tseslint.configs.recommended],
rules: {
// Carried over from gts.
'@typescript-eslint/ban-ts-comment': 'warn',
'n/no-missing-import': 'off',
'n/no-missing-require': 'off',
'n/no-unsupported-features/es-syntax': 'off',
// Project-specific.
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unsafe-function-type': 'off',
'@typescript-eslint/no-empty-object-type': 'off',
},
},

{
files: ['test/**/*.mjs'],
rules: {
'prefer-arrow-callback': 'off',
},
},

// Must be last: turns off rules that conflict with Prettier.
prettierConfig,
]);
80 changes: 46 additions & 34 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,76 @@
* SPDX-License-Identifier: ISC
*/

/* eslint-disable n/no-unpublished-import */

import { format } from 'util'
import { normalize, resolve } from 'path'
import { ArgsInput, Arguments, Parser, Options, DetailedArguments } from './yargs-parser-types.js'
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js'
import { YargsParser } from './yargs-parser.js'
import { readFileSync } from 'fs'
import { createRequire } from 'node:module';
import {format} from 'util';
import {normalize, resolve} from 'path';
import {
ArgsInput,
Arguments,
Parser,
Options,
DetailedArguments,
} from './yargs-parser-types.js';
import {camelCase, decamelize, looksLikeNumber} from './string-utils.js';
import {YargsParser} from './yargs-parser.js';
import {readFileSync} from 'fs';
import {createRequire} from 'node:module';

// 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)
: 20
const nodeVersion = process?.versions?.node ?? process?.version?.slice(1)
const minNodeVersion =
process && process.env && process.env.YARGS_MIN_NODE_VERSION
? Number(process.env.YARGS_MIN_NODE_VERSION)
: 20;
const nodeVersion = process?.versions?.node ?? process?.version?.slice(1);
if (nodeVersion) {
const major = Number(nodeVersion.match(/^([^.]+)/)![1])
const major = Number(nodeVersion.match(/^([^.]+)/)![1]);
if (major < minNodeVersion) {
throw Error(`yargs parser supports a minimum Node.js version of ${minNodeVersion}. Read our version support policy: https://github.com/yargs/yargs-parser#supported-nodejs-versions`)
throw Error(
`yargs parser supports a minimum Node.js version of ${minNodeVersion}. Read our version support policy: https://github.com/yargs/yargs-parser#supported-nodejs-versions`,
);
}
}

// Creates a yargs-parser instance using Node.js standard libraries:
const env = process ? process.env as { [key: string]: string } : {}
const env = process ? (process.env as {[key: string]: string}) : {};
const require = createRequire ? createRequire(import.meta.url) : undefined;

const parser = new YargsParser({
cwd: process.cwd,
env: () => {
return env
return env;
},
format,
normalize,
resolve,
require: (path: string) => {
if (typeof require !== 'undefined') {
return require(path)
return require(path);
} else if (path.match(/\.json$/)) {
// Addresses: https://github.com/yargs/yargs/issues/2040
return JSON.parse(readFileSync(path, 'utf8'))
return JSON.parse(readFileSync(path, 'utf8'));
} else {
throw Error('only .json config files are supported in ESM')
throw Error('only .json config files are supported in ESM');
}
}
})
const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Partial<Options>): Arguments {
const result = parser.parse(args.slice(), opts)
return result.argv
}
yargsParser.detailed = function (args: ArgsInput, opts?: Partial<Options>): DetailedArguments {
return parser.parse(args.slice(), opts)
}
yargsParser.camelCase = camelCase
yargsParser.decamelize = decamelize
yargsParser.looksLikeNumber = looksLikeNumber
export default yargsParser
},
});
const yargsParser: Parser = function Parser(
args: ArgsInput,
opts?: Partial<Options>,
): Arguments {
const result = parser.parse(args.slice(), opts);
return result.argv;
};
yargsParser.detailed = function (
args: ArgsInput,
opts?: Partial<Options>,
): DetailedArguments {
return parser.parse(args.slice(), opts);
};
yargsParser.camelCase = camelCase;
yargsParser.decamelize = decamelize;
yargsParser.looksLikeNumber = looksLikeNumber;
export default yargsParser;

// special syntax to allow unqualified default export from CommonJS
// eslint-disable-next-line prettier/prettier
export {yargsParser as 'module.exports'};
2 changes: 1 addition & 1 deletion lib/tokenize-arg-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function tokenizeArgString(argString: string | any[]): string[] {
argString = argString.trim();

let i = 0;
let prevC: string | null = null;
let prevC: string | null;
let c: string | null = null;
let opening: string | null = null;
const args: string[] = [];
Expand Down
Loading
Loading