Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(linter): switch to gts/standardx for linting #1777

Merged
merged 3 commits into from
Oct 11, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
test/
example/
21 changes: 0 additions & 21 deletions .eslintrc

This file was deleted.

13 changes: 13 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "./node_modules/gts/",
"overrides": [
{
"files": ["**/*.ts", "**/*.tsx"],
"rules": {
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-explicit-any": "off"
}
}
]
}
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto eol=lf

3 changes: 3 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('gts/.prettierrc.json')
}
8 changes: 4 additions & 4 deletions browser.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Bootstrap yargs for browser:
import browserPlatformShim from './lib/platform-shims/browser.mjs'
import { YargsWithShim } from './build/lib/yargs-factory.js'
import browserPlatformShim from './lib/platform-shims/browser.mjs';
import {YargsWithShim} from './build/lib/yargs-factory.js';

const Yargs = YargsWithShim(browserPlatformShim)
const Yargs = YargsWithShim(browserPlatformShim);

export default Yargs
export default Yargs;
14 changes: 7 additions & 7 deletions deno.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Bootstrap yargs for Deno platform:
import denoPlatformShim from './lib/platform-shims/deno.ts'
import { YargsWithShim } from './build/lib/yargs-factory.js'
import type { YargsInstance as YargsType } from './build/lib/yargs-factory.d.ts'
import denoPlatformShim from './lib/platform-shims/deno.ts';
import {YargsWithShim} from './build/lib/yargs-factory.js';
import type {YargsInstance as YargsType} from './build/lib/yargs-factory.d.ts';

const WrappedYargs = YargsWithShim(denoPlatformShim)
const WrappedYargs = YargsWithShim(denoPlatformShim);

function Yargs (args?: string[]): YargsType {
return WrappedYargs(args)
function Yargs(args?: string[]): YargsType {
return WrappedYargs(args);
}

export default Yargs
export default Yargs;
18 changes: 7 additions & 11 deletions helpers.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { applyExtends as _applyExtends } from './build/lib/utils/apply-extends.js'
import { hideBin } from './build/lib/utils/process-argv.js'
import Parser from 'yargs-parser'
import shim from './lib/platform-shims/esm.mjs'
import {applyExtends as _applyExtends} from './build/lib/utils/apply-extends.js';
import {hideBin} from './build/lib/utils/process-argv.js';
import Parser from 'yargs-parser';
import shim from './lib/platform-shims/esm.mjs';

const applyExtends = (config, cwd, mergeExtends) => {
return _applyExtends(config, cwd, mergeExtends, shim)
}
return _applyExtends(config, cwd, mergeExtends, shim);
};

export {
applyExtends,
hideBin,
Parser,
}
export {applyExtends, hideBin, Parser};
34 changes: 17 additions & 17 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict'
'use strict';
// classic singleton yargs API, to use yargs
// without running as a singleton do:
// require('yargs/yargs')(process.argv.slice(2))
const { Yargs, processArgv } = require('./build/index.cjs')
const {Yargs, processArgv} = require('./build/index.cjs');

Argv(processArgv.hideBin(process.argv))
Argv(processArgv.hideBin(process.argv));

module.exports = Argv
module.exports = Argv;

function Argv (processArgs, cwd) {
const argv = Yargs(processArgs, cwd, require)
singletonify(argv)
return argv
function Argv(processArgs, cwd) {
const argv = Yargs(processArgs, cwd, require);
singletonify(argv);
return argv;
}

/* Hack an instance of Argv with process.argv into Argv
Expand All @@ -21,19 +21,19 @@ function Argv (processArgs, cwd) {
require('yargs').argv
to get a parsed version of process.argv.
*/
function singletonify (inst) {
Object.keys(inst).forEach((key) => {
function singletonify(inst) {
Object.keys(inst).forEach(key => {
if (key === 'argv') {
Argv.__defineGetter__(key, inst.__lookupGetter__(key))
Argv.__defineGetter__(key, inst.__lookupGetter__(key));
} else if (typeof inst[key] === 'function') {
Argv[key] = inst[key].bind(inst)
Argv[key] = inst[key].bind(inst);
} else {
Argv.__defineGetter__('$0', () => {
return inst.$0
})
return inst.$0;
});
Argv.__defineGetter__('parsed', () => {
return inst.parsed
})
return inst.parsed;
});
}
})
});
}
10 changes: 5 additions & 5 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'
'use strict';

// Bootstraps yargs for ESM:
import esmPlatformShim from './lib/platform-shims/esm.mjs'
import { YargsWithShim } from './build/lib/yargs-factory.js'
import esmPlatformShim from './lib/platform-shims/esm.mjs';
import {YargsWithShim} from './build/lib/yargs-factory.js';

const Yargs = YargsWithShim(esmPlatformShim)
export default Yargs
const Yargs = YargsWithShim(esmPlatformShim);
export default Yargs;
114 changes: 75 additions & 39 deletions lib/argsert.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,101 @@
import { YError } from './yerror.js'
import { parseCommand, ParsedCommand } from './parse-command.js'
import {YError} from './yerror.js';
import {parseCommand, ParsedCommand} from './parse-command.js';

const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
export function argsert (callerArguments: any[], length?: number): void
export function argsert (expected: string, callerArguments: any[], length?: number): void
export function argsert (arg1: string | any[], arg2?: any[] | number, arg3?: number): void {
function parseArgs (): [Pick<ParsedCommand, 'demanded' | 'optional'>, any[], number?] {
const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'];
export function argsert(callerArguments: any[], length?: number): void;
export function argsert(
expected: string,
callerArguments: any[],
length?: number
): void;
export function argsert(
arg1: string | any[],
arg2?: any[] | number,
arg3?: number
): void {
function parseArgs(): [
Pick<ParsedCommand, 'demanded' | 'optional'>,
any[],
number?
] {
return typeof arg1 === 'object'
? [{ demanded: [], optional: [] }, arg1, arg2 as number | undefined]
: [parseCommand(`cmd ${arg1}`), arg2 as any[], arg3 as number | undefined]
? [{demanded: [], optional: []}, arg1, arg2 as number | undefined]
: [
parseCommand(`cmd ${arg1}`),
arg2 as any[],
arg3 as number | undefined,
];
}
// TODO: should this eventually raise an exception.
try {
// preface the argument description with "cmd", so
// that we can run it through yargs' command parser.
let position = 0
let [parsed, callerArguments, length] = parseArgs()
const args = [].slice.call(callerArguments)
let position = 0;
const [parsed, callerArguments, _length] = parseArgs();
const args = [].slice.call(callerArguments);

while (args.length && args[args.length - 1] === undefined) args.pop()
length = length || args.length
while (args.length && args[args.length - 1] === undefined) args.pop();
const length = _length || args.length;

if (length < parsed.demanded.length) {
throw new YError(`Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`)
throw new YError(
`Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`
);
}

const totalCommands = parsed.demanded.length + parsed.optional.length
const totalCommands = parsed.demanded.length + parsed.optional.length;
if (length > totalCommands) {
throw new YError(`Too many arguments provided. Expected max ${totalCommands} but received ${length}.`)
throw new YError(
`Too many arguments provided. Expected max ${totalCommands} but received ${length}.`
);
}

parsed.demanded.forEach((demanded) => {
const arg = args.shift()
const observedType = guessType(arg)
const matchingTypes = demanded.cmd.filter(type => type === observedType || type === '*')
if (matchingTypes.length === 0) argumentTypeError(observedType, demanded.cmd, position)
position += 1
})
parsed.demanded.forEach(demanded => {
const arg = args.shift();
const observedType = guessType(arg);
const matchingTypes = demanded.cmd.filter(
type => type === observedType || type === '*'
);
if (matchingTypes.length === 0)
argumentTypeError(observedType, demanded.cmd, position);
position += 1;
});

parsed.optional.forEach((optional) => {
if (args.length === 0) return
const arg = args.shift()
const observedType = guessType(arg)
const matchingTypes = optional.cmd.filter(type => type === observedType || type === '*')
if (matchingTypes.length === 0) argumentTypeError(observedType, optional.cmd, position)
position += 1
})
parsed.optional.forEach(optional => {
if (args.length === 0) return;
const arg = args.shift();
const observedType = guessType(arg);
const matchingTypes = optional.cmd.filter(
type => type === observedType || type === '*'
);
if (matchingTypes.length === 0)
argumentTypeError(observedType, optional.cmd, position);
position += 1;
});
} catch (err) {
console.warn(err.stack)
console.warn(err.stack);
}
}

function guessType (arg: any) {
function guessType(arg: any) {
if (Array.isArray(arg)) {
return 'array'
return 'array';
} else if (arg === null) {
return 'null'
return 'null';
}
return typeof arg
return typeof arg;
}

function argumentTypeError (observedType: string, allowedTypes: string[], position: number) {
throw new YError(`Invalid ${positionName[position] || 'manyith'} argument. Expected ${allowedTypes.join(' or ')} but received ${observedType}.`)
function argumentTypeError(
observedType: string,
allowedTypes: string[],
position: number
) {
throw new YError(
`Invalid ${
positionName[position] || 'manyith'
} argument. Expected ${allowedTypes.join(
' or '
)} but received ${observedType}.`
);
}
42 changes: 23 additions & 19 deletions lib/cjs.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
'use strict'
'use strict';
// Bootstraps yargs for a CommonJS runtime:

import { applyExtends } from './utils/apply-extends'
import { argsert } from './argsert.js'
import { isPromise } from './utils/is-promise.js'
import { objFilter } from './utils/obj-filter.js'
import { globalMiddlewareFactory } from './middleware.js'
import { parseCommand } from './parse-command.js'
import * as processArgv from './utils/process-argv.js'
import { YargsWithShim, rebase } from './yargs-factory.js'
import { YError } from './yerror.js'
import cjsPlatformShim from './platform-shims/cjs.js'
import {applyExtends} from './utils/apply-extends';
import {argsert} from './argsert.js';
import {isPromise} from './utils/is-promise.js';
import {objFilter} from './utils/obj-filter.js';
import {globalMiddlewareFactory} from './middleware.js';
import {parseCommand} from './parse-command.js';
import * as processArgv from './utils/process-argv.js';
import {YargsWithShim, rebase} from './yargs-factory.js';
import {YError} from './yerror.js';
import cjsPlatformShim from './platform-shims/cjs.js';

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

const Parser = require('yargs-parser')
const Yargs = YargsWithShim(cjsPlatformShim)
const Parser = require('yargs-parser');
const Yargs = YargsWithShim(cjsPlatformShim);

export default {
applyExtends,
Expand All @@ -38,5 +42,5 @@ export default {
Parser,
processArgv,
rebase,
YError
}
YError,
};