eslint 9.22.0 → 9.24.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 (417) hide show
  1. package/README.md +48 -46
  2. package/bin/eslint.js +92 -90
  3. package/conf/default-cli-options.js +22 -22
  4. package/conf/ecma-version.js +1 -1
  5. package/conf/globals.js +97 -98
  6. package/conf/replacements.json +24 -20
  7. package/conf/rule-type-list.json +88 -92
  8. package/lib/api.js +12 -12
  9. package/lib/cli-engine/cli-engine.js +830 -810
  10. package/lib/cli-engine/file-enumerator.js +381 -387
  11. package/lib/cli-engine/formatters/formatters-meta.json +16 -16
  12. package/lib/cli-engine/formatters/html.js +107 -99
  13. package/lib/cli-engine/formatters/json-with-metadata.js +5 -5
  14. package/lib/cli-engine/formatters/json.js +2 -2
  15. package/lib/cli-engine/formatters/stylish.js +96 -75
  16. package/lib/cli-engine/hash.js +1 -1
  17. package/lib/cli-engine/index.js +1 -1
  18. package/lib/cli-engine/lint-result-cache.js +144 -145
  19. package/lib/cli-engine/load-rules.js +16 -16
  20. package/lib/cli.js +638 -457
  21. package/lib/config/config-loader.js +726 -622
  22. package/lib/config/config.js +247 -221
  23. package/lib/config/default-config.js +54 -45
  24. package/lib/config/flat-config-array.js +167 -172
  25. package/lib/config/flat-config-helpers.js +72 -72
  26. package/lib/config/flat-config-schema.js +375 -368
  27. package/lib/config/rule-validator.js +139 -144
  28. package/lib/config-api.js +2 -2
  29. package/lib/eslint/eslint-helpers.js +756 -681
  30. package/lib/eslint/eslint.js +934 -912
  31. package/lib/eslint/index.js +2 -2
  32. package/lib/eslint/legacy-eslint.js +577 -533
  33. package/lib/languages/js/index.js +263 -264
  34. package/lib/languages/js/source-code/index.js +1 -1
  35. package/lib/languages/js/source-code/source-code.js +1128 -1057
  36. package/lib/languages/js/source-code/token-store/backward-token-comment-cursor.js +39 -35
  37. package/lib/languages/js/source-code/token-store/backward-token-cursor.js +35 -36
  38. package/lib/languages/js/source-code/token-store/cursor.js +36 -36
  39. package/lib/languages/js/source-code/token-store/cursors.js +80 -52
  40. package/lib/languages/js/source-code/token-store/decorative-cursor.js +17 -18
  41. package/lib/languages/js/source-code/token-store/filter-cursor.js +19 -20
  42. package/lib/languages/js/source-code/token-store/forward-token-comment-cursor.js +40 -32
  43. package/lib/languages/js/source-code/token-store/forward-token-cursor.js +40 -41
  44. package/lib/languages/js/source-code/token-store/index.js +592 -498
  45. package/lib/languages/js/source-code/token-store/limit-cursor.js +17 -18
  46. package/lib/languages/js/source-code/token-store/padded-token-cursor.js +23 -16
  47. package/lib/languages/js/source-code/token-store/skip-cursor.js +19 -20
  48. package/lib/languages/js/source-code/token-store/utils.js +63 -60
  49. package/lib/languages/js/validate-language-options.js +104 -89
  50. package/lib/linter/apply-disable-directives.js +467 -383
  51. package/lib/linter/code-path-analysis/code-path-analyzer.js +650 -672
  52. package/lib/linter/code-path-analysis/code-path-segment.js +215 -216
  53. package/lib/linter/code-path-analysis/code-path-state.js +2118 -2096
  54. package/lib/linter/code-path-analysis/code-path.js +307 -319
  55. package/lib/linter/code-path-analysis/debug-helpers.js +183 -163
  56. package/lib/linter/code-path-analysis/fork-context.js +296 -271
  57. package/lib/linter/code-path-analysis/id-generator.js +22 -23
  58. package/lib/linter/file-context.js +119 -120
  59. package/lib/linter/index.js +3 -3
  60. package/lib/linter/interpolate.js +16 -16
  61. package/lib/linter/linter.js +2403 -2045
  62. package/lib/linter/node-event-generator.js +284 -225
  63. package/lib/linter/report-translator.js +256 -219
  64. package/lib/linter/rule-fixer.js +122 -124
  65. package/lib/linter/rules.js +36 -36
  66. package/lib/linter/safe-emitter.js +18 -18
  67. package/lib/linter/source-code-fixer.js +94 -92
  68. package/lib/linter/timing.js +104 -101
  69. package/lib/linter/vfile.js +70 -73
  70. package/lib/options.js +404 -361
  71. package/lib/rule-tester/index.js +1 -1
  72. package/lib/rule-tester/rule-tester.js +1308 -1046
  73. package/lib/rules/accessor-pairs.js +298 -263
  74. package/lib/rules/array-bracket-newline.js +250 -238
  75. package/lib/rules/array-bracket-spacing.js +263 -224
  76. package/lib/rules/array-callback-return.js +402 -356
  77. package/lib/rules/array-element-newline.js +358 -313
  78. package/lib/rules/arrow-body-style.js +400 -281
  79. package/lib/rules/arrow-parens.js +206 -173
  80. package/lib/rules/arrow-spacing.js +169 -163
  81. package/lib/rules/block-scoped-var.js +125 -123
  82. package/lib/rules/block-spacing.js +186 -176
  83. package/lib/rules/brace-style.js +262 -199
  84. package/lib/rules/callback-return.js +203 -190
  85. package/lib/rules/camelcase.js +403 -392
  86. package/lib/rules/capitalized-comments.js +253 -232
  87. package/lib/rules/class-methods-use-this.js +224 -172
  88. package/lib/rules/comma-dangle.js +379 -346
  89. package/lib/rules/comma-spacing.js +193 -195
  90. package/lib/rules/comma-style.js +375 -316
  91. package/lib/rules/complexity.js +173 -169
  92. package/lib/rules/computed-property-spacing.js +236 -211
  93. package/lib/rules/consistent-return.js +181 -170
  94. package/lib/rules/consistent-this.js +167 -147
  95. package/lib/rules/constructor-super.js +412 -404
  96. package/lib/rules/curly.js +407 -332
  97. package/lib/rules/default-case-last.js +38 -31
  98. package/lib/rules/default-case.js +89 -85
  99. package/lib/rules/default-param-last.js +69 -54
  100. package/lib/rules/dot-location.js +122 -110
  101. package/lib/rules/dot-notation.js +192 -156
  102. package/lib/rules/eol-last.js +122 -120
  103. package/lib/rules/eqeqeq.js +168 -155
  104. package/lib/rules/for-direction.js +146 -121
  105. package/lib/rules/func-call-spacing.js +261 -231
  106. package/lib/rules/func-name-matching.js +293 -209
  107. package/lib/rules/func-names.js +165 -164
  108. package/lib/rules/func-style.js +159 -127
  109. package/lib/rules/function-call-argument-newline.js +152 -129
  110. package/lib/rules/function-paren-newline.js +349 -291
  111. package/lib/rules/generator-star-spacing.js +229 -210
  112. package/lib/rules/getter-return.js +208 -172
  113. package/lib/rules/global-require.js +85 -74
  114. package/lib/rules/grouped-accessor-pairs.js +170 -150
  115. package/lib/rules/guard-for-in.js +72 -63
  116. package/lib/rules/handle-callback-err.js +108 -103
  117. package/lib/rules/id-blacklist.js +182 -199
  118. package/lib/rules/id-denylist.js +168 -187
  119. package/lib/rules/id-length.js +197 -171
  120. package/lib/rules/id-match.js +344 -289
  121. package/lib/rules/implicit-arrow-linebreak.js +102 -79
  122. package/lib/rules/indent-legacy.js +1344 -1118
  123. package/lib/rules/indent.js +2272 -1759
  124. package/lib/rules/index.js +317 -292
  125. package/lib/rules/init-declarations.js +137 -107
  126. package/lib/rules/jsx-quotes.js +94 -82
  127. package/lib/rules/key-spacing.js +750 -633
  128. package/lib/rules/keyword-spacing.js +648 -605
  129. package/lib/rules/line-comment-position.js +142 -128
  130. package/lib/rules/linebreak-style.js +107 -106
  131. package/lib/rules/lines-around-comment.js +540 -448
  132. package/lib/rules/lines-around-directive.js +233 -203
  133. package/lib/rules/lines-between-class-members.js +305 -234
  134. package/lib/rules/logical-assignment-operators.js +582 -399
  135. package/lib/rules/max-classes-per-file.js +69 -68
  136. package/lib/rules/max-depth.js +146 -143
  137. package/lib/rules/max-len.js +473 -434
  138. package/lib/rules/max-lines-per-function.js +201 -176
  139. package/lib/rules/max-lines.js +158 -162
  140. package/lib/rules/max-nested-callbacks.js +102 -104
  141. package/lib/rules/max-params.js +78 -76
  142. package/lib/rules/max-statements-per-line.js +205 -198
  143. package/lib/rules/max-statements.js +168 -164
  144. package/lib/rules/multiline-comment-style.js +637 -479
  145. package/lib/rules/multiline-ternary.js +241 -176
  146. package/lib/rules/new-cap.js +233 -213
  147. package/lib/rules/new-parens.js +88 -79
  148. package/lib/rules/newline-after-var.js +287 -250
  149. package/lib/rules/newline-before-return.js +229 -222
  150. package/lib/rules/newline-per-chained-call.js +142 -127
  151. package/lib/rules/no-alert.js +90 -79
  152. package/lib/rules/no-array-constructor.js +125 -113
  153. package/lib/rules/no-async-promise-executor.js +30 -24
  154. package/lib/rules/no-await-in-loop.js +69 -71
  155. package/lib/rules/no-bitwise.js +124 -100
  156. package/lib/rules/no-buffer-constructor.js +55 -47
  157. package/lib/rules/no-caller.js +39 -33
  158. package/lib/rules/no-case-declarations.js +61 -57
  159. package/lib/rules/no-catch-shadow.js +76 -73
  160. package/lib/rules/no-class-assign.js +51 -48
  161. package/lib/rules/no-compare-neg-zero.js +62 -48
  162. package/lib/rules/no-cond-assign.js +148 -132
  163. package/lib/rules/no-confusing-arrow.js +98 -81
  164. package/lib/rules/no-console.js +202 -199
  165. package/lib/rules/no-const-assign.js +47 -41
  166. package/lib/rules/no-constant-binary-expression.js +500 -405
  167. package/lib/rules/no-constant-condition.js +158 -143
  168. package/lib/rules/no-constructor-return.js +49 -49
  169. package/lib/rules/no-continue.js +25 -27
  170. package/lib/rules/no-control-regex.js +125 -121
  171. package/lib/rules/no-debugger.js +28 -30
  172. package/lib/rules/no-delete-var.js +29 -29
  173. package/lib/rules/no-div-regex.js +47 -41
  174. package/lib/rules/no-dupe-args.js +68 -69
  175. package/lib/rules/no-dupe-class-members.js +102 -89
  176. package/lib/rules/no-dupe-else-if.js +100 -77
  177. package/lib/rules/no-dupe-keys.js +133 -110
  178. package/lib/rules/no-duplicate-case.js +50 -43
  179. package/lib/rules/no-duplicate-imports.js +179 -176
  180. package/lib/rules/no-else-return.js +430 -385
  181. package/lib/rules/no-empty-character-class.js +57 -50
  182. package/lib/rules/no-empty-function.js +127 -128
  183. package/lib/rules/no-empty-pattern.js +63 -58
  184. package/lib/rules/no-empty-static-block.js +37 -35
  185. package/lib/rules/no-empty.js +98 -86
  186. package/lib/rules/no-eq-null.js +37 -32
  187. package/lib/rules/no-eval.js +256 -250
  188. package/lib/rules/no-ex-assign.js +42 -39
  189. package/lib/rules/no-extend-native.js +161 -159
  190. package/lib/rules/no-extra-bind.js +201 -190
  191. package/lib/rules/no-extra-boolean-cast.js +398 -348
  192. package/lib/rules/no-extra-label.js +150 -131
  193. package/lib/rules/no-extra-parens.js +1654 -1325
  194. package/lib/rules/no-extra-semi.js +146 -144
  195. package/lib/rules/no-fallthrough.js +199 -157
  196. package/lib/rules/no-floating-decimal.js +74 -66
  197. package/lib/rules/no-func-assign.js +54 -55
  198. package/lib/rules/no-global-assign.js +78 -73
  199. package/lib/rules/no-implicit-coercion.js +349 -293
  200. package/lib/rules/no-implicit-globals.js +158 -135
  201. package/lib/rules/no-implied-eval.js +140 -112
  202. package/lib/rules/no-import-assign.js +145 -159
  203. package/lib/rules/no-inline-comments.js +101 -95
  204. package/lib/rules/no-inner-declarations.js +115 -101
  205. package/lib/rules/no-invalid-regexp.js +222 -190
  206. package/lib/rules/no-invalid-this.js +123 -117
  207. package/lib/rules/no-irregular-whitespace.js +266 -252
  208. package/lib/rules/no-iterator.js +29 -33
  209. package/lib/rules/no-label-var.js +59 -62
  210. package/lib/rules/no-labels.js +138 -133
  211. package/lib/rules/no-lone-blocks.js +127 -123
  212. package/lib/rules/no-lonely-if.js +108 -77
  213. package/lib/rules/no-loop-func.js +238 -213
  214. package/lib/rules/no-loss-of-precision.js +218 -201
  215. package/lib/rules/no-magic-numbers.js +246 -218
  216. package/lib/rules/no-misleading-character-class.js +499 -446
  217. package/lib/rules/no-mixed-operators.js +188 -182
  218. package/lib/rules/no-mixed-requires.js +253 -240
  219. package/lib/rules/no-mixed-spaces-and-tabs.js +134 -121
  220. package/lib/rules/no-multi-assign.js +46 -44
  221. package/lib/rules/no-multi-spaces.js +163 -143
  222. package/lib/rules/no-multi-str.js +42 -41
  223. package/lib/rules/no-multiple-empty-lines.js +196 -158
  224. package/lib/rules/no-native-reassign.js +90 -85
  225. package/lib/rules/no-negated-condition.js +79 -75
  226. package/lib/rules/no-negated-in-lhs.js +45 -43
  227. package/lib/rules/no-nested-ternary.js +33 -32
  228. package/lib/rules/no-new-func.js +71 -62
  229. package/lib/rules/no-new-native-nonconstructor.js +43 -39
  230. package/lib/rules/no-new-object.js +48 -48
  231. package/lib/rules/no-new-require.js +48 -47
  232. package/lib/rules/no-new-symbol.js +52 -50
  233. package/lib/rules/no-new-wrappers.js +43 -41
  234. package/lib/rules/no-new.js +28 -29
  235. package/lib/rules/no-nonoctal-decimal-escape.js +141 -121
  236. package/lib/rules/no-obj-calls.js +66 -53
  237. package/lib/rules/no-object-constructor.js +104 -97
  238. package/lib/rules/no-octal-escape.js +40 -43
  239. package/lib/rules/no-octal.js +32 -32
  240. package/lib/rules/no-param-reassign.js +235 -217
  241. package/lib/rules/no-path-concat.js +66 -67
  242. package/lib/rules/no-plusplus.js +60 -61
  243. package/lib/rules/no-process-env.js +49 -48
  244. package/lib/rules/no-process-exit.js +54 -50
  245. package/lib/rules/no-promise-executor-return.js +214 -182
  246. package/lib/rules/no-proto.js +26 -29
  247. package/lib/rules/no-prototype-builtins.js +146 -124
  248. package/lib/rules/no-redeclare.js +154 -152
  249. package/lib/rules/no-regex-spaces.js +183 -161
  250. package/lib/rules/no-restricted-exports.js +208 -185
  251. package/lib/rules/no-restricted-globals.js +111 -112
  252. package/lib/rules/no-restricted-imports.js +657 -537
  253. package/lib/rules/no-restricted-modules.js +222 -202
  254. package/lib/rules/no-restricted-properties.js +181 -153
  255. package/lib/rules/no-restricted-syntax.js +56 -52
  256. package/lib/rules/no-return-assign.js +55 -50
  257. package/lib/rules/no-return-await.js +148 -124
  258. package/lib/rules/no-script-url.js +52 -45
  259. package/lib/rules/no-self-assign.js +148 -146
  260. package/lib/rules/no-self-compare.js +63 -46
  261. package/lib/rules/no-sequences.js +135 -116
  262. package/lib/rules/no-setter-return.js +185 -152
  263. package/lib/rules/no-shadow-restricted-names.js +61 -46
  264. package/lib/rules/no-shadow.js +342 -316
  265. package/lib/rules/no-spaced-func.js +82 -77
  266. package/lib/rules/no-sparse-arrays.js +54 -59
  267. package/lib/rules/no-sync.js +61 -60
  268. package/lib/rules/no-tabs.js +83 -72
  269. package/lib/rules/no-template-curly-in-string.js +33 -32
  270. package/lib/rules/no-ternary.js +25 -29
  271. package/lib/rules/no-this-before-super.js +321 -319
  272. package/lib/rules/no-throw-literal.js +31 -36
  273. package/lib/rules/no-trailing-spaces.js +199 -191
  274. package/lib/rules/no-undef-init.js +76 -61
  275. package/lib/rules/no-undef.js +51 -48
  276. package/lib/rules/no-undefined.js +73 -75
  277. package/lib/rules/no-underscore-dangle.js +370 -327
  278. package/lib/rules/no-unexpected-multiline.js +112 -102
  279. package/lib/rules/no-unmodified-loop-condition.js +254 -254
  280. package/lib/rules/no-unneeded-ternary.js +212 -147
  281. package/lib/rules/no-unreachable-loop.js +145 -142
  282. package/lib/rules/no-unreachable.js +255 -248
  283. package/lib/rules/no-unsafe-finally.js +93 -85
  284. package/lib/rules/no-unsafe-negation.js +105 -83
  285. package/lib/rules/no-unsafe-optional-chaining.js +192 -178
  286. package/lib/rules/no-unused-expressions.js +178 -162
  287. package/lib/rules/no-unused-labels.js +139 -124
  288. package/lib/rules/no-unused-private-class-members.js +206 -182
  289. package/lib/rules/no-unused-vars.js +1669 -1449
  290. package/lib/rules/no-use-before-define.js +229 -231
  291. package/lib/rules/no-useless-assignment.js +590 -511
  292. package/lib/rules/no-useless-backreference.js +212 -193
  293. package/lib/rules/no-useless-call.js +58 -53
  294. package/lib/rules/no-useless-catch.js +40 -40
  295. package/lib/rules/no-useless-computed-key.js +144 -115
  296. package/lib/rules/no-useless-concat.js +65 -60
  297. package/lib/rules/no-useless-constructor.js +158 -111
  298. package/lib/rules/no-useless-escape.js +342 -291
  299. package/lib/rules/no-useless-rename.js +183 -156
  300. package/lib/rules/no-useless-return.js +344 -312
  301. package/lib/rules/no-var.js +233 -212
  302. package/lib/rules/no-void.js +50 -48
  303. package/lib/rules/no-warning-comments.js +191 -186
  304. package/lib/rules/no-whitespace-before-property.js +131 -115
  305. package/lib/rules/no-with.js +24 -26
  306. package/lib/rules/nonblock-statement-body-position.js +149 -130
  307. package/lib/rules/object-curly-newline.js +306 -265
  308. package/lib/rules/object-curly-spacing.js +360 -314
  309. package/lib/rules/object-property-newline.js +137 -106
  310. package/lib/rules/object-shorthand.js +607 -502
  311. package/lib/rules/one-var-declaration-per-line.js +104 -100
  312. package/lib/rules/one-var.js +653 -537
  313. package/lib/rules/operator-assignment.js +219 -161
  314. package/lib/rules/operator-linebreak.js +295 -251
  315. package/lib/rules/padded-blocks.js +346 -308
  316. package/lib/rules/padding-line-between-statements.js +443 -439
  317. package/lib/rules/prefer-arrow-callback.js +362 -313
  318. package/lib/rules/prefer-const.js +418 -377
  319. package/lib/rules/prefer-destructuring.js +301 -279
  320. package/lib/rules/prefer-exponentiation-operator.js +176 -133
  321. package/lib/rules/prefer-named-capture-group.js +153 -140
  322. package/lib/rules/prefer-numeric-literals.js +121 -113
  323. package/lib/rules/prefer-object-has-own.js +116 -82
  324. package/lib/rules/prefer-object-spread.js +213 -193
  325. package/lib/rules/prefer-promise-reject-errors.js +140 -122
  326. package/lib/rules/prefer-reflect.js +127 -107
  327. package/lib/rules/prefer-regex-literals.js +578 -466
  328. package/lib/rules/prefer-rest-params.js +79 -80
  329. package/lib/rules/prefer-spread.js +47 -44
  330. package/lib/rules/prefer-template.js +266 -195
  331. package/lib/rules/quote-props.js +373 -307
  332. package/lib/rules/quotes.js +374 -326
  333. package/lib/rules/radix.js +152 -136
  334. package/lib/rules/require-atomic-updates.js +316 -285
  335. package/lib/rules/require-await.js +144 -116
  336. package/lib/rules/require-unicode-regexp.js +282 -177
  337. package/lib/rules/require-yield.js +53 -54
  338. package/lib/rules/rest-spread-spacing.js +128 -116
  339. package/lib/rules/semi-spacing.js +281 -250
  340. package/lib/rules/semi-style.js +176 -134
  341. package/lib/rules/semi.js +456 -436
  342. package/lib/rules/sort-imports.js +306 -233
  343. package/lib/rules/sort-keys.js +219 -188
  344. package/lib/rules/sort-vars.js +127 -93
  345. package/lib/rules/space-before-blocks.js +199 -189
  346. package/lib/rules/space-before-function-paren.js +186 -166
  347. package/lib/rules/space-in-parens.js +359 -288
  348. package/lib/rules/space-infix-ops.js +237 -201
  349. package/lib/rules/space-unary-ops.js +356 -298
  350. package/lib/rules/spaced-comment.js +363 -319
  351. package/lib/rules/strict.js +265 -230
  352. package/lib/rules/switch-colon-spacing.js +130 -122
  353. package/lib/rules/symbol-description.js +45 -48
  354. package/lib/rules/template-curly-spacing.js +148 -142
  355. package/lib/rules/template-tag-spacing.js +98 -88
  356. package/lib/rules/unicode-bom.js +54 -56
  357. package/lib/rules/use-isnan.js +237 -206
  358. package/lib/rules/utils/ast-utils.js +2039 -1860
  359. package/lib/rules/utils/char-source.js +162 -155
  360. package/lib/rules/utils/fix-tracker.js +83 -80
  361. package/lib/rules/utils/keywords.js +59 -59
  362. package/lib/rules/utils/lazy-loading-rule-map.js +79 -76
  363. package/lib/rules/utils/regular-expressions.js +32 -24
  364. package/lib/rules/utils/unicode/index.js +4 -4
  365. package/lib/rules/utils/unicode/is-combining-character.js +1 -1
  366. package/lib/rules/utils/unicode/is-emoji-modifier.js +1 -1
  367. package/lib/rules/utils/unicode/is-regional-indicator-symbol.js +1 -1
  368. package/lib/rules/utils/unicode/is-surrogate-pair.js +1 -1
  369. package/lib/rules/valid-typeof.js +153 -111
  370. package/lib/rules/vars-on-top.js +152 -145
  371. package/lib/rules/wrap-iife.js +204 -191
  372. package/lib/rules/wrap-regex.js +70 -58
  373. package/lib/rules/yield-star-spacing.js +145 -134
  374. package/lib/rules/yoda.js +283 -272
  375. package/lib/services/parser-service.js +35 -35
  376. package/lib/services/processor-service.js +66 -73
  377. package/lib/services/suppressions-service.js +289 -0
  378. package/lib/shared/ajv.js +14 -14
  379. package/lib/shared/assert.js +3 -4
  380. package/lib/shared/ast-utils.js +7 -6
  381. package/lib/shared/deep-merge-arrays.js +24 -22
  382. package/lib/shared/directives.js +3 -2
  383. package/lib/shared/flags.js +50 -17
  384. package/lib/shared/logging.js +24 -25
  385. package/lib/shared/option-utils.js +43 -36
  386. package/lib/shared/runtime-info.js +136 -127
  387. package/lib/shared/serialization.js +27 -27
  388. package/lib/shared/severity.js +22 -22
  389. package/lib/shared/stats.js +5 -5
  390. package/lib/shared/string-utils.js +16 -16
  391. package/lib/shared/text-table.js +28 -27
  392. package/lib/shared/traverser.js +153 -146
  393. package/lib/shared/types.js +4 -27
  394. package/lib/types/index.d.ts +2010 -1559
  395. package/lib/types/rules.d.ts +5253 -5140
  396. package/lib/types/use-at-your-own-risk.d.ts +32 -30
  397. package/lib/unsupported-api.js +5 -5
  398. package/messages/all-files-ignored.js +3 -3
  399. package/messages/all-matched-files-ignored.js +3 -3
  400. package/messages/config-file-missing.js +2 -2
  401. package/messages/config-plugin-missing.js +3 -3
  402. package/messages/config-serialize-function.js +9 -7
  403. package/messages/eslintrc-incompat.js +13 -15
  404. package/messages/eslintrc-plugins.js +3 -4
  405. package/messages/extend-config-missing.js +3 -3
  406. package/messages/failed-to-read-json.js +3 -3
  407. package/messages/file-not-found.js +3 -3
  408. package/messages/invalid-rule-options.js +2 -2
  409. package/messages/invalid-rule-severity.js +2 -2
  410. package/messages/no-config-found.js +3 -3
  411. package/messages/plugin-conflict.js +8 -8
  412. package/messages/plugin-invalid.js +3 -3
  413. package/messages/plugin-missing.js +3 -3
  414. package/messages/print-config-with-directory-path.js +2 -2
  415. package/messages/shared.js +6 -1
  416. package/messages/whitespace-found.js +3 -3
  417. package/package.json +14 -20
@@ -20,1123 +20,1349 @@ const astUtils = require("./utils/ast-utils");
20
20
  //------------------------------------------------------------------------------
21
21
  // this rule has known coverage issues, but it's deprecated and shouldn't be updated in the future anyway.
22
22
  /* c8 ignore next */
23
- /** @type {import('../shared/types').Rule} */
23
+ /** @type {import('../types').Rule.RuleModule} */
24
24
  module.exports = {
25
- meta: {
26
- type: "layout",
27
-
28
- docs: {
29
- description: "Enforce consistent indentation",
30
- recommended: false,
31
- url: "https://eslint.org/docs/latest/rules/indent-legacy"
32
- },
33
-
34
- deprecated: {
35
- message: "Formatting rules are being moved out of ESLint core.",
36
- url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
37
- deprecatedSince: "4.0.0",
38
- replacedBy: [
39
- {
40
- message: "ESLint Stylistic now maintains deprecated stylistic core rules.",
41
- url: "https://eslint.style/guide/migration",
42
- plugin: {
43
- name: "@stylistic/eslint-plugin-js",
44
- url: "https://eslint.style/packages/js"
45
- },
46
- rule: {
47
- name: "indent",
48
- url: "https://eslint.style/rules/js/indent"
49
- }
50
- }
51
- ]
52
- },
53
-
54
- fixable: "whitespace",
55
-
56
- schema: [
57
- {
58
- oneOf: [
59
- {
60
- enum: ["tab"]
61
- },
62
- {
63
- type: "integer",
64
- minimum: 0
65
- }
66
- ]
67
- },
68
- {
69
- type: "object",
70
- properties: {
71
- SwitchCase: {
72
- type: "integer",
73
- minimum: 0
74
- },
75
- VariableDeclarator: {
76
- oneOf: [
77
- {
78
- type: "integer",
79
- minimum: 0
80
- },
81
- {
82
- type: "object",
83
- properties: {
84
- var: {
85
- type: "integer",
86
- minimum: 0
87
- },
88
- let: {
89
- type: "integer",
90
- minimum: 0
91
- },
92
- const: {
93
- type: "integer",
94
- minimum: 0
95
- }
96
- }
97
- }
98
- ]
99
- },
100
- outerIIFEBody: {
101
- type: "integer",
102
- minimum: 0
103
- },
104
- MemberExpression: {
105
- type: "integer",
106
- minimum: 0
107
- },
108
- FunctionDeclaration: {
109
- type: "object",
110
- properties: {
111
- parameters: {
112
- oneOf: [
113
- {
114
- type: "integer",
115
- minimum: 0
116
- },
117
- {
118
- enum: ["first"]
119
- }
120
- ]
121
- },
122
- body: {
123
- type: "integer",
124
- minimum: 0
125
- }
126
- }
127
- },
128
- FunctionExpression: {
129
- type: "object",
130
- properties: {
131
- parameters: {
132
- oneOf: [
133
- {
134
- type: "integer",
135
- minimum: 0
136
- },
137
- {
138
- enum: ["first"]
139
- }
140
- ]
141
- },
142
- body: {
143
- type: "integer",
144
- minimum: 0
145
- }
146
- }
147
- },
148
- CallExpression: {
149
- type: "object",
150
- properties: {
151
- parameters: {
152
- oneOf: [
153
- {
154
- type: "integer",
155
- minimum: 0
156
- },
157
- {
158
- enum: ["first"]
159
- }
160
- ]
161
- }
162
- }
163
- },
164
- ArrayExpression: {
165
- oneOf: [
166
- {
167
- type: "integer",
168
- minimum: 0
169
- },
170
- {
171
- enum: ["first"]
172
- }
173
- ]
174
- },
175
- ObjectExpression: {
176
- oneOf: [
177
- {
178
- type: "integer",
179
- minimum: 0
180
- },
181
- {
182
- enum: ["first"]
183
- }
184
- ]
185
- }
186
- },
187
- additionalProperties: false
188
- }
189
- ],
190
- messages: {
191
- expected: "Expected indentation of {{expected}} but found {{actual}}."
192
- }
193
- },
194
-
195
- create(context) {
196
- const DEFAULT_VARIABLE_INDENT = 1;
197
- const DEFAULT_PARAMETER_INDENT = null; // For backwards compatibility, don't check parameter indentation unless specified in the config
198
- const DEFAULT_FUNCTION_BODY_INDENT = 1;
199
-
200
- let indentType = "space";
201
- let indentSize = 4;
202
- const options = {
203
- SwitchCase: 0,
204
- VariableDeclarator: {
205
- var: DEFAULT_VARIABLE_INDENT,
206
- let: DEFAULT_VARIABLE_INDENT,
207
- const: DEFAULT_VARIABLE_INDENT
208
- },
209
- outerIIFEBody: null,
210
- FunctionDeclaration: {
211
- parameters: DEFAULT_PARAMETER_INDENT,
212
- body: DEFAULT_FUNCTION_BODY_INDENT
213
- },
214
- FunctionExpression: {
215
- parameters: DEFAULT_PARAMETER_INDENT,
216
- body: DEFAULT_FUNCTION_BODY_INDENT
217
- },
218
- CallExpression: {
219
- arguments: DEFAULT_PARAMETER_INDENT
220
- },
221
- ArrayExpression: 1,
222
- ObjectExpression: 1
223
- };
224
-
225
- const sourceCode = context.sourceCode;
226
-
227
- if (context.options.length) {
228
- if (context.options[0] === "tab") {
229
- indentSize = 1;
230
- indentType = "tab";
231
- } else /* c8 ignore start */ if (typeof context.options[0] === "number") {
232
- indentSize = context.options[0];
233
- indentType = "space";
234
- }/* c8 ignore stop */
235
-
236
- if (context.options[1]) {
237
- const opts = context.options[1];
238
-
239
- options.SwitchCase = opts.SwitchCase || 0;
240
- const variableDeclaratorRules = opts.VariableDeclarator;
241
-
242
- if (typeof variableDeclaratorRules === "number") {
243
- options.VariableDeclarator = {
244
- var: variableDeclaratorRules,
245
- let: variableDeclaratorRules,
246
- const: variableDeclaratorRules
247
- };
248
- } else if (typeof variableDeclaratorRules === "object") {
249
- Object.assign(options.VariableDeclarator, variableDeclaratorRules);
250
- }
251
-
252
- if (typeof opts.outerIIFEBody === "number") {
253
- options.outerIIFEBody = opts.outerIIFEBody;
254
- }
255
-
256
- if (typeof opts.MemberExpression === "number") {
257
- options.MemberExpression = opts.MemberExpression;
258
- }
259
-
260
- if (typeof opts.FunctionDeclaration === "object") {
261
- Object.assign(options.FunctionDeclaration, opts.FunctionDeclaration);
262
- }
263
-
264
- if (typeof opts.FunctionExpression === "object") {
265
- Object.assign(options.FunctionExpression, opts.FunctionExpression);
266
- }
267
-
268
- if (typeof opts.CallExpression === "object") {
269
- Object.assign(options.CallExpression, opts.CallExpression);
270
- }
271
-
272
- if (typeof opts.ArrayExpression === "number" || typeof opts.ArrayExpression === "string") {
273
- options.ArrayExpression = opts.ArrayExpression;
274
- }
275
-
276
- if (typeof opts.ObjectExpression === "number" || typeof opts.ObjectExpression === "string") {
277
- options.ObjectExpression = opts.ObjectExpression;
278
- }
279
- }
280
- }
281
-
282
- const caseIndentStore = {};
283
-
284
- /**
285
- * Creates an error message for a line, given the expected/actual indentation.
286
- * @param {int} expectedAmount The expected amount of indentation characters for this line
287
- * @param {int} actualSpaces The actual number of indentation spaces that were found on this line
288
- * @param {int} actualTabs The actual number of indentation tabs that were found on this line
289
- * @returns {string} An error message for this line
290
- */
291
- function createErrorMessageData(expectedAmount, actualSpaces, actualTabs) {
292
- const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs"
293
- const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space"
294
- const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs"
295
- let foundStatement;
296
-
297
- if (actualSpaces > 0 && actualTabs > 0) {
298
- foundStatement = `${actualSpaces} ${foundSpacesWord} and ${actualTabs} ${foundTabsWord}`; // e.g. "1 space and 2 tabs"
299
- } else if (actualSpaces > 0) {
300
-
301
- /*
302
- * Abbreviate the message if the expected indentation is also spaces.
303
- * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces'
304
- */
305
- foundStatement = indentType === "space" ? actualSpaces : `${actualSpaces} ${foundSpacesWord}`;
306
- } else if (actualTabs > 0) {
307
- foundStatement = indentType === "tab" ? actualTabs : `${actualTabs} ${foundTabsWord}`;
308
- } else {
309
- foundStatement = "0";
310
- }
311
- return {
312
- expected: expectedStatement,
313
- actual: foundStatement
314
- };
315
- }
316
-
317
- /**
318
- * Reports a given indent violation
319
- * @param {ASTNode} node Node violating the indent rule
320
- * @param {int} needed Expected indentation character count
321
- * @param {int} gottenSpaces Indentation space count in the actual node/code
322
- * @param {int} gottenTabs Indentation tab count in the actual node/code
323
- * @param {Object} [loc] Error line and column location
324
- * @param {boolean} isLastNodeCheck Is the error for last node check
325
- * @returns {void}
326
- */
327
- function report(node, needed, gottenSpaces, gottenTabs, loc, isLastNodeCheck) {
328
- if (gottenSpaces && gottenTabs) {
329
-
330
- // To avoid conflicts with `no-mixed-spaces-and-tabs`, don't report lines that have both spaces and tabs.
331
- return;
332
- }
333
-
334
- const desiredIndent = (indentType === "space" ? " " : "\t").repeat(needed);
335
-
336
- const textRange = isLastNodeCheck
337
- ? [node.range[1] - node.loc.end.column, node.range[1] - node.loc.end.column + gottenSpaces + gottenTabs]
338
- : [node.range[0] - node.loc.start.column, node.range[0] - node.loc.start.column + gottenSpaces + gottenTabs];
339
-
340
- context.report({
341
- node,
342
- loc,
343
- messageId: "expected",
344
- data: createErrorMessageData(needed, gottenSpaces, gottenTabs),
345
- fix: fixer => fixer.replaceTextRange(textRange, desiredIndent)
346
- });
347
- }
348
-
349
- /**
350
- * Get the actual indent of node
351
- * @param {ASTNode|Token} node Node to examine
352
- * @param {boolean} [byLastLine=false] get indent of node's last line
353
- * @returns {Object} The node's indent. Contains keys `space` and `tab`, representing the indent of each character. Also
354
- * contains keys `goodChar` and `badChar`, where `goodChar` is the amount of the user's desired indentation character, and
355
- * `badChar` is the amount of the other indentation character.
356
- */
357
- function getNodeIndent(node, byLastLine) {
358
- const token = byLastLine ? sourceCode.getLastToken(node) : sourceCode.getFirstToken(node);
359
- const srcCharsBeforeNode = sourceCode.getText(token, token.loc.start.column).split("");
360
- const indentChars = srcCharsBeforeNode.slice(0, srcCharsBeforeNode.findIndex(char => char !== " " && char !== "\t"));
361
- const spaces = indentChars.filter(char => char === " ").length;
362
- const tabs = indentChars.filter(char => char === "\t").length;
363
-
364
- return {
365
- space: spaces,
366
- tab: tabs,
367
- goodChar: indentType === "space" ? spaces : tabs,
368
- badChar: indentType === "space" ? tabs : spaces
369
- };
370
- }
371
-
372
- /**
373
- * Checks node is the first in its own start line. By default it looks by start line.
374
- * @param {ASTNode} node The node to check
375
- * @param {boolean} [byEndLocation=false] Lookup based on start position or end
376
- * @returns {boolean} true if its the first in the its start line
377
- */
378
- function isNodeFirstInLine(node, byEndLocation) {
379
- const firstToken = byEndLocation === true ? sourceCode.getLastToken(node, 1) : sourceCode.getTokenBefore(node),
380
- startLine = byEndLocation === true ? node.loc.end.line : node.loc.start.line,
381
- endLine = firstToken ? firstToken.loc.end.line : -1;
382
-
383
- return startLine !== endLine;
384
- }
385
-
386
- /**
387
- * Check indent for node
388
- * @param {ASTNode} node Node to check
389
- * @param {int} neededIndent needed indent
390
- * @returns {void}
391
- */
392
- function checkNodeIndent(node, neededIndent) {
393
- const actualIndent = getNodeIndent(node, false);
394
-
395
- if (
396
- node.type !== "ArrayExpression" &&
397
- node.type !== "ObjectExpression" &&
398
- (actualIndent.goodChar !== neededIndent || actualIndent.badChar !== 0) &&
399
- isNodeFirstInLine(node)
400
- ) {
401
- report(node, neededIndent, actualIndent.space, actualIndent.tab);
402
- }
403
-
404
- if (node.type === "IfStatement" && node.alternate) {
405
- const elseToken = sourceCode.getTokenBefore(node.alternate);
406
-
407
- checkNodeIndent(elseToken, neededIndent);
408
-
409
- if (!isNodeFirstInLine(node.alternate)) {
410
- checkNodeIndent(node.alternate, neededIndent);
411
- }
412
- }
413
-
414
- if (node.type === "TryStatement" && node.handler) {
415
- const catchToken = sourceCode.getFirstToken(node.handler);
416
-
417
- checkNodeIndent(catchToken, neededIndent);
418
- }
419
-
420
- if (node.type === "TryStatement" && node.finalizer) {
421
- const finallyToken = sourceCode.getTokenBefore(node.finalizer);
422
-
423
- checkNodeIndent(finallyToken, neededIndent);
424
- }
425
-
426
- if (node.type === "DoWhileStatement") {
427
- const whileToken = sourceCode.getTokenAfter(node.body);
428
-
429
- checkNodeIndent(whileToken, neededIndent);
430
- }
431
- }
432
-
433
- /**
434
- * Check indent for nodes list
435
- * @param {ASTNode[]} nodes list of node objects
436
- * @param {int} indent needed indent
437
- * @returns {void}
438
- */
439
- function checkNodesIndent(nodes, indent) {
440
- nodes.forEach(node => checkNodeIndent(node, indent));
441
- }
442
-
443
- /**
444
- * Check last node line indent this detects, that block closed correctly
445
- * @param {ASTNode} node Node to examine
446
- * @param {int} lastLineIndent needed indent
447
- * @returns {void}
448
- */
449
- function checkLastNodeLineIndent(node, lastLineIndent) {
450
- const lastToken = sourceCode.getLastToken(node);
451
- const endIndent = getNodeIndent(lastToken, true);
452
-
453
- if ((endIndent.goodChar !== lastLineIndent || endIndent.badChar !== 0) && isNodeFirstInLine(node, true)) {
454
- report(
455
- node,
456
- lastLineIndent,
457
- endIndent.space,
458
- endIndent.tab,
459
- { line: lastToken.loc.start.line, column: lastToken.loc.start.column },
460
- true
461
- );
462
- }
463
- }
464
-
465
- /**
466
- * Check last node line indent this detects, that block closed correctly
467
- * This function for more complicated return statement case, where closing parenthesis may be followed by ';'
468
- * @param {ASTNode} node Node to examine
469
- * @param {int} firstLineIndent first line needed indent
470
- * @returns {void}
471
- */
472
- function checkLastReturnStatementLineIndent(node, firstLineIndent) {
473
-
474
- /*
475
- * in case if return statement ends with ');' we have traverse back to ')'
476
- * otherwise we'll measure indent for ';' and replace ')'
477
- */
478
- const lastToken = sourceCode.getLastToken(node, astUtils.isClosingParenToken);
479
- const textBeforeClosingParenthesis = sourceCode.getText(lastToken, lastToken.loc.start.column).slice(0, -1);
480
-
481
- if (textBeforeClosingParenthesis.trim()) {
482
-
483
- // There are tokens before the closing paren, don't report this case
484
- return;
485
- }
486
-
487
- const endIndent = getNodeIndent(lastToken, true);
488
-
489
- if (endIndent.goodChar !== firstLineIndent) {
490
- report(
491
- node,
492
- firstLineIndent,
493
- endIndent.space,
494
- endIndent.tab,
495
- { line: lastToken.loc.start.line, column: lastToken.loc.start.column },
496
- true
497
- );
498
- }
499
- }
500
-
501
- /**
502
- * Check first node line indent is correct
503
- * @param {ASTNode} node Node to examine
504
- * @param {int} firstLineIndent needed indent
505
- * @returns {void}
506
- */
507
- function checkFirstNodeLineIndent(node, firstLineIndent) {
508
- const startIndent = getNodeIndent(node, false);
509
-
510
- if ((startIndent.goodChar !== firstLineIndent || startIndent.badChar !== 0) && isNodeFirstInLine(node)) {
511
- report(
512
- node,
513
- firstLineIndent,
514
- startIndent.space,
515
- startIndent.tab,
516
- { line: node.loc.start.line, column: node.loc.start.column }
517
- );
518
- }
519
- }
520
-
521
- /**
522
- * Returns a parent node of given node based on a specified type
523
- * if not present then return null
524
- * @param {ASTNode} node node to examine
525
- * @param {string} type type that is being looked for
526
- * @param {string} stopAtList end points for the evaluating code
527
- * @returns {ASTNode|void} if found then node otherwise null
528
- */
529
- function getParentNodeByType(node, type, stopAtList) {
530
- let parent = node.parent;
531
- const stopAtSet = new Set(stopAtList || ["Program"]);
532
-
533
- while (parent.type !== type && !stopAtSet.has(parent.type) && parent.type !== "Program") {
534
- parent = parent.parent;
535
- }
536
-
537
- return parent.type === type ? parent : null;
538
- }
539
-
540
- /**
541
- * Returns the VariableDeclarator based on the current node
542
- * if not present then return null
543
- * @param {ASTNode} node node to examine
544
- * @returns {ASTNode|void} if found then node otherwise null
545
- */
546
- function getVariableDeclaratorNode(node) {
547
- return getParentNodeByType(node, "VariableDeclarator");
548
- }
549
-
550
- /**
551
- * Check to see if the node is part of the multi-line variable declaration.
552
- * Also if its on the same line as the varNode
553
- * @param {ASTNode} node node to check
554
- * @param {ASTNode} varNode variable declaration node to check against
555
- * @returns {boolean} True if all the above condition satisfy
556
- */
557
- function isNodeInVarOnTop(node, varNode) {
558
- return varNode &&
559
- varNode.parent.loc.start.line === node.loc.start.line &&
560
- varNode.parent.declarations.length > 1;
561
- }
562
-
563
- /**
564
- * Check to see if the argument before the callee node is multi-line and
565
- * there should only be 1 argument before the callee node
566
- * @param {ASTNode} node node to check
567
- * @returns {boolean} True if arguments are multi-line
568
- */
569
- function isArgBeforeCalleeNodeMultiline(node) {
570
- const parent = node.parent;
571
-
572
- if (parent.arguments.length >= 2 && parent.arguments[1] === node) {
573
- return parent.arguments[0].loc.end.line > parent.arguments[0].loc.start.line;
574
- }
575
-
576
- return false;
577
- }
578
-
579
- /**
580
- * Check to see if the node is a file level IIFE
581
- * @param {ASTNode} node The function node to check.
582
- * @returns {boolean} True if the node is the outer IIFE
583
- */
584
- function isOuterIIFE(node) {
585
- const parent = node.parent;
586
- let stmt = parent.parent;
587
-
588
- /*
589
- * Verify that the node is an IIEF
590
- */
591
- if (
592
- parent.type !== "CallExpression" ||
593
- parent.callee !== node) {
594
-
595
- return false;
596
- }
597
-
598
- /*
599
- * Navigate legal ancestors to determine whether this IIEF is outer
600
- */
601
- while (
602
- stmt.type === "UnaryExpression" && (
603
- stmt.operator === "!" ||
604
- stmt.operator === "~" ||
605
- stmt.operator === "+" ||
606
- stmt.operator === "-") ||
607
- stmt.type === "AssignmentExpression" ||
608
- stmt.type === "LogicalExpression" ||
609
- stmt.type === "SequenceExpression" ||
610
- stmt.type === "VariableDeclarator") {
611
-
612
- stmt = stmt.parent;
613
- }
614
-
615
- return ((
616
- stmt.type === "ExpressionStatement" ||
617
- stmt.type === "VariableDeclaration") &&
618
- stmt.parent && stmt.parent.type === "Program"
619
- );
620
- }
621
-
622
- /**
623
- * Check indent for function block content
624
- * @param {ASTNode} node A BlockStatement node that is inside of a function.
625
- * @returns {void}
626
- */
627
- function checkIndentInFunctionBlock(node) {
628
-
629
- /*
630
- * Search first caller in chain.
631
- * Ex.:
632
- *
633
- * Models <- Identifier
634
- * .User
635
- * .find()
636
- * .exec(function() {
637
- * // function body
638
- * });
639
- *
640
- * Looks for 'Models'
641
- */
642
- const calleeNode = node.parent; // FunctionExpression
643
- let indent;
644
-
645
- if (calleeNode.parent &&
646
- (calleeNode.parent.type === "Property" ||
647
- calleeNode.parent.type === "ArrayExpression")) {
648
-
649
- // If function is part of array or object, comma can be put at left
650
- indent = getNodeIndent(calleeNode, false).goodChar;
651
- } else {
652
-
653
- // If function is standalone, simple calculate indent
654
- indent = getNodeIndent(calleeNode).goodChar;
655
- }
656
-
657
- if (calleeNode.parent.type === "CallExpression") {
658
- const calleeParent = calleeNode.parent;
659
-
660
- if (calleeNode.type !== "FunctionExpression" && calleeNode.type !== "ArrowFunctionExpression") {
661
- if (calleeParent && calleeParent.loc.start.line < node.loc.start.line) {
662
- indent = getNodeIndent(calleeParent).goodChar;
663
- }
664
- } else {
665
- if (isArgBeforeCalleeNodeMultiline(calleeNode) &&
666
- calleeParent.callee.loc.start.line === calleeParent.callee.loc.end.line &&
667
- !isNodeFirstInLine(calleeNode)) {
668
- indent = getNodeIndent(calleeParent).goodChar;
669
- }
670
- }
671
- }
672
-
673
- /*
674
- * function body indent should be indent + indent size, unless this
675
- * is a FunctionDeclaration, FunctionExpression, or outer IIFE and the corresponding options are enabled.
676
- */
677
- let functionOffset = indentSize;
678
-
679
- if (options.outerIIFEBody !== null && isOuterIIFE(calleeNode)) {
680
- functionOffset = options.outerIIFEBody * indentSize;
681
- } else if (calleeNode.type === "FunctionExpression") {
682
- functionOffset = options.FunctionExpression.body * indentSize;
683
- } else if (calleeNode.type === "FunctionDeclaration") {
684
- functionOffset = options.FunctionDeclaration.body * indentSize;
685
- }
686
- indent += functionOffset;
687
-
688
- // check if the node is inside a variable
689
- const parentVarNode = getVariableDeclaratorNode(node);
690
-
691
- if (parentVarNode && isNodeInVarOnTop(node, parentVarNode)) {
692
- indent += indentSize * options.VariableDeclarator[parentVarNode.parent.kind];
693
- }
694
-
695
- if (node.body.length > 0) {
696
- checkNodesIndent(node.body, indent);
697
- }
698
-
699
- checkLastNodeLineIndent(node, indent - functionOffset);
700
- }
701
-
702
-
703
- /**
704
- * Checks if the given node starts and ends on the same line
705
- * @param {ASTNode} node The node to check
706
- * @returns {boolean} Whether or not the block starts and ends on the same line.
707
- */
708
- function isSingleLineNode(node) {
709
- const lastToken = sourceCode.getLastToken(node),
710
- startLine = node.loc.start.line,
711
- endLine = lastToken.loc.end.line;
712
-
713
- return startLine === endLine;
714
- }
715
-
716
- /**
717
- * Check indent for array block content or object block content
718
- * @param {ASTNode} node node to examine
719
- * @returns {void}
720
- */
721
- function checkIndentInArrayOrObjectBlock(node) {
722
-
723
- // Skip inline
724
- if (isSingleLineNode(node)) {
725
- return;
726
- }
727
-
728
- let elements = (node.type === "ArrayExpression") ? node.elements : node.properties;
729
-
730
- // filter out empty elements example would be [ , 2] so remove first element as espree considers it as null
731
- elements = elements.filter(elem => elem !== null);
732
-
733
- let nodeIndent;
734
- let elementsIndent;
735
- const parentVarNode = getVariableDeclaratorNode(node);
736
-
737
- // TODO - come up with a better strategy in future
738
- if (isNodeFirstInLine(node)) {
739
- const parent = node.parent;
740
-
741
- nodeIndent = getNodeIndent(parent).goodChar;
742
- if (!parentVarNode || parentVarNode.loc.start.line !== node.loc.start.line) {
743
- if (parent.type !== "VariableDeclarator" || parentVarNode === parentVarNode.parent.declarations[0]) {
744
- if (parent.type === "VariableDeclarator" && parentVarNode.loc.start.line === parent.loc.start.line) {
745
- nodeIndent += (indentSize * options.VariableDeclarator[parentVarNode.parent.kind]);
746
- } else if (parent.type === "ObjectExpression" || parent.type === "ArrayExpression") {
747
- const parentElements = node.parent.type === "ObjectExpression" ? node.parent.properties : node.parent.elements;
748
-
749
- if (parentElements[0] &&
750
- parentElements[0].loc.start.line === parent.loc.start.line &&
751
- parentElements[0].loc.end.line !== parent.loc.start.line) {
752
-
753
- /*
754
- * If the first element of the array spans multiple lines, don't increase the expected indentation of the rest.
755
- * e.g. [{
756
- * foo: 1
757
- * },
758
- * {
759
- * bar: 1
760
- * }]
761
- * the second object is not indented.
762
- */
763
- } else if (typeof options[parent.type] === "number") {
764
- nodeIndent += options[parent.type] * indentSize;
765
- } else {
766
- nodeIndent = parentElements[0].loc.start.column;
767
- }
768
- } else if (parent.type === "CallExpression" || parent.type === "NewExpression") {
769
- if (typeof options.CallExpression.arguments === "number") {
770
- nodeIndent += options.CallExpression.arguments * indentSize;
771
- } else if (options.CallExpression.arguments === "first") {
772
- if (parent.arguments.includes(node)) {
773
- nodeIndent = parent.arguments[0].loc.start.column;
774
- }
775
- } else {
776
- nodeIndent += indentSize;
777
- }
778
- } else if (parent.type === "LogicalExpression" || parent.type === "ArrowFunctionExpression") {
779
- nodeIndent += indentSize;
780
- }
781
- }
782
- }
783
-
784
- checkFirstNodeLineIndent(node, nodeIndent);
785
- } else {
786
- nodeIndent = getNodeIndent(node).goodChar;
787
- }
788
-
789
- if (options[node.type] === "first") {
790
- elementsIndent = elements.length ? elements[0].loc.start.column : 0; // If there are no elements, elementsIndent doesn't matter.
791
- } else {
792
- elementsIndent = nodeIndent + indentSize * options[node.type];
793
- }
794
-
795
- /*
796
- * Check if the node is a multiple variable declaration; if so, then
797
- * make sure indentation takes that into account.
798
- */
799
- if (isNodeInVarOnTop(node, parentVarNode)) {
800
- elementsIndent += indentSize * options.VariableDeclarator[parentVarNode.parent.kind];
801
- }
802
-
803
- checkNodesIndent(elements, elementsIndent);
804
-
805
- if (elements.length > 0) {
806
-
807
- // Skip last block line check if last item in same line
808
- if (elements.at(-1).loc.end.line === node.loc.end.line) {
809
- return;
810
- }
811
- }
812
-
813
- checkLastNodeLineIndent(node, nodeIndent +
814
- (isNodeInVarOnTop(node, parentVarNode) ? options.VariableDeclarator[parentVarNode.parent.kind] * indentSize : 0));
815
- }
816
-
817
- /**
818
- * Check if the node or node body is a BlockStatement or not
819
- * @param {ASTNode} node node to test
820
- * @returns {boolean} True if it or its body is a block statement
821
- */
822
- function isNodeBodyBlock(node) {
823
- return node.type === "BlockStatement" || node.type === "ClassBody" || (node.body && node.body.type === "BlockStatement") ||
824
- (node.consequent && node.consequent.type === "BlockStatement");
825
- }
826
-
827
- /**
828
- * Check indentation for blocks
829
- * @param {ASTNode} node node to check
830
- * @returns {void}
831
- */
832
- function blockIndentationCheck(node) {
833
-
834
- // Skip inline blocks
835
- if (isSingleLineNode(node)) {
836
- return;
837
- }
838
-
839
- if (node.parent && (
840
- node.parent.type === "FunctionExpression" ||
841
- node.parent.type === "FunctionDeclaration" ||
842
- node.parent.type === "ArrowFunctionExpression")
843
- ) {
844
- checkIndentInFunctionBlock(node);
845
- return;
846
- }
847
-
848
- let indent;
849
- let nodesToCheck;
850
-
851
- /*
852
- * For this statements we should check indent from statement beginning,
853
- * not from the beginning of the block.
854
- */
855
- const statementsWithProperties = [
856
- "IfStatement", "WhileStatement", "ForStatement", "ForInStatement", "ForOfStatement", "DoWhileStatement", "ClassDeclaration", "TryStatement"
857
- ];
858
-
859
- if (node.parent && statementsWithProperties.includes(node.parent.type) && isNodeBodyBlock(node)) {
860
- indent = getNodeIndent(node.parent).goodChar;
861
- } else if (node.parent && node.parent.type === "CatchClause") {
862
- indent = getNodeIndent(node.parent.parent).goodChar;
863
- } else {
864
- indent = getNodeIndent(node).goodChar;
865
- }
866
-
867
- if (node.type === "IfStatement" && node.consequent.type !== "BlockStatement") {
868
- nodesToCheck = [node.consequent];
869
- } else if (Array.isArray(node.body)) {
870
- nodesToCheck = node.body;
871
- } else {
872
- nodesToCheck = [node.body];
873
- }
874
-
875
- if (nodesToCheck.length > 0) {
876
- checkNodesIndent(nodesToCheck, indent + indentSize);
877
- }
878
-
879
- if (node.type === "BlockStatement") {
880
- checkLastNodeLineIndent(node, indent);
881
- }
882
- }
883
-
884
- /**
885
- * Filter out the elements which are on the same line of each other or the node.
886
- * basically have only 1 elements from each line except the variable declaration line.
887
- * @param {ASTNode} node Variable declaration node
888
- * @returns {ASTNode[]} Filtered elements
889
- */
890
- function filterOutSameLineVars(node) {
891
- return node.declarations.reduce((finalCollection, elem) => {
892
- const lastElem = finalCollection.at(-1);
893
-
894
- if ((elem.loc.start.line !== node.loc.start.line && !lastElem) ||
895
- (lastElem && lastElem.loc.start.line !== elem.loc.start.line)) {
896
- finalCollection.push(elem);
897
- }
898
-
899
- return finalCollection;
900
- }, []);
901
- }
902
-
903
- /**
904
- * Check indentation for variable declarations
905
- * @param {ASTNode} node node to examine
906
- * @returns {void}
907
- */
908
- function checkIndentInVariableDeclarations(node) {
909
- const elements = filterOutSameLineVars(node);
910
- const nodeIndent = getNodeIndent(node).goodChar;
911
- const lastElement = elements.at(-1);
912
-
913
- const elementsIndent = nodeIndent + indentSize * options.VariableDeclarator[node.kind];
914
-
915
- checkNodesIndent(elements, elementsIndent);
916
-
917
- // Only check the last line if there is any token after the last item
918
- if (sourceCode.getLastToken(node).loc.end.line <= lastElement.loc.end.line) {
919
- return;
920
- }
921
-
922
- const tokenBeforeLastElement = sourceCode.getTokenBefore(lastElement);
923
-
924
- if (tokenBeforeLastElement.value === ",") {
925
-
926
- // Special case for comma-first syntax where the semicolon is indented
927
- checkLastNodeLineIndent(node, getNodeIndent(tokenBeforeLastElement).goodChar);
928
- } else {
929
- checkLastNodeLineIndent(node, elementsIndent - indentSize);
930
- }
931
- }
932
-
933
- /**
934
- * Check and decide whether to check for indentation for blockless nodes
935
- * Scenarios are for or while statements without braces around them
936
- * @param {ASTNode} node node to examine
937
- * @returns {void}
938
- */
939
- function blockLessNodes(node) {
940
- if (node.body.type !== "BlockStatement") {
941
- blockIndentationCheck(node);
942
- }
943
- }
944
-
945
- /**
946
- * Returns the expected indentation for the case statement
947
- * @param {ASTNode} node node to examine
948
- * @param {int} [providedSwitchIndent] indent for switch statement
949
- * @returns {int} indent size
950
- */
951
- function expectedCaseIndent(node, providedSwitchIndent) {
952
- const switchNode = (node.type === "SwitchStatement") ? node : node.parent;
953
- const switchIndent = typeof providedSwitchIndent === "undefined"
954
- ? getNodeIndent(switchNode).goodChar
955
- : providedSwitchIndent;
956
- let caseIndent;
957
-
958
- if (caseIndentStore[switchNode.loc.start.line]) {
959
- return caseIndentStore[switchNode.loc.start.line];
960
- }
961
-
962
- if (switchNode.cases.length > 0 && options.SwitchCase === 0) {
963
- caseIndent = switchIndent;
964
- } else {
965
- caseIndent = switchIndent + (indentSize * options.SwitchCase);
966
- }
967
-
968
- caseIndentStore[switchNode.loc.start.line] = caseIndent;
969
- return caseIndent;
970
-
971
- }
972
-
973
- /**
974
- * Checks whether a return statement is wrapped in ()
975
- * @param {ASTNode} node node to examine
976
- * @returns {boolean} the result
977
- */
978
- function isWrappedInParenthesis(node) {
979
- const regex = /^return\s*?\(\s*?\);*?/u;
980
-
981
- const statementWithoutArgument = sourceCode.getText(node).replace(
982
- sourceCode.getText(node.argument), ""
983
- );
984
-
985
- return regex.test(statementWithoutArgument);
986
- }
987
-
988
- return {
989
- Program(node) {
990
- if (node.body.length > 0) {
991
-
992
- // Root nodes should have no indent
993
- checkNodesIndent(node.body, getNodeIndent(node).goodChar);
994
- }
995
- },
996
-
997
- ClassBody: blockIndentationCheck,
998
-
999
- BlockStatement: blockIndentationCheck,
1000
-
1001
- WhileStatement: blockLessNodes,
1002
-
1003
- ForStatement: blockLessNodes,
1004
-
1005
- ForInStatement: blockLessNodes,
1006
-
1007
- ForOfStatement: blockLessNodes,
1008
-
1009
- DoWhileStatement: blockLessNodes,
1010
-
1011
- IfStatement(node) {
1012
- if (node.consequent.type !== "BlockStatement" && node.consequent.loc.start.line > node.loc.start.line) {
1013
- blockIndentationCheck(node);
1014
- }
1015
- },
1016
-
1017
- VariableDeclaration(node) {
1018
- if (node.declarations.at(-1).loc.start.line > node.declarations[0].loc.start.line) {
1019
- checkIndentInVariableDeclarations(node);
1020
- }
1021
- },
1022
-
1023
- ObjectExpression(node) {
1024
- checkIndentInArrayOrObjectBlock(node);
1025
- },
1026
-
1027
- ArrayExpression(node) {
1028
- checkIndentInArrayOrObjectBlock(node);
1029
- },
1030
-
1031
- MemberExpression(node) {
1032
-
1033
- if (typeof options.MemberExpression === "undefined") {
1034
- return;
1035
- }
1036
-
1037
- if (isSingleLineNode(node)) {
1038
- return;
1039
- }
1040
-
1041
- /*
1042
- * The typical layout of variable declarations and assignments
1043
- * alter the expectation of correct indentation. Skip them.
1044
- * TODO: Add appropriate configuration options for variable
1045
- * declarations and assignments.
1046
- */
1047
- if (getParentNodeByType(node, "VariableDeclarator", ["FunctionExpression", "ArrowFunctionExpression"])) {
1048
- return;
1049
- }
1050
-
1051
- if (getParentNodeByType(node, "AssignmentExpression", ["FunctionExpression"])) {
1052
- return;
1053
- }
1054
-
1055
- const propertyIndent = getNodeIndent(node).goodChar + indentSize * options.MemberExpression;
1056
-
1057
- const checkNodes = [node.property];
1058
-
1059
- const dot = sourceCode.getTokenBefore(node.property);
1060
-
1061
- if (dot.type === "Punctuator" && dot.value === ".") {
1062
- checkNodes.push(dot);
1063
- }
1064
-
1065
- checkNodesIndent(checkNodes, propertyIndent);
1066
- },
1067
-
1068
- SwitchStatement(node) {
1069
-
1070
- // Switch is not a 'BlockStatement'
1071
- const switchIndent = getNodeIndent(node).goodChar;
1072
- const caseIndent = expectedCaseIndent(node, switchIndent);
1073
-
1074
- checkNodesIndent(node.cases, caseIndent);
1075
-
1076
-
1077
- checkLastNodeLineIndent(node, switchIndent);
1078
- },
1079
-
1080
- SwitchCase(node) {
1081
-
1082
- // Skip inline cases
1083
- if (isSingleLineNode(node)) {
1084
- return;
1085
- }
1086
- const caseIndent = expectedCaseIndent(node);
1087
-
1088
- checkNodesIndent(node.consequent, caseIndent + indentSize);
1089
- },
1090
-
1091
- FunctionDeclaration(node) {
1092
- if (isSingleLineNode(node)) {
1093
- return;
1094
- }
1095
- if (options.FunctionDeclaration.parameters === "first" && node.params.length) {
1096
- checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column);
1097
- } else if (options.FunctionDeclaration.parameters !== null) {
1098
- checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionDeclaration.parameters);
1099
- }
1100
- },
1101
-
1102
- FunctionExpression(node) {
1103
- if (isSingleLineNode(node)) {
1104
- return;
1105
- }
1106
- if (options.FunctionExpression.parameters === "first" && node.params.length) {
1107
- checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column);
1108
- } else if (options.FunctionExpression.parameters !== null) {
1109
- checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionExpression.parameters);
1110
- }
1111
- },
1112
-
1113
- ReturnStatement(node) {
1114
- if (isSingleLineNode(node)) {
1115
- return;
1116
- }
1117
-
1118
- const firstLineIndent = getNodeIndent(node).goodChar;
1119
-
1120
- // in case if return statement is wrapped in parenthesis
1121
- if (isWrappedInParenthesis(node)) {
1122
- checkLastReturnStatementLineIndent(node, firstLineIndent);
1123
- } else {
1124
- checkNodeIndent(node, firstLineIndent);
1125
- }
1126
- },
1127
-
1128
- CallExpression(node) {
1129
- if (isSingleLineNode(node)) {
1130
- return;
1131
- }
1132
- if (options.CallExpression.arguments === "first" && node.arguments.length) {
1133
- checkNodesIndent(node.arguments.slice(1), node.arguments[0].loc.start.column);
1134
- } else if (options.CallExpression.arguments !== null) {
1135
- checkNodesIndent(node.arguments, getNodeIndent(node).goodChar + indentSize * options.CallExpression.arguments);
1136
- }
1137
- }
1138
-
1139
- };
1140
-
1141
- }
25
+ meta: {
26
+ type: "layout",
27
+
28
+ docs: {
29
+ description: "Enforce consistent indentation",
30
+ recommended: false,
31
+ url: "https://eslint.org/docs/latest/rules/indent-legacy",
32
+ },
33
+
34
+ deprecated: {
35
+ message: "Formatting rules are being moved out of ESLint core.",
36
+ url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
37
+ deprecatedSince: "4.0.0",
38
+ replacedBy: [
39
+ {
40
+ message:
41
+ "ESLint Stylistic now maintains deprecated stylistic core rules.",
42
+ url: "https://eslint.style/guide/migration",
43
+ plugin: {
44
+ name: "@stylistic/eslint-plugin-js",
45
+ url: "https://eslint.style/packages/js",
46
+ },
47
+ rule: {
48
+ name: "indent",
49
+ url: "https://eslint.style/rules/js/indent",
50
+ },
51
+ },
52
+ ],
53
+ },
54
+
55
+ fixable: "whitespace",
56
+
57
+ schema: [
58
+ {
59
+ oneOf: [
60
+ {
61
+ enum: ["tab"],
62
+ },
63
+ {
64
+ type: "integer",
65
+ minimum: 0,
66
+ },
67
+ ],
68
+ },
69
+ {
70
+ type: "object",
71
+ properties: {
72
+ SwitchCase: {
73
+ type: "integer",
74
+ minimum: 0,
75
+ },
76
+ VariableDeclarator: {
77
+ oneOf: [
78
+ {
79
+ type: "integer",
80
+ minimum: 0,
81
+ },
82
+ {
83
+ type: "object",
84
+ properties: {
85
+ var: {
86
+ type: "integer",
87
+ minimum: 0,
88
+ },
89
+ let: {
90
+ type: "integer",
91
+ minimum: 0,
92
+ },
93
+ const: {
94
+ type: "integer",
95
+ minimum: 0,
96
+ },
97
+ },
98
+ },
99
+ ],
100
+ },
101
+ outerIIFEBody: {
102
+ type: "integer",
103
+ minimum: 0,
104
+ },
105
+ MemberExpression: {
106
+ type: "integer",
107
+ minimum: 0,
108
+ },
109
+ FunctionDeclaration: {
110
+ type: "object",
111
+ properties: {
112
+ parameters: {
113
+ oneOf: [
114
+ {
115
+ type: "integer",
116
+ minimum: 0,
117
+ },
118
+ {
119
+ enum: ["first"],
120
+ },
121
+ ],
122
+ },
123
+ body: {
124
+ type: "integer",
125
+ minimum: 0,
126
+ },
127
+ },
128
+ },
129
+ FunctionExpression: {
130
+ type: "object",
131
+ properties: {
132
+ parameters: {
133
+ oneOf: [
134
+ {
135
+ type: "integer",
136
+ minimum: 0,
137
+ },
138
+ {
139
+ enum: ["first"],
140
+ },
141
+ ],
142
+ },
143
+ body: {
144
+ type: "integer",
145
+ minimum: 0,
146
+ },
147
+ },
148
+ },
149
+ CallExpression: {
150
+ type: "object",
151
+ properties: {
152
+ parameters: {
153
+ oneOf: [
154
+ {
155
+ type: "integer",
156
+ minimum: 0,
157
+ },
158
+ {
159
+ enum: ["first"],
160
+ },
161
+ ],
162
+ },
163
+ },
164
+ },
165
+ ArrayExpression: {
166
+ oneOf: [
167
+ {
168
+ type: "integer",
169
+ minimum: 0,
170
+ },
171
+ {
172
+ enum: ["first"],
173
+ },
174
+ ],
175
+ },
176
+ ObjectExpression: {
177
+ oneOf: [
178
+ {
179
+ type: "integer",
180
+ minimum: 0,
181
+ },
182
+ {
183
+ enum: ["first"],
184
+ },
185
+ ],
186
+ },
187
+ },
188
+ additionalProperties: false,
189
+ },
190
+ ],
191
+ messages: {
192
+ expected:
193
+ "Expected indentation of {{expected}} but found {{actual}}.",
194
+ },
195
+ },
196
+
197
+ create(context) {
198
+ const DEFAULT_VARIABLE_INDENT = 1;
199
+ const DEFAULT_PARAMETER_INDENT = null; // For backwards compatibility, don't check parameter indentation unless specified in the config
200
+ const DEFAULT_FUNCTION_BODY_INDENT = 1;
201
+
202
+ let indentType = "space";
203
+ let indentSize = 4;
204
+ const options = {
205
+ SwitchCase: 0,
206
+ VariableDeclarator: {
207
+ var: DEFAULT_VARIABLE_INDENT,
208
+ let: DEFAULT_VARIABLE_INDENT,
209
+ const: DEFAULT_VARIABLE_INDENT,
210
+ },
211
+ outerIIFEBody: null,
212
+ FunctionDeclaration: {
213
+ parameters: DEFAULT_PARAMETER_INDENT,
214
+ body: DEFAULT_FUNCTION_BODY_INDENT,
215
+ },
216
+ FunctionExpression: {
217
+ parameters: DEFAULT_PARAMETER_INDENT,
218
+ body: DEFAULT_FUNCTION_BODY_INDENT,
219
+ },
220
+ CallExpression: {
221
+ arguments: DEFAULT_PARAMETER_INDENT,
222
+ },
223
+ ArrayExpression: 1,
224
+ ObjectExpression: 1,
225
+ };
226
+
227
+ const sourceCode = context.sourceCode;
228
+
229
+ if (context.options.length) {
230
+ if (context.options[0] === "tab") {
231
+ indentSize = 1;
232
+ indentType = "tab";
233
+ } /* c8 ignore start */ else if (
234
+ typeof context.options[0] === "number"
235
+ ) {
236
+ indentSize = context.options[0];
237
+ indentType = "space";
238
+ } /* c8 ignore stop */
239
+
240
+ if (context.options[1]) {
241
+ const opts = context.options[1];
242
+
243
+ options.SwitchCase = opts.SwitchCase || 0;
244
+ const variableDeclaratorRules = opts.VariableDeclarator;
245
+
246
+ if (typeof variableDeclaratorRules === "number") {
247
+ options.VariableDeclarator = {
248
+ var: variableDeclaratorRules,
249
+ let: variableDeclaratorRules,
250
+ const: variableDeclaratorRules,
251
+ };
252
+ } else if (typeof variableDeclaratorRules === "object") {
253
+ Object.assign(
254
+ options.VariableDeclarator,
255
+ variableDeclaratorRules,
256
+ );
257
+ }
258
+
259
+ if (typeof opts.outerIIFEBody === "number") {
260
+ options.outerIIFEBody = opts.outerIIFEBody;
261
+ }
262
+
263
+ if (typeof opts.MemberExpression === "number") {
264
+ options.MemberExpression = opts.MemberExpression;
265
+ }
266
+
267
+ if (typeof opts.FunctionDeclaration === "object") {
268
+ Object.assign(
269
+ options.FunctionDeclaration,
270
+ opts.FunctionDeclaration,
271
+ );
272
+ }
273
+
274
+ if (typeof opts.FunctionExpression === "object") {
275
+ Object.assign(
276
+ options.FunctionExpression,
277
+ opts.FunctionExpression,
278
+ );
279
+ }
280
+
281
+ if (typeof opts.CallExpression === "object") {
282
+ Object.assign(options.CallExpression, opts.CallExpression);
283
+ }
284
+
285
+ if (
286
+ typeof opts.ArrayExpression === "number" ||
287
+ typeof opts.ArrayExpression === "string"
288
+ ) {
289
+ options.ArrayExpression = opts.ArrayExpression;
290
+ }
291
+
292
+ if (
293
+ typeof opts.ObjectExpression === "number" ||
294
+ typeof opts.ObjectExpression === "string"
295
+ ) {
296
+ options.ObjectExpression = opts.ObjectExpression;
297
+ }
298
+ }
299
+ }
300
+
301
+ const caseIndentStore = {};
302
+
303
+ /**
304
+ * Creates an error message for a line, given the expected/actual indentation.
305
+ * @param {int} expectedAmount The expected amount of indentation characters for this line
306
+ * @param {int} actualSpaces The actual number of indentation spaces that were found on this line
307
+ * @param {int} actualTabs The actual number of indentation tabs that were found on this line
308
+ * @returns {string} An error message for this line
309
+ */
310
+ function createErrorMessageData(
311
+ expectedAmount,
312
+ actualSpaces,
313
+ actualTabs,
314
+ ) {
315
+ const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs"
316
+ const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space"
317
+ const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs"
318
+ let foundStatement;
319
+
320
+ if (actualSpaces > 0 && actualTabs > 0) {
321
+ foundStatement = `${actualSpaces} ${foundSpacesWord} and ${actualTabs} ${foundTabsWord}`; // e.g. "1 space and 2 tabs"
322
+ } else if (actualSpaces > 0) {
323
+ /*
324
+ * Abbreviate the message if the expected indentation is also spaces.
325
+ * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces'
326
+ */
327
+ foundStatement =
328
+ indentType === "space"
329
+ ? actualSpaces
330
+ : `${actualSpaces} ${foundSpacesWord}`;
331
+ } else if (actualTabs > 0) {
332
+ foundStatement =
333
+ indentType === "tab"
334
+ ? actualTabs
335
+ : `${actualTabs} ${foundTabsWord}`;
336
+ } else {
337
+ foundStatement = "0";
338
+ }
339
+ return {
340
+ expected: expectedStatement,
341
+ actual: foundStatement,
342
+ };
343
+ }
344
+
345
+ /**
346
+ * Reports a given indent violation
347
+ * @param {ASTNode} node Node violating the indent rule
348
+ * @param {int} needed Expected indentation character count
349
+ * @param {int} gottenSpaces Indentation space count in the actual node/code
350
+ * @param {int} gottenTabs Indentation tab count in the actual node/code
351
+ * @param {Object} [loc] Error line and column location
352
+ * @param {boolean} isLastNodeCheck Is the error for last node check
353
+ * @returns {void}
354
+ */
355
+ function report(
356
+ node,
357
+ needed,
358
+ gottenSpaces,
359
+ gottenTabs,
360
+ loc,
361
+ isLastNodeCheck,
362
+ ) {
363
+ if (gottenSpaces && gottenTabs) {
364
+ // To avoid conflicts with `no-mixed-spaces-and-tabs`, don't report lines that have both spaces and tabs.
365
+ return;
366
+ }
367
+
368
+ const desiredIndent = (indentType === "space" ? " " : "\t").repeat(
369
+ needed,
370
+ );
371
+
372
+ const textRange = isLastNodeCheck
373
+ ? [
374
+ node.range[1] - node.loc.end.column,
375
+ node.range[1] -
376
+ node.loc.end.column +
377
+ gottenSpaces +
378
+ gottenTabs,
379
+ ]
380
+ : [
381
+ node.range[0] - node.loc.start.column,
382
+ node.range[0] -
383
+ node.loc.start.column +
384
+ gottenSpaces +
385
+ gottenTabs,
386
+ ];
387
+
388
+ context.report({
389
+ node,
390
+ loc,
391
+ messageId: "expected",
392
+ data: createErrorMessageData(needed, gottenSpaces, gottenTabs),
393
+ fix: fixer => fixer.replaceTextRange(textRange, desiredIndent),
394
+ });
395
+ }
396
+
397
+ /**
398
+ * Get the actual indent of node
399
+ * @param {ASTNode|Token} node Node to examine
400
+ * @param {boolean} [byLastLine=false] get indent of node's last line
401
+ * @returns {Object} The node's indent. Contains keys `space` and `tab`, representing the indent of each character. Also
402
+ * contains keys `goodChar` and `badChar`, where `goodChar` is the amount of the user's desired indentation character, and
403
+ * `badChar` is the amount of the other indentation character.
404
+ */
405
+ function getNodeIndent(node, byLastLine) {
406
+ const token = byLastLine
407
+ ? sourceCode.getLastToken(node)
408
+ : sourceCode.getFirstToken(node);
409
+ const srcCharsBeforeNode = sourceCode
410
+ .getText(token, token.loc.start.column)
411
+ .split("");
412
+ const indentChars = srcCharsBeforeNode.slice(
413
+ 0,
414
+ srcCharsBeforeNode.findIndex(
415
+ char => char !== " " && char !== "\t",
416
+ ),
417
+ );
418
+ const spaces = indentChars.filter(char => char === " ").length;
419
+ const tabs = indentChars.filter(char => char === "\t").length;
420
+
421
+ return {
422
+ space: spaces,
423
+ tab: tabs,
424
+ goodChar: indentType === "space" ? spaces : tabs,
425
+ badChar: indentType === "space" ? tabs : spaces,
426
+ };
427
+ }
428
+
429
+ /**
430
+ * Checks node is the first in its own start line. By default it looks by start line.
431
+ * @param {ASTNode} node The node to check
432
+ * @param {boolean} [byEndLocation=false] Lookup based on start position or end
433
+ * @returns {boolean} true if its the first in the its start line
434
+ */
435
+ function isNodeFirstInLine(node, byEndLocation) {
436
+ const firstToken =
437
+ byEndLocation === true
438
+ ? sourceCode.getLastToken(node, 1)
439
+ : sourceCode.getTokenBefore(node),
440
+ startLine =
441
+ byEndLocation === true
442
+ ? node.loc.end.line
443
+ : node.loc.start.line,
444
+ endLine = firstToken ? firstToken.loc.end.line : -1;
445
+
446
+ return startLine !== endLine;
447
+ }
448
+
449
+ /**
450
+ * Check indent for node
451
+ * @param {ASTNode} node Node to check
452
+ * @param {int} neededIndent needed indent
453
+ * @returns {void}
454
+ */
455
+ function checkNodeIndent(node, neededIndent) {
456
+ const actualIndent = getNodeIndent(node, false);
457
+
458
+ if (
459
+ node.type !== "ArrayExpression" &&
460
+ node.type !== "ObjectExpression" &&
461
+ (actualIndent.goodChar !== neededIndent ||
462
+ actualIndent.badChar !== 0) &&
463
+ isNodeFirstInLine(node)
464
+ ) {
465
+ report(
466
+ node,
467
+ neededIndent,
468
+ actualIndent.space,
469
+ actualIndent.tab,
470
+ );
471
+ }
472
+
473
+ if (node.type === "IfStatement" && node.alternate) {
474
+ const elseToken = sourceCode.getTokenBefore(node.alternate);
475
+
476
+ checkNodeIndent(elseToken, neededIndent);
477
+
478
+ if (!isNodeFirstInLine(node.alternate)) {
479
+ checkNodeIndent(node.alternate, neededIndent);
480
+ }
481
+ }
482
+
483
+ if (node.type === "TryStatement" && node.handler) {
484
+ const catchToken = sourceCode.getFirstToken(node.handler);
485
+
486
+ checkNodeIndent(catchToken, neededIndent);
487
+ }
488
+
489
+ if (node.type === "TryStatement" && node.finalizer) {
490
+ const finallyToken = sourceCode.getTokenBefore(node.finalizer);
491
+
492
+ checkNodeIndent(finallyToken, neededIndent);
493
+ }
494
+
495
+ if (node.type === "DoWhileStatement") {
496
+ const whileToken = sourceCode.getTokenAfter(node.body);
497
+
498
+ checkNodeIndent(whileToken, neededIndent);
499
+ }
500
+ }
501
+
502
+ /**
503
+ * Check indent for nodes list
504
+ * @param {ASTNode[]} nodes list of node objects
505
+ * @param {int} indent needed indent
506
+ * @returns {void}
507
+ */
508
+ function checkNodesIndent(nodes, indent) {
509
+ nodes.forEach(node => checkNodeIndent(node, indent));
510
+ }
511
+
512
+ /**
513
+ * Check last node line indent this detects, that block closed correctly
514
+ * @param {ASTNode} node Node to examine
515
+ * @param {int} lastLineIndent needed indent
516
+ * @returns {void}
517
+ */
518
+ function checkLastNodeLineIndent(node, lastLineIndent) {
519
+ const lastToken = sourceCode.getLastToken(node);
520
+ const endIndent = getNodeIndent(lastToken, true);
521
+
522
+ if (
523
+ (endIndent.goodChar !== lastLineIndent ||
524
+ endIndent.badChar !== 0) &&
525
+ isNodeFirstInLine(node, true)
526
+ ) {
527
+ report(
528
+ node,
529
+ lastLineIndent,
530
+ endIndent.space,
531
+ endIndent.tab,
532
+ {
533
+ line: lastToken.loc.start.line,
534
+ column: lastToken.loc.start.column,
535
+ },
536
+ true,
537
+ );
538
+ }
539
+ }
540
+
541
+ /**
542
+ * Check last node line indent this detects, that block closed correctly
543
+ * This function for more complicated return statement case, where closing parenthesis may be followed by ';'
544
+ * @param {ASTNode} node Node to examine
545
+ * @param {int} firstLineIndent first line needed indent
546
+ * @returns {void}
547
+ */
548
+ function checkLastReturnStatementLineIndent(node, firstLineIndent) {
549
+ /*
550
+ * in case if return statement ends with ');' we have traverse back to ')'
551
+ * otherwise we'll measure indent for ';' and replace ')'
552
+ */
553
+ const lastToken = sourceCode.getLastToken(
554
+ node,
555
+ astUtils.isClosingParenToken,
556
+ );
557
+ const textBeforeClosingParenthesis = sourceCode
558
+ .getText(lastToken, lastToken.loc.start.column)
559
+ .slice(0, -1);
560
+
561
+ if (textBeforeClosingParenthesis.trim()) {
562
+ // There are tokens before the closing paren, don't report this case
563
+ return;
564
+ }
565
+
566
+ const endIndent = getNodeIndent(lastToken, true);
567
+
568
+ if (endIndent.goodChar !== firstLineIndent) {
569
+ report(
570
+ node,
571
+ firstLineIndent,
572
+ endIndent.space,
573
+ endIndent.tab,
574
+ {
575
+ line: lastToken.loc.start.line,
576
+ column: lastToken.loc.start.column,
577
+ },
578
+ true,
579
+ );
580
+ }
581
+ }
582
+
583
+ /**
584
+ * Check first node line indent is correct
585
+ * @param {ASTNode} node Node to examine
586
+ * @param {int} firstLineIndent needed indent
587
+ * @returns {void}
588
+ */
589
+ function checkFirstNodeLineIndent(node, firstLineIndent) {
590
+ const startIndent = getNodeIndent(node, false);
591
+
592
+ if (
593
+ (startIndent.goodChar !== firstLineIndent ||
594
+ startIndent.badChar !== 0) &&
595
+ isNodeFirstInLine(node)
596
+ ) {
597
+ report(
598
+ node,
599
+ firstLineIndent,
600
+ startIndent.space,
601
+ startIndent.tab,
602
+ {
603
+ line: node.loc.start.line,
604
+ column: node.loc.start.column,
605
+ },
606
+ );
607
+ }
608
+ }
609
+
610
+ /**
611
+ * Returns a parent node of given node based on a specified type
612
+ * if not present then return null
613
+ * @param {ASTNode} node node to examine
614
+ * @param {string} type type that is being looked for
615
+ * @param {string} stopAtList end points for the evaluating code
616
+ * @returns {ASTNode|void} if found then node otherwise null
617
+ */
618
+ function getParentNodeByType(node, type, stopAtList) {
619
+ let parent = node.parent;
620
+ const stopAtSet = new Set(stopAtList || ["Program"]);
621
+
622
+ while (
623
+ parent.type !== type &&
624
+ !stopAtSet.has(parent.type) &&
625
+ parent.type !== "Program"
626
+ ) {
627
+ parent = parent.parent;
628
+ }
629
+
630
+ return parent.type === type ? parent : null;
631
+ }
632
+
633
+ /**
634
+ * Returns the VariableDeclarator based on the current node
635
+ * if not present then return null
636
+ * @param {ASTNode} node node to examine
637
+ * @returns {ASTNode|void} if found then node otherwise null
638
+ */
639
+ function getVariableDeclaratorNode(node) {
640
+ return getParentNodeByType(node, "VariableDeclarator");
641
+ }
642
+
643
+ /**
644
+ * Check to see if the node is part of the multi-line variable declaration.
645
+ * Also if its on the same line as the varNode
646
+ * @param {ASTNode} node node to check
647
+ * @param {ASTNode} varNode variable declaration node to check against
648
+ * @returns {boolean} True if all the above condition satisfy
649
+ */
650
+ function isNodeInVarOnTop(node, varNode) {
651
+ return (
652
+ varNode &&
653
+ varNode.parent.loc.start.line === node.loc.start.line &&
654
+ varNode.parent.declarations.length > 1
655
+ );
656
+ }
657
+
658
+ /**
659
+ * Check to see if the argument before the callee node is multi-line and
660
+ * there should only be 1 argument before the callee node
661
+ * @param {ASTNode} node node to check
662
+ * @returns {boolean} True if arguments are multi-line
663
+ */
664
+ function isArgBeforeCalleeNodeMultiline(node) {
665
+ const parent = node.parent;
666
+
667
+ if (parent.arguments.length >= 2 && parent.arguments[1] === node) {
668
+ return (
669
+ parent.arguments[0].loc.end.line >
670
+ parent.arguments[0].loc.start.line
671
+ );
672
+ }
673
+
674
+ return false;
675
+ }
676
+
677
+ /**
678
+ * Check to see if the node is a file level IIFE
679
+ * @param {ASTNode} node The function node to check.
680
+ * @returns {boolean} True if the node is the outer IIFE
681
+ */
682
+ function isOuterIIFE(node) {
683
+ const parent = node.parent;
684
+ let stmt = parent.parent;
685
+
686
+ /*
687
+ * Verify that the node is an IIEF
688
+ */
689
+ if (parent.type !== "CallExpression" || parent.callee !== node) {
690
+ return false;
691
+ }
692
+
693
+ /*
694
+ * Navigate legal ancestors to determine whether this IIEF is outer
695
+ */
696
+ while (
697
+ (stmt.type === "UnaryExpression" &&
698
+ (stmt.operator === "!" ||
699
+ stmt.operator === "~" ||
700
+ stmt.operator === "+" ||
701
+ stmt.operator === "-")) ||
702
+ stmt.type === "AssignmentExpression" ||
703
+ stmt.type === "LogicalExpression" ||
704
+ stmt.type === "SequenceExpression" ||
705
+ stmt.type === "VariableDeclarator"
706
+ ) {
707
+ stmt = stmt.parent;
708
+ }
709
+
710
+ return (
711
+ (stmt.type === "ExpressionStatement" ||
712
+ stmt.type === "VariableDeclaration") &&
713
+ stmt.parent &&
714
+ stmt.parent.type === "Program"
715
+ );
716
+ }
717
+
718
+ /**
719
+ * Check indent for function block content
720
+ * @param {ASTNode} node A BlockStatement node that is inside of a function.
721
+ * @returns {void}
722
+ */
723
+ function checkIndentInFunctionBlock(node) {
724
+ /*
725
+ * Search first caller in chain.
726
+ * Ex.:
727
+ *
728
+ * Models <- Identifier
729
+ * .User
730
+ * .find()
731
+ * .exec(function() {
732
+ * // function body
733
+ * });
734
+ *
735
+ * Looks for 'Models'
736
+ */
737
+ const calleeNode = node.parent; // FunctionExpression
738
+ let indent;
739
+
740
+ if (
741
+ calleeNode.parent &&
742
+ (calleeNode.parent.type === "Property" ||
743
+ calleeNode.parent.type === "ArrayExpression")
744
+ ) {
745
+ // If function is part of array or object, comma can be put at left
746
+ indent = getNodeIndent(calleeNode, false).goodChar;
747
+ } else {
748
+ // If function is standalone, simple calculate indent
749
+ indent = getNodeIndent(calleeNode).goodChar;
750
+ }
751
+
752
+ if (calleeNode.parent.type === "CallExpression") {
753
+ const calleeParent = calleeNode.parent;
754
+
755
+ if (
756
+ calleeNode.type !== "FunctionExpression" &&
757
+ calleeNode.type !== "ArrowFunctionExpression"
758
+ ) {
759
+ if (
760
+ calleeParent &&
761
+ calleeParent.loc.start.line < node.loc.start.line
762
+ ) {
763
+ indent = getNodeIndent(calleeParent).goodChar;
764
+ }
765
+ } else {
766
+ if (
767
+ isArgBeforeCalleeNodeMultiline(calleeNode) &&
768
+ calleeParent.callee.loc.start.line ===
769
+ calleeParent.callee.loc.end.line &&
770
+ !isNodeFirstInLine(calleeNode)
771
+ ) {
772
+ indent = getNodeIndent(calleeParent).goodChar;
773
+ }
774
+ }
775
+ }
776
+
777
+ /*
778
+ * function body indent should be indent + indent size, unless this
779
+ * is a FunctionDeclaration, FunctionExpression, or outer IIFE and the corresponding options are enabled.
780
+ */
781
+ let functionOffset = indentSize;
782
+
783
+ if (options.outerIIFEBody !== null && isOuterIIFE(calleeNode)) {
784
+ functionOffset = options.outerIIFEBody * indentSize;
785
+ } else if (calleeNode.type === "FunctionExpression") {
786
+ functionOffset = options.FunctionExpression.body * indentSize;
787
+ } else if (calleeNode.type === "FunctionDeclaration") {
788
+ functionOffset = options.FunctionDeclaration.body * indentSize;
789
+ }
790
+ indent += functionOffset;
791
+
792
+ // check if the node is inside a variable
793
+ const parentVarNode = getVariableDeclaratorNode(node);
794
+
795
+ if (parentVarNode && isNodeInVarOnTop(node, parentVarNode)) {
796
+ indent +=
797
+ indentSize *
798
+ options.VariableDeclarator[parentVarNode.parent.kind];
799
+ }
800
+
801
+ if (node.body.length > 0) {
802
+ checkNodesIndent(node.body, indent);
803
+ }
804
+
805
+ checkLastNodeLineIndent(node, indent - functionOffset);
806
+ }
807
+
808
+ /**
809
+ * Checks if the given node starts and ends on the same line
810
+ * @param {ASTNode} node The node to check
811
+ * @returns {boolean} Whether or not the block starts and ends on the same line.
812
+ */
813
+ function isSingleLineNode(node) {
814
+ const lastToken = sourceCode.getLastToken(node),
815
+ startLine = node.loc.start.line,
816
+ endLine = lastToken.loc.end.line;
817
+
818
+ return startLine === endLine;
819
+ }
820
+
821
+ /**
822
+ * Check indent for array block content or object block content
823
+ * @param {ASTNode} node node to examine
824
+ * @returns {void}
825
+ */
826
+ function checkIndentInArrayOrObjectBlock(node) {
827
+ // Skip inline
828
+ if (isSingleLineNode(node)) {
829
+ return;
830
+ }
831
+
832
+ let elements =
833
+ node.type === "ArrayExpression"
834
+ ? node.elements
835
+ : node.properties;
836
+
837
+ // filter out empty elements example would be [ , 2] so remove first element as espree considers it as null
838
+ elements = elements.filter(elem => elem !== null);
839
+
840
+ let nodeIndent;
841
+ let elementsIndent;
842
+ const parentVarNode = getVariableDeclaratorNode(node);
843
+
844
+ // TODO - come up with a better strategy in future
845
+ if (isNodeFirstInLine(node)) {
846
+ const parent = node.parent;
847
+
848
+ nodeIndent = getNodeIndent(parent).goodChar;
849
+ if (
850
+ !parentVarNode ||
851
+ parentVarNode.loc.start.line !== node.loc.start.line
852
+ ) {
853
+ if (
854
+ parent.type !== "VariableDeclarator" ||
855
+ parentVarNode === parentVarNode.parent.declarations[0]
856
+ ) {
857
+ if (
858
+ parent.type === "VariableDeclarator" &&
859
+ parentVarNode.loc.start.line ===
860
+ parent.loc.start.line
861
+ ) {
862
+ nodeIndent +=
863
+ indentSize *
864
+ options.VariableDeclarator[
865
+ parentVarNode.parent.kind
866
+ ];
867
+ } else if (
868
+ parent.type === "ObjectExpression" ||
869
+ parent.type === "ArrayExpression"
870
+ ) {
871
+ const parentElements =
872
+ node.parent.type === "ObjectExpression"
873
+ ? node.parent.properties
874
+ : node.parent.elements;
875
+
876
+ if (
877
+ parentElements[0] &&
878
+ parentElements[0].loc.start.line ===
879
+ parent.loc.start.line &&
880
+ parentElements[0].loc.end.line !==
881
+ parent.loc.start.line
882
+ ) {
883
+ /*
884
+ * If the first element of the array spans multiple lines, don't increase the expected indentation of the rest.
885
+ * e.g. [{
886
+ * foo: 1
887
+ * },
888
+ * {
889
+ * bar: 1
890
+ * }]
891
+ * the second object is not indented.
892
+ */
893
+ } else if (
894
+ typeof options[parent.type] === "number"
895
+ ) {
896
+ nodeIndent += options[parent.type] * indentSize;
897
+ } else {
898
+ nodeIndent = parentElements[0].loc.start.column;
899
+ }
900
+ } else if (
901
+ parent.type === "CallExpression" ||
902
+ parent.type === "NewExpression"
903
+ ) {
904
+ if (
905
+ typeof options.CallExpression.arguments ===
906
+ "number"
907
+ ) {
908
+ nodeIndent +=
909
+ options.CallExpression.arguments *
910
+ indentSize;
911
+ } else if (
912
+ options.CallExpression.arguments === "first"
913
+ ) {
914
+ if (parent.arguments.includes(node)) {
915
+ nodeIndent =
916
+ parent.arguments[0].loc.start.column;
917
+ }
918
+ } else {
919
+ nodeIndent += indentSize;
920
+ }
921
+ } else if (
922
+ parent.type === "LogicalExpression" ||
923
+ parent.type === "ArrowFunctionExpression"
924
+ ) {
925
+ nodeIndent += indentSize;
926
+ }
927
+ }
928
+ }
929
+
930
+ checkFirstNodeLineIndent(node, nodeIndent);
931
+ } else {
932
+ nodeIndent = getNodeIndent(node).goodChar;
933
+ }
934
+
935
+ if (options[node.type] === "first") {
936
+ elementsIndent = elements.length
937
+ ? elements[0].loc.start.column
938
+ : 0; // If there are no elements, elementsIndent doesn't matter.
939
+ } else {
940
+ elementsIndent = nodeIndent + indentSize * options[node.type];
941
+ }
942
+
943
+ /*
944
+ * Check if the node is a multiple variable declaration; if so, then
945
+ * make sure indentation takes that into account.
946
+ */
947
+ if (isNodeInVarOnTop(node, parentVarNode)) {
948
+ elementsIndent +=
949
+ indentSize *
950
+ options.VariableDeclarator[parentVarNode.parent.kind];
951
+ }
952
+
953
+ checkNodesIndent(elements, elementsIndent);
954
+
955
+ if (elements.length > 0) {
956
+ // Skip last block line check if last item in same line
957
+ if (elements.at(-1).loc.end.line === node.loc.end.line) {
958
+ return;
959
+ }
960
+ }
961
+
962
+ checkLastNodeLineIndent(
963
+ node,
964
+ nodeIndent +
965
+ (isNodeInVarOnTop(node, parentVarNode)
966
+ ? options.VariableDeclarator[
967
+ parentVarNode.parent.kind
968
+ ] * indentSize
969
+ : 0),
970
+ );
971
+ }
972
+
973
+ /**
974
+ * Check if the node or node body is a BlockStatement or not
975
+ * @param {ASTNode} node node to test
976
+ * @returns {boolean} True if it or its body is a block statement
977
+ */
978
+ function isNodeBodyBlock(node) {
979
+ return (
980
+ node.type === "BlockStatement" ||
981
+ node.type === "ClassBody" ||
982
+ (node.body && node.body.type === "BlockStatement") ||
983
+ (node.consequent && node.consequent.type === "BlockStatement")
984
+ );
985
+ }
986
+
987
+ /**
988
+ * Check indentation for blocks
989
+ * @param {ASTNode} node node to check
990
+ * @returns {void}
991
+ */
992
+ function blockIndentationCheck(node) {
993
+ // Skip inline blocks
994
+ if (isSingleLineNode(node)) {
995
+ return;
996
+ }
997
+
998
+ if (
999
+ node.parent &&
1000
+ (node.parent.type === "FunctionExpression" ||
1001
+ node.parent.type === "FunctionDeclaration" ||
1002
+ node.parent.type === "ArrowFunctionExpression")
1003
+ ) {
1004
+ checkIndentInFunctionBlock(node);
1005
+ return;
1006
+ }
1007
+
1008
+ let indent;
1009
+ let nodesToCheck;
1010
+
1011
+ /*
1012
+ * For this statements we should check indent from statement beginning,
1013
+ * not from the beginning of the block.
1014
+ */
1015
+ const statementsWithProperties = [
1016
+ "IfStatement",
1017
+ "WhileStatement",
1018
+ "ForStatement",
1019
+ "ForInStatement",
1020
+ "ForOfStatement",
1021
+ "DoWhileStatement",
1022
+ "ClassDeclaration",
1023
+ "TryStatement",
1024
+ ];
1025
+
1026
+ if (
1027
+ node.parent &&
1028
+ statementsWithProperties.includes(node.parent.type) &&
1029
+ isNodeBodyBlock(node)
1030
+ ) {
1031
+ indent = getNodeIndent(node.parent).goodChar;
1032
+ } else if (node.parent && node.parent.type === "CatchClause") {
1033
+ indent = getNodeIndent(node.parent.parent).goodChar;
1034
+ } else {
1035
+ indent = getNodeIndent(node).goodChar;
1036
+ }
1037
+
1038
+ if (
1039
+ node.type === "IfStatement" &&
1040
+ node.consequent.type !== "BlockStatement"
1041
+ ) {
1042
+ nodesToCheck = [node.consequent];
1043
+ } else if (Array.isArray(node.body)) {
1044
+ nodesToCheck = node.body;
1045
+ } else {
1046
+ nodesToCheck = [node.body];
1047
+ }
1048
+
1049
+ if (nodesToCheck.length > 0) {
1050
+ checkNodesIndent(nodesToCheck, indent + indentSize);
1051
+ }
1052
+
1053
+ if (node.type === "BlockStatement") {
1054
+ checkLastNodeLineIndent(node, indent);
1055
+ }
1056
+ }
1057
+
1058
+ /**
1059
+ * Filter out the elements which are on the same line of each other or the node.
1060
+ * basically have only 1 elements from each line except the variable declaration line.
1061
+ * @param {ASTNode} node Variable declaration node
1062
+ * @returns {ASTNode[]} Filtered elements
1063
+ */
1064
+ function filterOutSameLineVars(node) {
1065
+ return node.declarations.reduce((finalCollection, elem) => {
1066
+ const lastElem = finalCollection.at(-1);
1067
+
1068
+ if (
1069
+ (elem.loc.start.line !== node.loc.start.line &&
1070
+ !lastElem) ||
1071
+ (lastElem &&
1072
+ lastElem.loc.start.line !== elem.loc.start.line)
1073
+ ) {
1074
+ finalCollection.push(elem);
1075
+ }
1076
+
1077
+ return finalCollection;
1078
+ }, []);
1079
+ }
1080
+
1081
+ /**
1082
+ * Check indentation for variable declarations
1083
+ * @param {ASTNode} node node to examine
1084
+ * @returns {void}
1085
+ */
1086
+ function checkIndentInVariableDeclarations(node) {
1087
+ const elements = filterOutSameLineVars(node);
1088
+ const nodeIndent = getNodeIndent(node).goodChar;
1089
+ const lastElement = elements.at(-1);
1090
+
1091
+ const elementsIndent =
1092
+ nodeIndent + indentSize * options.VariableDeclarator[node.kind];
1093
+
1094
+ checkNodesIndent(elements, elementsIndent);
1095
+
1096
+ // Only check the last line if there is any token after the last item
1097
+ if (
1098
+ sourceCode.getLastToken(node).loc.end.line <=
1099
+ lastElement.loc.end.line
1100
+ ) {
1101
+ return;
1102
+ }
1103
+
1104
+ const tokenBeforeLastElement =
1105
+ sourceCode.getTokenBefore(lastElement);
1106
+
1107
+ if (tokenBeforeLastElement.value === ",") {
1108
+ // Special case for comma-first syntax where the semicolon is indented
1109
+ checkLastNodeLineIndent(
1110
+ node,
1111
+ getNodeIndent(tokenBeforeLastElement).goodChar,
1112
+ );
1113
+ } else {
1114
+ checkLastNodeLineIndent(node, elementsIndent - indentSize);
1115
+ }
1116
+ }
1117
+
1118
+ /**
1119
+ * Check and decide whether to check for indentation for blockless nodes
1120
+ * Scenarios are for or while statements without braces around them
1121
+ * @param {ASTNode} node node to examine
1122
+ * @returns {void}
1123
+ */
1124
+ function blockLessNodes(node) {
1125
+ if (node.body.type !== "BlockStatement") {
1126
+ blockIndentationCheck(node);
1127
+ }
1128
+ }
1129
+
1130
+ /**
1131
+ * Returns the expected indentation for the case statement
1132
+ * @param {ASTNode} node node to examine
1133
+ * @param {int} [providedSwitchIndent] indent for switch statement
1134
+ * @returns {int} indent size
1135
+ */
1136
+ function expectedCaseIndent(node, providedSwitchIndent) {
1137
+ const switchNode =
1138
+ node.type === "SwitchStatement" ? node : node.parent;
1139
+ const switchIndent =
1140
+ typeof providedSwitchIndent === "undefined"
1141
+ ? getNodeIndent(switchNode).goodChar
1142
+ : providedSwitchIndent;
1143
+ let caseIndent;
1144
+
1145
+ if (caseIndentStore[switchNode.loc.start.line]) {
1146
+ return caseIndentStore[switchNode.loc.start.line];
1147
+ }
1148
+
1149
+ if (switchNode.cases.length > 0 && options.SwitchCase === 0) {
1150
+ caseIndent = switchIndent;
1151
+ } else {
1152
+ caseIndent = switchIndent + indentSize * options.SwitchCase;
1153
+ }
1154
+
1155
+ caseIndentStore[switchNode.loc.start.line] = caseIndent;
1156
+ return caseIndent;
1157
+ }
1158
+
1159
+ /**
1160
+ * Checks whether a return statement is wrapped in ()
1161
+ * @param {ASTNode} node node to examine
1162
+ * @returns {boolean} the result
1163
+ */
1164
+ function isWrappedInParenthesis(node) {
1165
+ const regex = /^return\s*?\(\s*?\);*?/u;
1166
+
1167
+ const statementWithoutArgument = sourceCode
1168
+ .getText(node)
1169
+ .replace(sourceCode.getText(node.argument), "");
1170
+
1171
+ return regex.test(statementWithoutArgument);
1172
+ }
1173
+
1174
+ return {
1175
+ Program(node) {
1176
+ if (node.body.length > 0) {
1177
+ // Root nodes should have no indent
1178
+ checkNodesIndent(node.body, getNodeIndent(node).goodChar);
1179
+ }
1180
+ },
1181
+
1182
+ ClassBody: blockIndentationCheck,
1183
+
1184
+ BlockStatement: blockIndentationCheck,
1185
+
1186
+ WhileStatement: blockLessNodes,
1187
+
1188
+ ForStatement: blockLessNodes,
1189
+
1190
+ ForInStatement: blockLessNodes,
1191
+
1192
+ ForOfStatement: blockLessNodes,
1193
+
1194
+ DoWhileStatement: blockLessNodes,
1195
+
1196
+ IfStatement(node) {
1197
+ if (
1198
+ node.consequent.type !== "BlockStatement" &&
1199
+ node.consequent.loc.start.line > node.loc.start.line
1200
+ ) {
1201
+ blockIndentationCheck(node);
1202
+ }
1203
+ },
1204
+
1205
+ VariableDeclaration(node) {
1206
+ if (
1207
+ node.declarations.at(-1).loc.start.line >
1208
+ node.declarations[0].loc.start.line
1209
+ ) {
1210
+ checkIndentInVariableDeclarations(node);
1211
+ }
1212
+ },
1213
+
1214
+ ObjectExpression(node) {
1215
+ checkIndentInArrayOrObjectBlock(node);
1216
+ },
1217
+
1218
+ ArrayExpression(node) {
1219
+ checkIndentInArrayOrObjectBlock(node);
1220
+ },
1221
+
1222
+ MemberExpression(node) {
1223
+ if (typeof options.MemberExpression === "undefined") {
1224
+ return;
1225
+ }
1226
+
1227
+ if (isSingleLineNode(node)) {
1228
+ return;
1229
+ }
1230
+
1231
+ /*
1232
+ * The typical layout of variable declarations and assignments
1233
+ * alter the expectation of correct indentation. Skip them.
1234
+ * TODO: Add appropriate configuration options for variable
1235
+ * declarations and assignments.
1236
+ */
1237
+ if (
1238
+ getParentNodeByType(node, "VariableDeclarator", [
1239
+ "FunctionExpression",
1240
+ "ArrowFunctionExpression",
1241
+ ])
1242
+ ) {
1243
+ return;
1244
+ }
1245
+
1246
+ if (
1247
+ getParentNodeByType(node, "AssignmentExpression", [
1248
+ "FunctionExpression",
1249
+ ])
1250
+ ) {
1251
+ return;
1252
+ }
1253
+
1254
+ const propertyIndent =
1255
+ getNodeIndent(node).goodChar +
1256
+ indentSize * options.MemberExpression;
1257
+
1258
+ const checkNodes = [node.property];
1259
+
1260
+ const dot = sourceCode.getTokenBefore(node.property);
1261
+
1262
+ if (dot.type === "Punctuator" && dot.value === ".") {
1263
+ checkNodes.push(dot);
1264
+ }
1265
+
1266
+ checkNodesIndent(checkNodes, propertyIndent);
1267
+ },
1268
+
1269
+ SwitchStatement(node) {
1270
+ // Switch is not a 'BlockStatement'
1271
+ const switchIndent = getNodeIndent(node).goodChar;
1272
+ const caseIndent = expectedCaseIndent(node, switchIndent);
1273
+
1274
+ checkNodesIndent(node.cases, caseIndent);
1275
+
1276
+ checkLastNodeLineIndent(node, switchIndent);
1277
+ },
1278
+
1279
+ SwitchCase(node) {
1280
+ // Skip inline cases
1281
+ if (isSingleLineNode(node)) {
1282
+ return;
1283
+ }
1284
+ const caseIndent = expectedCaseIndent(node);
1285
+
1286
+ checkNodesIndent(node.consequent, caseIndent + indentSize);
1287
+ },
1288
+
1289
+ FunctionDeclaration(node) {
1290
+ if (isSingleLineNode(node)) {
1291
+ return;
1292
+ }
1293
+ if (
1294
+ options.FunctionDeclaration.parameters === "first" &&
1295
+ node.params.length
1296
+ ) {
1297
+ checkNodesIndent(
1298
+ node.params.slice(1),
1299
+ node.params[0].loc.start.column,
1300
+ );
1301
+ } else if (options.FunctionDeclaration.parameters !== null) {
1302
+ checkNodesIndent(
1303
+ node.params,
1304
+ getNodeIndent(node).goodChar +
1305
+ indentSize * options.FunctionDeclaration.parameters,
1306
+ );
1307
+ }
1308
+ },
1309
+
1310
+ FunctionExpression(node) {
1311
+ if (isSingleLineNode(node)) {
1312
+ return;
1313
+ }
1314
+ if (
1315
+ options.FunctionExpression.parameters === "first" &&
1316
+ node.params.length
1317
+ ) {
1318
+ checkNodesIndent(
1319
+ node.params.slice(1),
1320
+ node.params[0].loc.start.column,
1321
+ );
1322
+ } else if (options.FunctionExpression.parameters !== null) {
1323
+ checkNodesIndent(
1324
+ node.params,
1325
+ getNodeIndent(node).goodChar +
1326
+ indentSize * options.FunctionExpression.parameters,
1327
+ );
1328
+ }
1329
+ },
1330
+
1331
+ ReturnStatement(node) {
1332
+ if (isSingleLineNode(node)) {
1333
+ return;
1334
+ }
1335
+
1336
+ const firstLineIndent = getNodeIndent(node).goodChar;
1337
+
1338
+ // in case if return statement is wrapped in parenthesis
1339
+ if (isWrappedInParenthesis(node)) {
1340
+ checkLastReturnStatementLineIndent(node, firstLineIndent);
1341
+ } else {
1342
+ checkNodeIndent(node, firstLineIndent);
1343
+ }
1344
+ },
1345
+
1346
+ CallExpression(node) {
1347
+ if (isSingleLineNode(node)) {
1348
+ return;
1349
+ }
1350
+ if (
1351
+ options.CallExpression.arguments === "first" &&
1352
+ node.arguments.length
1353
+ ) {
1354
+ checkNodesIndent(
1355
+ node.arguments.slice(1),
1356
+ node.arguments[0].loc.start.column,
1357
+ );
1358
+ } else if (options.CallExpression.arguments !== null) {
1359
+ checkNodesIndent(
1360
+ node.arguments,
1361
+ getNodeIndent(node).goodChar +
1362
+ indentSize * options.CallExpression.arguments,
1363
+ );
1364
+ }
1365
+ },
1366
+ };
1367
+ },
1142
1368
  };