docs(linter): add more examples for unicorn/prefer-array-some (#7411)

This commit is contained in:
Dmitry Zakharov 2024-11-22 09:06:46 +03:00 committed by GitHub
parent f4fda2dba6
commit 6730e3effe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -35,7 +35,9 @@ pub struct PreferArraySome;
declare_oxc_lint!(
/// ### What it does
///
/// Prefers using [`Array#some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) over [`Array#find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find), [`Array#findLast()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast) and a non-zero length check on the result of [`Array#filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
/// Prefers using [`Array#some()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) over [`Array#find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find), [`Array#findLast()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast) with comparing to undefined,
/// or [`Array#findIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex), [`Array#findLastIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex)
/// and a non-zero length check on the result of [`Array#filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
///
/// ### Why is this bad?
///
@ -46,17 +48,25 @@ declare_oxc_lint!(
/// Examples of **incorrect** code for this rule:
/// ```javascript
/// const foo = array.find(fn) ? bar : baz;
/// const foo = array.findLast(elem => hasRole(elem)) !== null;
/// foo.findIndex(bar) < 0;
/// foo.findIndex(element => element.bar === 1) !== -1;
/// foo.findLastIndex(element => element.bar === 1) !== -1;
/// array.filter(fn).length === 0;
/// ```
///
/// Examples of **correct** code for this rule:
/// ```javascript
/// const foo = array.some(fn) ? bar : baz;
/// foo.some(element => element.bar === 1);
/// !array.some(fn);
/// ```
PreferArraySome,
pedantic,
fix
);
/// <https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v56.0.1/docs/rules/prefer-array-some.md>
impl Rule for PreferArraySome {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {