Skip to content

Commit

Permalink
Chore: eslint unicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Apr 25, 2024
1 parent e37e664 commit 362e27c
Show file tree
Hide file tree
Showing 22 changed files with 82 additions and 73 deletions.
21 changes: 13 additions & 8 deletions eslint.config.js
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion packages/checkbox/src/index.mts
Expand Up @@ -247,4 +247,4 @@ export default createPrompt(
},
);

export { Separator };
export { Separator } from '@inquirer/core';
8 changes: 4 additions & 4 deletions packages/core/src/lib/create-prompt.mts
Expand Up @@ -47,8 +47,8 @@ export function createPrompt<Value, Config>(view: ViewFunction<Value, Config>) {
store.hooksCleanup.forEach((cleanFn) => {
cleanFn?.();
});
} catch (err) {
reject(err);
} catch (error) {
reject(error);
}

if (context?.clearPromptOnDone) {
Expand Down Expand Up @@ -89,9 +89,9 @@ export function createPrompt<Value, Config>(view: ViewFunction<Value, Config>) {
screen.render(content, bottomContent);

effectScheduler.run();
} catch (err) {
} catch (error) {
onExit();
reject(err);
reject(error);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/demo/demos/input.mjs
Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions packages/editor/editor.test.mts
Expand Up @@ -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;
}
}
},
});
Expand Down
28 changes: 15 additions & 13 deletions packages/inquirer/examples/long-list.js
Expand Up @@ -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([
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/examples/regex-validate-input.js
Expand Up @@ -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.');
},
},
];
Expand Down
4 changes: 2 additions & 2 deletions packages/inquirer/examples/rx-observable-array.js
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions packages/inquirer/lib/prompts/base.js
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/lib/prompts/list.js
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/lib/utils/events.js
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions 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 = {};
Expand Down Expand Up @@ -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;
10 changes: 4 additions & 6 deletions packages/inquirer/test/specs/inquirer.test.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down
18 changes: 10 additions & 8 deletions packages/inquirer/test/specs/prompts/checkbox.test.js
Expand Up @@ -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);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/test/specs/prompts/confirm.test.js
Expand Up @@ -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');
}));
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/test/specs/prompts/number.test.js
Expand Up @@ -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();
});

Expand Down
17 changes: 8 additions & 9 deletions 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';
2 changes: 1 addition & 1 deletion packages/rawlist/src/index.mts
Expand Up @@ -104,4 +104,4 @@ export default createPrompt(
},
);

export { Separator };
export { Separator } from '@inquirer/core';
2 changes: 1 addition & 1 deletion packages/select/src/index.mts
Expand Up @@ -198,4 +198,4 @@ export default createPrompt(
},
);

export { Separator };
export { Separator } from '@inquirer/core';
6 changes: 3 additions & 3 deletions tools/fix-ext.mjs
Expand Up @@ -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
Expand All @@ -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'"),
Expand Down
4 changes: 2 additions & 2 deletions tools/setup-packages.mjs
Expand Up @@ -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) {
Expand Down Expand Up @@ -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('/');
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Expand Up @@ -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)),
},
],
},
Expand Down

0 comments on commit 362e27c

Please sign in to comment.