From 91b63a02efaf468422f3c9d9acb63ca0a6a03751 Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Thu, 25 Apr 2024 11:56:56 -0400 Subject: [PATCH] Chore: eslint unicorn --- eslint.config.js | 21 ++++++++------ packages/checkbox/src/index.mts | 2 +- packages/core/src/lib/create-prompt.mts | 8 +++--- packages/demo/demos/input.mjs | 2 +- packages/editor/editor.test.mts | 9 ++++-- packages/inquirer/examples/long-list.js | 28 ++++++++++--------- .../inquirer/examples/regex-validate-input.js | 2 +- .../inquirer/examples/rx-observable-array.js | 4 +-- packages/inquirer/lib/prompts/base.js | 4 +-- packages/inquirer/lib/prompts/list.js | 2 +- packages/inquirer/lib/utils/events.js | 2 +- packages/inquirer/test/helpers/readline.js | 6 ++-- packages/inquirer/test/specs/inquirer.test.js | 10 +++---- .../test/specs/prompts/checkbox.test.js | 18 ++++++------ .../test/specs/prompts/confirm.test.js | 2 +- .../test/specs/prompts/number.test.js | 2 +- packages/prompts/src/index.mts | 17 ++++++----- packages/rawlist/src/index.mts | 2 +- packages/select/src/index.mts | 2 +- tools/fix-ext.mjs | 6 ++-- tools/setup-packages.mjs | 4 +-- vitest.config.ts | 2 +- 22 files changed, 82 insertions(+), 73 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index cfd0d8a79..c6bf3d0b6 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -17,23 +17,28 @@ export default tseslint.config( 'packages/*/__snapshots__/**', ], }, + eslint.configs.recommended, + nodePlugin.configs['flat/recommended-module'], + eslintPluginUnicorn.configs['flat/recommended'], { languageOptions: { globals: { ...globals.nodeBuiltin, }, }, - plugins: { - unicorn: eslintPluginUnicorn, - }, rules: { - 'unicorn/no-abusive-eslint-disable': 'error', - 'unicorn/prefer-module': 'error', - 'unicorn/prefer-node-protocol': 'error', + 'unicorn/consistent-function-scoping': 'off', + 'unicorn/filename-case': 'off', + 'unicorn/no-array-callback-reference': 'off', + 'unicorn/no-array-for-each': 'off', + 'unicorn/no-array-reduce': 'off', + 'unicorn/no-null': 'off', + 'unicorn/no-process-exit': 'off', + 'unicorn/prefer-event-target': 'off', + 'unicorn/prefer-top-level-await': 'off', + 'unicorn/prevent-abbreviations': 'off', }, }, - eslint.configs.recommended, - nodePlugin.configs['flat/recommended-module'], { files: ['**/*.mts', '**/*.ts'], extends: [...tseslint.configs.recommended], diff --git a/packages/checkbox/src/index.mts b/packages/checkbox/src/index.mts index d6539d250..4e9be2a70 100644 --- a/packages/checkbox/src/index.mts +++ b/packages/checkbox/src/index.mts @@ -247,4 +247,4 @@ export default createPrompt( }, ); -export { Separator }; +export { Separator } from '@inquirer/core'; diff --git a/packages/core/src/lib/create-prompt.mts b/packages/core/src/lib/create-prompt.mts index 8f530f969..ed86e5570 100644 --- a/packages/core/src/lib/create-prompt.mts +++ b/packages/core/src/lib/create-prompt.mts @@ -47,8 +47,8 @@ export function createPrompt(view: ViewFunction) { store.hooksCleanup.forEach((cleanFn) => { cleanFn?.(); }); - } catch (err) { - reject(err); + } catch (error) { + reject(error); } if (context?.clearPromptOnDone) { @@ -89,9 +89,9 @@ export function createPrompt(view: ViewFunction) { screen.render(content, bottomContent); effectScheduler.run(); - } catch (err) { + } catch (error) { onExit(); - reject(err); + reject(error); } } diff --git a/packages/demo/demos/input.mjs b/packages/demo/demos/input.mjs index f9cbd4e41..7e720de2f 100644 --- a/packages/demo/demos/input.mjs +++ b/packages/demo/demos/input.mjs @@ -2,7 +2,7 @@ import * as url from 'node:url'; import chalk from 'chalk'; import { input } from '@inquirer/prompts'; -const hexRegEx = /([0-9]|[a-f])/gim; +const hexRegEx = /(\d|[a-f])/gim; const isHex = (value) => (value.match(hexRegEx) || []).length === value.length && (value.length === 3 || value.length === 6); diff --git a/packages/editor/editor.test.mts b/packages/editor/editor.test.mts index 38088ad04..c39c0ebb5 100644 --- a/packages/editor/editor.test.mts +++ b/packages/editor/editor.test.mts @@ -75,12 +75,15 @@ describe('editor prompt', () => { message: 'Add a description', validate(value) { switch (value) { - case '1': + case '1': { return true; - case '2': + } + case '2': { return '"2" is not an allowed value'; - default: + } + default: { return false; + } } }, }); diff --git a/packages/inquirer/examples/long-list.js b/packages/inquirer/examples/long-list.js index ff92cc0c6..663028fe9 100644 --- a/packages/inquirer/examples/long-list.js +++ b/packages/inquirer/examples/long-list.js @@ -5,20 +5,22 @@ import inquirer from '../lib/inquirer.js'; const choices = Array.apply(0, Array.from({ length: 26 })).map((x, y) => - String.fromCharCode(y + 65), + String.fromCodePoint(y + 65), +); +choices.push( + 'Multiline option 1\n super cool feature \n more lines', + 'Multiline option 2\n super cool feature \n more lines', + 'Multiline option 3\n super cool feature \n more lines', + 'Multiline option 4\n super cool feature \n more lines', + 'Multiline option 5\n super cool feature \n more lines', + new inquirer.Separator(), + 'Multiline option \n super cool feature', + { + name: 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.', + value: 'foo', + short: 'The long option', + }, ); -choices.push('Multiline option 1\n super cool feature \n more lines'); -choices.push('Multiline option 2\n super cool feature \n more lines'); -choices.push('Multiline option 3\n super cool feature \n more lines'); -choices.push('Multiline option 4\n super cool feature \n more lines'); -choices.push('Multiline option 5\n super cool feature \n more lines'); -choices.push(new inquirer.Separator()); -choices.push('Multiline option \n super cool feature'); -choices.push({ - name: 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.', - value: 'foo', - short: 'The long option', -}); inquirer .prompt([ diff --git a/packages/inquirer/examples/regex-validate-input.js b/packages/inquirer/examples/regex-validate-input.js index 90bce8338..d646906dc 100644 --- a/packages/inquirer/examples/regex-validate-input.js +++ b/packages/inquirer/examples/regex-validate-input.js @@ -14,7 +14,7 @@ const questions = [ return true; } - throw Error('Please provide a valid API key secret.'); + throw new Error('Please provide a valid API key secret.'); }, }, ]; diff --git a/packages/inquirer/examples/rx-observable-array.js b/packages/inquirer/examples/rx-observable-array.js index 2f6c964e1..f4df67dad 100644 --- a/packages/inquirer/examples/rx-observable-array.js +++ b/packages/inquirer/examples/rx-observable-array.js @@ -36,10 +36,10 @@ const observable = from(questions); inquirer.prompt(observable).ui.process.subscribe( (ans) => { - console.log('Answer is: ', ans); + console.log('Answer is:', ans); }, (err) => { - console.log('Error: ', err); + console.log('Error:', err); }, () => { console.log('Completed'); diff --git a/packages/inquirer/lib/prompts/base.js b/packages/inquirer/lib/prompts/base.js index 19dada43f..3ba95ffff 100644 --- a/packages/inquirer/lib/prompts/base.js +++ b/packages/inquirer/lib/prompts/base.js @@ -104,10 +104,10 @@ export default class Prompt { this.startSpinner(filteredValue, this.opt.validatingText); return validate(filteredValue, this.answers).then( (isValid) => ({ isValid, value: filteredValue }), - (err) => ({ isValid: err, value: filteredValue }), + (error_) => ({ isValid: error_, value: filteredValue }), ); }, - (err) => ({ isValid: err }), + (error_) => ({ isValid: error_ }), ); }), share(), diff --git a/packages/inquirer/lib/prompts/list.js b/packages/inquirer/lib/prompts/list.js index 3772b3785..eebe91d31 100644 --- a/packages/inquirer/lib/prompts/list.js +++ b/packages/inquirer/lib/prompts/list.js @@ -60,7 +60,7 @@ export default class ListPrompt extends Base { take(1), map(this.getCurrentValue.bind(this)), flatMap((value) => - runAsync(this.opt.filter)(value, this.answers).catch((err) => err), + runAsync(this.opt.filter)(value, this.answers).catch((error) => error), ), ) .forEach(this.onSubmit.bind(this)); diff --git a/packages/inquirer/lib/utils/events.js b/packages/inquirer/lib/utils/events.js index 36b69b0d0..84af5d660 100644 --- a/packages/inquirer/lib/utils/events.js +++ b/packages/inquirer/lib/utils/events.js @@ -4,7 +4,7 @@ function normalizeKeypressEvents(value, key) { return { value, key: key || {} }; } -export default function (rl) { +export default function observe(rl) { const keypress = fromEvent(rl.input, 'keypress', normalizeKeypressEvents) .pipe(takeUntil(fromEvent(rl, 'close'))) // Ignore `enter` key. On the readline, we only care about the `line` event. diff --git a/packages/inquirer/test/helpers/readline.js b/packages/inquirer/test/helpers/readline.js index 63cd85f02..85952fccf 100644 --- a/packages/inquirer/test/helpers/readline.js +++ b/packages/inquirer/test/helpers/readline.js @@ -1,5 +1,5 @@ import { EventEmitter } from 'node:events'; -import util from 'node:util'; +import { inherits } from 'node:util'; import { vi } from 'vitest'; const stub = {}; @@ -27,10 +27,10 @@ const ReadlineStub = function () { this.line = ''; this.input = new EventEmitter(); - EventEmitter.apply(this, arguments); + Reflect.apply(EventEmitter, this, arguments); }; -util.inherits(ReadlineStub, EventEmitter); +inherits(ReadlineStub, EventEmitter); Object.assign(ReadlineStub.prototype, stub); export default ReadlineStub; diff --git a/packages/inquirer/test/specs/inquirer.test.js b/packages/inquirer/test/specs/inquirer.test.js index 13fc688ba..0584ab171 100644 --- a/packages/inquirer/test/specs/inquirer.test.js +++ b/packages/inquirer/test/specs/inquirer.test.js @@ -14,6 +14,10 @@ import { autosubmit } from '../helpers/events.js'; const ostype = os.type(); +function throwFunc(step) { + throw new Error(`askAnswered Error ${step}`); +} + describe('inquirer.prompt', () => { let prompt; @@ -687,9 +691,6 @@ describe('inquirer.prompt', () => { }); it('should not run prompt if answer exists for question', async () => { - const throwFunc = function (step) { - throw new Error(`askAnswered Error ${step}`); - }; const prompts = [ { type: 'input', @@ -712,9 +713,6 @@ describe('inquirer.prompt', () => { }); it('should not run prompt if nested answer exists for question', async () => { - const throwFunc = function (step) { - throw new Error(`askAnswered Error ${step}`); - }; const prompts = [ { type: 'input', diff --git a/packages/inquirer/test/specs/prompts/checkbox.test.js b/packages/inquirer/test/specs/prompts/checkbox.test.js index 0dae6e9cb..9fb06d2ea 100644 --- a/packages/inquirer/test/specs/prompts/checkbox.test.js +++ b/packages/inquirer/test/specs/prompts/checkbox.test.js @@ -222,14 +222,16 @@ describe('`checkbox` prompt', () => { describe('with disabled choices', () => { beforeEach(() => { - fixture.choices.push({ - name: 'dis1', - disabled: true, - }); - fixture.choices.push({ - name: 'dis2', - disabled: 'uh oh', - }); + fixture.choices.push( + { + name: 'dis1', + disabled: true, + }, + { + name: 'dis2', + disabled: 'uh oh', + }, + ); checkbox = new Checkbox(fixture, rl); }); diff --git a/packages/inquirer/test/specs/prompts/confirm.test.js b/packages/inquirer/test/specs/prompts/confirm.test.js index 4a0abb1a5..ab84dc69c 100644 --- a/packages/inquirer/test/specs/prompts/confirm.test.js +++ b/packages/inquirer/test/specs/prompts/confirm.test.js @@ -140,7 +140,7 @@ describe('`confirm` prompt', () => { expect(answer).toEqual('👍'); done(); }) - .catch((err) => console.log(err)); + .catch((error) => console.log(error)); rl.emit('line', 'y'); })); diff --git a/packages/inquirer/test/specs/prompts/number.test.js b/packages/inquirer/test/specs/prompts/number.test.js index 5d67f6354..d6634836a 100644 --- a/packages/inquirer/test/specs/prompts/number.test.js +++ b/packages/inquirer/test/specs/prompts/number.test.js @@ -90,7 +90,7 @@ describe('`number` prompt', () => { it('should parse a float with no digits after the decimal', () => new Promise((done) => { number.run().then((answer) => { - expect(answer).toBeCloseTo(1234.0, ACCEPTABLE_ERROR); + expect(answer).toBeCloseTo(1234, ACCEPTABLE_ERROR); done(); }); diff --git a/packages/prompts/src/index.mts b/packages/prompts/src/index.mts index d6872324c..3bbd85c8c 100644 --- a/packages/prompts/src/index.mts +++ b/packages/prompts/src/index.mts @@ -1,10 +1,9 @@ -import checkbox, { Separator } from '@inquirer/checkbox'; -import confirm from '@inquirer/confirm'; -import editor from '@inquirer/editor'; -import expand from '@inquirer/expand'; -import input from '@inquirer/input'; -import password from '@inquirer/password'; -import rawlist from '@inquirer/rawlist'; -import select from '@inquirer/select'; +export { default as checkbox, Separator } from '@inquirer/checkbox'; +export { default as editor } from '@inquirer/editor'; +export { default as confirm } from '@inquirer/confirm'; +export { default as input } from '@inquirer/input'; +export { default as expand } from '@inquirer/expand'; +export { default as rawlist } from '@inquirer/rawlist'; +export { default as password } from '@inquirer/password'; -export { checkbox, confirm, editor, expand, input, password, rawlist, select, Separator }; +export { default as select } from '@inquirer/select'; diff --git a/packages/rawlist/src/index.mts b/packages/rawlist/src/index.mts index 520ae23d5..cfab4a62a 100644 --- a/packages/rawlist/src/index.mts +++ b/packages/rawlist/src/index.mts @@ -104,4 +104,4 @@ export default createPrompt( }, ); -export { Separator }; +export { Separator } from '@inquirer/core'; diff --git a/packages/select/src/index.mts b/packages/select/src/index.mts index 443a2a1ec..fa09d3619 100644 --- a/packages/select/src/index.mts +++ b/packages/select/src/index.mts @@ -198,4 +198,4 @@ export default createPrompt( }, ); -export { Separator }; +export { Separator } from '@inquirer/core'; diff --git a/tools/fix-ext.mjs b/tools/fix-ext.mjs index d6304610e..ba75ff9d8 100644 --- a/tools/fix-ext.mjs +++ b/tools/fix-ext.mjs @@ -7,10 +7,10 @@ import { globby } from 'globby'; const mjsFiles = await globby(['dist/cjs/**/*.mjs', '!**/node_modules']); mjsFiles.forEach(async (pathname) => { // 1. Rename imports - const fileContent = await fs.readFile(pathname, 'utf-8'); + const fileContent = await fs.readFile(pathname, 'utf8'); await fs.writeFile( pathname, - fileContent.replaceAll(/require\(['"]([^'"]*)\.mjs['"]\)/g, "require('$1.js')"), + fileContent.replaceAll(/require\(["']([^"']*)\.mjs["']\)/g, "require('$1.js')"), ); // 2. Rename files @@ -29,7 +29,7 @@ mjsFiles.forEach(async (pathname) => { const dmtsFiles = await globby(['dist/cjs/**/*.d.mts', '!**/node_modules']); dmtsFiles.forEach(async (pathname) => { // 1. Rename imports from `.mjs` to `.js` - const fileContent = await fs.readFile(pathname, 'utf-8'); + const fileContent = await fs.readFile(pathname, 'utf8'); await fs.writeFile( pathname, fileContent.replaceAll(/from '([^']*)\.mjs'/g, "from '$1.js'"), diff --git a/tools/setup-packages.mjs b/tools/setup-packages.mjs index cf2ee95c1..15df912bd 100644 --- a/tools/setup-packages.mjs +++ b/tools/setup-packages.mjs @@ -4,7 +4,7 @@ import { globby } from 'globby'; import prettier from 'prettier'; function readFile(filepath) { - return fs.readFile(filepath, 'utf-8'); + return fs.readFile(filepath, 'utf8'); } function readJSONFile(filepath) { @@ -44,7 +44,7 @@ paths.forEach(async (pkgPath) => { pkg.author = rootPkg.author; pkg.license = rootPkg.license; pkg.repository = rootPkg.repository; - pkg.keywords = Array.from(new Set([...rootPkg.keywords, ...(pkg.keywords ?? [])])); + pkg.keywords = [...new Set([...rootPkg.keywords, ...(pkg.keywords ?? [])])]; if (hasReadme) { const repoPath = dir.split('/').slice(-2).join('/'); diff --git a/vitest.config.ts b/vitest.config.ts index e68db6984..c5a70787f 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -22,7 +22,7 @@ export default defineConfig({ { // Resolve @inquirer/* packages to their source code find: /@inquirer\/(.*)/, - replacement: fileURLToPath(new URL('./packages/$1/src', import.meta.url)), + replacement: fileURLToPath(new URL('packages/$1/src', import.meta.url)), }, ], },