Skip to content

Commit

Permalink
docs(eslint-plugin): fix ignorePrimitives examples in prefer-nullish-…
Browse files Browse the repository at this point in the history
…coalescing docs (#7650)

docs: Fix ignorePrimitives examples in prefer-nullish-coalescing docs

The examples for the `ignorePrimitives` option were flipped. Setting `ignorePrimitives` to true means that any logical OR expression with a primitive operand is ignored by the lint rule.

I also made two other edits to this docs page. The first is to change "you can safely replace" to "you may consider replacing" in the lint rule's description. The reason for this change is that this lint rule suggests changes that alter the code's behavior in subtle cases where `x || y !== x ?? y`.

There is also a small change to reword the tense in one of the sentences to help it read more smoothly.
  • Loading branch information
ide committed Sep 15, 2023
1 parent 2d66716 commit 7826910
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: 'Enforce using the nullish coalescing operator instead of logical a
The `??` nullish coalescing runtime operator allows providing a default value when dealing with `null` or `undefined`.
Because the nullish coalescing operator _only_ coalesces when the original value is `null` or `undefined`, it is much safer than relying upon logical OR operator chaining `||`, which coalesces on any _falsy_ value.

This rule reports when you can safely replace:
This rule reports when you may consider replacing:

- An `||` operator with `??`
- An `||=` operator with `??=`
Expand Down Expand Up @@ -144,28 +144,28 @@ a ?? (b && c && d);

### `ignorePrimitives`

If you would like to ignore certain primitive types that can be falsy then you may pass an object containing a boolean value for each primitive:
If you would like to ignore expressions containing operands of certain primitive types that can be falsy then you may pass an object containing a boolean value for each primitive:

- `string: true`, ignores `null` or `undefined` unions with `string` (default: false).
- `number: true`, ignores `null` or `undefined` unions with `number` (default: false).
- `bigint: true`, ignores `null` or `undefined` unions with `bigint` (default: false).
- `boolean: true`, ignores `null` or `undefined` unions with `boolean` (default: false).

Incorrect code for `ignorePrimitives: { string: true }`, and correct code for `ignorePrimitives: { string: false }`:
Incorrect code for `ignorePrimitives: { string: false }`, and correct code for `ignorePrimitives: { string: true }`:

```ts
const foo: string | undefined = 'bar';
foo || 'a string';
```

Correct code for `ignorePrimitives: { string: true }`:
Correct code for both `ignorePrimitives: { string: false }` and `ignorePrimitives: { string: true }`:

```ts
const foo: string | undefined = 'bar';
foo ?? 'a string';
```

Also, if you would like to ignore all primitives types, you can set `ignorePrimitives: true`. It would be equivalent to `ignorePrimitives: { string: true, number: true, bigint: true, boolean: true }`.
Also, if you would like to ignore all primitives types, you can set `ignorePrimitives: true`. It is equivalent to `ignorePrimitives: { string: true, number: true, bigint: true, boolean: true }`.

## When Not To Use It

Expand Down

0 comments on commit 7826910

Please sign in to comment.