]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/docs/src/rules/guard-for-in.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / guard-for-in.md
index c6cdbab24ad12e86eea7ec8301b2211899baa97a..0cdecb43a531d7513126368a2f26b33be1613942 100644 (file)
@@ -1,8 +1,8 @@
 ---
 title: guard-for-in
-layout: doc
 rule_type: suggestion
 related_rules:
+- prefer-object-has-own
 - no-prototype-builtins
 further_reading:
 - https://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/
@@ -18,6 +18,10 @@ for (key in foo) {
 }
 ```
 
+For codebases that do not support ES2022, `Object.prototype.hasOwnProperty.call(foo, key)` can be used as a check that the property is not inherited.
+
+For codebases that do support ES2022, `Object.hasOwn(foo, key)` can be used as a shorter alternative; see [prefer-object-has-own](prefer-object-has-own).
+
 Note that simply checking `foo.hasOwnProperty(key)` is likely to cause an error in some cases; see [no-prototype-builtins](no-prototype-builtins).
 
 ## Rule Details
@@ -45,6 +49,12 @@ Examples of **correct** code for this rule:
 ```js
 /*eslint guard-for-in: "error"*/
 
+for (key in foo) {
+    if (Object.hasOwn(foo, key)) {
+        doSomething(key);
+    }
+}
+
 for (key in foo) {
     if (Object.prototype.hasOwnProperty.call(foo, key)) {
         doSomething(key);