eslint 8.2.0 → 8.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (304) hide show
  1. package/README.md +7 -11
  2. package/conf/globals.js +144 -0
  3. package/lib/cli.js +1 -1
  4. package/lib/config/default-config.js +11 -2
  5. package/lib/config/flat-config-array.js +2 -2
  6. package/lib/config/flat-config-helpers.js +67 -0
  7. package/lib/config/flat-config-schema.js +13 -8
  8. package/lib/config/rule-validator.js +28 -27
  9. package/lib/eslint/eslint.js +11 -3
  10. package/lib/linter/code-path-analysis/code-path-analyzer.js +6 -1
  11. package/lib/linter/code-path-analysis/code-path.js +1 -1
  12. package/lib/linter/linter.js +457 -45
  13. package/lib/options.js +6 -6
  14. package/lib/rule-tester/rule-tester.js +14 -0
  15. package/lib/rules/accessor-pairs.js +1 -0
  16. package/lib/rules/array-bracket-newline.js +1 -0
  17. package/lib/rules/array-bracket-spacing.js +1 -0
  18. package/lib/rules/array-callback-return.js +1 -0
  19. package/lib/rules/array-element-newline.js +1 -0
  20. package/lib/rules/arrow-body-style.js +1 -0
  21. package/lib/rules/arrow-parens.js +1 -0
  22. package/lib/rules/arrow-spacing.js +1 -0
  23. package/lib/rules/block-scoped-var.js +3 -0
  24. package/lib/rules/block-spacing.js +11 -3
  25. package/lib/rules/brace-style.js +7 -0
  26. package/lib/rules/callback-return.js +1 -0
  27. package/lib/rules/camelcase.js +1 -0
  28. package/lib/rules/capitalized-comments.js +1 -0
  29. package/lib/rules/class-methods-use-this.js +11 -1
  30. package/lib/rules/comma-dangle.js +1 -0
  31. package/lib/rules/comma-spacing.js +1 -0
  32. package/lib/rules/comma-style.js +1 -0
  33. package/lib/rules/complexity.js +15 -6
  34. package/lib/rules/computed-property-spacing.js +1 -0
  35. package/lib/rules/consistent-return.js +1 -0
  36. package/lib/rules/consistent-this.js +1 -0
  37. package/lib/rules/constructor-super.js +1 -0
  38. package/lib/rules/curly.js +1 -0
  39. package/lib/rules/default-case-last.js +1 -0
  40. package/lib/rules/default-case.js +1 -0
  41. package/lib/rules/default-param-last.js +1 -0
  42. package/lib/rules/dot-location.js +1 -0
  43. package/lib/rules/dot-notation.js +1 -0
  44. package/lib/rules/eol-last.js +1 -0
  45. package/lib/rules/eqeqeq.js +1 -0
  46. package/lib/rules/for-direction.js +1 -0
  47. package/lib/rules/func-call-spacing.js +1 -0
  48. package/lib/rules/func-name-matching.js +1 -0
  49. package/lib/rules/func-names.js +1 -0
  50. package/lib/rules/func-style.js +1 -0
  51. package/lib/rules/function-call-argument-newline.js +1 -0
  52. package/lib/rules/function-paren-newline.js +1 -0
  53. package/lib/rules/generator-star-spacing.js +1 -0
  54. package/lib/rules/getter-return.js +1 -0
  55. package/lib/rules/global-require.js +1 -0
  56. package/lib/rules/grouped-accessor-pairs.js +1 -0
  57. package/lib/rules/guard-for-in.js +1 -0
  58. package/lib/rules/handle-callback-err.js +1 -0
  59. package/lib/rules/id-blacklist.js +1 -0
  60. package/lib/rules/id-denylist.js +1 -0
  61. package/lib/rules/id-length.js +1 -0
  62. package/lib/rules/id-match.js +24 -0
  63. package/lib/rules/implicit-arrow-linebreak.js +1 -0
  64. package/lib/rules/indent-legacy.js +1 -0
  65. package/lib/rules/indent.js +22 -0
  66. package/lib/rules/index.js +1 -0
  67. package/lib/rules/init-declarations.js +1 -0
  68. package/lib/rules/jsx-quotes.js +1 -0
  69. package/lib/rules/key-spacing.js +1 -0
  70. package/lib/rules/keyword-spacing.js +2 -0
  71. package/lib/rules/line-comment-position.js +1 -0
  72. package/lib/rules/linebreak-style.js +1 -0
  73. package/lib/rules/lines-around-comment.js +55 -7
  74. package/lib/rules/lines-around-directive.js +1 -0
  75. package/lib/rules/lines-between-class-members.js +1 -0
  76. package/lib/rules/max-classes-per-file.js +1 -0
  77. package/lib/rules/max-depth.js +3 -0
  78. package/lib/rules/max-len.js +1 -0
  79. package/lib/rules/max-lines-per-function.js +1 -0
  80. package/lib/rules/max-lines.js +1 -0
  81. package/lib/rules/max-nested-callbacks.js +1 -0
  82. package/lib/rules/max-params.js +1 -0
  83. package/lib/rules/max-statements-per-line.js +1 -0
  84. package/lib/rules/max-statements.js +11 -0
  85. package/lib/rules/multiline-comment-style.js +1 -0
  86. package/lib/rules/multiline-ternary.js +1 -0
  87. package/lib/rules/new-cap.js +1 -0
  88. package/lib/rules/new-parens.js +1 -0
  89. package/lib/rules/newline-after-var.js +1 -0
  90. package/lib/rules/newline-before-return.js +1 -0
  91. package/lib/rules/newline-per-chained-call.js +1 -0
  92. package/lib/rules/no-alert.js +1 -0
  93. package/lib/rules/no-array-constructor.js +1 -0
  94. package/lib/rules/no-async-promise-executor.js +1 -0
  95. package/lib/rules/no-await-in-loop.js +1 -0
  96. package/lib/rules/no-bitwise.js +1 -0
  97. package/lib/rules/no-buffer-constructor.js +1 -0
  98. package/lib/rules/no-caller.js +1 -0
  99. package/lib/rules/no-case-declarations.js +1 -0
  100. package/lib/rules/no-catch-shadow.js +1 -0
  101. package/lib/rules/no-class-assign.js +1 -0
  102. package/lib/rules/no-compare-neg-zero.js +1 -0
  103. package/lib/rules/no-cond-assign.js +1 -0
  104. package/lib/rules/no-confusing-arrow.js +1 -0
  105. package/lib/rules/no-console.js +1 -0
  106. package/lib/rules/no-const-assign.js +1 -0
  107. package/lib/rules/no-constant-condition.js +4 -1
  108. package/lib/rules/no-constructor-return.js +1 -0
  109. package/lib/rules/no-continue.js +1 -0
  110. package/lib/rules/no-control-regex.js +1 -0
  111. package/lib/rules/no-debugger.js +1 -0
  112. package/lib/rules/no-delete-var.js +1 -0
  113. package/lib/rules/no-div-regex.js +1 -0
  114. package/lib/rules/no-dupe-args.js +1 -0
  115. package/lib/rules/no-dupe-class-members.js +1 -0
  116. package/lib/rules/no-dupe-else-if.js +1 -0
  117. package/lib/rules/no-dupe-keys.js +1 -0
  118. package/lib/rules/no-duplicate-case.js +1 -0
  119. package/lib/rules/no-duplicate-imports.js +1 -0
  120. package/lib/rules/no-else-return.js +1 -0
  121. package/lib/rules/no-empty-character-class.js +1 -0
  122. package/lib/rules/no-empty-function.js +1 -0
  123. package/lib/rules/no-empty-pattern.js +1 -0
  124. package/lib/rules/no-empty.js +1 -0
  125. package/lib/rules/no-eq-null.js +1 -0
  126. package/lib/rules/no-eval.js +3 -0
  127. package/lib/rules/no-ex-assign.js +1 -0
  128. package/lib/rules/no-extend-native.js +1 -0
  129. package/lib/rules/no-extra-bind.js +1 -0
  130. package/lib/rules/no-extra-boolean-cast.js +1 -0
  131. package/lib/rules/no-extra-label.js +1 -0
  132. package/lib/rules/no-extra-parens.js +1 -0
  133. package/lib/rules/no-extra-semi.js +2 -1
  134. package/lib/rules/no-fallthrough.js +1 -0
  135. package/lib/rules/no-floating-decimal.js +1 -0
  136. package/lib/rules/no-func-assign.js +1 -0
  137. package/lib/rules/no-global-assign.js +1 -0
  138. package/lib/rules/no-implicit-coercion.js +1 -0
  139. package/lib/rules/no-implicit-globals.js +1 -0
  140. package/lib/rules/no-implied-eval.js +1 -0
  141. package/lib/rules/no-import-assign.js +1 -0
  142. package/lib/rules/no-inline-comments.js +1 -0
  143. package/lib/rules/no-inner-declarations.js +27 -4
  144. package/lib/rules/no-invalid-regexp.js +1 -0
  145. package/lib/rules/no-invalid-this.js +5 -0
  146. package/lib/rules/no-irregular-whitespace.js +1 -0
  147. package/lib/rules/no-iterator.js +1 -0
  148. package/lib/rules/no-label-var.js +1 -0
  149. package/lib/rules/no-labels.js +1 -0
  150. package/lib/rules/no-lone-blocks.js +9 -2
  151. package/lib/rules/no-lonely-if.js +1 -0
  152. package/lib/rules/no-loop-func.js +1 -0
  153. package/lib/rules/no-loss-of-precision.js +1 -0
  154. package/lib/rules/no-magic-numbers.js +1 -0
  155. package/lib/rules/no-misleading-character-class.js +1 -0
  156. package/lib/rules/no-mixed-operators.js +1 -0
  157. package/lib/rules/no-mixed-requires.js +1 -0
  158. package/lib/rules/no-mixed-spaces-and-tabs.js +1 -0
  159. package/lib/rules/no-multi-assign.js +1 -0
  160. package/lib/rules/no-multi-spaces.js +1 -0
  161. package/lib/rules/no-multi-str.js +1 -0
  162. package/lib/rules/no-multiple-empty-lines.js +1 -0
  163. package/lib/rules/no-native-reassign.js +1 -0
  164. package/lib/rules/no-negated-condition.js +1 -0
  165. package/lib/rules/no-negated-in-lhs.js +1 -0
  166. package/lib/rules/no-nested-ternary.js +1 -0
  167. package/lib/rules/no-new-func.js +1 -0
  168. package/lib/rules/no-new-object.js +1 -0
  169. package/lib/rules/no-new-require.js +1 -0
  170. package/lib/rules/no-new-symbol.js +1 -0
  171. package/lib/rules/no-new-wrappers.js +1 -0
  172. package/lib/rules/no-new.js +1 -0
  173. package/lib/rules/no-nonoctal-decimal-escape.js +1 -0
  174. package/lib/rules/no-obj-calls.js +1 -0
  175. package/lib/rules/no-octal-escape.js +1 -0
  176. package/lib/rules/no-octal.js +1 -0
  177. package/lib/rules/no-param-reassign.js +1 -0
  178. package/lib/rules/no-path-concat.js +1 -0
  179. package/lib/rules/no-plusplus.js +1 -0
  180. package/lib/rules/no-process-env.js +1 -0
  181. package/lib/rules/no-process-exit.js +1 -0
  182. package/lib/rules/no-promise-executor-return.js +1 -0
  183. package/lib/rules/no-proto.js +1 -0
  184. package/lib/rules/no-prototype-builtins.js +1 -0
  185. package/lib/rules/no-redeclare.js +3 -0
  186. package/lib/rules/no-regex-spaces.js +1 -0
  187. package/lib/rules/no-restricted-exports.js +1 -0
  188. package/lib/rules/no-restricted-globals.js +1 -0
  189. package/lib/rules/no-restricted-imports.js +1 -0
  190. package/lib/rules/no-restricted-modules.js +1 -0
  191. package/lib/rules/no-restricted-properties.js +1 -0
  192. package/lib/rules/no-restricted-syntax.js +1 -0
  193. package/lib/rules/no-return-assign.js +1 -0
  194. package/lib/rules/no-return-await.js +1 -0
  195. package/lib/rules/no-script-url.js +1 -0
  196. package/lib/rules/no-self-assign.js +1 -0
  197. package/lib/rules/no-self-compare.js +1 -0
  198. package/lib/rules/no-sequences.js +1 -0
  199. package/lib/rules/no-setter-return.js +1 -0
  200. package/lib/rules/no-shadow-restricted-names.js +1 -0
  201. package/lib/rules/no-shadow.js +1 -0
  202. package/lib/rules/no-spaced-func.js +1 -0
  203. package/lib/rules/no-sparse-arrays.js +1 -0
  204. package/lib/rules/no-sync.js +1 -0
  205. package/lib/rules/no-tabs.js +1 -0
  206. package/lib/rules/no-template-curly-in-string.js +1 -0
  207. package/lib/rules/no-ternary.js +1 -0
  208. package/lib/rules/no-this-before-super.js +1 -0
  209. package/lib/rules/no-throw-literal.js +1 -0
  210. package/lib/rules/no-trailing-spaces.js +1 -0
  211. package/lib/rules/no-undef-init.js +1 -0
  212. package/lib/rules/no-undef.js +1 -0
  213. package/lib/rules/no-undefined.js +1 -0
  214. package/lib/rules/no-underscore-dangle.js +1 -0
  215. package/lib/rules/no-unexpected-multiline.js +1 -0
  216. package/lib/rules/no-unmodified-loop-condition.js +1 -0
  217. package/lib/rules/no-unneeded-ternary.js +1 -0
  218. package/lib/rules/no-unreachable-loop.js +1 -0
  219. package/lib/rules/no-unreachable.js +1 -0
  220. package/lib/rules/no-unsafe-finally.js +1 -0
  221. package/lib/rules/no-unsafe-negation.js +1 -0
  222. package/lib/rules/no-unsafe-optional-chaining.js +1 -0
  223. package/lib/rules/no-unused-expressions.js +7 -0
  224. package/lib/rules/no-unused-labels.js +1 -0
  225. package/lib/rules/no-unused-private-class-members.js +1 -0
  226. package/lib/rules/no-unused-vars.js +1 -0
  227. package/lib/rules/no-use-before-define.js +33 -9
  228. package/lib/rules/no-useless-backreference.js +1 -0
  229. package/lib/rules/no-useless-call.js +1 -0
  230. package/lib/rules/no-useless-catch.js +1 -0
  231. package/lib/rules/no-useless-computed-key.js +1 -0
  232. package/lib/rules/no-useless-concat.js +1 -0
  233. package/lib/rules/no-useless-constructor.js +1 -0
  234. package/lib/rules/no-useless-escape.js +1 -0
  235. package/lib/rules/no-useless-rename.js +1 -0
  236. package/lib/rules/no-useless-return.js +1 -0
  237. package/lib/rules/no-var.js +1 -0
  238. package/lib/rules/no-void.js +1 -0
  239. package/lib/rules/no-warning-comments.js +1 -0
  240. package/lib/rules/no-whitespace-before-property.js +1 -0
  241. package/lib/rules/no-with.js +1 -0
  242. package/lib/rules/nonblock-statement-body-position.js +1 -0
  243. package/lib/rules/object-curly-newline.js +1 -0
  244. package/lib/rules/object-curly-spacing.js +1 -0
  245. package/lib/rules/object-property-newline.js +1 -0
  246. package/lib/rules/object-shorthand.js +1 -0
  247. package/lib/rules/one-var-declaration-per-line.js +1 -0
  248. package/lib/rules/one-var.js +6 -1
  249. package/lib/rules/operator-assignment.js +1 -0
  250. package/lib/rules/operator-linebreak.js +1 -0
  251. package/lib/rules/padded-blocks.js +9 -0
  252. package/lib/rules/padding-line-between-statements.js +3 -0
  253. package/lib/rules/prefer-arrow-callback.js +1 -0
  254. package/lib/rules/prefer-const.js +2 -1
  255. package/lib/rules/prefer-destructuring.js +1 -0
  256. package/lib/rules/prefer-exponentiation-operator.js +1 -0
  257. package/lib/rules/prefer-named-capture-group.js +1 -0
  258. package/lib/rules/prefer-numeric-literals.js +1 -0
  259. package/lib/rules/prefer-object-has-own.js +112 -0
  260. package/lib/rules/prefer-object-spread.js +1 -0
  261. package/lib/rules/prefer-promise-reject-errors.js +1 -0
  262. package/lib/rules/prefer-reflect.js +1 -0
  263. package/lib/rules/prefer-regex-literals.js +218 -1
  264. package/lib/rules/prefer-rest-params.js +1 -0
  265. package/lib/rules/prefer-spread.js +1 -0
  266. package/lib/rules/prefer-template.js +2 -1
  267. package/lib/rules/quote-props.js +1 -0
  268. package/lib/rules/quotes.js +1 -0
  269. package/lib/rules/radix.js +1 -0
  270. package/lib/rules/require-atomic-updates.js +15 -2
  271. package/lib/rules/require-await.js +1 -0
  272. package/lib/rules/require-jsdoc.js +1 -0
  273. package/lib/rules/require-unicode-regexp.js +1 -0
  274. package/lib/rules/require-yield.js +1 -0
  275. package/lib/rules/rest-spread-spacing.js +1 -0
  276. package/lib/rules/semi-spacing.js +1 -0
  277. package/lib/rules/semi-style.js +9 -2
  278. package/lib/rules/semi.js +19 -9
  279. package/lib/rules/sort-imports.js +1 -0
  280. package/lib/rules/sort-keys.js +1 -0
  281. package/lib/rules/sort-vars.js +1 -0
  282. package/lib/rules/space-before-blocks.js +1 -0
  283. package/lib/rules/space-before-function-paren.js +1 -0
  284. package/lib/rules/space-in-parens.js +1 -0
  285. package/lib/rules/space-infix-ops.js +1 -0
  286. package/lib/rules/space-unary-ops.js +1 -0
  287. package/lib/rules/spaced-comment.js +1 -0
  288. package/lib/rules/strict.js +1 -0
  289. package/lib/rules/switch-colon-spacing.js +1 -0
  290. package/lib/rules/symbol-description.js +1 -0
  291. package/lib/rules/template-curly-spacing.js +1 -0
  292. package/lib/rules/template-tag-spacing.js +1 -0
  293. package/lib/rules/unicode-bom.js +1 -0
  294. package/lib/rules/use-isnan.js +1 -0
  295. package/lib/rules/utils/ast-utils.js +15 -3
  296. package/lib/rules/valid-jsdoc.js +1 -0
  297. package/lib/rules/valid-typeof.js +1 -0
  298. package/lib/rules/vars-on-top.js +26 -12
  299. package/lib/rules/wrap-iife.js +1 -0
  300. package/lib/rules/wrap-regex.js +1 -0
  301. package/lib/rules/yield-star-spacing.js +1 -0
  302. package/lib/rules/yoda.js +1 -0
  303. package/lib/shared/types.js +10 -0
  304. package/package.json +8 -8
@@ -45,25 +45,37 @@ function isInRange(node, location) {
45
45
 
46
46
  /**
47
47
  * Checks whether or not a given location is inside of the range of a class static initializer.
48
+ * Static initializers are static blocks and initializers of static fields.
48
49
  * @param {ASTNode} node `ClassBody` node to check static initializers.
49
50
  * @param {number} location A location to check.
50
51
  * @returns {boolean} `true` if the location is inside of a class static initializer.
51
52
  */
52
53
  function isInClassStaticInitializerRange(node, location) {
53
54
  return node.body.some(classMember => (
54
- classMember.type === "PropertyDefinition" &&
55
- classMember.static &&
56
- classMember.value &&
57
- isInRange(classMember.value, location)
55
+ (
56
+ classMember.type === "StaticBlock" &&
57
+ isInRange(classMember, location)
58
+ ) ||
59
+ (
60
+ classMember.type === "PropertyDefinition" &&
61
+ classMember.static &&
62
+ classMember.value &&
63
+ isInRange(classMember.value, location)
64
+ )
58
65
  ));
59
66
  }
60
67
 
61
68
  /**
62
- * Checks whether a given scope is the scope of a static class field initializer.
69
+ * Checks whether a given scope is the scope of a a class static initializer.
70
+ * Static initializers are static blocks and initializers of static fields.
63
71
  * @param {eslint-scope.Scope} scope A scope to check.
64
72
  * @returns {boolean} `true` if the scope is a class static initializer scope.
65
73
  */
66
74
  function isClassStaticInitializerScope(scope) {
75
+ if (scope.type === "class-static-block") {
76
+ return true;
77
+ }
78
+
67
79
  if (scope.type === "class-field-initializer") {
68
80
 
69
81
  // `scope.block` is PropertyDefinition#value node
@@ -82,7 +94,8 @@ function isClassStaticInitializerScope(scope) {
82
94
  * - top-level
83
95
  * - functions
84
96
  * - class field initializers (implicit functions)
85
- * Static class field initializers are automatically run during the class definition evaluation,
97
+ * - class static blocks (implicit functions)
98
+ * Static class field initializers and class static blocks are automatically run during the class definition evaluation,
86
99
  * and therefore we'll consider them as a part of the parent execution context.
87
100
  * Example:
88
101
  *
@@ -90,6 +103,7 @@ function isClassStaticInitializerScope(scope) {
90
103
  *
91
104
  * x; // returns `false`
92
105
  * () => x; // returns `true`
106
+ *
93
107
  * class C {
94
108
  * field = x; // returns `true`
95
109
  * static field = x; // returns `false`
@@ -97,6 +111,14 @@ function isClassStaticInitializerScope(scope) {
97
111
  * method() {
98
112
  * x; // returns `true`
99
113
  * }
114
+ *
115
+ * static method() {
116
+ * x; // returns `true`
117
+ * }
118
+ *
119
+ * static {
120
+ * x; // returns `false`
121
+ * }
100
122
  * }
101
123
  * @param {eslint-scope.Reference} reference A reference to check.
102
124
  * @returns {boolean} `true` if the reference is from a separate execution context.
@@ -127,8 +149,9 @@ function isFromSeparateExecutionContext(reference) {
127
149
  * var {a = a} = obj
128
150
  * for (var a in a) {}
129
151
  * for (var a of a) {}
130
- * var C = class { [C]; }
131
- * var C = class { static foo = C; }
152
+ * var C = class { [C]; };
153
+ * var C = class { static foo = C; };
154
+ * var C = class { static { foo = C; } };
132
155
  * class C extends C {}
133
156
  * class C extends (class { static foo = C; }) {}
134
157
  * class C { [C]; }
@@ -158,7 +181,7 @@ function isEvaluatedDuringInitialization(reference) {
158
181
 
159
182
  /*
160
183
  * Class binding is initialized before running static initializers.
161
- * For example, `class C { static foo = C; }` is valid.
184
+ * For example, `class C { static foo = C; static { bar = C; } }` is valid.
162
185
  */
163
186
  !isInClassStaticInitializerRange(classDefinition.body, location)
164
187
  );
@@ -195,6 +218,7 @@ function isEvaluatedDuringInitialization(reference) {
195
218
  // Rule Definition
196
219
  //------------------------------------------------------------------------------
197
220
 
221
+ /** @type {import('../shared/types').Rule} */
198
222
  module.exports = {
199
223
  meta: {
200
224
  type: "problem",
@@ -58,6 +58,7 @@ function isNegativeLookaround(node) {
58
58
  // Rule Definition
59
59
  //------------------------------------------------------------------------------
60
60
 
61
+ /** @type {import('../shared/types').Rule} */
61
62
  module.exports = {
62
63
  meta: {
63
64
  type: "problem",
@@ -49,6 +49,7 @@ function isValidThisArg(expectedThis, thisArg, sourceCode) {
49
49
  // Rule Definition
50
50
  //------------------------------------------------------------------------------
51
51
 
52
+ /** @type {import('../shared/types').Rule} */
52
53
  module.exports = {
53
54
  meta: {
54
55
  type: "suggestion",
@@ -9,6 +9,7 @@
9
9
  // Rule Definition
10
10
  //------------------------------------------------------------------------------
11
11
 
12
+ /** @type {import('../shared/types').Rule} */
12
13
  module.exports = {
13
14
  meta: {
14
15
  type: "suggestion",
@@ -85,6 +85,7 @@ function hasUselessComputedKey(node) {
85
85
  // Rule Definition
86
86
  //------------------------------------------------------------------------------
87
87
 
88
+ /** @type {import('../shared/types').Rule} */
88
89
  module.exports = {
89
90
  meta: {
90
91
  type: "suggestion",
@@ -64,6 +64,7 @@ function getRight(node) {
64
64
  // Rule Definition
65
65
  //------------------------------------------------------------------------------
66
66
 
67
+ /** @type {import('../shared/types').Rule} */
67
68
  module.exports = {
68
69
  meta: {
69
70
  type: "suggestion",
@@ -132,6 +132,7 @@ function isRedundantSuperCall(body, ctorParams) {
132
132
  // Rule Definition
133
133
  //------------------------------------------------------------------------------
134
134
 
135
+ /** @type {import('../shared/types').Rule} */
135
136
  module.exports = {
136
137
  meta: {
137
138
  type: "suggestion",
@@ -78,6 +78,7 @@ function parseRegExp(regExpText) {
78
78
  return charList;
79
79
  }
80
80
 
81
+ /** @type {import('../shared/types').Rule} */
81
82
  module.exports = {
82
83
  meta: {
83
84
  type: "suggestion",
@@ -15,6 +15,7 @@ const astUtils = require("./utils/ast-utils");
15
15
  // Rule Definition
16
16
  //------------------------------------------------------------------------------
17
17
 
18
+ /** @type {import('../shared/types').Rule} */
18
19
  module.exports = {
19
20
  meta: {
20
21
  type: "suggestion",
@@ -61,6 +61,7 @@ function isInFinally(node) {
61
61
  // Rule Definition
62
62
  //------------------------------------------------------------------------------
63
63
 
64
+ /** @type {import('../shared/types').Rule} */
64
65
  module.exports = {
65
66
  meta: {
66
67
  type: "suggestion",
@@ -179,6 +179,7 @@ function hasNameDisallowedForLetDeclarations(variable) {
179
179
  // Rule Definition
180
180
  //------------------------------------------------------------------------------
181
181
 
182
+ /** @type {import('../shared/types').Rule} */
182
183
  module.exports = {
183
184
  meta: {
184
185
  type: "suggestion",
@@ -8,6 +8,7 @@
8
8
  // Rule Definition
9
9
  //------------------------------------------------------------------------------
10
10
 
11
+ /** @type {import('../shared/types').Rule} */
11
12
  module.exports = {
12
13
  meta: {
13
14
  type: "suggestion",
@@ -14,6 +14,7 @@ const CHAR_LIMIT = 40;
14
14
  // Rule Definition
15
15
  //------------------------------------------------------------------------------
16
16
 
17
+ /** @type {import('../shared/types').Rule} */
17
18
  module.exports = {
18
19
  meta: {
19
20
  type: "suggestion",
@@ -14,6 +14,7 @@ const astUtils = require("./utils/ast-utils");
14
14
  // Rule Definition
15
15
  //------------------------------------------------------------------------------
16
16
 
17
+ /** @type {import('../shared/types').Rule} */
17
18
  module.exports = {
18
19
  meta: {
19
20
  type: "layout",
@@ -9,6 +9,7 @@
9
9
  // Rule Definition
10
10
  //------------------------------------------------------------------------------
11
11
 
12
+ /** @type {import('../shared/types').Rule} */
12
13
  module.exports = {
13
14
  meta: {
14
15
  type: "suggestion",
@@ -10,6 +10,7 @@
10
10
 
11
11
  const POSITION_SCHEMA = { enum: ["beside", "below", "any"] };
12
12
 
13
+ /** @type {import('../shared/types').Rule} */
13
14
  module.exports = {
14
15
  meta: {
15
16
  type: "layout",
@@ -144,6 +144,7 @@ function areLineBreaksRequired(node, options, first, last) {
144
144
  // Rule Definition
145
145
  //------------------------------------------------------------------------------
146
146
 
147
+ /** @type {import('../shared/types').Rule} */
147
148
  module.exports = {
148
149
  meta: {
149
150
  type: "layout",
@@ -10,6 +10,7 @@ const astUtils = require("./utils/ast-utils");
10
10
  // Rule Definition
11
11
  //------------------------------------------------------------------------------
12
12
 
13
+ /** @type {import('../shared/types').Rule} */
13
14
  module.exports = {
14
15
  meta: {
15
16
  type: "layout",
@@ -9,6 +9,7 @@
9
9
  // Rule Definition
10
10
  //------------------------------------------------------------------------------
11
11
 
12
+ /** @type {import('../shared/types').Rule} */
12
13
  module.exports = {
13
14
  meta: {
14
15
  type: "layout",
@@ -22,6 +22,7 @@ const astUtils = require("./utils/ast-utils");
22
22
  //------------------------------------------------------------------------------
23
23
  // Rule Definition
24
24
  //------------------------------------------------------------------------------
25
+ /** @type {import('../shared/types').Rule} */
25
26
  module.exports = {
26
27
  meta: {
27
28
  type: "suggestion",
@@ -8,6 +8,7 @@
8
8
  // Rule Definition
9
9
  //------------------------------------------------------------------------------
10
10
 
11
+ /** @type {import('../shared/types').Rule} */
11
12
  module.exports = {
12
13
  meta: {
13
14
  type: "suggestion",
@@ -28,6 +28,7 @@ function isInStatementList(node) {
28
28
  // Rule Definition
29
29
  //------------------------------------------------------------------------------
30
30
 
31
+ /** @type {import('../shared/types').Rule} */
31
32
  module.exports = {
32
33
  meta: {
33
34
  type: "suggestion",
@@ -541,6 +542,8 @@ module.exports = {
541
542
  FunctionDeclaration: startFunction,
542
543
  FunctionExpression: startFunction,
543
544
  ArrowFunctionExpression: startFunction,
545
+ StaticBlock: startFunction, // StaticBlock creates a new scope for `var` variables
546
+
544
547
  BlockStatement: startBlock,
545
548
  ForStatement: startBlock,
546
549
  ForInStatement: startBlock,
@@ -552,10 +555,12 @@ module.exports = {
552
555
  "ForInStatement:exit": endBlock,
553
556
  "SwitchStatement:exit": endBlock,
554
557
  "BlockStatement:exit": endBlock,
558
+
555
559
  "Program:exit": endFunction,
556
560
  "FunctionDeclaration:exit": endFunction,
557
561
  "FunctionExpression:exit": endFunction,
558
- "ArrowFunctionExpression:exit": endFunction
562
+ "ArrowFunctionExpression:exit": endFunction,
563
+ "StaticBlock:exit": endFunction
559
564
  };
560
565
 
561
566
  }
@@ -57,6 +57,7 @@ function canBeFixed(node) {
57
57
  );
58
58
  }
59
59
 
60
+ /** @type {import('../shared/types').Rule} */
60
61
  module.exports = {
61
62
  meta: {
62
63
  type: "suggestion",
@@ -15,6 +15,7 @@ const astUtils = require("./utils/ast-utils");
15
15
  // Rule Definition
16
16
  //------------------------------------------------------------------------------
17
17
 
18
+ /** @type {import('../shared/types').Rule} */
18
19
  module.exports = {
19
20
  meta: {
20
21
  type: "layout",
@@ -15,6 +15,7 @@ const astUtils = require("./utils/ast-utils");
15
15
  // Rule Definition
16
16
  //------------------------------------------------------------------------------
17
17
 
18
+ /** @type {import('../shared/types').Rule} */
18
19
  module.exports = {
19
20
  meta: {
20
21
  type: "layout",
@@ -106,6 +107,12 @@ module.exports = {
106
107
  if (node.type === "SwitchStatement") {
107
108
  return sourceCode.getTokenBefore(node.cases[0]);
108
109
  }
110
+
111
+ if (node.type === "StaticBlock") {
112
+ return sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token
113
+ }
114
+
115
+ // `BlockStatement` or `ClassBody`
109
116
  return sourceCode.getFirstToken(node);
110
117
  }
111
118
 
@@ -172,6 +179,7 @@ module.exports = {
172
179
  function requirePaddingFor(node) {
173
180
  switch (node.type) {
174
181
  case "BlockStatement":
182
+ case "StaticBlock":
175
183
  return options.blocks;
176
184
  case "SwitchStatement":
177
185
  return options.switches;
@@ -282,6 +290,7 @@ module.exports = {
282
290
  }
283
291
  checkPadding(node);
284
292
  };
293
+ rule.StaticBlock = rule.BlockStatement;
285
294
  }
286
295
 
287
296
  if (Object.prototype.hasOwnProperty.call(options, "classes")) {
@@ -425,6 +425,7 @@ const StatementTypes = {
425
425
  // Rule Definition
426
426
  //------------------------------------------------------------------------------
427
427
 
428
+ /** @type {import('../shared/types').Rule} */
428
429
  module.exports = {
429
430
  meta: {
430
431
  type: "layout",
@@ -618,9 +619,11 @@ module.exports = {
618
619
  Program: enterScope,
619
620
  BlockStatement: enterScope,
620
621
  SwitchStatement: enterScope,
622
+ StaticBlock: enterScope,
621
623
  "Program:exit": exitScope,
622
624
  "BlockStatement:exit": exitScope,
623
625
  "SwitchStatement:exit": exitScope,
626
+ "StaticBlock:exit": exitScope,
624
627
 
625
628
  ":statement": verify,
626
629
 
@@ -145,6 +145,7 @@ function hasDuplicateParams(paramsList) {
145
145
  // Rule Definition
146
146
  //------------------------------------------------------------------------------
147
147
 
148
+ /** @type {import('../shared/types').Rule} */
148
149
  module.exports = {
149
150
  meta: {
150
151
  type: "suggestion",
@@ -17,7 +17,7 @@ const astUtils = require("./utils/ast-utils");
17
17
  //------------------------------------------------------------------------------
18
18
 
19
19
  const PATTERN_TYPE = /^(?:.+?Pattern|RestElement|SpreadProperty|ExperimentalRestProperty|Property)$/u;
20
- const DECLARATION_HOST_TYPE = /^(?:Program|BlockStatement|SwitchCase)$/u;
20
+ const DECLARATION_HOST_TYPE = /^(?:Program|BlockStatement|StaticBlock|SwitchCase)$/u;
21
21
  const DESTRUCTURING_HOST_TYPE = /^(?:VariableDeclarator|AssignmentExpression)$/u;
22
22
 
23
23
  /**
@@ -326,6 +326,7 @@ function findUp(node, type, shouldStop) {
326
326
  // Rule Definition
327
327
  //------------------------------------------------------------------------------
328
328
 
329
+ /** @type {import('../shared/types').Rule} */
329
330
  module.exports = {
330
331
  meta: {
331
332
  type: "suggestion",
@@ -20,6 +20,7 @@ const PRECEDENCE_OF_ASSIGNMENT_EXPR = astUtils.getPrecedence({ type: "Assignment
20
20
  // Rule Definition
21
21
  //------------------------------------------------------------------------------
22
22
 
23
+ /** @type {import('../shared/types').Rule} */
23
24
  module.exports = {
24
25
  meta: {
25
26
  type: "suggestion",
@@ -84,6 +84,7 @@ function parenthesizeIfShould(text, shouldParenthesize) {
84
84
  // Rule Definition
85
85
  //------------------------------------------------------------------------------
86
86
 
87
+ /** @type {import('../shared/types').Rule} */
87
88
  module.exports = {
88
89
  meta: {
89
90
  type: "suggestion",
@@ -27,6 +27,7 @@ const parser = new regexpp.RegExpParser();
27
27
  // Rule Definition
28
28
  //------------------------------------------------------------------------------
29
29
 
30
+ /** @type {import('../shared/types').Rule} */
30
31
  module.exports = {
31
32
  meta: {
32
33
  type: "suggestion",
@@ -39,6 +39,7 @@ function isParseInt(calleeNode) {
39
39
  // Rule Definition
40
40
  //------------------------------------------------------------------------------
41
41
 
42
+ /** @type {import('../shared/types').Rule} */
42
43
  module.exports = {
43
44
  meta: {
44
45
  type: "suggestion",
@@ -0,0 +1,112 @@
1
+ /**
2
+ * @fileoverview Prefers Object.hasOwn() instead of Object.prototype.hasOwnProperty.call()
3
+ * @author Nitin Kumar
4
+ * @author Gautam Arora
5
+ */
6
+
7
+ "use strict";
8
+
9
+ //------------------------------------------------------------------------------
10
+ // Requirements
11
+ //------------------------------------------------------------------------------
12
+
13
+ const astUtils = require("./utils/ast-utils");
14
+
15
+ //------------------------------------------------------------------------------
16
+ // Helpers
17
+ //------------------------------------------------------------------------------
18
+
19
+ /**
20
+ * Checks if the given node is considered to be an access to a property of `Object.prototype`.
21
+ * @param {ASTNode} node `MemberExpression` node to evaluate.
22
+ * @returns {boolean} `true` if `node.object` is `Object`, `Object.prototype`, or `{}` (empty 'ObjectExpression' node).
23
+ */
24
+ function hasLeftHandObject(node) {
25
+
26
+ /*
27
+ * ({}).hasOwnProperty.call(obj, prop) - `true`
28
+ * ({ foo }.hasOwnProperty.call(obj, prop)) - `false`, object literal should be empty
29
+ */
30
+ if (node.object.type === "ObjectExpression" && node.object.properties.length === 0) {
31
+ return true;
32
+ }
33
+
34
+ const objectNodeToCheck = node.object.type === "MemberExpression" && astUtils.getStaticPropertyName(node.object) === "prototype" ? node.object.object : node.object;
35
+
36
+ if (objectNodeToCheck.type === "Identifier" && objectNodeToCheck.name === "Object") {
37
+ return true;
38
+ }
39
+
40
+ return false;
41
+ }
42
+
43
+ //------------------------------------------------------------------------------
44
+ // Rule Definition
45
+ //------------------------------------------------------------------------------
46
+
47
+ /** @type {import('../shared/types').Rule} */
48
+ module.exports = {
49
+ meta: {
50
+ type: "suggestion",
51
+ docs: {
52
+ description:
53
+ "disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`",
54
+ recommended: false,
55
+ url: "https://eslint.org/docs/rules/prefer-object-has-own"
56
+ },
57
+ schema: [],
58
+ messages: {
59
+ useHasOwn: "Use 'Object.hasOwn()' instead of 'Object.prototype.hasOwnProperty.call()'."
60
+ },
61
+ fixable: "code"
62
+ },
63
+ create(context) {
64
+ return {
65
+ CallExpression(node) {
66
+ if (!(node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression")) {
67
+ return;
68
+ }
69
+
70
+ const calleePropertyName = astUtils.getStaticPropertyName(node.callee);
71
+ const objectPropertyName = astUtils.getStaticPropertyName(node.callee.object);
72
+ const isObject = hasLeftHandObject(node.callee.object);
73
+
74
+ // check `Object` scope
75
+ const scope = context.getScope();
76
+ const variable = astUtils.getVariableByName(scope, "Object");
77
+
78
+ if (
79
+ calleePropertyName === "call" &&
80
+ objectPropertyName === "hasOwnProperty" &&
81
+ isObject &&
82
+ variable && variable.scope.type === "global"
83
+ ) {
84
+ context.report({
85
+ node,
86
+ messageId: "useHasOwn",
87
+ fix(fixer) {
88
+ const sourceCode = context.getSourceCode();
89
+
90
+ if (sourceCode.getCommentsInside(node.callee).length > 0) {
91
+ return null;
92
+ }
93
+
94
+ const tokenJustBeforeNode = sourceCode.getTokenBefore(node.callee, { includeComments: true });
95
+
96
+ // for https://github.com/eslint/eslint/pull/15346#issuecomment-991417335
97
+ if (
98
+ tokenJustBeforeNode &&
99
+ tokenJustBeforeNode.range[1] === node.callee.range[0] &&
100
+ !astUtils.canTokensBeAdjacent(tokenJustBeforeNode, "Object.hasOwn")
101
+ ) {
102
+ return fixer.replaceText(node.callee, " Object.hasOwn");
103
+ }
104
+
105
+ return fixer.replaceText(node.callee, "Object.hasOwn");
106
+ }
107
+ });
108
+ }
109
+ }
110
+ };
111
+ }
112
+ };
@@ -240,6 +240,7 @@ function defineFixer(node, sourceCode) {
240
240
  };
241
241
  }
242
242
 
243
+ /** @type {import('../shared/types').Rule} */
243
244
  module.exports = {
244
245
  meta: {
245
246
  type: "suggestion",
@@ -10,6 +10,7 @@ const astUtils = require("./utils/ast-utils");
10
10
  // Rule Definition
11
11
  //------------------------------------------------------------------------------
12
12
 
13
+ /** @type {import('../shared/types').Rule} */
13
14
  module.exports = {
14
15
  meta: {
15
16
  type: "suggestion",
@@ -9,6 +9,7 @@
9
9
  // Rule Definition
10
10
  //------------------------------------------------------------------------------
11
11
 
12
+ /** @type {import('../shared/types').Rule} */
12
13
  module.exports = {
13
14
  meta: {
14
15
  type: "suggestion",