Skip to content

Commit faca7a8

Browse files
authoredMar 14, 2025··
docs(linter): improve eslint-no-self-assign (#9768)
- Adds correctness examples and document options in line with #6050 - Add missing test cases
1 parent fcdd810 commit faca7a8

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed
 

‎crates/oxc_linter/src/rules/eslint/no_self_assign.rs

+66-4
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,70 @@ impl Default for NoSelfAssign {
3232
declare_oxc_lint!(
3333
/// ### What it does
3434
///
35-
/// Disallow assignments where both sides are exactly the same
35+
/// Disallow assignments where both sides are exactly the same.
3636
///
3737
/// ### Why is this bad?
3838
///
39-
/// Self assignments have no effect, so probably those are an error due to incomplete refactoring. Those indicate that what you should do is still remaining.
39+
/// Self assignments have no effect, so probably those are an error due to incomplete
40+
/// refactoring. Those indicate that what you should do is still remaining.
4041
///
41-
/// ### Example
42+
/// ### Examples
43+
///
44+
/// Examples of **incorrect** code for this rule:
4245
/// ```javascript
4346
/// foo = foo;
44-
/// [bar, baz] = [bar, qiz];
47+
///
48+
/// [a, b] = [a, b];
49+
/// [a, ...b] = [x, ...b];
50+
///
51+
/// ({a, b} = {a, x});
52+
///
53+
/// foo &&= foo;
54+
/// foo ||= foo;
55+
/// foo ??= foo;
56+
/// ```
57+
///
58+
/// ```javascript
59+
/// obj.a = obj.a;
60+
/// obj.a.b = obj.a.b;
61+
/// obj["a"] = obj["a"];
62+
/// obj[a] = obj[a];
63+
/// ```
64+
///
65+
/// Examples of **correct** code for this rule:
66+
/// ```javascript
67+
/// foo = bar;
68+
/// [a, b] = [b, a];
69+
///
70+
/// // This pattern is warned by the `no-use-before-define` rule.
71+
/// let foo = foo;
72+
///
73+
/// // The default values have an effect.
74+
/// [foo = 1] = [foo];
75+
///
76+
/// // This ignores if there is a function call.
77+
/// obj.a().b = obj.a().b;
78+
/// a().b = a().b;
79+
///
80+
/// // `&=` and `|=` have an effect on non-integers.
81+
/// foo &= foo;
82+
/// foo |= foo;
83+
/// ```
84+
///
85+
/// ### Options
86+
///
87+
/// #### props
88+
///
89+
/// `{ type: boolean, default: true }`
90+
///
91+
/// The `props` option when set to `false`, disables the checking of properties.
92+
///
93+
/// With `props` set to `false` the following are examples of correct code:
94+
/// ```javascript
95+
/// obj.a = obj.a;
96+
/// obj.a.b = obj.a.b;
97+
/// obj["a"] = obj["a"];
98+
/// obj[a] = obj[a];
4599
/// ```
46100
NoSelfAssign,
47101
eslint,
@@ -303,6 +357,7 @@ fn test() {
303357
("[a, ...b] = [0, ...b, 1]", None),
304358
("[a, b] = {a, b}", None),
305359
("({a} = a)", None),
360+
("[foo = 1] = [foo];", None),
306361
("({a = 1} = {a})", None),
307362
("({a: b} = {a})", None),
308363
("({a} = {a: b})", None),
@@ -323,11 +378,18 @@ fn test() {
323378
("a.b.c = a.b.c", Some(serde_json::json!([{ "props": false }]))),
324379
("a[b] = a[b]", Some(serde_json::json!([{ "props": false }]))),
325380
("a['b'] = a['b']", Some(serde_json::json!([{ "props": false }]))),
381+
(r#"obj[a] = obj["a"];"#, None),
326382
("a[\n 'b'\n] = a[\n 'b'\n]", Some(serde_json::json!([{ "props": false }]))),
327383
("this.x = this.y", Some(serde_json::json!([{ "props": true }]))),
328384
("this.x = this.x", Some(serde_json::json!([{ "props": false }]))),
329385
("class C { #field; foo() { this['#field'] = this.#field; } }", None),
330386
("class C { #field; foo() { this.#field = this['#field']; } }", None),
387+
(r#"obj["a" + "b"] = obj["a" + "b"];"#, None),
388+
("obj[a + b] = obj[a + b];", None),
389+
// `&=` and `|=` have an effect on non-integers.
390+
("foo |= foo;", None),
391+
("foo &= foo;", None),
392+
("let foo = foo;", None),
331393
];
332394

333395
let fail = vec![

0 commit comments

Comments
 (0)
Please sign in to comment.