Skip to content

Commit

Permalink
Chore(all): Stricter linting rules from unicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Apr 25, 2024
1 parent b2f13eb commit ec60ab0
Show file tree
Hide file tree
Showing 26 changed files with 51 additions and 62 deletions.
10 changes: 9 additions & 1 deletion .eslintrc.json
Expand Up @@ -50,11 +50,19 @@
"import/order": "error",
"prettier/prettier": "error",
"unicorn/better-regex": "error",
"unicorn/explicit-length-check": "error",
"unicorn/no-abusive-eslint-disable": "error",
"unicorn/no-new-array": "error",
"unicorn/numeric-separators-style": "error",
"unicorn/prefer-at": "error",
"unicorn/prefer-includes": "error",
"unicorn/prefer-module": "error",
"unicorn/prefer-node-protocol": "error",
"unicorn/prefer-number-properties": "error",
"unicorn/prefer-regexp-test": "error",
"unicorn/prefer-string-replace-all": "error"
"unicorn/prefer-spread": "error",
"unicorn/prefer-string-replace-all": "error",
"unicorn/prefer-ternary": "error"
},
"overrides": [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/Separator.mts
Expand Up @@ -7,7 +7,7 @@ import figures from '@inquirer/figures';
*/

export class Separator {
readonly separator = chalk.dim(new Array(15).join(figures.line));
readonly separator = chalk.dim(Array.from({ length: 15 }).join(figures.line));
readonly type = 'separator';

constructor(separator?: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/pagination/lines.mts
Expand Up @@ -20,7 +20,7 @@ function split(content: string, width: number) {
function rotate<T>(count: number, items: readonly T[]): readonly T[] {
const max = items.length;
const offset = ((count % max) + max) % max;
return items.slice(offset).concat(items.slice(0, offset));
return [...items.slice(offset), ...items.slice(0, offset)];
}

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ export function lines<T>({
const renderItemAt = (index: number) => split(renderItem(layoutsInPage[index]!), width);

// Create a blank array of lines for the page
const pageBuffer = new Array(pageSize);
const pageBuffer = Array.from({ length: pageSize });

// Render the active item to decide the position
const activeItem = renderItemAt(requested).slice(0, pageSize);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/pagination/lines.test.mts
Expand Up @@ -19,7 +19,7 @@ describe('lines(...)', () => {
isActive: boolean;
index: number;
}): string =>
new Array(itemHeight)
Array.from({ length: itemHeight })
.fill(0)
.map((_, i) => {
if (i === 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/screen-manager.mts
Expand Up @@ -30,7 +30,7 @@ export default class ScreenManager {
// rl.line (mainly because of the password prompt), so just rely on it's
// length.
let prompt = rawPromptLine;
if (this.rl.line.length) {
if (this.rl.line.length > 0) {
prompt = prompt.slice(0, -this.rl.line.length);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/examples/checkbox.js
Expand Up @@ -55,7 +55,7 @@ inquirer
},
],
validate(answer) {
if (answer.length < 1) {
if (answer.length === 0) {
return 'You must choose at least one topping.';
}

Expand Down
4 changes: 3 additions & 1 deletion packages/inquirer/examples/long-list.js
Expand Up @@ -4,7 +4,9 @@

import inquirer from '../lib/inquirer.js';

const choices = Array.apply(0, new Array(26)).map((x, y) => String.fromCharCode(y + 65));
const choices = Array.apply(0, Array.from({ length: 26 })).map((x, y) =>
String.fromCharCode(y + 65),
);
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');
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/examples/pizza.js
Expand Up @@ -44,7 +44,7 @@ const questions = [
name: 'quantity',
message: 'How many do you need?',
validate(value) {
const valid = !isNaN(parseFloat(value));
const valid = !Number.isNaN(Number.parseFloat(value));
return valid || 'Please enter a number';
},
filter: Number,
Expand Down
7 changes: 2 additions & 5 deletions packages/inquirer/lib/objects/choice.js
Expand Up @@ -26,10 +26,7 @@ export default class Choice {
});
}

if (typeof val.disabled === 'function') {
this.disabled = val.disabled(answers);
} else {
this.disabled = val.disabled;
}
this.disabled =
typeof val.disabled === 'function' ? val.disabled(answers) : val.disabled;
}
}
2 changes: 1 addition & 1 deletion packages/inquirer/lib/objects/separator.js
Expand Up @@ -11,7 +11,7 @@ import figures from '@inquirer/figures';
export default class Separator {
constructor(line) {
this.type = 'separator';
this.line = chalk.dim(line || new Array(15).join(figures.line));
this.line = chalk.dim(line || Array.from({ length: 15 }).join(figures.line));
}

/**
Expand Down
9 changes: 4 additions & 5 deletions packages/inquirer/lib/prompts/base.js
Expand Up @@ -166,11 +166,10 @@ export default class Prompt {
this.status !== 'answered'
) {
// If default password is supplied, hide it
if (this.opt.type === 'password') {
message += chalk.italic.dim('[hidden] ');
} else {
message += chalk.dim('(' + this.opt.default + ') ');
}
message +=
this.opt.type === 'password'
? chalk.italic.dim('[hidden] ')
: chalk.dim('(' + this.opt.default + ') ');
}

return message;
Expand Down
7 changes: 2 additions & 5 deletions packages/inquirer/lib/prompts/checkbox.js
Expand Up @@ -248,11 +248,8 @@ function renderChoices(choices, pointer) {
})`;
} else {
const line = getCheckbox(choice.checked) + ' ' + choice.name;
if (i - separatorOffset === pointer) {
output += chalk.cyan(figures.pointer + line);
} else {
output += ' ' + line;
}
output +=
i - separatorOffset === pointer ? chalk.cyan(figures.pointer + line) : ' ' + line;
}

output += '\n';
Expand Down
9 changes: 4 additions & 5 deletions packages/inquirer/lib/prompts/editor.js
Expand Up @@ -54,11 +54,10 @@ export default class EditorPrompt extends Base {
let bottomContent = '';
let message = this.getQuestion();

if (this.status === 'answered') {
message += chalk.dim('Received');
} else {
message += chalk.dim('Press <enter> to launch your preferred editor.');
}
message +=
this.status === 'answered'
? chalk.dim('Received')
: chalk.dim('Press <enter> to launch your preferred editor.');

if (error) {
bottomContent = chalk.red('>> ') + error;
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/lib/prompts/expand.js
Expand Up @@ -209,7 +209,7 @@ export default class ExpandPrompt extends Base {
);
}

if (errors.length) {
if (errors.length > 0) {
throw new Error(
'Duplicate key error: `key` param must be unique. Duplicates: ' +
[...new Set(errors)].join(','),
Expand Down
6 changes: 1 addition & 5 deletions packages/inquirer/lib/prompts/input.js
Expand Up @@ -47,11 +47,7 @@ export default class InputPrompt extends Base {
const { transformer } = this.opt;
const isFinal = this.status === 'answered';

if (isFinal) {
appendContent = this.answer;
} else {
appendContent = this.rl.line;
}
appendContent = isFinal ? this.answer : this.rl.line;

if (transformer) {
message += transformer(appendContent, this.answers, { isFinal });
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/lib/prompts/number.js
Expand Up @@ -21,6 +21,6 @@ export default class NumberPrompt extends Input {
}

// If the input was invalid return the default value.
return this.opt.default == null ? NaN : this.opt.default;
return this.opt.default == null ? Number.NaN : this.opt.default;
}
}
11 changes: 5 additions & 6 deletions packages/inquirer/lib/prompts/password.js
Expand Up @@ -14,7 +14,7 @@ function mask(input, maskChar) {
return '';
}

return new Array(input.length + 1).join(maskChar);
return Array.from({ length: input.length + 1 }).join(maskChar);
}

export default class PasswordPrompt extends Base {
Expand Down Expand Up @@ -55,11 +55,10 @@ export default class PasswordPrompt extends Base {
let message = this.getQuestion();
let bottomContent = '';

if (this.status === 'answered') {
message += this.getMaskedValue(this.answer);
} else {
message += this.getMaskedValue(this.rl.line || '');
}
message +=
this.status === 'answered'
? this.getMaskedValue(this.answer)
: this.getMaskedValue(this.rl.line || '');

if (error) {
bottomContent = '\n' + chalk.red('>> ') + error;
Expand Down
10 changes: 3 additions & 7 deletions packages/inquirer/lib/prompts/rawlist.js
Expand Up @@ -150,17 +150,13 @@ export default class RawListPrompt extends Base {
let index;

if (this.lastKey === 'arrow') {
index = this.hiddenLine.length ? Number(this.hiddenLine) - 1 : 0;
index = this.hiddenLine.length > 0 ? Number(this.hiddenLine) - 1 : 0;
} else {
index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
index = this.rl.line.length > 0 ? Number(this.rl.line) - 1 : 0;
}
this.lastKey = '';

if (this.opt.choices.getChoice(index)) {
this.selected = index;
} else {
this.selected = undefined;
}
this.selected = this.opt.choices.getChoice(index) ? index : undefined;
this.render();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/lib/ui/bottom-bar.js
Expand Up @@ -80,7 +80,7 @@ export default class BottomBar extends Base {
this.height = msgLines.length;

// Write message to screen and setPrompt to control backspace
this.rl.setPrompt(msgLines[msgLines.length - 1]);
this.rl.setPrompt(msgLines.at(-1));

if (this.rl.output.rows === 0 && this.rl.output.columns === 0) {
/* When it's a tty through serial port there's no terminal info and the render will malfunction,
Expand Down
6 changes: 1 addition & 5 deletions packages/inquirer/lib/ui/prompt.js
Expand Up @@ -23,11 +23,7 @@ export default class PromptUI extends Base {

run(questions, answers) {
// Keep global reference to the answers
if (_.isPlainObject(answers)) {
this.answers = { ...answers };
} else {
this.answers = {};
}
this.answers = _.isPlainObject(answers) ? { ...answers } : {};

// Make sure questions is an array.
if (_.isPlainObject(questions)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/lib/utils/events.js
Expand Up @@ -31,7 +31,7 @@ export default function (rl) {
),

numberKey: keypress.pipe(
filter((e) => e.value && '123456789'.indexOf(e.value) >= 0),
filter((e) => e.value && '123456789'.includes(e.value)),
map((e) => Number(e.value)),
share(),
),
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/lib/utils/screen-manager.js
Expand Up @@ -67,7 +67,7 @@ export default class ScreenManager {
// rl.line (mainly because of the password prompt), so just rely on it's
// length.
let prompt = rawPromptLine;
if (this.rl.line.length) {
if (this.rl.line.length > 0) {
prompt = prompt.slice(0, -this.rl.line.length);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/test/specs/prompts/input.js
Expand Up @@ -36,7 +36,7 @@ describe('`input` prompt', () => {

it('should apply the provided transform to the value', function (done) {
this.fixture.transformer = function (value) {
return value.split('').reverse().join('');
return [...value].reverse().join('');
};

const prompt = new Input(this.fixture, this.rl);
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/test/specs/prompts/number.test.js
Expand Up @@ -80,7 +80,7 @@ describe('`number` prompt', () => {
it('should parse a float with no digits before the decimal', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toBeCloseTo(0.01264, ACCEPTABLE_ERROR);
expect(answer).toBeCloseTo(0.012_64, ACCEPTABLE_ERROR);
done();
});

Expand Down
2 changes: 1 addition & 1 deletion packages/rawlist/src/index.mts
Expand Up @@ -44,7 +44,7 @@ export default createPrompt(
if (isEnterKey(key)) {
let selectedChoice;
if (numberRegex.test(value)) {
const answer = parseInt(value, 10) - 1;
const answer = Number.parseInt(value, 10) - 1;
selectedChoice = choices.filter(isSelectableChoice)[answer];
} else {
const answer = value.toLowerCase();
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/src/index.mts
Expand Up @@ -33,7 +33,7 @@ class BufferedStream extends Stream.Writable {

getLastChunk({ raw }: { raw?: boolean }): string {
const chunks = raw ? this.#_rawChunks : this.#_chunks;
const lastChunk = chunks[chunks.length - 1];
const lastChunk = chunks.at(-1);
return lastChunk ?? '';
}

Expand Down

0 comments on commit ec60ab0

Please sign in to comment.