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

ESLint changes preparing for Prettier #2153

Merged
merged 27 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
def67d1
Separate into ESLint and Prettier, starting with JavaScript
shadowspawn Dec 19, 2023
6cf56ec
Add TypeScript back into linting
shadowspawn Dec 19, 2023
e00fed3
Minor lint. Prepare eslint-plugin-jsdoc
shadowspawn Dec 20, 2023
ce6423b
Apply prettier to lint config
shadowspawn Dec 20, 2023
374d552
Rework npm scripts
shadowspawn Dec 20, 2023
c9eb4a1
ESLint and JSDoc experimentation
shadowspawn Dec 20, 2023
8abde1c
Add json to Prettier coverage
shadowspawn Dec 20, 2023
db46519
Add prettier-plugin-jsdoc
shadowspawn Dec 22, 2023
45164ef
Upgrade to typescript-eslint which supports eslint flat configs
shadowspawn Feb 16, 2024
46e8105
Merge remote-tracking branch 'upstream/develop' into feature/prettier
shadowspawn Feb 16, 2024
2805ac5
Update eslint-plugin-jest with flat config
shadowspawn Feb 16, 2024
d5dd138
Use custom tsconfig for TypeScript eslint
shadowspawn Feb 16, 2024
c1461f8
Update eslint related dependencies
shadowspawn Feb 16, 2024
5b51a14
Rename "check" run-script
shadowspawn Feb 18, 2024
511ad59
Add fix run-scripts
shadowspawn Feb 18, 2024
7586f7f
Add typescript-eslint on javascript files, and rework how configured
shadowspawn Feb 23, 2024
be586dd
Comment out jsdoc and prettier for later
shadowspawn Feb 23, 2024
9f1f116
Reorder eslint configs
shadowspawn Feb 23, 2024
4fb3be4
Simplify eslint run-scripts
shadowspawn Feb 23, 2024
d8c3710
Update tests and instructions for script name change
shadowspawn Feb 23, 2024
57e7cad
Update comment
shadowspawn Feb 24, 2024
87c64a5
Add explicit (missing) @example
shadowspawn Mar 1, 2024
b47d056
Using comments in tsconfig*.json so be explicit that they are jsonc f…
shadowspawn Mar 1, 2024
a36df74
Activate all the checks
shadowspawn Mar 7, 2024
487620e
Do not run format checks yet
shadowspawn Mar 7, 2024
18eb11b
Turn on eslint-config-prettier
shadowspawn Mar 8, 2024
c89e499
Turn off prettier-plugin-jsdoc
shadowspawn Mar 8, 2024
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
67 changes: 0 additions & 67 deletions .eslintrc.js

This file was deleted.

4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ and can be deleted.

Please submit pull requests against the develop branch.

Follow the existing code style. Check the tests succeed, including lint.
Follow the existing code style. Check the tests succeed, including format and lint.
npm run test
npm run lint
npm run check

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

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ jobs:
run: npm ci
- name: npm test
run: npm test
- name: npm run lint
run: npm run lint
- name: npm run check:lint
# switch to full check when have run prettier on all files
run: npm run check:lint
10 changes: 10 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# exclude everything, and opt-in to types we want to format
**.*
# add the filetypes we want to format
!**.js
!**.mjs
!**.cjs
!**.ts
!**.mts
!**.cts
!**.json
12 changes: 12 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const config = {
// plugins: ['prettier-plugin-jsdoc'],
singleQuote: true,
overrides: [
{
files: ['tsconfig*.json'],
options: { parser: 'jsonc' },
},
],
};

module.exports = config;
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ or after six months otherwise.

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

Follow the existing code style. Check the tests succeed, including lint.
Follow the existing code style. Check the tests succeed, including format and lint.

- `npm run test`
- `npm run lint`
- `npm run check`

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

Expand Down
75 changes: 75 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const globals = require('globals');
const esLintjs = require('@eslint/js');
const jest = require('eslint-plugin-jest');
const tseslint = require('typescript-eslint');
const prettier = require('eslint-config-prettier');
//const jsdoc = require('eslint-plugin-jsdoc');

// Using tseslint config helper to customise its setup the tseslint way.
const tsconfigTsFiles = ['**/*.{ts,mts}']; // match "include" in tsconfig.ts.json;
const tsconfigJsFiles = ['*.{js,mjs}', 'lib/**/*.{js,mjs}']; // match "include" in tsconfig.js.json
const tseslintConfigs = tseslint.config(
{
files: tsconfigJsFiles,
languageOptions: {
parserOptions: { project: './tsconfig.js.json' },
},
extends: [
...tseslint.configs.recommended,
],
rules: {
'@typescript-eslint/no-var-requires': 'off', // (tseslint does not autodetect commonjs context )
},
}, {
files: tsconfigTsFiles,
languageOptions: {
parserOptions: { project: './tsconfig.ts.json' },
},
extends: [
...tseslint.configs.recommended,
],
},
);

module.exports = [
esLintjs.configs.recommended,
// jsdoc.configs['flat/recommended'],
jest.configs['flat/recommended'],
...tseslintConfigs,
prettier, // Do Prettier last so it can override previous configs.

// Customise rules.
{
files: ['**/*.{js,mjs,cjs}', '**/*.{ts,mts,cts}'],
rules: {
'no-else-return': ['error', { allowElseIf: false }],

// 'jsdoc/tag-lines': 'off',
// 'jsdoc/require-jsdoc': 'off',
// 'jsdoc/require-param-description': 'off',
// 'jsdoc/require-returns-description': 'off',
},
languageOptions: {
globals: {
...globals.node,
},
},
},
{
files: ['**/*.test.{js,mjs,cjs}'],
rules: {
'no-unused-vars': 'off', // lots in tests, minimise churn for now
}
},
{
files: [...tsconfigTsFiles, ...tsconfigJsFiles],
rules: {
'@typescript-eslint/ban-ts-comment': ['error', {
'ts-expect-error': 'allow-with-description',
'ts-ignore': 'allow-with-description',
'ts-nocheck': true,
'ts-check': true,
}],
},
},
];
2 changes: 1 addition & 1 deletion examples/arguments-custom-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const commander = require('../'); // include commander in git clone of commander repo
const program = new commander.Command();

function myParseInt(value, dummyPrevious) {
function myParseInt(value) {
// parseInt takes a string and a radix
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
Expand Down
4 changes: 2 additions & 2 deletions examples/custom-command-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class CommandWithTrace extends commander.Command {
cmd.option('-t, --trace', 'display extra information when run command');
return cmd;
}
};
}

function inpectCommand(command) {
// The option value is stored as property on command because we called .storeOptionsAsProperties()
console.log(`Called '${command.name()}'`);
console.log(`args: ${command.args}`);
console.log('opts: %o', command.opts());
};
}

const program = new CommandWithTrace('program')
.option('-v, ---verbose')
Expand Down
4 changes: 2 additions & 2 deletions examples/options-custom-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const commander = require('../'); // include commander in git clone of commander repo
const program = new commander.Command();

function myParseInt(value, dummyPrevious) {
function myParseInt(value) {
// parseInt takes a string and a radix
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
Expand All @@ -25,7 +25,7 @@ function collect(value, previous) {
return previous.concat([value]);
}

function commaSeparatedList(value, dummyPrevious) {
function commaSeparatedList(value) {
return value.split(',');
}

Expand Down
14 changes: 8 additions & 6 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Command extends EventEmitter {

_getCommandAndAncestors() {
const result = [];
// eslint-disable-next-line @typescript-eslint/no-this-alias
for (let command = this; command; command = command.parent) {
result.push(command);
}
Expand Down Expand Up @@ -361,6 +362,7 @@ class Command extends EventEmitter {
/**
* Customise or override default help command. By default a help command is automatically added if your command has subcommands.
*
* @example
* program.helpCommand('help [cmd]');
* program.helpCommand('help [cmd]', 'show help');
* program.helpCommand(false); // suppress default help command
Expand Down Expand Up @@ -929,7 +931,6 @@ Expecting one of '${allowedValues.join("', '")}'`);
// Default to using process.argv
if (argv === undefined) {
argv = process.argv;
// @ts-ignore: unknown property
if (process.versions && process.versions.electron) {
parseOptions.from = 'electron';
}
Expand All @@ -945,7 +946,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
userArgs = argv.slice(2);
break;
case 'electron':
// @ts-ignore: unknown property
// @ts-ignore: because defaultApp is an unknown property
if (process.defaultApp) {
this._scriptPath = argv[1];
userArgs = argv.slice(2);
Expand Down Expand Up @@ -1097,7 +1098,6 @@ Expecting one of '${allowedValues.join("', '")}'`);
if (!proc.killed) { // testing mainly to avoid leak warnings during unit tests with mocked spawn
const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
signals.forEach((signal) => {
// @ts-ignore
process.on(signal, () => {
if (proc.killed === false && proc.exitCode === null) {
proc.kill(signal);
Expand All @@ -1108,7 +1108,7 @@ Expecting one of '${allowedValues.join("', '")}'`);

// By default terminate process when spawned process terminates.
const exitCallback = this._exitCallback;
proc.on('close', (code, _signal) => {
proc.on('close', (code) => {
code = code ?? 1; // code is null if spawned process terminated due to a signal
if (!exitCallback) {
process.exit(code);
Expand All @@ -1117,7 +1117,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
}
});
proc.on('error', (err) => {
// @ts-ignore
// @ts-ignore: because err.code is an unknown property
if (err.code === 'ENOENT') {
const executableDirMessage = executableDir
? `searched for local subcommand relative to directory '${executableDir}'`
Expand All @@ -1127,7 +1127,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
- if the default executable name is not suitable, use the executableFile option to supply a custom name or path
- ${executableDirMessage}`;
throw new Error(executableMissing);
// @ts-ignore
// @ts-ignore: because err.code is an unknown property
} else if (err.code === 'EACCES') {
throw new Error(`'${executableFile}' not executable`);
}
Expand Down Expand Up @@ -1818,6 +1818,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
if (flag.startsWith('--') && this._showSuggestionAfterError) {
// Looping to pick up the global options too
let candidateFlags = [];
// eslint-disable-next-line @typescript-eslint/no-this-alias
let command = this;
do {
const moreFlags = command.createHelp().visibleOptions(command)
Expand Down Expand Up @@ -1944,6 +1945,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
if (alias === undefined) return this._aliases[0]; // just return first, for backwards compatibility

/** @type {Command} */
// eslint-disable-next-line @typescript-eslint/no-this-alias
let command = this;
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
// assume adding alias for last added executable subcommand, rather than this
Expand Down
6 changes: 3 additions & 3 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Help {
}
if (this.sortSubcommands) {
visibleCommands.sort((a, b) => {
// @ts-ignore: overloaded return type
// @ts-ignore: because overloaded return type
return a.name().localeCompare(b.name());
});
}
Expand Down Expand Up @@ -248,7 +248,7 @@ class Help {
*/

commandDescription(cmd) {
// @ts-ignore: overloaded return type
// @ts-ignore: because overloaded return type
return cmd.description();
}

Expand All @@ -261,7 +261,7 @@ class Help {
*/

subcommandDescription(cmd) {
// @ts-ignore: overloaded return type
// @ts-ignore: because overloaded return type
return cmd.summary() || cmd.description();
}

Expand Down
Loading