Skip to content

Commit

Permalink
docs(linter): add more examples for unicorn/prefer-array-some (#7411)
Browse files Browse the repository at this point in the history
  • Loading branch information
pumano authored Nov 22, 2024
1 parent f4fda2d commit 6730e3e
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
///
Expand All @@ -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() {
Expand Down

0 comments on commit 6730e3e

Please sign in to comment.