Skip to content

Commit

Permalink
refactor: simplify/fix linting (#1867)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Feb 16, 2021
1 parent e0f9363 commit d30ffac
Show file tree
Hide file tree
Showing 28 changed files with 260 additions and 212 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
@@ -1,4 +1,6 @@
build/
test/
example/
helpers/
lib/platform-shims/esm.mjs
test/fixtures/**/**
example.mjs
2 changes: 2 additions & 0 deletions lib/platform-shims/browser.mjs
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
'use strict';

import cliui from 'https://unpkg.com/cliui@7.0.1/index.mjs'; // eslint-disable-line
Expand Down Expand Up @@ -44,6 +45,7 @@ export default {
// exit is noop browser:
exit: () => {},
nextTick: cb => {
// eslint-disable-next-line no-undef
window.setTimeout(cb, 1);
},
stdColumns: 80,
Expand Down
13 changes: 2 additions & 11 deletions package.json
Expand Up @@ -71,14 +71,13 @@
"rimraf": "^3.0.2",
"rollup": "^2.23.0",
"rollup-plugin-cleanup": "^3.1.1",
"standardx": "^7.0.0",
"typescript": "^4.0.2",
"which": "^2.0.0",
"yargs-test-extends": "^1.0.1"
},
"scripts": {
"fix": "gts fix && npm run fix:js",
"fix:js": "standardx --fix **/*.mjs && standardx --fix **/*.cjs && standardx --fix ./*.mjs && standardx --fix ./*.cjs",
"fix:js": "eslint . --ext cjs --ext mjs --ext js --fix",
"posttest": "npm run check",
"test": "c8 mocha ./test/*.cjs --require ./test/before.cjs --timeout=12000 --check-leaks",
"test:esm": "c8 mocha ./test/esm/*.mjs --check-leaks",
Expand All @@ -90,22 +89,14 @@
"build:cjs": "rollup -c rollup.config.cjs",
"postbuild:cjs": "rimraf ./build/index.cjs.d.ts",
"check": "gts lint && npm run check:js",
"check:js": "standardx **/*.mjs && standardx **/*.cjs && standardx ./*.mjs && standardx ./*.cjs",
"check:js": "eslint . --ext cjs --ext mjs --ext js",
"clean": "gts clean"
},
"repository": {
"type": "git",
"url": "https://github.com/yargs/yargs.git"
},
"homepage": "https://yargs.js.org/",
"standardx": {
"ignore": [
"build",
"helpers",
"**/example/**",
"**/platform-shims/esm.mjs"
]
},
"keywords": [
"argument",
"args",
Expand Down
3 changes: 2 additions & 1 deletion test/command.cjs
@@ -1,5 +1,6 @@
'use strict';
/* global describe, it, beforeEach */
/* eslint-disable no-unused-vars */
'use strict';
const yargs = require('../index.cjs');
const expect = require('chai').expect;
const checkOutput = require('./helpers/utils.cjs').checkOutput;
Expand Down
1 change: 1 addition & 0 deletions test/completion.cjs
@@ -1,5 +1,6 @@
'use strict';
/* global describe, it, before, beforeEach, after */
/* eslint-disable no-unused-vars */
const checkUsage = require('./helpers/utils.cjs').checkOutput;
const yargs = require('../index.cjs');

Expand Down
34 changes: 21 additions & 13 deletions test/deno/platform-shim.test.ts
@@ -1,22 +1,30 @@
/* global Deno */

import {
assertEquals
} from 'https://deno.land/std/testing/asserts.ts'
import shim from '../../lib/platform-shims/deno.ts'
import {assertEquals} from 'https://deno.land/std/testing/asserts.ts';
import shim from '../../lib/platform-shims/deno.ts';

// y18n.
Deno.test('__ behaves like sprintf', () => {
const str = shim.y18n.__('hello %s, goodnight %s', 'world', 'moon')
assertEquals(str, 'hello world, goodnight moon')
})
const str = shim.y18n.__('hello %s, goodnight %s', 'world', 'moon');
assertEquals(str, 'hello world, goodnight moon');
});

Deno.test('__n uses first string if singular', () => {
const str = shim.y18n.__n('Missing required argument: %s', 'Missing required arguments: %s', 1, 'foo, bar')
assertEquals(str, 'Missing required argument: foo, bar')
})
const str = shim.y18n.__n(
'Missing required argument: %s',
'Missing required arguments: %s',
1,
'foo, bar'
);
assertEquals(str, 'Missing required argument: foo, bar');
});

Deno.test('__n uses second string if plural', () => {
const str = shim.y18n.__n('Missing required argument: %s', 'Missing required arguments: %s', 2, 'foo, bar')
assertEquals(str, 'Missing required arguments: foo, bar')
})
const str = shim.y18n.__n(
'Missing required argument: %s',
'Missing required arguments: %s',
2,
'foo, bar'
);
assertEquals(str, 'Missing required arguments: foo, bar');
});
58 changes: 32 additions & 26 deletions test/deno/yargs.test.ts
Expand Up @@ -2,48 +2,54 @@

import {
assertEquals,
assertMatch
} from 'https://deno.land/std/testing/asserts.ts'
import yargs from '../../deno.ts'
import { Arguments } from '../../deno-types.ts'
import { commands } from '../esm/fixtures/commands/index.mjs'
assertMatch,
} from 'https://deno.land/std/testing/asserts.ts';
import yargs from '../../deno.ts';
import {Arguments} from '../../deno-types.ts';
import {commands} from '../esm/fixtures/commands/index.mjs';

Deno.test('demandCommand(1) throw error if no command provided', () => {
let err: Error|null = null
let err: Error | null = null;
yargs()
.demandCommand(1)
.parse(Deno.args, (_err: Error) => {
err = _err
})
assertMatch(err!.message, /Not enough non-option/)
})
err = _err;
});
assertMatch(err!.message, /Not enough non-option/);
});

// TODO: we should think of a way to support this functionality
Deno.test('guesses version # based on package.json', () => {
let output: string|null = null
yargs()
.parse('--version', (_err: Error, argv: Arguments, _output: string) => {
output = _output
})
assertMatch('' + output, /[0-9]+\.[0-9]+\.[0-9]+/)
})
let output: string | null = null;
yargs().parse(
'--version',
(_err: Error, argv: Arguments, _output: string) => {
output = _output;
}
);
assertMatch('' + output, /[0-9]+\.[0-9]+\.[0-9]+/);
});

// Handling of strings that look like numbers, see:
// https://github.com/yargs/yargs/issues/1758
Deno.test('does not drop .0 if positional is configured as string', async () => {
const argv = await yargs(['cmd', '33.0'])
.command('cmd [str]', 'a command', (yargs: any) => {
return yargs.positional('str', {
type: 'string'
Deno.test(
'does not drop .0 if positional is configured as string',
async () => {
const argv = (await yargs(['cmd', '33.0'])
.command('cmd [str]', 'a command', (yargs: any) => {
return yargs.positional('str', {
type: 'string',
});
})
}).parse() as Arguments
assertEquals(argv.str, '33.0')
})
.parse()) as Arguments;
assertEquals(argv.str, '33.0');
}
);

Deno.test('hierarchy of commands', async () => {
const context = {
output: {value: 0},
};
yargs().command(commands).parse('a c 10 5', context);
assertEquals(context.output.value, 15);
})
});
1 change: 1 addition & 0 deletions test/esm/fixtures/commands/a.mjs
Expand Up @@ -5,4 +5,5 @@ export const describe = 'numeric commands';
export const builder = yargs => {
yargs.command(commands);
};
// eslint-disable-next-line no-unused-vars
export const handler = function (argv) {};
1 change: 1 addition & 0 deletions test/esm/fixtures/commands/subcommands/c.mjs
@@ -1,5 +1,6 @@
export const command = 'c <x> <y>';
export const describe = 'add x to y';
// eslint-disable-next-line no-unused-vars
export const builder = yargs => {};
export const handler = function (argv) {
argv.output.value = argv.x + argv.y;
Expand Down
1 change: 1 addition & 0 deletions test/esm/fixtures/commands/subcommands/d.mjs
@@ -1,5 +1,6 @@
export const command = 'd <x> <y>';
export const describe = 'multiply x by y';
// eslint-disable-next-line no-unused-vars
export const builder = yargs => {};
export const handler = function (argv) {
argv.output.value = argv.x * argv.y;
Expand Down
9 changes: 4 additions & 5 deletions test/fixtures/bin.js
@@ -1,6 +1,5 @@
#!/usr/bin/env node
var argv = require('../../')
.help('help')
.completion()
.argv
console.log(JSON.stringify(argv._))
/* eslint-disable node/shebang */
// eslint-disable-next-line no-undef
const argv = require('../../').help('help').completion().argv;
console.log(JSON.stringify(argv._));
41 changes: 24 additions & 17 deletions test/fixtures/cmddir/deep/deeper/deeper_still/limbo.js
@@ -1,28 +1,35 @@
exports.command = 'limbo [opts]'
exports.desc = 'Get lost in pure subconscious'
/* eslint-disable no-undef */
exports.command = 'limbo [opts]';
exports.desc = 'Get lost in pure subconscious';
exports.builder = {
'with-self-exit': {
desc: 'Pretty much your only way out',
type: 'boolean'
type: 'boolean',
},
'with-totem': {
desc: 'Take your totem with you',
type: 'boolean'
}
}
type: 'boolean',
},
};
exports.handler = function (argv) {
var factor = 3
if (!argv.withSelfExit) throw new Error('You entered limbo without a way out!')
if (!argv.withTotem) factor -= 2
let factor = 3;
if (!argv.withSelfExit)
throw new Error('You entered limbo without a way out!');
if (!argv.withTotem) factor -= 2;
if (argv.extract) {
if (!chancesLevel4(factor)) throw new Error('You didn\'t have much chance anyway, you\'re stuck in limbo!')
if (!argv._msg) argv._msg = 'You have accomplished the impossible. Inception successful.'
return
if (!chancesLevel4(factor))
throw new Error(
"You didn't have much chance anyway, you're stuck in limbo!"
);
if (!argv._msg)
argv._msg = 'You have accomplished the impossible. Inception successful.';
return;
}
if (!chancesLevel4(factor)) throw new Error('You rolled the dice and lost, you\'re stuck in limbo!')
if (!argv._msg) argv._msg = 'Can you ever be sure of what\'s real anymore?'
}
if (!chancesLevel4(factor))
throw new Error("You rolled the dice and lost, you're stuck in limbo!");
if (!argv._msg) argv._msg = "Can you ever be sure of what's real anymore?";
};

function chancesLevel4 (factor) {
return Math.floor(Math.random() * 10) < factor
function chancesLevel4(factor) {
return Math.floor(Math.random() * 10) < factor;
}
41 changes: 24 additions & 17 deletions test/fixtures/cmddir/deep/deeper/inception.js
@@ -1,32 +1,39 @@
exports.command = 'inception [command] [opts]'
exports.desc = 'Enter another dream, where inception is possible'
/* eslint-disable no-undef */
exports.command = 'inception [command] [opts]';
exports.desc = 'Enter another dream, where inception is possible';
exports.builder = function (yargs) {
return yargs
.commandDir('deeper_still')
.option('with-sedation', {
desc: 'Apply a sedative?',
type: 'boolean',
global: true
global: true,
})
.option('with-timed-kick', {
desc: 'Plan an elaborate timed kick at each layer?',
type: 'boolean',
global: true
})
}
global: true,
});
};
exports.handler = function (argv) {
var factor = 5
let factor = 5;
if (argv.extract) {
if (!argv.withSedation) factor -= 1
if (!argv.withTimedKick) factor -= 1
if (!chancesLevel3(factor)) throw new Error('Something went wrong at level 3! Check your options for increased chance of success.')
if (!argv._msg) argv._msg = 'You have narrowly escaped disaster. Inception successful.'
return
if (!argv.withSedation) factor -= 1;
if (!argv.withTimedKick) factor -= 1;
if (!chancesLevel3(factor))
throw new Error(
'Something went wrong at level 3! Check your options for increased chance of success.'
);
if (!argv._msg)
argv._msg = 'You have narrowly escaped disaster. Inception successful.';
return;
}
if (!chancesLevel3(factor)) throw new Error('You can no longer tell a dream from reality!')
if (!argv._msg) argv._msg = 'Be very careful, you\'re starting to lose grip on reality.'
}
if (!chancesLevel3(factor))
throw new Error('You can no longer tell a dream from reality!');
if (!argv._msg)
argv._msg = "Be very careful, you're starting to lose grip on reality.";
};

function chancesLevel3 (factor) {
return Math.floor(Math.random() * 10) < factor
function chancesLevel3(factor) {
return Math.floor(Math.random() * 10) < factor;
}
39 changes: 21 additions & 18 deletions test/fixtures/cmddir/deep/within-a-dream.js
@@ -1,28 +1,31 @@
/* eslint-disable no-undef */
module.exports = {
command: 'within-a-dream [command] [opts]',
desc: 'Dream within a dream',
builder: function (yargs) {
return yargs
.commandDir('deeper')
.option('with-kick', {
desc: 'Plan a kick for controlled wake up?',
type: 'boolean',
global: true
})
return yargs.commandDir('deeper').option('with-kick', {
desc: 'Plan a kick for controlled wake up?',
type: 'boolean',
global: true,
});
},
handler: function (argv) {
var factor = 7
if (argv.context) argv.context.counter++ // keep track of how many times we've invoked this handler.
let factor = 7;
if (argv.context) argv.context.counter++; // keep track of how many times we've invoked this handler.
if (argv.extract) {
if (!argv.withKick) factor -= 2
if (!chancesLevel2(factor)) throw new Error('Something went wrong at level 2! Check your options for increased chance of success.')
if (!argv._msg) argv._msg = 'You got lucky this time. Extraction successful.'
return
if (!argv.withKick) factor -= 2;
if (!chancesLevel2(factor))
throw new Error(
'Something went wrong at level 2! Check your options for increased chance of success.'
);
if (!argv._msg)
argv._msg = 'You got lucky this time. Extraction successful.';
return;
}
if (!argv._msg) argv._msg = 'Let\'s not make a habit of this.'
}
}
if (!argv._msg) argv._msg = "Let's not make a habit of this.";
},
};

function chancesLevel2 (factor) {
return Math.floor(Math.random() * 10) < factor
function chancesLevel2(factor) {
return Math.floor(Math.random() * 10) < factor;
}

0 comments on commit d30ffac

Please sign in to comment.