Skip to content

Commit 6fc933e

Browse files
authored
ESLint changes preparing for Prettier (#2153)
Switch from StandardJS to ESLint using flat configuration. Add Prettier and configuration.
1 parent 41b12cf commit 6fc933e

25 files changed

+1454
-2142
lines changed

.eslintrc.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ and can be deleted.
66
77
Please submit pull requests against the develop branch.
88
9-
Follow the existing code style. Check the tests succeed, including lint.
9+
Follow the existing code style. Check the tests succeed, including format and lint.
1010
npm run test
11-
npm run lint
11+
npm run check
1212
1313
Don't update the CHANGELOG or command version number. That gets done by maintainers when preparing the release.
1414

.github/workflows/tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ jobs:
2727
run: npm ci
2828
- name: npm test
2929
run: npm test
30-
- name: npm run lint
31-
run: npm run lint
30+
- name: npm run check:lint
31+
# switch to full check when have run prettier on all files
32+
run: npm run check:lint

.prettierignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# exclude everything, and opt-in to types we want to format
2+
**.*
3+
# add the filetypes we want to format
4+
!**.js
5+
!**.mjs
6+
!**.cjs
7+
!**.ts
8+
!**.mts
9+
!**.cts
10+
!**.json

.prettierrc.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const config = {
2+
// plugins: ['prettier-plugin-jsdoc'],
3+
singleQuote: true,
4+
overrides: [
5+
{
6+
files: ['tsconfig*.json'],
7+
options: { parser: 'jsonc' },
8+
},
9+
],
10+
};
11+
12+
module.exports = config;

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ or after six months otherwise.
1414

1515
Pull Requests will be considered. Please submit pull requests against the develop branch.
1616

17-
Follow the existing code style. Check the tests succeed, including lint.
17+
Follow the existing code style. Check the tests succeed, including format and lint.
1818

1919
- `npm run test`
20-
- `npm run lint`
20+
- `npm run check`
2121

2222
Don't update the CHANGELOG or command version number. That gets done by maintainers when preparing the release.
2323

eslint.config.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const globals = require('globals');
2+
const esLintjs = require('@eslint/js');
3+
const jest = require('eslint-plugin-jest');
4+
const tseslint = require('typescript-eslint');
5+
const prettier = require('eslint-config-prettier');
6+
//const jsdoc = require('eslint-plugin-jsdoc');
7+
8+
// Using tseslint config helper to customise its setup the tseslint way.
9+
const tsconfigTsFiles = ['**/*.{ts,mts}']; // match "include" in tsconfig.ts.json;
10+
const tsconfigJsFiles = ['*.{js,mjs}', 'lib/**/*.{js,mjs}']; // match "include" in tsconfig.js.json
11+
const tseslintConfigs = tseslint.config(
12+
{
13+
files: tsconfigJsFiles,
14+
languageOptions: {
15+
parserOptions: { project: './tsconfig.js.json' },
16+
},
17+
extends: [
18+
...tseslint.configs.recommended,
19+
],
20+
rules: {
21+
'@typescript-eslint/no-var-requires': 'off', // (tseslint does not autodetect commonjs context )
22+
},
23+
}, {
24+
files: tsconfigTsFiles,
25+
languageOptions: {
26+
parserOptions: { project: './tsconfig.ts.json' },
27+
},
28+
extends: [
29+
...tseslint.configs.recommended,
30+
],
31+
},
32+
);
33+
34+
module.exports = [
35+
esLintjs.configs.recommended,
36+
// jsdoc.configs['flat/recommended'],
37+
jest.configs['flat/recommended'],
38+
...tseslintConfigs,
39+
prettier, // Do Prettier last so it can override previous configs.
40+
41+
// Customise rules.
42+
{
43+
files: ['**/*.{js,mjs,cjs}', '**/*.{ts,mts,cts}'],
44+
rules: {
45+
'no-else-return': ['error', { allowElseIf: false }],
46+
47+
// 'jsdoc/tag-lines': 'off',
48+
// 'jsdoc/require-jsdoc': 'off',
49+
// 'jsdoc/require-param-description': 'off',
50+
// 'jsdoc/require-returns-description': 'off',
51+
},
52+
languageOptions: {
53+
globals: {
54+
...globals.node,
55+
},
56+
},
57+
},
58+
{
59+
files: ['**/*.test.{js,mjs,cjs}'],
60+
rules: {
61+
'no-unused-vars': 'off', // lots in tests, minimise churn for now
62+
}
63+
},
64+
{
65+
files: [...tsconfigTsFiles, ...tsconfigJsFiles],
66+
rules: {
67+
'@typescript-eslint/ban-ts-comment': ['error', {
68+
'ts-expect-error': 'allow-with-description',
69+
'ts-ignore': 'allow-with-description',
70+
'ts-nocheck': true,
71+
'ts-check': true,
72+
}],
73+
},
74+
},
75+
];

examples/arguments-custom-processing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const commander = require('commander');
88
const program = new commander.Command();
99

10-
function myParseInt(value, dummyPrevious) {
10+
function myParseInt(value) {
1111
// parseInt takes a string and a radix
1212
const parsedValue = parseInt(value, 10);
1313
if (isNaN(parsedValue)) {

examples/custom-command-class.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class CommandWithTrace extends commander.Command {
1010
cmd.option('-t, --trace', 'display extra information when run command');
1111
return cmd;
1212
}
13-
};
13+
}
1414

1515
function inpectCommand(command) {
1616
// The option value is stored as property on command because we called .storeOptionsAsProperties()
1717
console.log(`Called '${command.name()}'`);
1818
console.log(`args: ${command.args}`);
1919
console.log('opts: %o', command.opts());
20-
};
20+
}
2121

2222
const program = new CommandWithTrace('program')
2323
.option('-v, ---verbose')

examples/options-custom-processing.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const commander = require('commander');
88
const program = new commander.Command();
99

10-
function myParseInt(value, dummyPrevious) {
10+
function myParseInt(value) {
1111
// parseInt takes a string and a radix
1212
const parsedValue = parseInt(value, 10);
1313
if (isNaN(parsedValue)) {
@@ -24,7 +24,7 @@ function collect(value, previous) {
2424
return previous.concat([value]);
2525
}
2626

27-
function commaSeparatedList(value, dummyPrevious) {
27+
function commaSeparatedList(value) {
2828
return value.split(',');
2929
}
3030

lib/command.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class Command extends EventEmitter {
105105

106106
_getCommandAndAncestors() {
107107
const result = [];
108+
// eslint-disable-next-line @typescript-eslint/no-this-alias
108109
for (let command = this; command; command = command.parent) {
109110
result.push(command);
110111
}
@@ -361,6 +362,7 @@ class Command extends EventEmitter {
361362
/**
362363
* Customise or override default help command. By default a help command is automatically added if your command has subcommands.
363364
*
365+
* @example
364366
* program.helpCommand('help [cmd]');
365367
* program.helpCommand('help [cmd]', 'show help');
366368
* program.helpCommand(false); // suppress default help command
@@ -929,7 +931,6 @@ Expecting one of '${allowedValues.join("', '")}'`);
929931
// Default to using process.argv
930932
if (argv === undefined) {
931933
argv = process.argv;
932-
// @ts-ignore: unknown property
933934
if (process.versions && process.versions.electron) {
934935
parseOptions.from = 'electron';
935936
}
@@ -945,7 +946,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
945946
userArgs = argv.slice(2);
946947
break;
947948
case 'electron':
948-
// @ts-ignore: unknown property
949+
// @ts-ignore: because defaultApp is an unknown property
949950
if (process.defaultApp) {
950951
this._scriptPath = argv[1];
951952
userArgs = argv.slice(2);
@@ -1097,7 +1098,6 @@ Expecting one of '${allowedValues.join("', '")}'`);
10971098
if (!proc.killed) { // testing mainly to avoid leak warnings during unit tests with mocked spawn
10981099
const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
10991100
signals.forEach((signal) => {
1100-
// @ts-ignore
11011101
process.on(signal, () => {
11021102
if (proc.killed === false && proc.exitCode === null) {
11031103
proc.kill(signal);
@@ -1108,7 +1108,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
11081108

11091109
// By default terminate process when spawned process terminates.
11101110
const exitCallback = this._exitCallback;
1111-
proc.on('close', (code, _signal) => {
1111+
proc.on('close', (code) => {
11121112
code = code ?? 1; // code is null if spawned process terminated due to a signal
11131113
if (!exitCallback) {
11141114
process.exit(code);
@@ -1117,7 +1117,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
11171117
}
11181118
});
11191119
proc.on('error', (err) => {
1120-
// @ts-ignore
1120+
// @ts-ignore: because err.code is an unknown property
11211121
if (err.code === 'ENOENT') {
11221122
const executableDirMessage = executableDir
11231123
? `searched for local subcommand relative to directory '${executableDir}'`
@@ -1127,7 +1127,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
11271127
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
11281128
- ${executableDirMessage}`;
11291129
throw new Error(executableMissing);
1130-
// @ts-ignore
1130+
// @ts-ignore: because err.code is an unknown property
11311131
} else if (err.code === 'EACCES') {
11321132
throw new Error(`'${executableFile}' not executable`);
11331133
}
@@ -1818,6 +1818,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
18181818
if (flag.startsWith('--') && this._showSuggestionAfterError) {
18191819
// Looping to pick up the global options too
18201820
let candidateFlags = [];
1821+
// eslint-disable-next-line @typescript-eslint/no-this-alias
18211822
let command = this;
18221823
do {
18231824
const moreFlags = command.createHelp().visibleOptions(command)
@@ -1944,6 +1945,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
19441945
if (alias === undefined) return this._aliases[0]; // just return first, for backwards compatibility
19451946

19461947
/** @type {Command} */
1948+
// eslint-disable-next-line @typescript-eslint/no-this-alias
19471949
let command = this;
19481950
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
19491951
// assume adding alias for last added executable subcommand, rather than this

lib/help.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Help {
3232
}
3333
if (this.sortSubcommands) {
3434
visibleCommands.sort((a, b) => {
35-
// @ts-ignore: overloaded return type
35+
// @ts-ignore: because overloaded return type
3636
return a.name().localeCompare(b.name());
3737
});
3838
}
@@ -248,7 +248,7 @@ class Help {
248248
*/
249249

250250
commandDescription(cmd) {
251-
// @ts-ignore: overloaded return type
251+
// @ts-ignore: because overloaded return type
252252
return cmd.description();
253253
}
254254

@@ -261,7 +261,7 @@ class Help {
261261
*/
262262

263263
subcommandDescription(cmd) {
264-
// @ts-ignore: overloaded return type
264+
// @ts-ignore: because overloaded return type
265265
return cmd.summary() || cmd.description();
266266
}
267267

0 commit comments

Comments
 (0)