eslint 9.21.0 → 9.23.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 (425) hide show
  1. package/README.md +52 -48
  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 +828 -808
  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 +541 -457
  21. package/lib/config/config-loader.js +648 -618
  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 +65 -68
  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 +12 -0
  29. package/lib/eslint/eslint-helpers.js +709 -679
  30. package/lib/eslint/eslint.js +944 -886
  31. package/lib/eslint/index.js +2 -2
  32. package/lib/eslint/legacy-eslint.js +576 -532
  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 +1129 -1054
  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 +2402 -2044
  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 +35 -35
  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 +375 -361
  71. package/lib/rule-tester/index.js +1 -1
  72. package/lib/rule-tester/rule-tester.js +1307 -1045
  73. package/lib/rules/accessor-pairs.js +297 -262
  74. package/lib/rules/array-bracket-newline.js +249 -237
  75. package/lib/rules/array-bracket-spacing.js +262 -223
  76. package/lib/rules/array-callback-return.js +401 -355
  77. package/lib/rules/array-element-newline.js +357 -312
  78. package/lib/rules/arrow-body-style.js +399 -280
  79. package/lib/rules/arrow-parens.js +205 -172
  80. package/lib/rules/arrow-spacing.js +168 -162
  81. package/lib/rules/block-scoped-var.js +124 -122
  82. package/lib/rules/block-spacing.js +185 -175
  83. package/lib/rules/brace-style.js +261 -198
  84. package/lib/rules/callback-return.js +202 -189
  85. package/lib/rules/camelcase.js +402 -391
  86. package/lib/rules/capitalized-comments.js +252 -231
  87. package/lib/rules/class-methods-use-this.js +179 -171
  88. package/lib/rules/comma-dangle.js +378 -345
  89. package/lib/rules/comma-spacing.js +192 -194
  90. package/lib/rules/comma-style.js +374 -315
  91. package/lib/rules/complexity.js +172 -168
  92. package/lib/rules/computed-property-spacing.js +235 -210
  93. package/lib/rules/consistent-return.js +180 -169
  94. package/lib/rules/consistent-this.js +166 -146
  95. package/lib/rules/constructor-super.js +411 -403
  96. package/lib/rules/curly.js +406 -331
  97. package/lib/rules/default-case-last.js +37 -30
  98. package/lib/rules/default-case.js +88 -84
  99. package/lib/rules/default-param-last.js +68 -53
  100. package/lib/rules/dot-location.js +121 -109
  101. package/lib/rules/dot-notation.js +191 -155
  102. package/lib/rules/eol-last.js +121 -119
  103. package/lib/rules/eqeqeq.js +167 -154
  104. package/lib/rules/for-direction.js +145 -120
  105. package/lib/rules/func-call-spacing.js +260 -230
  106. package/lib/rules/func-name-matching.js +292 -208
  107. package/lib/rules/func-names.js +164 -163
  108. package/lib/rules/func-style.js +158 -126
  109. package/lib/rules/function-call-argument-newline.js +151 -128
  110. package/lib/rules/function-paren-newline.js +348 -290
  111. package/lib/rules/generator-star-spacing.js +228 -209
  112. package/lib/rules/getter-return.js +207 -171
  113. package/lib/rules/global-require.js +84 -73
  114. package/lib/rules/grouped-accessor-pairs.js +169 -149
  115. package/lib/rules/guard-for-in.js +71 -62
  116. package/lib/rules/handle-callback-err.js +107 -102
  117. package/lib/rules/id-blacklist.js +181 -198
  118. package/lib/rules/id-denylist.js +167 -186
  119. package/lib/rules/id-length.js +196 -170
  120. package/lib/rules/id-match.js +343 -288
  121. package/lib/rules/implicit-arrow-linebreak.js +101 -78
  122. package/lib/rules/indent-legacy.js +1343 -1117
  123. package/lib/rules/indent.js +2271 -1758
  124. package/lib/rules/index.js +317 -292
  125. package/lib/rules/init-declarations.js +115 -106
  126. package/lib/rules/jsx-quotes.js +93 -81
  127. package/lib/rules/key-spacing.js +749 -632
  128. package/lib/rules/keyword-spacing.js +647 -604
  129. package/lib/rules/line-comment-position.js +141 -127
  130. package/lib/rules/linebreak-style.js +106 -105
  131. package/lib/rules/lines-around-comment.js +539 -447
  132. package/lib/rules/lines-around-directive.js +232 -202
  133. package/lib/rules/lines-between-class-members.js +304 -233
  134. package/lib/rules/logical-assignment-operators.js +581 -398
  135. package/lib/rules/max-classes-per-file.js +68 -67
  136. package/lib/rules/max-depth.js +145 -142
  137. package/lib/rules/max-len.js +472 -433
  138. package/lib/rules/max-lines-per-function.js +200 -175
  139. package/lib/rules/max-lines.js +157 -161
  140. package/lib/rules/max-nested-callbacks.js +101 -103
  141. package/lib/rules/max-params.js +77 -75
  142. package/lib/rules/max-statements-per-line.js +204 -197
  143. package/lib/rules/max-statements.js +167 -163
  144. package/lib/rules/multiline-comment-style.js +636 -478
  145. package/lib/rules/multiline-ternary.js +240 -175
  146. package/lib/rules/new-cap.js +232 -212
  147. package/lib/rules/new-parens.js +87 -78
  148. package/lib/rules/newline-after-var.js +286 -249
  149. package/lib/rules/newline-before-return.js +228 -221
  150. package/lib/rules/newline-per-chained-call.js +141 -126
  151. package/lib/rules/no-alert.js +89 -78
  152. package/lib/rules/no-array-constructor.js +121 -112
  153. package/lib/rules/no-async-promise-executor.js +29 -23
  154. package/lib/rules/no-await-in-loop.js +68 -70
  155. package/lib/rules/no-bitwise.js +123 -99
  156. package/lib/rules/no-buffer-constructor.js +54 -46
  157. package/lib/rules/no-caller.js +38 -32
  158. package/lib/rules/no-case-declarations.js +60 -56
  159. package/lib/rules/no-catch-shadow.js +75 -72
  160. package/lib/rules/no-class-assign.js +50 -47
  161. package/lib/rules/no-compare-neg-zero.js +61 -47
  162. package/lib/rules/no-cond-assign.js +147 -131
  163. package/lib/rules/no-confusing-arrow.js +97 -80
  164. package/lib/rules/no-console.js +201 -190
  165. package/lib/rules/no-const-assign.js +46 -40
  166. package/lib/rules/no-constant-binary-expression.js +499 -404
  167. package/lib/rules/no-constant-condition.js +157 -142
  168. package/lib/rules/no-constructor-return.js +48 -48
  169. package/lib/rules/no-continue.js +24 -26
  170. package/lib/rules/no-control-regex.js +124 -120
  171. package/lib/rules/no-debugger.js +27 -29
  172. package/lib/rules/no-delete-var.js +28 -28
  173. package/lib/rules/no-div-regex.js +46 -40
  174. package/lib/rules/no-dupe-args.js +67 -68
  175. package/lib/rules/no-dupe-class-members.js +92 -88
  176. package/lib/rules/no-dupe-else-if.js +99 -76
  177. package/lib/rules/no-dupe-keys.js +132 -109
  178. package/lib/rules/no-duplicate-case.js +49 -42
  179. package/lib/rules/no-duplicate-imports.js +178 -175
  180. package/lib/rules/no-else-return.js +429 -384
  181. package/lib/rules/no-empty-character-class.js +56 -49
  182. package/lib/rules/no-empty-function.js +126 -127
  183. package/lib/rules/no-empty-pattern.js +62 -57
  184. package/lib/rules/no-empty-static-block.js +36 -34
  185. package/lib/rules/no-empty.js +97 -85
  186. package/lib/rules/no-eq-null.js +36 -31
  187. package/lib/rules/no-eval.js +255 -249
  188. package/lib/rules/no-ex-assign.js +41 -38
  189. package/lib/rules/no-extend-native.js +160 -158
  190. package/lib/rules/no-extra-bind.js +200 -189
  191. package/lib/rules/no-extra-boolean-cast.js +397 -347
  192. package/lib/rules/no-extra-label.js +149 -130
  193. package/lib/rules/no-extra-parens.js +1653 -1324
  194. package/lib/rules/no-extra-semi.js +145 -143
  195. package/lib/rules/no-fallthrough.js +198 -156
  196. package/lib/rules/no-floating-decimal.js +73 -65
  197. package/lib/rules/no-func-assign.js +53 -54
  198. package/lib/rules/no-global-assign.js +77 -72
  199. package/lib/rules/no-implicit-coercion.js +348 -292
  200. package/lib/rules/no-implicit-globals.js +157 -134
  201. package/lib/rules/no-implied-eval.js +139 -111
  202. package/lib/rules/no-import-assign.js +144 -158
  203. package/lib/rules/no-inline-comments.js +100 -94
  204. package/lib/rules/no-inner-declarations.js +114 -100
  205. package/lib/rules/no-invalid-regexp.js +221 -189
  206. package/lib/rules/no-invalid-this.js +122 -116
  207. package/lib/rules/no-irregular-whitespace.js +265 -251
  208. package/lib/rules/no-iterator.js +28 -32
  209. package/lib/rules/no-label-var.js +58 -61
  210. package/lib/rules/no-labels.js +137 -132
  211. package/lib/rules/no-lone-blocks.js +126 -122
  212. package/lib/rules/no-lonely-if.js +107 -76
  213. package/lib/rules/no-loop-func.js +233 -212
  214. package/lib/rules/no-loss-of-precision.js +215 -200
  215. package/lib/rules/no-magic-numbers.js +245 -217
  216. package/lib/rules/no-misleading-character-class.js +498 -445
  217. package/lib/rules/no-mixed-operators.js +187 -181
  218. package/lib/rules/no-mixed-requires.js +252 -239
  219. package/lib/rules/no-mixed-spaces-and-tabs.js +133 -120
  220. package/lib/rules/no-multi-assign.js +45 -43
  221. package/lib/rules/no-multi-spaces.js +162 -142
  222. package/lib/rules/no-multi-str.js +41 -40
  223. package/lib/rules/no-multiple-empty-lines.js +195 -157
  224. package/lib/rules/no-native-reassign.js +89 -84
  225. package/lib/rules/no-negated-condition.js +78 -74
  226. package/lib/rules/no-negated-in-lhs.js +44 -42
  227. package/lib/rules/no-nested-ternary.js +32 -31
  228. package/lib/rules/no-new-func.js +70 -61
  229. package/lib/rules/no-new-native-nonconstructor.js +42 -38
  230. package/lib/rules/no-new-object.js +47 -47
  231. package/lib/rules/no-new-require.js +47 -46
  232. package/lib/rules/no-new-symbol.js +51 -49
  233. package/lib/rules/no-new-wrappers.js +42 -40
  234. package/lib/rules/no-new.js +27 -28
  235. package/lib/rules/no-nonoctal-decimal-escape.js +140 -120
  236. package/lib/rules/no-obj-calls.js +65 -52
  237. package/lib/rules/no-object-constructor.js +103 -96
  238. package/lib/rules/no-octal-escape.js +39 -42
  239. package/lib/rules/no-octal.js +31 -31
  240. package/lib/rules/no-param-reassign.js +234 -216
  241. package/lib/rules/no-path-concat.js +65 -66
  242. package/lib/rules/no-plusplus.js +59 -60
  243. package/lib/rules/no-process-env.js +48 -47
  244. package/lib/rules/no-process-exit.js +53 -49
  245. package/lib/rules/no-promise-executor-return.js +213 -181
  246. package/lib/rules/no-proto.js +25 -28
  247. package/lib/rules/no-prototype-builtins.js +145 -123
  248. package/lib/rules/no-redeclare.js +153 -151
  249. package/lib/rules/no-regex-spaces.js +182 -160
  250. package/lib/rules/no-restricted-exports.js +207 -184
  251. package/lib/rules/no-restricted-globals.js +110 -111
  252. package/lib/rules/no-restricted-imports.js +656 -536
  253. package/lib/rules/no-restricted-modules.js +221 -201
  254. package/lib/rules/no-restricted-properties.js +180 -152
  255. package/lib/rules/no-restricted-syntax.js +55 -51
  256. package/lib/rules/no-return-assign.js +54 -49
  257. package/lib/rules/no-return-await.js +147 -123
  258. package/lib/rules/no-script-url.js +51 -44
  259. package/lib/rules/no-self-assign.js +147 -145
  260. package/lib/rules/no-self-compare.js +62 -45
  261. package/lib/rules/no-sequences.js +134 -115
  262. package/lib/rules/no-setter-return.js +184 -151
  263. package/lib/rules/no-shadow-restricted-names.js +60 -45
  264. package/lib/rules/no-shadow.js +341 -315
  265. package/lib/rules/no-spaced-func.js +81 -76
  266. package/lib/rules/no-sparse-arrays.js +53 -58
  267. package/lib/rules/no-sync.js +60 -59
  268. package/lib/rules/no-tabs.js +82 -71
  269. package/lib/rules/no-template-curly-in-string.js +32 -31
  270. package/lib/rules/no-ternary.js +24 -28
  271. package/lib/rules/no-this-before-super.js +320 -318
  272. package/lib/rules/no-throw-literal.js +30 -35
  273. package/lib/rules/no-trailing-spaces.js +198 -190
  274. package/lib/rules/no-undef-init.js +75 -60
  275. package/lib/rules/no-undef.js +50 -47
  276. package/lib/rules/no-undefined.js +72 -74
  277. package/lib/rules/no-underscore-dangle.js +369 -326
  278. package/lib/rules/no-unexpected-multiline.js +111 -101
  279. package/lib/rules/no-unmodified-loop-condition.js +253 -253
  280. package/lib/rules/no-unneeded-ternary.js +211 -146
  281. package/lib/rules/no-unreachable-loop.js +144 -141
  282. package/lib/rules/no-unreachable.js +254 -247
  283. package/lib/rules/no-unsafe-finally.js +92 -84
  284. package/lib/rules/no-unsafe-negation.js +104 -82
  285. package/lib/rules/no-unsafe-optional-chaining.js +191 -177
  286. package/lib/rules/no-unused-expressions.js +177 -161
  287. package/lib/rules/no-unused-labels.js +138 -123
  288. package/lib/rules/no-unused-private-class-members.js +205 -181
  289. package/lib/rules/no-unused-vars.js +1668 -1448
  290. package/lib/rules/no-use-before-define.js +228 -230
  291. package/lib/rules/no-useless-assignment.js +589 -510
  292. package/lib/rules/no-useless-backreference.js +211 -192
  293. package/lib/rules/no-useless-call.js +57 -52
  294. package/lib/rules/no-useless-catch.js +39 -39
  295. package/lib/rules/no-useless-computed-key.js +143 -114
  296. package/lib/rules/no-useless-concat.js +64 -59
  297. package/lib/rules/no-useless-constructor.js +157 -110
  298. package/lib/rules/no-useless-escape.js +341 -290
  299. package/lib/rules/no-useless-rename.js +182 -155
  300. package/lib/rules/no-useless-return.js +343 -311
  301. package/lib/rules/no-var.js +232 -211
  302. package/lib/rules/no-void.js +49 -47
  303. package/lib/rules/no-warning-comments.js +190 -185
  304. package/lib/rules/no-whitespace-before-property.js +130 -114
  305. package/lib/rules/no-with.js +23 -25
  306. package/lib/rules/nonblock-statement-body-position.js +148 -129
  307. package/lib/rules/object-curly-newline.js +305 -264
  308. package/lib/rules/object-curly-spacing.js +359 -313
  309. package/lib/rules/object-property-newline.js +136 -105
  310. package/lib/rules/object-shorthand.js +606 -501
  311. package/lib/rules/one-var-declaration-per-line.js +103 -99
  312. package/lib/rules/one-var.js +652 -536
  313. package/lib/rules/operator-assignment.js +218 -160
  314. package/lib/rules/operator-linebreak.js +294 -250
  315. package/lib/rules/padded-blocks.js +345 -307
  316. package/lib/rules/padding-line-between-statements.js +442 -438
  317. package/lib/rules/prefer-arrow-callback.js +361 -312
  318. package/lib/rules/prefer-const.js +417 -376
  319. package/lib/rules/prefer-destructuring.js +300 -278
  320. package/lib/rules/prefer-exponentiation-operator.js +175 -132
  321. package/lib/rules/prefer-named-capture-group.js +152 -139
  322. package/lib/rules/prefer-numeric-literals.js +120 -112
  323. package/lib/rules/prefer-object-has-own.js +115 -81
  324. package/lib/rules/prefer-object-spread.js +212 -192
  325. package/lib/rules/prefer-promise-reject-errors.js +139 -121
  326. package/lib/rules/prefer-reflect.js +126 -106
  327. package/lib/rules/prefer-regex-literals.js +577 -465
  328. package/lib/rules/prefer-rest-params.js +78 -79
  329. package/lib/rules/prefer-spread.js +46 -43
  330. package/lib/rules/prefer-template.js +265 -194
  331. package/lib/rules/quote-props.js +372 -306
  332. package/lib/rules/quotes.js +373 -325
  333. package/lib/rules/radix.js +151 -135
  334. package/lib/rules/require-atomic-updates.js +315 -284
  335. package/lib/rules/require-await.js +143 -115
  336. package/lib/rules/require-unicode-regexp.js +281 -176
  337. package/lib/rules/require-yield.js +52 -53
  338. package/lib/rules/rest-spread-spacing.js +127 -115
  339. package/lib/rules/semi-spacing.js +280 -249
  340. package/lib/rules/semi-style.js +175 -133
  341. package/lib/rules/semi.js +455 -435
  342. package/lib/rules/sort-imports.js +305 -232
  343. package/lib/rules/sort-keys.js +218 -187
  344. package/lib/rules/sort-vars.js +126 -92
  345. package/lib/rules/space-before-blocks.js +198 -188
  346. package/lib/rules/space-before-function-paren.js +185 -165
  347. package/lib/rules/space-in-parens.js +358 -287
  348. package/lib/rules/space-infix-ops.js +236 -200
  349. package/lib/rules/space-unary-ops.js +355 -297
  350. package/lib/rules/spaced-comment.js +362 -318
  351. package/lib/rules/strict.js +264 -229
  352. package/lib/rules/switch-colon-spacing.js +129 -121
  353. package/lib/rules/symbol-description.js +44 -47
  354. package/lib/rules/template-curly-spacing.js +147 -141
  355. package/lib/rules/template-tag-spacing.js +97 -87
  356. package/lib/rules/unicode-bom.js +53 -55
  357. package/lib/rules/use-isnan.js +236 -205
  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 +152 -110
  370. package/lib/rules/vars-on-top.js +151 -144
  371. package/lib/rules/wrap-iife.js +203 -190
  372. package/lib/rules/wrap-regex.js +69 -57
  373. package/lib/rules/yield-star-spacing.js +144 -133
  374. package/lib/rules/yoda.js +282 -271
  375. package/lib/services/parser-service.js +35 -35
  376. package/lib/services/processor-service.js +66 -73
  377. package/lib/shared/ajv.js +14 -14
  378. package/lib/shared/assert.js +3 -4
  379. package/lib/shared/ast-utils.js +7 -6
  380. package/lib/shared/deep-merge-arrays.js +24 -22
  381. package/lib/shared/directives.js +3 -2
  382. package/lib/shared/flags.js +46 -17
  383. package/lib/shared/logging.js +24 -25
  384. package/lib/shared/option-utils.js +43 -36
  385. package/lib/shared/runtime-info.js +136 -127
  386. package/lib/shared/serialization.js +27 -27
  387. package/lib/shared/severity.js +22 -22
  388. package/lib/shared/stats.js +5 -5
  389. package/lib/shared/string-utils.js +16 -16
  390. package/lib/shared/text-table.js +28 -27
  391. package/lib/shared/traverser.js +153 -146
  392. package/lib/types/config-api.d.ts +8 -0
  393. package/lib/types/index.d.ts +2010 -1559
  394. package/lib/types/rules.d.ts +5312 -0
  395. package/lib/types/use-at-your-own-risk.d.ts +32 -30
  396. package/lib/unsupported-api.js +5 -5
  397. package/messages/all-files-ignored.js +3 -3
  398. package/messages/all-matched-files-ignored.js +3 -3
  399. package/messages/config-file-missing.js +2 -2
  400. package/messages/config-plugin-missing.js +3 -3
  401. package/messages/config-serialize-function.js +9 -7
  402. package/messages/eslintrc-incompat.js +13 -15
  403. package/messages/eslintrc-plugins.js +3 -4
  404. package/messages/extend-config-missing.js +3 -3
  405. package/messages/failed-to-read-json.js +3 -3
  406. package/messages/file-not-found.js +3 -3
  407. package/messages/invalid-rule-options.js +2 -2
  408. package/messages/invalid-rule-severity.js +2 -2
  409. package/messages/no-config-found.js +3 -3
  410. package/messages/plugin-conflict.js +8 -8
  411. package/messages/plugin-invalid.js +3 -3
  412. package/messages/plugin-missing.js +3 -3
  413. package/messages/print-config-with-directory-path.js +2 -2
  414. package/messages/shared.js +6 -1
  415. package/messages/whitespace-found.js +3 -3
  416. package/package.json +22 -20
  417. package/lib/types/rules/best-practices.d.ts +0 -1143
  418. package/lib/types/rules/deprecated.d.ts +0 -252
  419. package/lib/types/rules/ecmascript-6.d.ts +0 -647
  420. package/lib/types/rules/index.d.ts +0 -50
  421. package/lib/types/rules/node-commonjs.d.ts +0 -171
  422. package/lib/types/rules/possible-errors.d.ts +0 -685
  423. package/lib/types/rules/strict-mode.d.ts +0 -38
  424. package/lib/types/rules/stylistic-issues.d.ts +0 -2043
  425. package/lib/types/rules/variables.d.ts +0 -234
@@ -22,1121 +22,1347 @@ const astUtils = require("./utils/ast-utils");
22
22
  /* c8 ignore next */
23
23
  /** @type {import('../shared/types').Rule} */
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
  };