Skip to content

Commit d54c275

Browse files
committed
docs(linter): Improve rule docs for 27 unicorn rules (#19903)
![jim carrey typing](https://github.com/user-attachments/assets/dd18d89f-7aa3-42b6-b004-65e0d44cb6b1) Ok I lied, it's 26 unicorn rules and 1 vue rule. Sorry. No AI here :) This PR generally improves the documentation for all of these rules to provide more examples, improved explanations for the rules' purposes, and so on. It also adds MDN links where relevant via new diagnostic notes. (note: if this gets merged before the release but after the website PR is generated, make sure to regenerate the website PR 🙂)
1 parent 14fbbfc commit d54c275

35 files changed

+737
-392
lines changed

crates/oxc_linter/src/rules/unicorn/consistent_assert.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use oxc_span::Span;
1010
use crate::{AstNode, context::LintContext, rule::Rule};
1111

1212
fn consistent_assert_diagnostic(assert_identifier: &str, span: Span) -> OxcDiagnostic {
13-
OxcDiagnostic::warn("Inconsistent assert usage")
14-
.with_help(format!("Prefer {assert_identifier}.ok(...) over {assert_identifier}(...)"))
13+
OxcDiagnostic::warn("Inconsistent assert usage.")
14+
.with_help(format!("Prefer `{assert_identifier}.ok(...)` over `{assert_identifier}(...)`."))
1515
.with_label(span)
1616
}
1717

@@ -25,7 +25,11 @@ declare_oxc_lint!(
2525
///
2626
/// ### Why is this bad?
2727
///
28-
/// Inconsistent usage of the `assert` module can lead to confusion and errors.
28+
/// Inconsistent usage of the `assert` module can make code
29+
/// harder to follow and understand.
30+
///
31+
/// `assert.ok(...)` is preferred as it makes the intent of
32+
/// the assertion clearer.
2933
///
3034
/// ### Examples
3135
///

crates/oxc_linter/src/rules/unicorn/explicit_length_check.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ pub struct ExplicitLengthCheck {
5858
declare_oxc_lint!(
5959
/// ### What it does
6060
///
61-
/// Enforce explicitly comparing the length or size property of a value.
61+
/// Enforce explicitly comparing the `length` or `size` property of a value.
6262
///
6363
/// ### Why is this bad?
6464
///
65+
/// Using the explicit `length` or `size` properties can help make code clearer
66+
/// and easier to understand, as it avoids relying on implicit truthy/falsy
67+
/// evaluations.
68+
///
6569
/// ### Examples
6670
///
6771
/// Examples of **incorrect** code for this rule:
@@ -80,6 +84,10 @@ declare_oxc_lint!(
8084
/// Examples of **correct** code for this rule:
8185
/// ```javascript
8286
/// const isEmpty = foo.length === 0;
87+
///
88+
/// if (foo.length > 0 || bar.length > 0) {}
89+
///
90+
/// const unicorn = foo.length > 0 ? 1 : 2;
8391
/// ```
8492
ExplicitLengthCheck,
8593
unicorn,

crates/oxc_linter/src/rules/unicorn/no_accessor_recursion.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,43 @@ pub struct NoAccessorRecursion;
2929
declare_oxc_lint!(
3030
/// ### What it does
3131
///
32-
/// Disallow recursive access to this within getters and setters
32+
/// Disallow recursive access to `this` within getters and setters.
3333
///
3434
/// ### Why is this bad?
3535
///
36-
/// This rule prevents recursive access to this within getter and setter methods in objects and classes,
37-
/// avoiding infinite recursion and stack overflow errors.
36+
/// This rule prevents recursive access to `this` within getter and
37+
/// setter methods in objects and classes, avoiding infinite recursion
38+
/// and stack overflow errors.
3839
///
3940
/// ### Examples
4041
///
4142
/// Examples of **incorrect** code for this rule:
4243
/// ```js
4344
/// const foo = {
44-
/// get bar() {
45-
/// return this.bar;
46-
/// }
45+
/// get bar() {
46+
/// return this.bar;
47+
/// }
48+
/// };
49+
///
50+
/// const baz = {
51+
/// set bar(value) {
52+
/// this.bar = value;
53+
/// }
4754
/// };
4855
/// ```
4956
///
5057
/// Examples of **correct** code for this rule:
5158
/// ```js
5259
/// const foo = {
53-
/// get bar() {
54-
/// return this.baz;
55-
/// }
60+
/// get bar() {
61+
/// return this.qux;
62+
/// }
63+
/// };
64+
///
65+
/// const baz = {
66+
/// set bar(value) {
67+
/// this._bar = value;
68+
/// }
5669
/// };
5770
/// ```
5871
NoAccessorRecursion,

crates/oxc_linter/src/rules/unicorn/no_console_spaces.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ declare_oxc_lint!(
3030
///
3131
/// ### Why is this bad?
3232
///
33-
/// The `console.log()` method and similar methods join the parameters with a space so adding a leading/trailing space to a parameter, results in two spaces being added.
33+
/// The `console.log()` method and similar methods join the parameters
34+
/// with a space so adding a leading/trailing space to a parameter,
35+
/// results in two spaces being added.
3436
///
3537
/// ### Examples
3638
///

crates/oxc_linter/src/rules/unicorn/no_document_cookie.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use crate::{
1212
};
1313

1414
fn no_document_cookie_diagnostic(span: Span) -> OxcDiagnostic {
15-
OxcDiagnostic::warn("Do not use `document.cookie` directly")
16-
.with_help("Use the Cookie Store API or a cookie library instead")
15+
OxcDiagnostic::warn("Do not use `document.cookie` directly.")
16+
.with_help("Use the Cookie Store API or a cookie library instead.")
17+
.with_note("https://developer.mozilla.org/en-US/docs/Web/API/Cookie_Store_API")
1718
.with_label(span)
1819
}
1920

@@ -23,7 +24,7 @@ pub struct NoDocumentCookie;
2324
declare_oxc_lint!(
2425
/// ### What it does
2526
///
26-
/// Disallow direct use of
27+
/// Disallows direct use of
2728
/// [`document.cookie`](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie).
2829
///
2930
/// ### Why is this bad?
@@ -33,7 +34,7 @@ declare_oxc_lint!(
3334
/// directly as it's easy to get the string wrong. Instead, you should use
3435
/// the [Cookie Store
3536
/// API](https://developer.mozilla.org/en-US/docs/Web/API/Cookie_Store_API)
36-
/// or a [cookie library](https://www.npmjs.com/search?q=cookie).
37+
/// or a [cookie library](https://npmx.dev/search?q=cookie).
3738
///
3839
/// ### Examples
3940
///

crates/oxc_linter/src/rules/unicorn/no_hex_escape.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ pub struct NoHexEscape;
2222
declare_oxc_lint!(
2323
/// ### What it does
2424
///
25-
/// Enforces a convention of using [Unicode escapes](https://mathiasbynens.be/notes/javascript-escapes#unicode) instead of [hexadecimal escapes](https://mathiasbynens.be/notes/javascript-escapes#hexadecimal) for consistency and clarity.
25+
/// Enforces a convention of using [Unicode escapes](https://mathiasbynens.be/notes/javascript-escapes#unicode)
26+
/// instead of [hexadecimal escapes](https://mathiasbynens.be/notes/javascript-escapes#hexadecimal) for
27+
/// consistency and clarity.
2628
///
2729
/// ### Why is this bad?
2830
///
31+
/// Using hexadecimal escapes can be less readable and harder to understand
32+
/// when compared to Unicode escapes.
33+
///
2934
/// ### Examples
3035
///
3136
/// Examples of **incorrect** code for this rule:

crates/oxc_linter/src/rules/unicorn/no_instanceof_array.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ declare_oxc_lint!(
2222
///
2323
/// ### Why is this bad?
2424
///
25-
/// The instanceof Array check doesn't work across realms/contexts, for example, frames/windows in browsers or the vm module in Node.js.
25+
/// The `instanceof Array` check doesn't work across realms/contexts.
26+
/// For example, frames/windows in browsers or the `vm` module in Node.js.
2627
///
2728
/// ### Examples
2829
///

crates/oxc_linter/src/rules/unicorn/no_invalid_fetch_options.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ declare_oxc_lint!(
4040
///
4141
/// Examples of **incorrect** code for this rule:
4242
/// ```javascript
43-
/// const response = await fetch('/', {method: 'GET', body: 'foo=bar'});
43+
/// const response = await fetch('/', { method: 'GET', body: 'foo=bar' });
4444
///
45-
/// const request = new Request('/', {method: 'GET', body: 'foo=bar'});
45+
/// const request = new Request('/', { method: 'GET', body: 'foo=bar' });
4646
/// ```
4747
///
4848
/// Examples of **correct** code for this rule:
4949
/// ```javascript
50-
/// const response = await fetch('/', {method: 'POST', body: 'foo=bar'});
50+
/// const response = await fetch('/', { method: 'POST', body: 'foo=bar' });
5151
///
52-
/// const request = new Request('/', {method: 'POST', body: 'foo=bar'});
52+
/// const request = new Request('/', { method: 'POST', body: 'foo=bar' });
5353
/// ```
5454
NoInvalidFetchOptions,
5555
unicorn,

crates/oxc_linter/src/rules/unicorn/no_magic_array_flat_depth.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ pub struct NoMagicArrayFlatDepth;
2020
declare_oxc_lint!(
2121
/// ### What it does
2222
///
23-
/// Disallow magic numbers for `Array.prototype.flat` depth.
23+
/// Disallow magic numbers for [`Array.prototype.flat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)
24+
/// depth.
2425
///
2526
/// ### Why is this bad?
2627
///
27-
/// Magic numbers are hard to understand and maintain. When calling `Array.prototype.flat`, it is usually called with `1` or infinity. If you are using a different number, it is better to add a comment explaining the depth.
28+
/// Magic numbers are hard to understand and maintain.
29+
/// When calling `Array.prototype.flat`, it is usually called with
30+
/// `1` or `Infinity`. If you are using a different number, it is
31+
/// better to add a comment explaining the reason for the depth provided.
2832
///
2933
/// ### Examples
3034
///

crates/oxc_linter/src/rules/unicorn/no_process_exit.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,34 @@ pub struct NoProcessExit;
1717
declare_oxc_lint!(
1818
/// ### What it does
1919
///
20-
/// Disallow `process.exit()`.
20+
/// Disallow all usage of `process.exit()`.
2121
///
2222
/// ### Why is this bad?
2323
///
24-
/// Only use `process.exit()` in CLI apps. Throw an error instead.
24+
/// `process.exit()` should generally only be used in command-line utilities. In all other
25+
/// types of applications, the code should throw an error instead.
2526
///
2627
/// ### Examples
2728
///
2829
/// Examples of **incorrect** code for this rule:
2930
/// ```javascript
30-
/// if (problem) process.exit(1);
31+
/// if (problem) {
32+
/// process.exit(1);
33+
/// }
3134
/// ```
3235
///
3336
/// Examples of **correct** code for this rule:
3437
/// ```javascript
35-
/// if (problem) throw new Error("message");
38+
/// if (problem) {
39+
/// throw new Error("message");
40+
/// }
3641
/// ```
3742
///
3843
/// ```
3944
/// #!/usr/bin/env node
40-
/// if (problem) process.exit(1);
45+
/// if (problem) {
46+
/// process.exit(1);
47+
/// }
4148
/// ```
4249
NoProcessExit,
4350
unicorn,

0 commit comments

Comments
 (0)