eslint 9.22.0 → 9.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (417) hide show
  1. package/README.md +48 -46
  2. package/bin/eslint.js +92 -90
  3. package/conf/default-cli-options.js +22 -22
  4. package/conf/ecma-version.js +1 -1
  5. package/conf/globals.js +97 -98
  6. package/conf/replacements.json +24 -20
  7. package/conf/rule-type-list.json +88 -92
  8. package/lib/api.js +12 -12
  9. package/lib/cli-engine/cli-engine.js +830 -810
  10. package/lib/cli-engine/file-enumerator.js +381 -387
  11. package/lib/cli-engine/formatters/formatters-meta.json +16 -16
  12. package/lib/cli-engine/formatters/html.js +107 -99
  13. package/lib/cli-engine/formatters/json-with-metadata.js +5 -5
  14. package/lib/cli-engine/formatters/json.js +2 -2
  15. package/lib/cli-engine/formatters/stylish.js +96 -75
  16. package/lib/cli-engine/hash.js +1 -1
  17. package/lib/cli-engine/index.js +1 -1
  18. package/lib/cli-engine/lint-result-cache.js +144 -145
  19. package/lib/cli-engine/load-rules.js +16 -16
  20. package/lib/cli.js +638 -457
  21. package/lib/config/config-loader.js +726 -622
  22. package/lib/config/config.js +247 -221
  23. package/lib/config/default-config.js +54 -45
  24. package/lib/config/flat-config-array.js +167 -172
  25. package/lib/config/flat-config-helpers.js +72 -72
  26. package/lib/config/flat-config-schema.js +375 -368
  27. package/lib/config/rule-validator.js +139 -144
  28. package/lib/config-api.js +2 -2
  29. package/lib/eslint/eslint-helpers.js +756 -681
  30. package/lib/eslint/eslint.js +934 -912
  31. package/lib/eslint/index.js +2 -2
  32. package/lib/eslint/legacy-eslint.js +577 -533
  33. package/lib/languages/js/index.js +263 -264
  34. package/lib/languages/js/source-code/index.js +1 -1
  35. package/lib/languages/js/source-code/source-code.js +1128 -1057
  36. package/lib/languages/js/source-code/token-store/backward-token-comment-cursor.js +39 -35
  37. package/lib/languages/js/source-code/token-store/backward-token-cursor.js +35 -36
  38. package/lib/languages/js/source-code/token-store/cursor.js +36 -36
  39. package/lib/languages/js/source-code/token-store/cursors.js +80 -52
  40. package/lib/languages/js/source-code/token-store/decorative-cursor.js +17 -18
  41. package/lib/languages/js/source-code/token-store/filter-cursor.js +19 -20
  42. package/lib/languages/js/source-code/token-store/forward-token-comment-cursor.js +40 -32
  43. package/lib/languages/js/source-code/token-store/forward-token-cursor.js +40 -41
  44. package/lib/languages/js/source-code/token-store/index.js +592 -498
  45. package/lib/languages/js/source-code/token-store/limit-cursor.js +17 -18
  46. package/lib/languages/js/source-code/token-store/padded-token-cursor.js +23 -16
  47. package/lib/languages/js/source-code/token-store/skip-cursor.js +19 -20
  48. package/lib/languages/js/source-code/token-store/utils.js +63 -60
  49. package/lib/languages/js/validate-language-options.js +104 -89
  50. package/lib/linter/apply-disable-directives.js +467 -383
  51. package/lib/linter/code-path-analysis/code-path-analyzer.js +650 -672
  52. package/lib/linter/code-path-analysis/code-path-segment.js +215 -216
  53. package/lib/linter/code-path-analysis/code-path-state.js +2118 -2096
  54. package/lib/linter/code-path-analysis/code-path.js +307 -319
  55. package/lib/linter/code-path-analysis/debug-helpers.js +183 -163
  56. package/lib/linter/code-path-analysis/fork-context.js +296 -271
  57. package/lib/linter/code-path-analysis/id-generator.js +22 -23
  58. package/lib/linter/file-context.js +119 -120
  59. package/lib/linter/index.js +3 -3
  60. package/lib/linter/interpolate.js +16 -16
  61. package/lib/linter/linter.js +2403 -2045
  62. package/lib/linter/node-event-generator.js +284 -225
  63. package/lib/linter/report-translator.js +256 -219
  64. package/lib/linter/rule-fixer.js +122 -124
  65. package/lib/linter/rules.js +36 -36
  66. package/lib/linter/safe-emitter.js +18 -18
  67. package/lib/linter/source-code-fixer.js +94 -92
  68. package/lib/linter/timing.js +104 -101
  69. package/lib/linter/vfile.js +70 -73
  70. package/lib/options.js +404 -361
  71. package/lib/rule-tester/index.js +1 -1
  72. package/lib/rule-tester/rule-tester.js +1308 -1046
  73. package/lib/rules/accessor-pairs.js +298 -263
  74. package/lib/rules/array-bracket-newline.js +250 -238
  75. package/lib/rules/array-bracket-spacing.js +263 -224
  76. package/lib/rules/array-callback-return.js +402 -356
  77. package/lib/rules/array-element-newline.js +358 -313
  78. package/lib/rules/arrow-body-style.js +400 -281
  79. package/lib/rules/arrow-parens.js +206 -173
  80. package/lib/rules/arrow-spacing.js +169 -163
  81. package/lib/rules/block-scoped-var.js +125 -123
  82. package/lib/rules/block-spacing.js +186 -176
  83. package/lib/rules/brace-style.js +262 -199
  84. package/lib/rules/callback-return.js +203 -190
  85. package/lib/rules/camelcase.js +403 -392
  86. package/lib/rules/capitalized-comments.js +253 -232
  87. package/lib/rules/class-methods-use-this.js +224 -172
  88. package/lib/rules/comma-dangle.js +379 -346
  89. package/lib/rules/comma-spacing.js +193 -195
  90. package/lib/rules/comma-style.js +375 -316
  91. package/lib/rules/complexity.js +173 -169
  92. package/lib/rules/computed-property-spacing.js +236 -211
  93. package/lib/rules/consistent-return.js +181 -170
  94. package/lib/rules/consistent-this.js +167 -147
  95. package/lib/rules/constructor-super.js +412 -404
  96. package/lib/rules/curly.js +407 -332
  97. package/lib/rules/default-case-last.js +38 -31
  98. package/lib/rules/default-case.js +89 -85
  99. package/lib/rules/default-param-last.js +69 -54
  100. package/lib/rules/dot-location.js +122 -110
  101. package/lib/rules/dot-notation.js +192 -156
  102. package/lib/rules/eol-last.js +122 -120
  103. package/lib/rules/eqeqeq.js +168 -155
  104. package/lib/rules/for-direction.js +146 -121
  105. package/lib/rules/func-call-spacing.js +261 -231
  106. package/lib/rules/func-name-matching.js +293 -209
  107. package/lib/rules/func-names.js +165 -164
  108. package/lib/rules/func-style.js +159 -127
  109. package/lib/rules/function-call-argument-newline.js +152 -129
  110. package/lib/rules/function-paren-newline.js +349 -291
  111. package/lib/rules/generator-star-spacing.js +229 -210
  112. package/lib/rules/getter-return.js +208 -172
  113. package/lib/rules/global-require.js +85 -74
  114. package/lib/rules/grouped-accessor-pairs.js +170 -150
  115. package/lib/rules/guard-for-in.js +72 -63
  116. package/lib/rules/handle-callback-err.js +108 -103
  117. package/lib/rules/id-blacklist.js +182 -199
  118. package/lib/rules/id-denylist.js +168 -187
  119. package/lib/rules/id-length.js +197 -171
  120. package/lib/rules/id-match.js +344 -289
  121. package/lib/rules/implicit-arrow-linebreak.js +102 -79
  122. package/lib/rules/indent-legacy.js +1344 -1118
  123. package/lib/rules/indent.js +2272 -1759
  124. package/lib/rules/index.js +317 -292
  125. package/lib/rules/init-declarations.js +137 -107
  126. package/lib/rules/jsx-quotes.js +94 -82
  127. package/lib/rules/key-spacing.js +750 -633
  128. package/lib/rules/keyword-spacing.js +648 -605
  129. package/lib/rules/line-comment-position.js +142 -128
  130. package/lib/rules/linebreak-style.js +107 -106
  131. package/lib/rules/lines-around-comment.js +540 -448
  132. package/lib/rules/lines-around-directive.js +233 -203
  133. package/lib/rules/lines-between-class-members.js +305 -234
  134. package/lib/rules/logical-assignment-operators.js +582 -399
  135. package/lib/rules/max-classes-per-file.js +69 -68
  136. package/lib/rules/max-depth.js +146 -143
  137. package/lib/rules/max-len.js +473 -434
  138. package/lib/rules/max-lines-per-function.js +201 -176
  139. package/lib/rules/max-lines.js +158 -162
  140. package/lib/rules/max-nested-callbacks.js +102 -104
  141. package/lib/rules/max-params.js +78 -76
  142. package/lib/rules/max-statements-per-line.js +205 -198
  143. package/lib/rules/max-statements.js +168 -164
  144. package/lib/rules/multiline-comment-style.js +637 -479
  145. package/lib/rules/multiline-ternary.js +241 -176
  146. package/lib/rules/new-cap.js +233 -213
  147. package/lib/rules/new-parens.js +88 -79
  148. package/lib/rules/newline-after-var.js +287 -250
  149. package/lib/rules/newline-before-return.js +229 -222
  150. package/lib/rules/newline-per-chained-call.js +142 -127
  151. package/lib/rules/no-alert.js +90 -79
  152. package/lib/rules/no-array-constructor.js +125 -113
  153. package/lib/rules/no-async-promise-executor.js +30 -24
  154. package/lib/rules/no-await-in-loop.js +69 -71
  155. package/lib/rules/no-bitwise.js +124 -100
  156. package/lib/rules/no-buffer-constructor.js +55 -47
  157. package/lib/rules/no-caller.js +39 -33
  158. package/lib/rules/no-case-declarations.js +61 -57
  159. package/lib/rules/no-catch-shadow.js +76 -73
  160. package/lib/rules/no-class-assign.js +51 -48
  161. package/lib/rules/no-compare-neg-zero.js +62 -48
  162. package/lib/rules/no-cond-assign.js +148 -132
  163. package/lib/rules/no-confusing-arrow.js +98 -81
  164. package/lib/rules/no-console.js +202 -199
  165. package/lib/rules/no-const-assign.js +47 -41
  166. package/lib/rules/no-constant-binary-expression.js +500 -405
  167. package/lib/rules/no-constant-condition.js +158 -143
  168. package/lib/rules/no-constructor-return.js +49 -49
  169. package/lib/rules/no-continue.js +25 -27
  170. package/lib/rules/no-control-regex.js +125 -121
  171. package/lib/rules/no-debugger.js +28 -30
  172. package/lib/rules/no-delete-var.js +29 -29
  173. package/lib/rules/no-div-regex.js +47 -41
  174. package/lib/rules/no-dupe-args.js +68 -69
  175. package/lib/rules/no-dupe-class-members.js +102 -89
  176. package/lib/rules/no-dupe-else-if.js +100 -77
  177. package/lib/rules/no-dupe-keys.js +133 -110
  178. package/lib/rules/no-duplicate-case.js +50 -43
  179. package/lib/rules/no-duplicate-imports.js +179 -176
  180. package/lib/rules/no-else-return.js +430 -385
  181. package/lib/rules/no-empty-character-class.js +57 -50
  182. package/lib/rules/no-empty-function.js +127 -128
  183. package/lib/rules/no-empty-pattern.js +63 -58
  184. package/lib/rules/no-empty-static-block.js +37 -35
  185. package/lib/rules/no-empty.js +98 -86
  186. package/lib/rules/no-eq-null.js +37 -32
  187. package/lib/rules/no-eval.js +256 -250
  188. package/lib/rules/no-ex-assign.js +42 -39
  189. package/lib/rules/no-extend-native.js +161 -159
  190. package/lib/rules/no-extra-bind.js +201 -190
  191. package/lib/rules/no-extra-boolean-cast.js +398 -348
  192. package/lib/rules/no-extra-label.js +150 -131
  193. package/lib/rules/no-extra-parens.js +1654 -1325
  194. package/lib/rules/no-extra-semi.js +146 -144
  195. package/lib/rules/no-fallthrough.js +199 -157
  196. package/lib/rules/no-floating-decimal.js +74 -66
  197. package/lib/rules/no-func-assign.js +54 -55
  198. package/lib/rules/no-global-assign.js +78 -73
  199. package/lib/rules/no-implicit-coercion.js +349 -293
  200. package/lib/rules/no-implicit-globals.js +158 -135
  201. package/lib/rules/no-implied-eval.js +140 -112
  202. package/lib/rules/no-import-assign.js +145 -159
  203. package/lib/rules/no-inline-comments.js +101 -95
  204. package/lib/rules/no-inner-declarations.js +115 -101
  205. package/lib/rules/no-invalid-regexp.js +222 -190
  206. package/lib/rules/no-invalid-this.js +123 -117
  207. package/lib/rules/no-irregular-whitespace.js +266 -252
  208. package/lib/rules/no-iterator.js +29 -33
  209. package/lib/rules/no-label-var.js +59 -62
  210. package/lib/rules/no-labels.js +138 -133
  211. package/lib/rules/no-lone-blocks.js +127 -123
  212. package/lib/rules/no-lonely-if.js +108 -77
  213. package/lib/rules/no-loop-func.js +238 -213
  214. package/lib/rules/no-loss-of-precision.js +218 -201
  215. package/lib/rules/no-magic-numbers.js +246 -218
  216. package/lib/rules/no-misleading-character-class.js +499 -446
  217. package/lib/rules/no-mixed-operators.js +188 -182
  218. package/lib/rules/no-mixed-requires.js +253 -240
  219. package/lib/rules/no-mixed-spaces-and-tabs.js +134 -121
  220. package/lib/rules/no-multi-assign.js +46 -44
  221. package/lib/rules/no-multi-spaces.js +163 -143
  222. package/lib/rules/no-multi-str.js +42 -41
  223. package/lib/rules/no-multiple-empty-lines.js +196 -158
  224. package/lib/rules/no-native-reassign.js +90 -85
  225. package/lib/rules/no-negated-condition.js +79 -75
  226. package/lib/rules/no-negated-in-lhs.js +45 -43
  227. package/lib/rules/no-nested-ternary.js +33 -32
  228. package/lib/rules/no-new-func.js +71 -62
  229. package/lib/rules/no-new-native-nonconstructor.js +43 -39
  230. package/lib/rules/no-new-object.js +48 -48
  231. package/lib/rules/no-new-require.js +48 -47
  232. package/lib/rules/no-new-symbol.js +52 -50
  233. package/lib/rules/no-new-wrappers.js +43 -41
  234. package/lib/rules/no-new.js +28 -29
  235. package/lib/rules/no-nonoctal-decimal-escape.js +141 -121
  236. package/lib/rules/no-obj-calls.js +66 -53
  237. package/lib/rules/no-object-constructor.js +104 -97
  238. package/lib/rules/no-octal-escape.js +40 -43
  239. package/lib/rules/no-octal.js +32 -32
  240. package/lib/rules/no-param-reassign.js +235 -217
  241. package/lib/rules/no-path-concat.js +66 -67
  242. package/lib/rules/no-plusplus.js +60 -61
  243. package/lib/rules/no-process-env.js +49 -48
  244. package/lib/rules/no-process-exit.js +54 -50
  245. package/lib/rules/no-promise-executor-return.js +214 -182
  246. package/lib/rules/no-proto.js +26 -29
  247. package/lib/rules/no-prototype-builtins.js +146 -124
  248. package/lib/rules/no-redeclare.js +154 -152
  249. package/lib/rules/no-regex-spaces.js +183 -161
  250. package/lib/rules/no-restricted-exports.js +208 -185
  251. package/lib/rules/no-restricted-globals.js +111 -112
  252. package/lib/rules/no-restricted-imports.js +657 -537
  253. package/lib/rules/no-restricted-modules.js +222 -202
  254. package/lib/rules/no-restricted-properties.js +181 -153
  255. package/lib/rules/no-restricted-syntax.js +56 -52
  256. package/lib/rules/no-return-assign.js +55 -50
  257. package/lib/rules/no-return-await.js +148 -124
  258. package/lib/rules/no-script-url.js +52 -45
  259. package/lib/rules/no-self-assign.js +148 -146
  260. package/lib/rules/no-self-compare.js +63 -46
  261. package/lib/rules/no-sequences.js +135 -116
  262. package/lib/rules/no-setter-return.js +185 -152
  263. package/lib/rules/no-shadow-restricted-names.js +61 -46
  264. package/lib/rules/no-shadow.js +342 -316
  265. package/lib/rules/no-spaced-func.js +82 -77
  266. package/lib/rules/no-sparse-arrays.js +54 -59
  267. package/lib/rules/no-sync.js +61 -60
  268. package/lib/rules/no-tabs.js +83 -72
  269. package/lib/rules/no-template-curly-in-string.js +33 -32
  270. package/lib/rules/no-ternary.js +25 -29
  271. package/lib/rules/no-this-before-super.js +321 -319
  272. package/lib/rules/no-throw-literal.js +31 -36
  273. package/lib/rules/no-trailing-spaces.js +199 -191
  274. package/lib/rules/no-undef-init.js +76 -61
  275. package/lib/rules/no-undef.js +51 -48
  276. package/lib/rules/no-undefined.js +73 -75
  277. package/lib/rules/no-underscore-dangle.js +370 -327
  278. package/lib/rules/no-unexpected-multiline.js +112 -102
  279. package/lib/rules/no-unmodified-loop-condition.js +254 -254
  280. package/lib/rules/no-unneeded-ternary.js +212 -147
  281. package/lib/rules/no-unreachable-loop.js +145 -142
  282. package/lib/rules/no-unreachable.js +255 -248
  283. package/lib/rules/no-unsafe-finally.js +93 -85
  284. package/lib/rules/no-unsafe-negation.js +105 -83
  285. package/lib/rules/no-unsafe-optional-chaining.js +192 -178
  286. package/lib/rules/no-unused-expressions.js +178 -162
  287. package/lib/rules/no-unused-labels.js +139 -124
  288. package/lib/rules/no-unused-private-class-members.js +206 -182
  289. package/lib/rules/no-unused-vars.js +1669 -1449
  290. package/lib/rules/no-use-before-define.js +229 -231
  291. package/lib/rules/no-useless-assignment.js +590 -511
  292. package/lib/rules/no-useless-backreference.js +212 -193
  293. package/lib/rules/no-useless-call.js +58 -53
  294. package/lib/rules/no-useless-catch.js +40 -40
  295. package/lib/rules/no-useless-computed-key.js +144 -115
  296. package/lib/rules/no-useless-concat.js +65 -60
  297. package/lib/rules/no-useless-constructor.js +158 -111
  298. package/lib/rules/no-useless-escape.js +342 -291
  299. package/lib/rules/no-useless-rename.js +183 -156
  300. package/lib/rules/no-useless-return.js +344 -312
  301. package/lib/rules/no-var.js +233 -212
  302. package/lib/rules/no-void.js +50 -48
  303. package/lib/rules/no-warning-comments.js +191 -186
  304. package/lib/rules/no-whitespace-before-property.js +131 -115
  305. package/lib/rules/no-with.js +24 -26
  306. package/lib/rules/nonblock-statement-body-position.js +149 -130
  307. package/lib/rules/object-curly-newline.js +306 -265
  308. package/lib/rules/object-curly-spacing.js +360 -314
  309. package/lib/rules/object-property-newline.js +137 -106
  310. package/lib/rules/object-shorthand.js +607 -502
  311. package/lib/rules/one-var-declaration-per-line.js +104 -100
  312. package/lib/rules/one-var.js +653 -537
  313. package/lib/rules/operator-assignment.js +219 -161
  314. package/lib/rules/operator-linebreak.js +295 -251
  315. package/lib/rules/padded-blocks.js +346 -308
  316. package/lib/rules/padding-line-between-statements.js +443 -439
  317. package/lib/rules/prefer-arrow-callback.js +362 -313
  318. package/lib/rules/prefer-const.js +418 -377
  319. package/lib/rules/prefer-destructuring.js +301 -279
  320. package/lib/rules/prefer-exponentiation-operator.js +176 -133
  321. package/lib/rules/prefer-named-capture-group.js +153 -140
  322. package/lib/rules/prefer-numeric-literals.js +121 -113
  323. package/lib/rules/prefer-object-has-own.js +116 -82
  324. package/lib/rules/prefer-object-spread.js +213 -193
  325. package/lib/rules/prefer-promise-reject-errors.js +140 -122
  326. package/lib/rules/prefer-reflect.js +127 -107
  327. package/lib/rules/prefer-regex-literals.js +578 -466
  328. package/lib/rules/prefer-rest-params.js +79 -80
  329. package/lib/rules/prefer-spread.js +47 -44
  330. package/lib/rules/prefer-template.js +266 -195
  331. package/lib/rules/quote-props.js +373 -307
  332. package/lib/rules/quotes.js +374 -326
  333. package/lib/rules/radix.js +152 -136
  334. package/lib/rules/require-atomic-updates.js +316 -285
  335. package/lib/rules/require-await.js +144 -116
  336. package/lib/rules/require-unicode-regexp.js +282 -177
  337. package/lib/rules/require-yield.js +53 -54
  338. package/lib/rules/rest-spread-spacing.js +128 -116
  339. package/lib/rules/semi-spacing.js +281 -250
  340. package/lib/rules/semi-style.js +176 -134
  341. package/lib/rules/semi.js +456 -436
  342. package/lib/rules/sort-imports.js +306 -233
  343. package/lib/rules/sort-keys.js +219 -188
  344. package/lib/rules/sort-vars.js +127 -93
  345. package/lib/rules/space-before-blocks.js +199 -189
  346. package/lib/rules/space-before-function-paren.js +186 -166
  347. package/lib/rules/space-in-parens.js +359 -288
  348. package/lib/rules/space-infix-ops.js +237 -201
  349. package/lib/rules/space-unary-ops.js +356 -298
  350. package/lib/rules/spaced-comment.js +363 -319
  351. package/lib/rules/strict.js +265 -230
  352. package/lib/rules/switch-colon-spacing.js +130 -122
  353. package/lib/rules/symbol-description.js +45 -48
  354. package/lib/rules/template-curly-spacing.js +148 -142
  355. package/lib/rules/template-tag-spacing.js +98 -88
  356. package/lib/rules/unicode-bom.js +54 -56
  357. package/lib/rules/use-isnan.js +237 -206
  358. package/lib/rules/utils/ast-utils.js +2039 -1860
  359. package/lib/rules/utils/char-source.js +162 -155
  360. package/lib/rules/utils/fix-tracker.js +83 -80
  361. package/lib/rules/utils/keywords.js +59 -59
  362. package/lib/rules/utils/lazy-loading-rule-map.js +79 -76
  363. package/lib/rules/utils/regular-expressions.js +32 -24
  364. package/lib/rules/utils/unicode/index.js +4 -4
  365. package/lib/rules/utils/unicode/is-combining-character.js +1 -1
  366. package/lib/rules/utils/unicode/is-emoji-modifier.js +1 -1
  367. package/lib/rules/utils/unicode/is-regional-indicator-symbol.js +1 -1
  368. package/lib/rules/utils/unicode/is-surrogate-pair.js +1 -1
  369. package/lib/rules/valid-typeof.js +153 -111
  370. package/lib/rules/vars-on-top.js +152 -145
  371. package/lib/rules/wrap-iife.js +204 -191
  372. package/lib/rules/wrap-regex.js +70 -58
  373. package/lib/rules/yield-star-spacing.js +145 -134
  374. package/lib/rules/yoda.js +283 -272
  375. package/lib/services/parser-service.js +35 -35
  376. package/lib/services/processor-service.js +66 -73
  377. package/lib/services/suppressions-service.js +289 -0
  378. package/lib/shared/ajv.js +14 -14
  379. package/lib/shared/assert.js +3 -4
  380. package/lib/shared/ast-utils.js +7 -6
  381. package/lib/shared/deep-merge-arrays.js +24 -22
  382. package/lib/shared/directives.js +3 -2
  383. package/lib/shared/flags.js +50 -17
  384. package/lib/shared/logging.js +24 -25
  385. package/lib/shared/option-utils.js +43 -36
  386. package/lib/shared/runtime-info.js +136 -127
  387. package/lib/shared/serialization.js +27 -27
  388. package/lib/shared/severity.js +22 -22
  389. package/lib/shared/stats.js +5 -5
  390. package/lib/shared/string-utils.js +16 -16
  391. package/lib/shared/text-table.js +28 -27
  392. package/lib/shared/traverser.js +153 -146
  393. package/lib/shared/types.js +4 -27
  394. package/lib/types/index.d.ts +2010 -1559
  395. package/lib/types/rules.d.ts +5253 -5140
  396. package/lib/types/use-at-your-own-risk.d.ts +32 -30
  397. package/lib/unsupported-api.js +5 -5
  398. package/messages/all-files-ignored.js +3 -3
  399. package/messages/all-matched-files-ignored.js +3 -3
  400. package/messages/config-file-missing.js +2 -2
  401. package/messages/config-plugin-missing.js +3 -3
  402. package/messages/config-serialize-function.js +9 -7
  403. package/messages/eslintrc-incompat.js +13 -15
  404. package/messages/eslintrc-plugins.js +3 -4
  405. package/messages/extend-config-missing.js +3 -3
  406. package/messages/failed-to-read-json.js +3 -3
  407. package/messages/file-not-found.js +3 -3
  408. package/messages/invalid-rule-options.js +2 -2
  409. package/messages/invalid-rule-severity.js +2 -2
  410. package/messages/no-config-found.js +3 -3
  411. package/messages/plugin-conflict.js +8 -8
  412. package/messages/plugin-invalid.js +3 -3
  413. package/messages/plugin-missing.js +3 -3
  414. package/messages/print-config-with-directory-path.js +2 -2
  415. package/messages/shared.js +6 -1
  416. package/messages/whitespace-found.js +3 -3
  417. package/package.json +14 -20
@@ -8,21 +8,21 @@
8
8
  // Requirements
9
9
  //------------------------------------------------------------------------------
10
10
 
11
- const
12
- { isCommentToken } = require("@eslint-community/eslint-utils"),
13
- TokenStore = require("./token-store"),
14
- astUtils = require("../../../shared/ast-utils"),
15
- Traverser = require("../../../shared/traverser"),
16
- globals = require("../../../../conf/globals"),
17
- {
18
- directivesPattern
19
- } = require("../../../shared/directives"),
20
-
21
- CodePathAnalyzer = require("../../../linter/code-path-analysis/code-path-analyzer"),
22
- createEmitter = require("../../../linter/safe-emitter"),
23
- { ConfigCommentParser, VisitNodeStep, CallMethodStep, Directive } = require("@eslint/plugin-kit"),
24
-
25
- eslintScope = require("eslint-scope");
11
+ const { isCommentToken } = require("@eslint-community/eslint-utils"),
12
+ TokenStore = require("./token-store"),
13
+ astUtils = require("../../../shared/ast-utils"),
14
+ Traverser = require("../../../shared/traverser"),
15
+ globals = require("../../../../conf/globals"),
16
+ { directivesPattern } = require("../../../shared/directives"),
17
+ CodePathAnalyzer = require("../../../linter/code-path-analysis/code-path-analyzer"),
18
+ createEmitter = require("../../../linter/safe-emitter"),
19
+ {
20
+ ConfigCommentParser,
21
+ VisitNodeStep,
22
+ CallMethodStep,
23
+ Directive,
24
+ } = require("@eslint/plugin-kit"),
25
+ eslintScope = require("eslint-scope");
26
26
 
27
27
  //------------------------------------------------------------------------------
28
28
  // Type Definitions
@@ -41,13 +41,13 @@ const
41
41
  const commentParser = new ConfigCommentParser();
42
42
 
43
43
  const CODE_PATH_EVENTS = [
44
- "onCodePathStart",
45
- "onCodePathEnd",
46
- "onCodePathSegmentStart",
47
- "onCodePathSegmentEnd",
48
- "onCodePathSegmentLoop",
49
- "onUnreachableCodePathSegmentStart",
50
- "onUnreachableCodePathSegmentEnd"
44
+ "onCodePathStart",
45
+ "onCodePathEnd",
46
+ "onCodePathSegmentStart",
47
+ "onCodePathSegmentEnd",
48
+ "onCodePathSegmentLoop",
49
+ "onUnreachableCodePathSegmentStart",
50
+ "onUnreachableCodePathSegmentEnd",
51
51
  ];
52
52
 
53
53
  /**
@@ -58,25 +58,25 @@ const CODE_PATH_EVENTS = [
58
58
  * @private
59
59
  */
60
60
  function validate(ast) {
61
- if (!ast) {
62
- throw new TypeError(`Unexpected empty AST. (${ast})`);
63
- }
61
+ if (!ast) {
62
+ throw new TypeError(`Unexpected empty AST. (${ast})`);
63
+ }
64
64
 
65
- if (!ast.tokens) {
66
- throw new TypeError("AST is missing the tokens array.");
67
- }
65
+ if (!ast.tokens) {
66
+ throw new TypeError("AST is missing the tokens array.");
67
+ }
68
68
 
69
- if (!ast.comments) {
70
- throw new TypeError("AST is missing the comments array.");
71
- }
69
+ if (!ast.comments) {
70
+ throw new TypeError("AST is missing the comments array.");
71
+ }
72
72
 
73
- if (!ast.loc) {
74
- throw new TypeError("AST is missing location information.");
75
- }
73
+ if (!ast.loc) {
74
+ throw new TypeError("AST is missing location information.");
75
+ }
76
76
 
77
- if (!ast.range) {
78
- throw new TypeError("AST is missing range information");
79
- }
77
+ if (!ast.range) {
78
+ throw new TypeError("AST is missing range information");
79
+ }
80
80
  }
81
81
 
82
82
  /**
@@ -85,21 +85,20 @@ function validate(ast) {
85
85
  * @returns {Object} The globals for the given ecmaVersion.
86
86
  */
87
87
  function getGlobalsForEcmaVersion(ecmaVersion) {
88
+ switch (ecmaVersion) {
89
+ case 3:
90
+ return globals.es3;
88
91
 
89
- switch (ecmaVersion) {
90
- case 3:
91
- return globals.es3;
92
+ case 5:
93
+ return globals.es5;
92
94
 
93
- case 5:
94
- return globals.es5;
95
+ default:
96
+ if (ecmaVersion < 2015) {
97
+ return globals[`es${ecmaVersion + 2009}`];
98
+ }
95
99
 
96
- default:
97
- if (ecmaVersion < 2015) {
98
- return globals[`es${ecmaVersion + 2009}`];
99
- }
100
-
101
- return globals[`es${ecmaVersion}`];
102
- }
100
+ return globals[`es${ecmaVersion}`];
101
+ }
103
102
  }
104
103
 
105
104
  /**
@@ -109,8 +108,12 @@ function getGlobalsForEcmaVersion(ecmaVersion) {
109
108
  * @private
110
109
  */
111
110
  function looksLikeExport(astNode) {
112
- return astNode.type === "ExportDefaultDeclaration" || astNode.type === "ExportNamedDeclaration" ||
113
- astNode.type === "ExportAllDeclaration" || astNode.type === "ExportSpecifier";
111
+ return (
112
+ astNode.type === "ExportDefaultDeclaration" ||
113
+ astNode.type === "ExportNamedDeclaration" ||
114
+ astNode.type === "ExportAllDeclaration" ||
115
+ astNode.type === "ExportSpecifier"
116
+ );
114
117
  }
115
118
 
116
119
  /**
@@ -121,19 +124,23 @@ function looksLikeExport(astNode) {
121
124
  * @private
122
125
  */
123
126
  function sortedMerge(tokens, comments) {
124
- const result = [];
125
- let tokenIndex = 0;
126
- let commentIndex = 0;
127
-
128
- while (tokenIndex < tokens.length || commentIndex < comments.length) {
129
- if (commentIndex >= comments.length || tokenIndex < tokens.length && tokens[tokenIndex].range[0] < comments[commentIndex].range[0]) {
130
- result.push(tokens[tokenIndex++]);
131
- } else {
132
- result.push(comments[commentIndex++]);
133
- }
134
- }
135
-
136
- return result;
127
+ const result = [];
128
+ let tokenIndex = 0;
129
+ let commentIndex = 0;
130
+
131
+ while (tokenIndex < tokens.length || commentIndex < comments.length) {
132
+ if (
133
+ commentIndex >= comments.length ||
134
+ (tokenIndex < tokens.length &&
135
+ tokens[tokenIndex].range[0] < comments[commentIndex].range[0])
136
+ ) {
137
+ result.push(tokens[tokenIndex++]);
138
+ } else {
139
+ result.push(comments[commentIndex++]);
140
+ }
141
+ }
142
+
143
+ return result;
137
144
  }
138
145
 
139
146
  /**
@@ -144,26 +151,28 @@ function sortedMerge(tokens, comments) {
144
151
  * @throws Error if global value is invalid
145
152
  */
146
153
  function normalizeConfigGlobal(configuredValue) {
147
- switch (configuredValue) {
148
- case "off":
149
- return "off";
150
-
151
- case true:
152
- case "true":
153
- case "writeable":
154
- case "writable":
155
- return "writable";
156
-
157
- case null:
158
- case false:
159
- case "false":
160
- case "readable":
161
- case "readonly":
162
- return "readonly";
163
-
164
- default:
165
- throw new Error(`'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`);
166
- }
154
+ switch (configuredValue) {
155
+ case "off":
156
+ return "off";
157
+
158
+ case true:
159
+ case "true":
160
+ case "writeable":
161
+ case "writable":
162
+ return "writable";
163
+
164
+ case null:
165
+ case false:
166
+ case "false":
167
+ case "readable":
168
+ case "readonly":
169
+ return "readonly";
170
+
171
+ default:
172
+ throw new Error(
173
+ `'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`,
174
+ );
175
+ }
167
176
  }
168
177
 
169
178
  /**
@@ -174,8 +183,11 @@ function normalizeConfigGlobal(configuredValue) {
174
183
  * @private
175
184
  */
176
185
  function nodesOrTokensOverlap(first, second) {
177
- return (first.range[0] <= second.range[0] && first.range[1] >= second.range[0]) ||
178
- (second.range[0] <= first.range[0] && second.range[1] >= first.range[0]);
186
+ return (
187
+ (first.range[0] <= second.range[0] &&
188
+ first.range[1] >= second.range[0]) ||
189
+ (second.range[0] <= first.range[0] && second.range[1] >= first.range[0])
190
+ );
179
191
  }
180
192
 
181
193
  /**
@@ -191,41 +203,41 @@ function nodesOrTokensOverlap(first, second) {
191
203
  * @public
192
204
  */
193
205
  function isSpaceBetween(sourceCode, first, second, checkInsideOfJSXText) {
194
- if (nodesOrTokensOverlap(first, second)) {
195
- return false;
196
- }
197
-
198
- const [startingNodeOrToken, endingNodeOrToken] = first.range[1] <= second.range[0]
199
- ? [first, second]
200
- : [second, first];
201
- const firstToken = sourceCode.getLastToken(startingNodeOrToken) || startingNodeOrToken;
202
- const finalToken = sourceCode.getFirstToken(endingNodeOrToken) || endingNodeOrToken;
203
- let currentToken = firstToken;
204
-
205
- while (currentToken !== finalToken) {
206
- const nextToken = sourceCode.getTokenAfter(currentToken, { includeComments: true });
207
-
208
- if (
209
- currentToken.range[1] !== nextToken.range[0] ||
210
-
211
- /*
212
- * For backward compatibility, check spaces in JSXText.
213
- * https://github.com/eslint/eslint/issues/12614
214
- */
215
- (
216
- checkInsideOfJSXText &&
217
- nextToken !== finalToken &&
218
- nextToken.type === "JSXText" &&
219
- /\s/u.test(nextToken.value)
220
- )
221
- ) {
222
- return true;
223
- }
224
-
225
- currentToken = nextToken;
226
- }
227
-
228
- return false;
206
+ if (nodesOrTokensOverlap(first, second)) {
207
+ return false;
208
+ }
209
+
210
+ const [startingNodeOrToken, endingNodeOrToken] =
211
+ first.range[1] <= second.range[0] ? [first, second] : [second, first];
212
+ const firstToken =
213
+ sourceCode.getLastToken(startingNodeOrToken) || startingNodeOrToken;
214
+ const finalToken =
215
+ sourceCode.getFirstToken(endingNodeOrToken) || endingNodeOrToken;
216
+ let currentToken = firstToken;
217
+
218
+ while (currentToken !== finalToken) {
219
+ const nextToken = sourceCode.getTokenAfter(currentToken, {
220
+ includeComments: true,
221
+ });
222
+
223
+ if (
224
+ currentToken.range[1] !== nextToken.range[0] ||
225
+ /*
226
+ * For backward compatibility, check spaces in JSXText.
227
+ * https://github.com/eslint/eslint/issues/12614
228
+ */
229
+ (checkInsideOfJSXText &&
230
+ nextToken !== finalToken &&
231
+ nextToken.type === "JSXText" &&
232
+ /\s/u.test(nextToken.value))
233
+ ) {
234
+ return true;
235
+ }
236
+
237
+ currentToken = nextToken;
238
+ }
239
+
240
+ return false;
229
241
  }
230
242
 
231
243
  //-----------------------------------------------------------------------------
@@ -241,62 +253,69 @@ function isSpaceBetween(sourceCode, first, second, checkInsideOfJSXText) {
241
253
  * @param {Object|undefined} inlineGlobals The globals declared in the source code
242
254
  * @returns {void}
243
255
  */
244
- function addDeclaredGlobals(globalScope, configGlobals = {}, inlineGlobals = {}) {
245
-
246
- // Define configured global variables.
247
- for (const id of new Set([...Object.keys(configGlobals), ...Object.keys(inlineGlobals)])) {
248
-
249
- /*
250
- * `normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would
251
- * typically be caught when validating a config anyway (validity for inline global comments is checked separately).
252
- */
253
- const configValue = configGlobals[id] === void 0 ? void 0 : normalizeConfigGlobal(configGlobals[id]);
254
- const commentValue = inlineGlobals[id] && inlineGlobals[id].value;
255
- const value = commentValue || configValue;
256
- const sourceComments = inlineGlobals[id] && inlineGlobals[id].comments;
257
-
258
- if (value === "off") {
259
- continue;
260
- }
261
-
262
- let variable = globalScope.set.get(id);
263
-
264
- if (!variable) {
265
- variable = new eslintScope.Variable(id, globalScope);
266
-
267
- globalScope.variables.push(variable);
268
- globalScope.set.set(id, variable);
269
- }
270
-
271
- variable.eslintImplicitGlobalSetting = configValue;
272
- variable.eslintExplicitGlobal = sourceComments !== void 0;
273
- variable.eslintExplicitGlobalComments = sourceComments;
274
- variable.writeable = (value === "writable");
275
- }
276
-
277
- /*
278
- * "through" contains all references which definitions cannot be found.
279
- * Since we augment the global scope using configuration, we need to update
280
- * references and remove the ones that were added by configuration.
281
- */
282
- globalScope.through = globalScope.through.filter(reference => {
283
- const name = reference.identifier.name;
284
- const variable = globalScope.set.get(name);
285
-
286
- if (variable) {
287
-
288
- /*
289
- * Links the variable and the reference.
290
- * And this reference is removed from `Scope#through`.
291
- */
292
- reference.resolved = variable;
293
- variable.references.push(reference);
294
-
295
- return false;
296
- }
297
-
298
- return true;
299
- });
256
+ function addDeclaredGlobals(
257
+ globalScope,
258
+ configGlobals = {},
259
+ inlineGlobals = {},
260
+ ) {
261
+ // Define configured global variables.
262
+ for (const id of new Set([
263
+ ...Object.keys(configGlobals),
264
+ ...Object.keys(inlineGlobals),
265
+ ])) {
266
+ /*
267
+ * `normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would
268
+ * typically be caught when validating a config anyway (validity for inline global comments is checked separately).
269
+ */
270
+ const configValue =
271
+ configGlobals[id] === void 0
272
+ ? void 0
273
+ : normalizeConfigGlobal(configGlobals[id]);
274
+ const commentValue = inlineGlobals[id] && inlineGlobals[id].value;
275
+ const value = commentValue || configValue;
276
+ const sourceComments = inlineGlobals[id] && inlineGlobals[id].comments;
277
+
278
+ if (value === "off") {
279
+ continue;
280
+ }
281
+
282
+ let variable = globalScope.set.get(id);
283
+
284
+ if (!variable) {
285
+ variable = new eslintScope.Variable(id, globalScope);
286
+
287
+ globalScope.variables.push(variable);
288
+ globalScope.set.set(id, variable);
289
+ }
290
+
291
+ variable.eslintImplicitGlobalSetting = configValue;
292
+ variable.eslintExplicitGlobal = sourceComments !== void 0;
293
+ variable.eslintExplicitGlobalComments = sourceComments;
294
+ variable.writeable = value === "writable";
295
+ }
296
+
297
+ /*
298
+ * "through" contains all references which definitions cannot be found.
299
+ * Since we augment the global scope using configuration, we need to update
300
+ * references and remove the ones that were added by configuration.
301
+ */
302
+ globalScope.through = globalScope.through.filter(reference => {
303
+ const name = reference.identifier.name;
304
+ const variable = globalScope.set.get(name);
305
+
306
+ if (variable) {
307
+ /*
308
+ * Links the variable and the reference.
309
+ * And this reference is removed from `Scope#through`.
310
+ */
311
+ reference.resolved = variable;
312
+ variable.references.push(reference);
313
+
314
+ return false;
315
+ }
316
+
317
+ return true;
318
+ });
300
319
  }
301
320
 
302
321
  /**
@@ -308,16 +327,14 @@ function addDeclaredGlobals(globalScope, configGlobals = {}, inlineGlobals = {})
308
327
  * @returns {void}
309
328
  */
310
329
  function markExportedVariables(globalScope, variables) {
311
-
312
- Object.keys(variables).forEach(name => {
313
- const variable = globalScope.set.get(name);
314
-
315
- if (variable) {
316
- variable.eslintUsed = true;
317
- variable.eslintExported = true;
318
- }
319
- });
320
-
330
+ Object.keys(variables).forEach(name => {
331
+ const variable = globalScope.set.get(name);
332
+
333
+ if (variable) {
334
+ variable.eslintUsed = true;
335
+ variable.eslintExported = true;
336
+ }
337
+ });
321
338
  }
322
339
 
323
340
  //------------------------------------------------------------------------------
@@ -331,876 +348,930 @@ const caches = Symbol("caches");
331
348
  * @implements {ISourceCode}
332
349
  */
333
350
  class SourceCode extends TokenStore {
334
-
335
- /**
336
- * The cache of steps that were taken while traversing the source code.
337
- * @type {Array<ITraversalStep>}
338
- */
339
- #steps;
340
-
341
- /**
342
- * Creates a new instance.
343
- * @param {string|Object} textOrConfig The source code text or config object.
344
- * @param {string} textOrConfig.text The source code text.
345
- * @param {ASTNode} textOrConfig.ast The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
346
- * @param {boolean} textOrConfig.hasBOM Indicates if the text has a Unicode BOM.
347
- * @param {Object|null} textOrConfig.parserServices The parser services.
348
- * @param {ScopeManager|null} textOrConfig.scopeManager The scope of this source code.
349
- * @param {Object|null} textOrConfig.visitorKeys The visitor keys to traverse AST.
350
- * @param {ASTNode} [astIfNoConfig] The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
351
- */
352
- constructor(textOrConfig, astIfNoConfig) {
353
- let text, hasBOM, ast, parserServices, scopeManager, visitorKeys;
354
-
355
- // Process overloading of arguments
356
- if (typeof textOrConfig === "string") {
357
- text = textOrConfig;
358
- ast = astIfNoConfig;
359
- hasBOM = false;
360
- } else if (typeof textOrConfig === "object" && textOrConfig !== null) {
361
- text = textOrConfig.text;
362
- ast = textOrConfig.ast;
363
- hasBOM = textOrConfig.hasBOM;
364
- parserServices = textOrConfig.parserServices;
365
- scopeManager = textOrConfig.scopeManager;
366
- visitorKeys = textOrConfig.visitorKeys;
367
- }
368
-
369
- validate(ast);
370
- super(ast.tokens, ast.comments);
371
-
372
- /**
373
- * General purpose caching for the class.
374
- */
375
- this[caches] = new Map([
376
- ["scopes", new WeakMap()],
377
- ["vars", new Map()],
378
- ["configNodes", void 0]
379
- ]);
380
-
381
- /**
382
- * Indicates if the AST is ESTree compatible.
383
- * @type {boolean}
384
- */
385
- this.isESTree = ast.type === "Program";
386
-
387
- /*
388
- * Backwards compatibility for BOM handling.
389
- *
390
- * The `hasBOM` property has been available on the `SourceCode` object
391
- * for a long time and is used to indicate if the source contains a BOM.
392
- * The linter strips the BOM and just passes the `hasBOM` property to the
393
- * `SourceCode` constructor to make it easier for languages to not deal with
394
- * the BOM.
395
- *
396
- * However, the text passed in to the `SourceCode` constructor might still
397
- * have a BOM if the constructor is called outside of the linter, so we still
398
- * need to check for the BOM in the text.
399
- */
400
- const textHasBOM = text.charCodeAt(0) === 0xFEFF;
401
-
402
- /**
403
- * The flag to indicate that the source code has Unicode BOM.
404
- * @type {boolean}
405
- */
406
- this.hasBOM = textHasBOM || !!hasBOM;
407
-
408
- /**
409
- * The original text source code.
410
- * BOM was stripped from this text.
411
- * @type {string}
412
- */
413
- this.text = (textHasBOM ? text.slice(1) : text);
414
-
415
- /**
416
- * The parsed AST for the source code.
417
- * @type {ASTNode}
418
- */
419
- this.ast = ast;
420
-
421
- /**
422
- * The parser services of this source code.
423
- * @type {Object}
424
- */
425
- this.parserServices = parserServices || {};
426
-
427
- /**
428
- * The scope of this source code.
429
- * @type {ScopeManager|null}
430
- */
431
- this.scopeManager = scopeManager || null;
432
-
433
- /**
434
- * The visitor keys to traverse AST.
435
- * @type {Object}
436
- */
437
- this.visitorKeys = visitorKeys || Traverser.DEFAULT_VISITOR_KEYS;
438
-
439
- // Check the source text for the presence of a shebang since it is parsed as a standard line comment.
440
- const shebangMatched = this.text.match(astUtils.shebangPattern);
441
- const hasShebang = shebangMatched && ast.comments.length && ast.comments[0].value === shebangMatched[1];
442
-
443
- if (hasShebang) {
444
- ast.comments[0].type = "Shebang";
445
- }
446
-
447
- this.tokensAndComments = sortedMerge(ast.tokens, ast.comments);
448
-
449
- /**
450
- * The source code split into lines according to ECMA-262 specification.
451
- * This is done to avoid each rule needing to do so separately.
452
- * @type {string[]}
453
- */
454
- this.lines = [];
455
- this.lineStartIndices = [0];
456
-
457
- const lineEndingPattern = astUtils.createGlobalLinebreakMatcher();
458
- let match;
459
-
460
- /*
461
- * Previously, this was implemented using a regex that
462
- * matched a sequence of non-linebreak characters followed by a
463
- * linebreak, then adding the lengths of the matches. However,
464
- * this caused a catastrophic backtracking issue when the end
465
- * of a file contained a large number of non-newline characters.
466
- * To avoid this, the current implementation just matches newlines
467
- * and uses match.index to get the correct line start indices.
468
- */
469
- while ((match = lineEndingPattern.exec(this.text))) {
470
- this.lines.push(this.text.slice(this.lineStartIndices.at(-1), match.index));
471
- this.lineStartIndices.push(match.index + match[0].length);
472
- }
473
- this.lines.push(this.text.slice(this.lineStartIndices.at(-1)));
474
-
475
- // don't allow further modification of this object
476
- Object.freeze(this);
477
- Object.freeze(this.lines);
478
- }
479
-
480
- /**
481
- * Split the source code into multiple lines based on the line delimiters.
482
- * @param {string} text Source code as a string.
483
- * @returns {string[]} Array of source code lines.
484
- * @public
485
- */
486
- static splitLines(text) {
487
- return text.split(astUtils.createGlobalLinebreakMatcher());
488
- }
489
-
490
- /**
491
- * Gets the source code for the given node.
492
- * @param {ASTNode} [node] The AST node to get the text for.
493
- * @param {int} [beforeCount] The number of characters before the node to retrieve.
494
- * @param {int} [afterCount] The number of characters after the node to retrieve.
495
- * @returns {string} The text representing the AST node.
496
- * @public
497
- */
498
- getText(node, beforeCount, afterCount) {
499
- if (node) {
500
- return this.text.slice(Math.max(node.range[0] - (beforeCount || 0), 0),
501
- node.range[1] + (afterCount || 0));
502
- }
503
- return this.text;
504
- }
505
-
506
- /**
507
- * Gets the entire source text split into an array of lines.
508
- * @returns {Array} The source text as an array of lines.
509
- * @public
510
- */
511
- getLines() {
512
- return this.lines;
513
- }
514
-
515
- /**
516
- * Retrieves an array containing all comments in the source code.
517
- * @returns {ASTNode[]} An array of comment nodes.
518
- * @public
519
- */
520
- getAllComments() {
521
- return this.ast.comments;
522
- }
523
-
524
- /**
525
- * Retrieves the JSDoc comment for a given node.
526
- * @param {ASTNode} node The AST node to get the comment for.
527
- * @returns {Token|null} The Block comment token containing the JSDoc comment
528
- * for the given node or null if not found.
529
- * @public
530
- * @deprecated
531
- */
532
- getJSDocComment(node) {
533
-
534
- /**
535
- * Checks for the presence of a JSDoc comment for the given node and returns it.
536
- * @param {ASTNode} astNode The AST node to get the comment for.
537
- * @returns {Token|null} The Block comment token containing the JSDoc comment
538
- * for the given node or null if not found.
539
- * @private
540
- */
541
- const findJSDocComment = astNode => {
542
- const tokenBefore = this.getTokenBefore(astNode, { includeComments: true });
543
-
544
- if (
545
- tokenBefore &&
546
- isCommentToken(tokenBefore) &&
547
- tokenBefore.type === "Block" &&
548
- tokenBefore.value.charAt(0) === "*" &&
549
- astNode.loc.start.line - tokenBefore.loc.end.line <= 1
550
- ) {
551
- return tokenBefore;
552
- }
553
-
554
- return null;
555
- };
556
- let parent = node.parent;
557
-
558
- switch (node.type) {
559
- case "ClassDeclaration":
560
- case "FunctionDeclaration":
561
- return findJSDocComment(looksLikeExport(parent) ? parent : node);
562
-
563
- case "ClassExpression":
564
- return findJSDocComment(parent.parent);
565
-
566
- case "ArrowFunctionExpression":
567
- case "FunctionExpression":
568
- if (parent.type !== "CallExpression" && parent.type !== "NewExpression") {
569
- while (
570
- !this.getCommentsBefore(parent).length &&
571
- !/Function/u.test(parent.type) &&
572
- parent.type !== "MethodDefinition" &&
573
- parent.type !== "Property"
574
- ) {
575
- parent = parent.parent;
576
-
577
- if (!parent) {
578
- break;
579
- }
580
- }
581
-
582
- if (parent && parent.type !== "FunctionDeclaration" && parent.type !== "Program") {
583
- return findJSDocComment(parent);
584
- }
585
- }
586
-
587
- return findJSDocComment(node);
588
-
589
- // falls through
590
- default:
591
- return null;
592
- }
593
- }
594
-
595
- /**
596
- * Gets the deepest node containing a range index.
597
- * @param {int} index Range index of the desired node.
598
- * @returns {ASTNode} The node if found or null if not found.
599
- * @public
600
- */
601
- getNodeByRangeIndex(index) {
602
- let result = null;
603
-
604
- Traverser.traverse(this.ast, {
605
- visitorKeys: this.visitorKeys,
606
- enter(node) {
607
- if (node.range[0] <= index && index < node.range[1]) {
608
- result = node;
609
- } else {
610
- this.skip();
611
- }
612
- },
613
- leave(node) {
614
- if (node === result) {
615
- this.break();
616
- }
617
- }
618
- });
619
-
620
- return result;
621
- }
622
-
623
- /**
624
- * Determines if two nodes or tokens have at least one whitespace character
625
- * between them. Order does not matter. Returns false if the given nodes or
626
- * tokens overlap.
627
- * @param {ASTNode|Token} first The first node or token to check between.
628
- * @param {ASTNode|Token} second The second node or token to check between.
629
- * @returns {boolean} True if there is a whitespace character between
630
- * any of the tokens found between the two given nodes or tokens.
631
- * @public
632
- */
633
- isSpaceBetween(first, second) {
634
- return isSpaceBetween(this, first, second, false);
635
- }
636
-
637
- /**
638
- * Determines if two nodes or tokens have at least one whitespace character
639
- * between them. Order does not matter. Returns false if the given nodes or
640
- * tokens overlap.
641
- * For backward compatibility, this method returns true if there are
642
- * `JSXText` tokens that contain whitespaces between the two.
643
- * @param {ASTNode|Token} first The first node or token to check between.
644
- * @param {ASTNode|Token} second The second node or token to check between.
645
- * @returns {boolean} True if there is a whitespace character between
646
- * any of the tokens found between the two given nodes or tokens.
647
- * @deprecated in favor of isSpaceBetween().
648
- * @public
649
- */
650
- isSpaceBetweenTokens(first, second) {
651
- return isSpaceBetween(this, first, second, true);
652
- }
653
-
654
- /**
655
- * Converts a source text index into a (line, column) pair.
656
- * @param {number} index The index of a character in a file
657
- * @throws {TypeError} If non-numeric index or index out of range.
658
- * @returns {Object} A {line, column} location object with a 0-indexed column
659
- * @public
660
- */
661
- getLocFromIndex(index) {
662
- if (typeof index !== "number") {
663
- throw new TypeError("Expected `index` to be a number.");
664
- }
665
-
666
- if (index < 0 || index > this.text.length) {
667
- throw new RangeError(`Index out of range (requested index ${index}, but source text has length ${this.text.length}).`);
668
- }
669
-
670
- /*
671
- * For an argument of this.text.length, return the location one "spot" past the last character
672
- * of the file. If the last character is a linebreak, the location will be column 0 of the next
673
- * line; otherwise, the location will be in the next column on the same line.
674
- *
675
- * See getIndexFromLoc for the motivation for this special case.
676
- */
677
- if (index === this.text.length) {
678
- return { line: this.lines.length, column: this.lines.at(-1).length };
679
- }
680
-
681
- /*
682
- * To figure out which line index is on, determine the last place at which index could
683
- * be inserted into lineStartIndices to keep the list sorted.
684
- */
685
- const lineNumber = index >= this.lineStartIndices.at(-1)
686
- ? this.lineStartIndices.length
687
- : this.lineStartIndices.findIndex(el => index < el);
688
-
689
- return { line: lineNumber, column: index - this.lineStartIndices[lineNumber - 1] };
690
- }
691
-
692
- /**
693
- * Converts a (line, column) pair into a range index.
694
- * @param {Object} loc A line/column location
695
- * @param {number} loc.line The line number of the location (1-indexed)
696
- * @param {number} loc.column The column number of the location (0-indexed)
697
- * @throws {TypeError|RangeError} If `loc` is not an object with a numeric
698
- * `line` and `column`, if the `line` is less than or equal to zero or
699
- * the line or column is out of the expected range.
700
- * @returns {number} The range index of the location in the file.
701
- * @public
702
- */
703
- getIndexFromLoc(loc) {
704
- if (typeof loc !== "object" || typeof loc.line !== "number" || typeof loc.column !== "number") {
705
- throw new TypeError("Expected `loc` to be an object with numeric `line` and `column` properties.");
706
- }
707
-
708
- if (loc.line <= 0) {
709
- throw new RangeError(`Line number out of range (line ${loc.line} requested). Line numbers should be 1-based.`);
710
- }
711
-
712
- if (loc.line > this.lineStartIndices.length) {
713
- throw new RangeError(`Line number out of range (line ${loc.line} requested, but only ${this.lineStartIndices.length} lines present).`);
714
- }
715
-
716
- const lineStartIndex = this.lineStartIndices[loc.line - 1];
717
- const lineEndIndex = loc.line === this.lineStartIndices.length ? this.text.length : this.lineStartIndices[loc.line];
718
- const positionIndex = lineStartIndex + loc.column;
719
-
720
- /*
721
- * By design, getIndexFromLoc({ line: lineNum, column: 0 }) should return the start index of
722
- * the given line, provided that the line number is valid element of this.lines. Since the
723
- * last element of this.lines is an empty string for files with trailing newlines, add a
724
- * special case where getting the index for the first location after the end of the file
725
- * will return the length of the file, rather than throwing an error. This allows rules to
726
- * use getIndexFromLoc consistently without worrying about edge cases at the end of a file.
727
- */
728
- if (
729
- loc.line === this.lineStartIndices.length && positionIndex > lineEndIndex ||
730
- loc.line < this.lineStartIndices.length && positionIndex >= lineEndIndex
731
- ) {
732
- throw new RangeError(`Column number out of range (column ${loc.column} requested, but the length of line ${loc.line} is ${lineEndIndex - lineStartIndex}).`);
733
- }
734
-
735
- return positionIndex;
736
- }
737
-
738
- /**
739
- * Gets the scope for the given node
740
- * @param {ASTNode} currentNode The node to get the scope of
741
- * @returns {Scope} The scope information for this node
742
- * @throws {TypeError} If the `currentNode` argument is missing.
743
- */
744
- getScope(currentNode) {
745
-
746
- if (!currentNode) {
747
- throw new TypeError("Missing required argument: node.");
748
- }
749
-
750
- // check cache first
751
- const cache = this[caches].get("scopes");
752
- const cachedScope = cache.get(currentNode);
753
-
754
- if (cachedScope) {
755
- return cachedScope;
756
- }
757
-
758
- // On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
759
- const inner = currentNode.type !== "Program";
760
-
761
- for (let node = currentNode; node; node = node.parent) {
762
- const scope = this.scopeManager.acquire(node, inner);
763
-
764
- if (scope) {
765
- if (scope.type === "function-expression-name") {
766
- cache.set(currentNode, scope.childScopes[0]);
767
- return scope.childScopes[0];
768
- }
769
-
770
- cache.set(currentNode, scope);
771
- return scope;
772
- }
773
- }
774
-
775
- cache.set(currentNode, this.scopeManager.scopes[0]);
776
- return this.scopeManager.scopes[0];
777
- }
778
-
779
- /**
780
- * Get the variables that `node` defines.
781
- * This is a convenience method that passes through
782
- * to the same method on the `scopeManager`.
783
- * @param {ASTNode} node The node for which the variables are obtained.
784
- * @returns {Array<Variable>} An array of variable nodes representing
785
- * the variables that `node` defines.
786
- */
787
- getDeclaredVariables(node) {
788
- return this.scopeManager.getDeclaredVariables(node);
789
- }
790
-
791
- /* eslint-disable class-methods-use-this -- node is owned by SourceCode */
792
- /**
793
- * Gets all the ancestors of a given node
794
- * @param {ASTNode} node The node
795
- * @returns {Array<ASTNode>} All the ancestor nodes in the AST, not including the provided node, starting
796
- * from the root node at index 0 and going inwards to the parent node.
797
- * @throws {TypeError} When `node` is missing.
798
- */
799
- getAncestors(node) {
800
-
801
- if (!node) {
802
- throw new TypeError("Missing required argument: node.");
803
- }
804
-
805
- const ancestorsStartingAtParent = [];
806
-
807
- for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) {
808
- ancestorsStartingAtParent.push(ancestor);
809
- }
810
-
811
- return ancestorsStartingAtParent.reverse();
812
- }
813
-
814
- /**
815
- * Returns the location of the given node or token.
816
- * @param {ASTNode|Token} nodeOrToken The node or token to get the location of.
817
- * @returns {SourceLocation} The location of the node or token.
818
- */
819
- getLoc(nodeOrToken) {
820
- return nodeOrToken.loc;
821
- }
822
-
823
- /**
824
- * Returns the range of the given node or token.
825
- * @param {ASTNode|Token} nodeOrToken The node or token to get the range of.
826
- * @returns {[number, number]} The range of the node or token.
827
- */
828
- getRange(nodeOrToken) {
829
- return nodeOrToken.range;
830
- }
831
-
832
- /* eslint-enable class-methods-use-this -- node is owned by SourceCode */
833
-
834
- /**
835
- * Marks a variable as used in the current scope
836
- * @param {string} name The name of the variable to mark as used.
837
- * @param {ASTNode} [refNode] The closest node to the variable reference.
838
- * @returns {boolean} True if the variable was found and marked as used, false if not.
839
- */
840
- markVariableAsUsed(name, refNode = this.ast) {
841
-
842
- const currentScope = this.getScope(refNode);
843
- let initialScope = currentScope;
844
-
845
- /*
846
- * When we are in an ESM or CommonJS module, we need to start searching
847
- * from the top-level scope, not the global scope. For ESM the top-level
848
- * scope is the module scope; for CommonJS the top-level scope is the
849
- * outer function scope.
850
- *
851
- * Without this check, we might miss a variable declared with `var` at
852
- * the top-level because it won't exist in the global scope.
853
- */
854
- if (
855
- currentScope.type === "global" &&
856
- currentScope.childScopes.length > 0 &&
857
-
858
- // top-level scopes refer to a `Program` node
859
- currentScope.childScopes[0].block === this.ast
860
- ) {
861
- initialScope = currentScope.childScopes[0];
862
- }
863
-
864
- for (let scope = initialScope; scope; scope = scope.upper) {
865
- const variable = scope.variables.find(scopeVar => scopeVar.name === name);
866
-
867
- if (variable) {
868
- variable.eslintUsed = true;
869
- return true;
870
- }
871
- }
872
-
873
- return false;
874
- }
875
-
876
-
877
- /**
878
- * Returns an array of all inline configuration nodes found in the
879
- * source code.
880
- * @returns {Array<Token>} An array of all inline configuration nodes.
881
- */
882
- getInlineConfigNodes() {
883
-
884
- // check the cache first
885
- let configNodes = this[caches].get("configNodes");
886
-
887
- if (configNodes) {
888
- return configNodes;
889
- }
890
-
891
- // calculate fresh config nodes
892
- configNodes = this.ast.comments.filter(comment => {
893
-
894
- // shebang comments are never directives
895
- if (comment.type === "Shebang") {
896
- return false;
897
- }
898
-
899
- const directive = commentParser.parseDirective(comment.value);
900
-
901
- if (!directive) {
902
- return false;
903
- }
904
-
905
- if (!directivesPattern.test(directive.label)) {
906
- return false;
907
- }
908
-
909
- // only certain comment types are supported as line comments
910
- return comment.type !== "Line" || !!/^eslint-disable-(next-)?line$/u.test(directive.label);
911
- });
912
-
913
- this[caches].set("configNodes", configNodes);
914
-
915
- return configNodes;
916
- }
917
-
918
- /**
919
- * Returns an all directive nodes that enable or disable rules along with any problems
920
- * encountered while parsing the directives.
921
- * @returns {{problems:Array<Problem>,directives:Array<Directive>}} Information
922
- * that ESLint needs to further process the directives.
923
- */
924
- getDisableDirectives() {
925
-
926
- // check the cache first
927
- const cachedDirectives = this[caches].get("disableDirectives");
928
-
929
- if (cachedDirectives) {
930
- return cachedDirectives;
931
- }
932
-
933
- const problems = [];
934
- const directives = [];
935
-
936
- this.getInlineConfigNodes().forEach(comment => {
937
-
938
- // Step 1: Parse the directive
939
- const {
940
- label,
941
- value,
942
- justification: justificationPart
943
- } = commentParser.parseDirective(comment.value);
944
-
945
- // Step 2: Extract the directive value
946
- const lineCommentSupported = /^eslint-disable-(next-)?line$/u.test(label);
947
-
948
- if (comment.type === "Line" && !lineCommentSupported) {
949
- return;
950
- }
951
-
952
- // Step 3: Validate the directive does not span multiple lines
953
- if (label === "eslint-disable-line" && comment.loc.start.line !== comment.loc.end.line) {
954
- const message = `${label} comment should not span multiple lines.`;
955
-
956
- problems.push({
957
- ruleId: null,
958
- message,
959
- loc: comment.loc
960
- });
961
- return;
962
- }
963
-
964
- // Step 4: Extract the directive value and create the Directive object
965
- switch (label) {
966
- case "eslint-disable":
967
- case "eslint-enable":
968
- case "eslint-disable-next-line":
969
- case "eslint-disable-line": {
970
- const directiveType = label.slice("eslint-".length);
971
-
972
- directives.push(new Directive({
973
- type: directiveType,
974
- node: comment,
975
- value,
976
- justification: justificationPart
977
- }));
978
- }
979
-
980
- // no default
981
- }
982
- });
983
-
984
- const result = { problems, directives };
985
-
986
- this[caches].set("disableDirectives", result);
987
-
988
- return result;
989
- }
990
-
991
- /**
992
- * Applies language options sent in from the core.
993
- * @param {Object} languageOptions The language options for this run.
994
- * @returns {void}
995
- */
996
- applyLanguageOptions(languageOptions) {
997
-
998
- /*
999
- * Add configured globals and language globals
1000
- *
1001
- * Using Object.assign instead of object spread for performance reasons
1002
- * https://github.com/eslint/eslint/issues/16302
1003
- */
1004
- const configGlobals = Object.assign(
1005
- Object.create(null), // https://github.com/eslint/eslint/issues/18363
1006
- getGlobalsForEcmaVersion(languageOptions.ecmaVersion),
1007
- languageOptions.sourceType === "commonjs" ? globals.commonjs : void 0,
1008
- languageOptions.globals
1009
- );
1010
- const varsCache = this[caches].get("vars");
1011
-
1012
- varsCache.set("configGlobals", configGlobals);
1013
- }
1014
-
1015
- /**
1016
- * Applies configuration found inside of the source code. This method is only
1017
- * called when ESLint is running with inline configuration allowed.
1018
- * @returns {{problems:Array<Problem>,configs:{config:FlatConfigArray,loc:Location}}} Information
1019
- * that ESLint needs to further process the inline configuration.
1020
- */
1021
- applyInlineConfig() {
1022
-
1023
- const problems = [];
1024
- const configs = [];
1025
- const exportedVariables = {};
1026
- const inlineGlobals = Object.create(null);
1027
-
1028
- this.getInlineConfigNodes().forEach(comment => {
1029
-
1030
- const { label, value } = commentParser.parseDirective(comment.value);
1031
-
1032
- switch (label) {
1033
- case "exported":
1034
- Object.assign(exportedVariables, commentParser.parseListConfig(value));
1035
- break;
1036
-
1037
- case "globals":
1038
- case "global":
1039
- for (const [id, idSetting] of Object.entries(commentParser.parseStringConfig(value))) {
1040
- let normalizedValue;
1041
-
1042
- try {
1043
- normalizedValue = normalizeConfigGlobal(idSetting);
1044
- } catch (err) {
1045
- problems.push({
1046
- ruleId: null,
1047
- loc: comment.loc,
1048
- message: err.message
1049
- });
1050
- continue;
1051
- }
1052
-
1053
- if (inlineGlobals[id]) {
1054
- inlineGlobals[id].comments.push(comment);
1055
- inlineGlobals[id].value = normalizedValue;
1056
- } else {
1057
- inlineGlobals[id] = {
1058
- comments: [comment],
1059
- value: normalizedValue
1060
- };
1061
- }
1062
- }
1063
- break;
1064
-
1065
- case "eslint": {
1066
- const parseResult = commentParser.parseJSONLikeConfig(value);
1067
-
1068
- if (parseResult.ok) {
1069
- configs.push({
1070
- config: {
1071
- rules: parseResult.config
1072
- },
1073
- loc: comment.loc
1074
- });
1075
- } else {
1076
- problems.push({
1077
- ruleId: null,
1078
- loc: comment.loc,
1079
- message: parseResult.error.message
1080
- });
1081
- }
1082
-
1083
- break;
1084
- }
1085
-
1086
- // no default
1087
- }
1088
- });
1089
-
1090
- // save all the new variables for later
1091
- const varsCache = this[caches].get("vars");
1092
-
1093
- varsCache.set("inlineGlobals", inlineGlobals);
1094
- varsCache.set("exportedVariables", exportedVariables);
1095
-
1096
- return {
1097
- configs,
1098
- problems
1099
- };
1100
- }
1101
-
1102
- /**
1103
- * Called by ESLint core to indicate that it has finished providing
1104
- * information. We now add in all the missing variables and ensure that
1105
- * state-changing methods cannot be called by rules.
1106
- * @returns {void}
1107
- */
1108
- finalize() {
1109
-
1110
- const varsCache = this[caches].get("vars");
1111
- const configGlobals = varsCache.get("configGlobals");
1112
- const inlineGlobals = varsCache.get("inlineGlobals");
1113
- const exportedVariables = varsCache.get("exportedVariables");
1114
- const globalScope = this.scopeManager.scopes[0];
1115
-
1116
- addDeclaredGlobals(globalScope, configGlobals, inlineGlobals);
1117
-
1118
- if (exportedVariables) {
1119
- markExportedVariables(globalScope, exportedVariables);
1120
- }
1121
-
1122
- }
1123
-
1124
- /**
1125
- * Traverse the source code and return the steps that were taken.
1126
- * @returns {Array<TraversalStep>} The steps that were taken while traversing the source code.
1127
- */
1128
- traverse() {
1129
-
1130
- // Because the AST doesn't mutate, we can cache the steps
1131
- if (this.#steps) {
1132
- return this.#steps;
1133
- }
1134
-
1135
- const steps = this.#steps = [];
1136
-
1137
- /*
1138
- * This logic works for any AST, not just ESTree. Because ESLint has allowed
1139
- * custom parsers to return any AST, we need to ensure that the traversal
1140
- * logic works for any AST.
1141
- */
1142
- const emitter = createEmitter();
1143
- let analyzer = {
1144
- enterNode(node) {
1145
- steps.push(new VisitNodeStep({
1146
- target: node,
1147
- phase: 1,
1148
- args: [node, node.parent]
1149
- }));
1150
- },
1151
- leaveNode(node) {
1152
- steps.push(new VisitNodeStep({
1153
- target: node,
1154
- phase: 2,
1155
- args: [node, node.parent]
1156
- }));
1157
- },
1158
- emitter
1159
- };
1160
-
1161
- /*
1162
- * We do code path analysis for ESTree only. Code path analysis is not
1163
- * necessary for other ASTs, and it's also not possible to do for other
1164
- * ASTs because the necessary information is not available.
1165
- *
1166
- * Generally speaking, we can tell that the AST is an ESTree if it has a
1167
- * Program node at the top level. This is not a perfect heuristic, but it
1168
- * is good enough for now.
1169
- */
1170
- if (this.isESTree) {
1171
- analyzer = new CodePathAnalyzer(analyzer);
1172
-
1173
- CODE_PATH_EVENTS.forEach(eventName => {
1174
- emitter.on(eventName, (...args) => {
1175
- steps.push(new CallMethodStep({
1176
- target: eventName,
1177
- args
1178
- }));
1179
- });
1180
- });
1181
- }
1182
-
1183
- /*
1184
- * The actual AST traversal is done by the `Traverser` class. This class
1185
- * is responsible for walking the AST and calling the appropriate methods
1186
- * on the `analyzer` object, which is appropriate for the given AST.
1187
- */
1188
- Traverser.traverse(this.ast, {
1189
- enter(node, parent) {
1190
-
1191
- // save the parent node on a property for backwards compatibility
1192
- node.parent = parent;
1193
-
1194
- analyzer.enterNode(node);
1195
- },
1196
- leave(node) {
1197
- analyzer.leaveNode(node);
1198
- },
1199
- visitorKeys: this.visitorKeys
1200
- });
1201
-
1202
- return steps;
1203
- }
351
+ /**
352
+ * The cache of steps that were taken while traversing the source code.
353
+ * @type {Array<ITraversalStep>}
354
+ */
355
+ #steps;
356
+
357
+ /**
358
+ * Creates a new instance.
359
+ * @param {string|Object} textOrConfig The source code text or config object.
360
+ * @param {string} textOrConfig.text The source code text.
361
+ * @param {ASTNode} textOrConfig.ast The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
362
+ * @param {boolean} textOrConfig.hasBOM Indicates if the text has a Unicode BOM.
363
+ * @param {Object|null} textOrConfig.parserServices The parser services.
364
+ * @param {ScopeManager|null} textOrConfig.scopeManager The scope of this source code.
365
+ * @param {Object|null} textOrConfig.visitorKeys The visitor keys to traverse AST.
366
+ * @param {ASTNode} [astIfNoConfig] The Program node of the AST representing the code. This AST should be created from the text that BOM was stripped.
367
+ */
368
+ constructor(textOrConfig, astIfNoConfig) {
369
+ let text, hasBOM, ast, parserServices, scopeManager, visitorKeys;
370
+
371
+ // Process overloading of arguments
372
+ if (typeof textOrConfig === "string") {
373
+ text = textOrConfig;
374
+ ast = astIfNoConfig;
375
+ hasBOM = false;
376
+ } else if (typeof textOrConfig === "object" && textOrConfig !== null) {
377
+ text = textOrConfig.text;
378
+ ast = textOrConfig.ast;
379
+ hasBOM = textOrConfig.hasBOM;
380
+ parserServices = textOrConfig.parserServices;
381
+ scopeManager = textOrConfig.scopeManager;
382
+ visitorKeys = textOrConfig.visitorKeys;
383
+ }
384
+
385
+ validate(ast);
386
+ super(ast.tokens, ast.comments);
387
+
388
+ /**
389
+ * General purpose caching for the class.
390
+ */
391
+ this[caches] = new Map([
392
+ ["scopes", new WeakMap()],
393
+ ["vars", new Map()],
394
+ ["configNodes", void 0],
395
+ ]);
396
+
397
+ /**
398
+ * Indicates if the AST is ESTree compatible.
399
+ * @type {boolean}
400
+ */
401
+ this.isESTree = ast.type === "Program";
402
+
403
+ /*
404
+ * Backwards compatibility for BOM handling.
405
+ *
406
+ * The `hasBOM` property has been available on the `SourceCode` object
407
+ * for a long time and is used to indicate if the source contains a BOM.
408
+ * The linter strips the BOM and just passes the `hasBOM` property to the
409
+ * `SourceCode` constructor to make it easier for languages to not deal with
410
+ * the BOM.
411
+ *
412
+ * However, the text passed in to the `SourceCode` constructor might still
413
+ * have a BOM if the constructor is called outside of the linter, so we still
414
+ * need to check for the BOM in the text.
415
+ */
416
+ const textHasBOM = text.charCodeAt(0) === 0xfeff;
417
+
418
+ /**
419
+ * The flag to indicate that the source code has Unicode BOM.
420
+ * @type {boolean}
421
+ */
422
+ this.hasBOM = textHasBOM || !!hasBOM;
423
+
424
+ /**
425
+ * The original text source code.
426
+ * BOM was stripped from this text.
427
+ * @type {string}
428
+ */
429
+ this.text = textHasBOM ? text.slice(1) : text;
430
+
431
+ /**
432
+ * The parsed AST for the source code.
433
+ * @type {ASTNode}
434
+ */
435
+ this.ast = ast;
436
+
437
+ /**
438
+ * The parser services of this source code.
439
+ * @type {Object}
440
+ */
441
+ this.parserServices = parserServices || {};
442
+
443
+ /**
444
+ * The scope of this source code.
445
+ * @type {ScopeManager|null}
446
+ */
447
+ this.scopeManager = scopeManager || null;
448
+
449
+ /**
450
+ * The visitor keys to traverse AST.
451
+ * @type {Object}
452
+ */
453
+ this.visitorKeys = visitorKeys || Traverser.DEFAULT_VISITOR_KEYS;
454
+
455
+ // Check the source text for the presence of a shebang since it is parsed as a standard line comment.
456
+ const shebangMatched = this.text.match(astUtils.shebangPattern);
457
+ const hasShebang =
458
+ shebangMatched &&
459
+ ast.comments.length &&
460
+ ast.comments[0].value === shebangMatched[1];
461
+
462
+ if (hasShebang) {
463
+ ast.comments[0].type = "Shebang";
464
+ }
465
+
466
+ this.tokensAndComments = sortedMerge(ast.tokens, ast.comments);
467
+
468
+ /**
469
+ * The source code split into lines according to ECMA-262 specification.
470
+ * This is done to avoid each rule needing to do so separately.
471
+ * @type {string[]}
472
+ */
473
+ this.lines = [];
474
+ this.lineStartIndices = [0];
475
+
476
+ const lineEndingPattern = astUtils.createGlobalLinebreakMatcher();
477
+ let match;
478
+
479
+ /*
480
+ * Previously, this was implemented using a regex that
481
+ * matched a sequence of non-linebreak characters followed by a
482
+ * linebreak, then adding the lengths of the matches. However,
483
+ * this caused a catastrophic backtracking issue when the end
484
+ * of a file contained a large number of non-newline characters.
485
+ * To avoid this, the current implementation just matches newlines
486
+ * and uses match.index to get the correct line start indices.
487
+ */
488
+ while ((match = lineEndingPattern.exec(this.text))) {
489
+ this.lines.push(
490
+ this.text.slice(this.lineStartIndices.at(-1), match.index),
491
+ );
492
+ this.lineStartIndices.push(match.index + match[0].length);
493
+ }
494
+ this.lines.push(this.text.slice(this.lineStartIndices.at(-1)));
495
+
496
+ // don't allow further modification of this object
497
+ Object.freeze(this);
498
+ Object.freeze(this.lines);
499
+ }
500
+
501
+ /**
502
+ * Split the source code into multiple lines based on the line delimiters.
503
+ * @param {string} text Source code as a string.
504
+ * @returns {string[]} Array of source code lines.
505
+ * @public
506
+ */
507
+ static splitLines(text) {
508
+ return text.split(astUtils.createGlobalLinebreakMatcher());
509
+ }
510
+
511
+ /**
512
+ * Gets the source code for the given node.
513
+ * @param {ASTNode} [node] The AST node to get the text for.
514
+ * @param {int} [beforeCount] The number of characters before the node to retrieve.
515
+ * @param {int} [afterCount] The number of characters after the node to retrieve.
516
+ * @returns {string} The text representing the AST node.
517
+ * @public
518
+ */
519
+ getText(node, beforeCount, afterCount) {
520
+ if (node) {
521
+ return this.text.slice(
522
+ Math.max(node.range[0] - (beforeCount || 0), 0),
523
+ node.range[1] + (afterCount || 0),
524
+ );
525
+ }
526
+ return this.text;
527
+ }
528
+
529
+ /**
530
+ * Gets the entire source text split into an array of lines.
531
+ * @returns {Array} The source text as an array of lines.
532
+ * @public
533
+ */
534
+ getLines() {
535
+ return this.lines;
536
+ }
537
+
538
+ /**
539
+ * Retrieves an array containing all comments in the source code.
540
+ * @returns {ASTNode[]} An array of comment nodes.
541
+ * @public
542
+ */
543
+ getAllComments() {
544
+ return this.ast.comments;
545
+ }
546
+
547
+ /**
548
+ * Retrieves the JSDoc comment for a given node.
549
+ * @param {ASTNode} node The AST node to get the comment for.
550
+ * @returns {Token|null} The Block comment token containing the JSDoc comment
551
+ * for the given node or null if not found.
552
+ * @public
553
+ * @deprecated
554
+ */
555
+ getJSDocComment(node) {
556
+ /**
557
+ * Checks for the presence of a JSDoc comment for the given node and returns it.
558
+ * @param {ASTNode} astNode The AST node to get the comment for.
559
+ * @returns {Token|null} The Block comment token containing the JSDoc comment
560
+ * for the given node or null if not found.
561
+ * @private
562
+ */
563
+ const findJSDocComment = astNode => {
564
+ const tokenBefore = this.getTokenBefore(astNode, {
565
+ includeComments: true,
566
+ });
567
+
568
+ if (
569
+ tokenBefore &&
570
+ isCommentToken(tokenBefore) &&
571
+ tokenBefore.type === "Block" &&
572
+ tokenBefore.value.charAt(0) === "*" &&
573
+ astNode.loc.start.line - tokenBefore.loc.end.line <= 1
574
+ ) {
575
+ return tokenBefore;
576
+ }
577
+
578
+ return null;
579
+ };
580
+ let parent = node.parent;
581
+
582
+ switch (node.type) {
583
+ case "ClassDeclaration":
584
+ case "FunctionDeclaration":
585
+ return findJSDocComment(
586
+ looksLikeExport(parent) ? parent : node,
587
+ );
588
+
589
+ case "ClassExpression":
590
+ return findJSDocComment(parent.parent);
591
+
592
+ case "ArrowFunctionExpression":
593
+ case "FunctionExpression":
594
+ if (
595
+ parent.type !== "CallExpression" &&
596
+ parent.type !== "NewExpression"
597
+ ) {
598
+ while (
599
+ !this.getCommentsBefore(parent).length &&
600
+ !/Function/u.test(parent.type) &&
601
+ parent.type !== "MethodDefinition" &&
602
+ parent.type !== "Property"
603
+ ) {
604
+ parent = parent.parent;
605
+
606
+ if (!parent) {
607
+ break;
608
+ }
609
+ }
610
+
611
+ if (
612
+ parent &&
613
+ parent.type !== "FunctionDeclaration" &&
614
+ parent.type !== "Program"
615
+ ) {
616
+ return findJSDocComment(parent);
617
+ }
618
+ }
619
+
620
+ return findJSDocComment(node);
621
+
622
+ // falls through
623
+ default:
624
+ return null;
625
+ }
626
+ }
627
+
628
+ /**
629
+ * Gets the deepest node containing a range index.
630
+ * @param {int} index Range index of the desired node.
631
+ * @returns {ASTNode} The node if found or null if not found.
632
+ * @public
633
+ */
634
+ getNodeByRangeIndex(index) {
635
+ let result = null;
636
+
637
+ Traverser.traverse(this.ast, {
638
+ visitorKeys: this.visitorKeys,
639
+ enter(node) {
640
+ if (node.range[0] <= index && index < node.range[1]) {
641
+ result = node;
642
+ } else {
643
+ this.skip();
644
+ }
645
+ },
646
+ leave(node) {
647
+ if (node === result) {
648
+ this.break();
649
+ }
650
+ },
651
+ });
652
+
653
+ return result;
654
+ }
655
+
656
+ /**
657
+ * Determines if two nodes or tokens have at least one whitespace character
658
+ * between them. Order does not matter. Returns false if the given nodes or
659
+ * tokens overlap.
660
+ * @param {ASTNode|Token} first The first node or token to check between.
661
+ * @param {ASTNode|Token} second The second node or token to check between.
662
+ * @returns {boolean} True if there is a whitespace character between
663
+ * any of the tokens found between the two given nodes or tokens.
664
+ * @public
665
+ */
666
+ isSpaceBetween(first, second) {
667
+ return isSpaceBetween(this, first, second, false);
668
+ }
669
+
670
+ /**
671
+ * Determines if two nodes or tokens have at least one whitespace character
672
+ * between them. Order does not matter. Returns false if the given nodes or
673
+ * tokens overlap.
674
+ * For backward compatibility, this method returns true if there are
675
+ * `JSXText` tokens that contain whitespaces between the two.
676
+ * @param {ASTNode|Token} first The first node or token to check between.
677
+ * @param {ASTNode|Token} second The second node or token to check between.
678
+ * @returns {boolean} True if there is a whitespace character between
679
+ * any of the tokens found between the two given nodes or tokens.
680
+ * @deprecated in favor of isSpaceBetween().
681
+ * @public
682
+ */
683
+ isSpaceBetweenTokens(first, second) {
684
+ return isSpaceBetween(this, first, second, true);
685
+ }
686
+
687
+ /**
688
+ * Converts a source text index into a (line, column) pair.
689
+ * @param {number} index The index of a character in a file
690
+ * @throws {TypeError} If non-numeric index or index out of range.
691
+ * @returns {Object} A {line, column} location object with a 0-indexed column
692
+ * @public
693
+ */
694
+ getLocFromIndex(index) {
695
+ if (typeof index !== "number") {
696
+ throw new TypeError("Expected `index` to be a number.");
697
+ }
698
+
699
+ if (index < 0 || index > this.text.length) {
700
+ throw new RangeError(
701
+ `Index out of range (requested index ${index}, but source text has length ${this.text.length}).`,
702
+ );
703
+ }
704
+
705
+ /*
706
+ * For an argument of this.text.length, return the location one "spot" past the last character
707
+ * of the file. If the last character is a linebreak, the location will be column 0 of the next
708
+ * line; otherwise, the location will be in the next column on the same line.
709
+ *
710
+ * See getIndexFromLoc for the motivation for this special case.
711
+ */
712
+ if (index === this.text.length) {
713
+ return {
714
+ line: this.lines.length,
715
+ column: this.lines.at(-1).length,
716
+ };
717
+ }
718
+
719
+ /*
720
+ * To figure out which line index is on, determine the last place at which index could
721
+ * be inserted into lineStartIndices to keep the list sorted.
722
+ */
723
+ const lineNumber =
724
+ index >= this.lineStartIndices.at(-1)
725
+ ? this.lineStartIndices.length
726
+ : this.lineStartIndices.findIndex(el => index < el);
727
+
728
+ return {
729
+ line: lineNumber,
730
+ column: index - this.lineStartIndices[lineNumber - 1],
731
+ };
732
+ }
733
+
734
+ /**
735
+ * Converts a (line, column) pair into a range index.
736
+ * @param {Object} loc A line/column location
737
+ * @param {number} loc.line The line number of the location (1-indexed)
738
+ * @param {number} loc.column The column number of the location (0-indexed)
739
+ * @throws {TypeError|RangeError} If `loc` is not an object with a numeric
740
+ * `line` and `column`, if the `line` is less than or equal to zero or
741
+ * the line or column is out of the expected range.
742
+ * @returns {number} The range index of the location in the file.
743
+ * @public
744
+ */
745
+ getIndexFromLoc(loc) {
746
+ if (
747
+ typeof loc !== "object" ||
748
+ typeof loc.line !== "number" ||
749
+ typeof loc.column !== "number"
750
+ ) {
751
+ throw new TypeError(
752
+ "Expected `loc` to be an object with numeric `line` and `column` properties.",
753
+ );
754
+ }
755
+
756
+ if (loc.line <= 0) {
757
+ throw new RangeError(
758
+ `Line number out of range (line ${loc.line} requested). Line numbers should be 1-based.`,
759
+ );
760
+ }
761
+
762
+ if (loc.line > this.lineStartIndices.length) {
763
+ throw new RangeError(
764
+ `Line number out of range (line ${loc.line} requested, but only ${this.lineStartIndices.length} lines present).`,
765
+ );
766
+ }
767
+
768
+ const lineStartIndex = this.lineStartIndices[loc.line - 1];
769
+ const lineEndIndex =
770
+ loc.line === this.lineStartIndices.length
771
+ ? this.text.length
772
+ : this.lineStartIndices[loc.line];
773
+ const positionIndex = lineStartIndex + loc.column;
774
+
775
+ /*
776
+ * By design, getIndexFromLoc({ line: lineNum, column: 0 }) should return the start index of
777
+ * the given line, provided that the line number is valid element of this.lines. Since the
778
+ * last element of this.lines is an empty string for files with trailing newlines, add a
779
+ * special case where getting the index for the first location after the end of the file
780
+ * will return the length of the file, rather than throwing an error. This allows rules to
781
+ * use getIndexFromLoc consistently without worrying about edge cases at the end of a file.
782
+ */
783
+ if (
784
+ (loc.line === this.lineStartIndices.length &&
785
+ positionIndex > lineEndIndex) ||
786
+ (loc.line < this.lineStartIndices.length &&
787
+ positionIndex >= lineEndIndex)
788
+ ) {
789
+ throw new RangeError(
790
+ `Column number out of range (column ${loc.column} requested, but the length of line ${loc.line} is ${lineEndIndex - lineStartIndex}).`,
791
+ );
792
+ }
793
+
794
+ return positionIndex;
795
+ }
796
+
797
+ /**
798
+ * Gets the scope for the given node
799
+ * @param {ASTNode} currentNode The node to get the scope of
800
+ * @returns {Scope} The scope information for this node
801
+ * @throws {TypeError} If the `currentNode` argument is missing.
802
+ */
803
+ getScope(currentNode) {
804
+ if (!currentNode) {
805
+ throw new TypeError("Missing required argument: node.");
806
+ }
807
+
808
+ // check cache first
809
+ const cache = this[caches].get("scopes");
810
+ const cachedScope = cache.get(currentNode);
811
+
812
+ if (cachedScope) {
813
+ return cachedScope;
814
+ }
815
+
816
+ // On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
817
+ const inner = currentNode.type !== "Program";
818
+
819
+ for (let node = currentNode; node; node = node.parent) {
820
+ const scope = this.scopeManager.acquire(node, inner);
821
+
822
+ if (scope) {
823
+ if (scope.type === "function-expression-name") {
824
+ cache.set(currentNode, scope.childScopes[0]);
825
+ return scope.childScopes[0];
826
+ }
827
+
828
+ cache.set(currentNode, scope);
829
+ return scope;
830
+ }
831
+ }
832
+
833
+ cache.set(currentNode, this.scopeManager.scopes[0]);
834
+ return this.scopeManager.scopes[0];
835
+ }
836
+
837
+ /**
838
+ * Get the variables that `node` defines.
839
+ * This is a convenience method that passes through
840
+ * to the same method on the `scopeManager`.
841
+ * @param {ASTNode} node The node for which the variables are obtained.
842
+ * @returns {Array<Variable>} An array of variable nodes representing
843
+ * the variables that `node` defines.
844
+ */
845
+ getDeclaredVariables(node) {
846
+ return this.scopeManager.getDeclaredVariables(node);
847
+ }
848
+
849
+ /* eslint-disable class-methods-use-this -- node is owned by SourceCode */
850
+ /**
851
+ * Gets all the ancestors of a given node
852
+ * @param {ASTNode} node The node
853
+ * @returns {Array<ASTNode>} All the ancestor nodes in the AST, not including the provided node, starting
854
+ * from the root node at index 0 and going inwards to the parent node.
855
+ * @throws {TypeError} When `node` is missing.
856
+ */
857
+ getAncestors(node) {
858
+ if (!node) {
859
+ throw new TypeError("Missing required argument: node.");
860
+ }
861
+
862
+ const ancestorsStartingAtParent = [];
863
+
864
+ for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) {
865
+ ancestorsStartingAtParent.push(ancestor);
866
+ }
867
+
868
+ return ancestorsStartingAtParent.reverse();
869
+ }
870
+
871
+ /**
872
+ * Returns the location of the given node or token.
873
+ * @param {ASTNode|Token} nodeOrToken The node or token to get the location of.
874
+ * @returns {SourceLocation} The location of the node or token.
875
+ */
876
+ getLoc(nodeOrToken) {
877
+ return nodeOrToken.loc;
878
+ }
879
+
880
+ /**
881
+ * Returns the range of the given node or token.
882
+ * @param {ASTNode|Token} nodeOrToken The node or token to get the range of.
883
+ * @returns {[number, number]} The range of the node or token.
884
+ */
885
+ getRange(nodeOrToken) {
886
+ return nodeOrToken.range;
887
+ }
888
+
889
+ /* eslint-enable class-methods-use-this -- node is owned by SourceCode */
890
+
891
+ /**
892
+ * Marks a variable as used in the current scope
893
+ * @param {string} name The name of the variable to mark as used.
894
+ * @param {ASTNode} [refNode] The closest node to the variable reference.
895
+ * @returns {boolean} True if the variable was found and marked as used, false if not.
896
+ */
897
+ markVariableAsUsed(name, refNode = this.ast) {
898
+ const currentScope = this.getScope(refNode);
899
+ let initialScope = currentScope;
900
+
901
+ /*
902
+ * When we are in an ESM or CommonJS module, we need to start searching
903
+ * from the top-level scope, not the global scope. For ESM the top-level
904
+ * scope is the module scope; for CommonJS the top-level scope is the
905
+ * outer function scope.
906
+ *
907
+ * Without this check, we might miss a variable declared with `var` at
908
+ * the top-level because it won't exist in the global scope.
909
+ */
910
+ if (
911
+ currentScope.type === "global" &&
912
+ currentScope.childScopes.length > 0 &&
913
+ // top-level scopes refer to a `Program` node
914
+ currentScope.childScopes[0].block === this.ast
915
+ ) {
916
+ initialScope = currentScope.childScopes[0];
917
+ }
918
+
919
+ for (let scope = initialScope; scope; scope = scope.upper) {
920
+ const variable = scope.variables.find(
921
+ scopeVar => scopeVar.name === name,
922
+ );
923
+
924
+ if (variable) {
925
+ variable.eslintUsed = true;
926
+ return true;
927
+ }
928
+ }
929
+
930
+ return false;
931
+ }
932
+
933
+ /**
934
+ * Returns an array of all inline configuration nodes found in the
935
+ * source code.
936
+ * @returns {Array<Token>} An array of all inline configuration nodes.
937
+ */
938
+ getInlineConfigNodes() {
939
+ // check the cache first
940
+ let configNodes = this[caches].get("configNodes");
941
+
942
+ if (configNodes) {
943
+ return configNodes;
944
+ }
945
+
946
+ // calculate fresh config nodes
947
+ configNodes = this.ast.comments.filter(comment => {
948
+ // shebang comments are never directives
949
+ if (comment.type === "Shebang") {
950
+ return false;
951
+ }
952
+
953
+ const directive = commentParser.parseDirective(comment.value);
954
+
955
+ if (!directive) {
956
+ return false;
957
+ }
958
+
959
+ if (!directivesPattern.test(directive.label)) {
960
+ return false;
961
+ }
962
+
963
+ // only certain comment types are supported as line comments
964
+ return (
965
+ comment.type !== "Line" ||
966
+ !!/^eslint-disable-(next-)?line$/u.test(directive.label)
967
+ );
968
+ });
969
+
970
+ this[caches].set("configNodes", configNodes);
971
+
972
+ return configNodes;
973
+ }
974
+
975
+ /**
976
+ * Returns an all directive nodes that enable or disable rules along with any problems
977
+ * encountered while parsing the directives.
978
+ * @returns {{problems:Array<Problem>,directives:Array<Directive>}} Information
979
+ * that ESLint needs to further process the directives.
980
+ */
981
+ getDisableDirectives() {
982
+ // check the cache first
983
+ const cachedDirectives = this[caches].get("disableDirectives");
984
+
985
+ if (cachedDirectives) {
986
+ return cachedDirectives;
987
+ }
988
+
989
+ const problems = [];
990
+ const directives = [];
991
+
992
+ this.getInlineConfigNodes().forEach(comment => {
993
+ // Step 1: Parse the directive
994
+ const {
995
+ label,
996
+ value,
997
+ justification: justificationPart,
998
+ } = commentParser.parseDirective(comment.value);
999
+
1000
+ // Step 2: Extract the directive value
1001
+ const lineCommentSupported = /^eslint-disable-(next-)?line$/u.test(
1002
+ label,
1003
+ );
1004
+
1005
+ if (comment.type === "Line" && !lineCommentSupported) {
1006
+ return;
1007
+ }
1008
+
1009
+ // Step 3: Validate the directive does not span multiple lines
1010
+ if (
1011
+ label === "eslint-disable-line" &&
1012
+ comment.loc.start.line !== comment.loc.end.line
1013
+ ) {
1014
+ const message = `${label} comment should not span multiple lines.`;
1015
+
1016
+ problems.push({
1017
+ ruleId: null,
1018
+ message,
1019
+ loc: comment.loc,
1020
+ });
1021
+ return;
1022
+ }
1023
+
1024
+ // Step 4: Extract the directive value and create the Directive object
1025
+ switch (label) {
1026
+ case "eslint-disable":
1027
+ case "eslint-enable":
1028
+ case "eslint-disable-next-line":
1029
+ case "eslint-disable-line": {
1030
+ const directiveType = label.slice("eslint-".length);
1031
+
1032
+ directives.push(
1033
+ new Directive({
1034
+ type: directiveType,
1035
+ node: comment,
1036
+ value,
1037
+ justification: justificationPart,
1038
+ }),
1039
+ );
1040
+ }
1041
+
1042
+ // no default
1043
+ }
1044
+ });
1045
+
1046
+ const result = { problems, directives };
1047
+
1048
+ this[caches].set("disableDirectives", result);
1049
+
1050
+ return result;
1051
+ }
1052
+
1053
+ /**
1054
+ * Applies language options sent in from the core.
1055
+ * @param {Object} languageOptions The language options for this run.
1056
+ * @returns {void}
1057
+ */
1058
+ applyLanguageOptions(languageOptions) {
1059
+ /*
1060
+ * Add configured globals and language globals
1061
+ *
1062
+ * Using Object.assign instead of object spread for performance reasons
1063
+ * https://github.com/eslint/eslint/issues/16302
1064
+ */
1065
+ const configGlobals = Object.assign(
1066
+ Object.create(null), // https://github.com/eslint/eslint/issues/18363
1067
+ getGlobalsForEcmaVersion(languageOptions.ecmaVersion),
1068
+ languageOptions.sourceType === "commonjs"
1069
+ ? globals.commonjs
1070
+ : void 0,
1071
+ languageOptions.globals,
1072
+ );
1073
+ const varsCache = this[caches].get("vars");
1074
+
1075
+ varsCache.set("configGlobals", configGlobals);
1076
+ }
1077
+
1078
+ /**
1079
+ * Applies configuration found inside of the source code. This method is only
1080
+ * called when ESLint is running with inline configuration allowed.
1081
+ * @returns {{problems:Array<Problem>,configs:{config:FlatConfigArray,loc:Location}}} Information
1082
+ * that ESLint needs to further process the inline configuration.
1083
+ */
1084
+ applyInlineConfig() {
1085
+ const problems = [];
1086
+ const configs = [];
1087
+ const exportedVariables = {};
1088
+ const inlineGlobals = Object.create(null);
1089
+
1090
+ this.getInlineConfigNodes().forEach(comment => {
1091
+ const { label, value } = commentParser.parseDirective(
1092
+ comment.value,
1093
+ );
1094
+
1095
+ switch (label) {
1096
+ case "exported":
1097
+ Object.assign(
1098
+ exportedVariables,
1099
+ commentParser.parseListConfig(value),
1100
+ );
1101
+ break;
1102
+
1103
+ case "globals":
1104
+ case "global":
1105
+ for (const [id, idSetting] of Object.entries(
1106
+ commentParser.parseStringConfig(value),
1107
+ )) {
1108
+ let normalizedValue;
1109
+
1110
+ try {
1111
+ normalizedValue = normalizeConfigGlobal(idSetting);
1112
+ } catch (err) {
1113
+ problems.push({
1114
+ ruleId: null,
1115
+ loc: comment.loc,
1116
+ message: err.message,
1117
+ });
1118
+ continue;
1119
+ }
1120
+
1121
+ if (inlineGlobals[id]) {
1122
+ inlineGlobals[id].comments.push(comment);
1123
+ inlineGlobals[id].value = normalizedValue;
1124
+ } else {
1125
+ inlineGlobals[id] = {
1126
+ comments: [comment],
1127
+ value: normalizedValue,
1128
+ };
1129
+ }
1130
+ }
1131
+ break;
1132
+
1133
+ case "eslint": {
1134
+ const parseResult =
1135
+ commentParser.parseJSONLikeConfig(value);
1136
+
1137
+ if (parseResult.ok) {
1138
+ configs.push({
1139
+ config: {
1140
+ rules: parseResult.config,
1141
+ },
1142
+ loc: comment.loc,
1143
+ });
1144
+ } else {
1145
+ problems.push({
1146
+ ruleId: null,
1147
+ loc: comment.loc,
1148
+ message: parseResult.error.message,
1149
+ });
1150
+ }
1151
+
1152
+ break;
1153
+ }
1154
+
1155
+ // no default
1156
+ }
1157
+ });
1158
+
1159
+ // save all the new variables for later
1160
+ const varsCache = this[caches].get("vars");
1161
+
1162
+ varsCache.set("inlineGlobals", inlineGlobals);
1163
+ varsCache.set("exportedVariables", exportedVariables);
1164
+
1165
+ return {
1166
+ configs,
1167
+ problems,
1168
+ };
1169
+ }
1170
+
1171
+ /**
1172
+ * Called by ESLint core to indicate that it has finished providing
1173
+ * information. We now add in all the missing variables and ensure that
1174
+ * state-changing methods cannot be called by rules.
1175
+ * @returns {void}
1176
+ */
1177
+ finalize() {
1178
+ const varsCache = this[caches].get("vars");
1179
+ const configGlobals = varsCache.get("configGlobals");
1180
+ const inlineGlobals = varsCache.get("inlineGlobals");
1181
+ const exportedVariables = varsCache.get("exportedVariables");
1182
+ const globalScope = this.scopeManager.scopes[0];
1183
+
1184
+ addDeclaredGlobals(globalScope, configGlobals, inlineGlobals);
1185
+
1186
+ if (exportedVariables) {
1187
+ markExportedVariables(globalScope, exportedVariables);
1188
+ }
1189
+ }
1190
+
1191
+ /**
1192
+ * Traverse the source code and return the steps that were taken.
1193
+ * @returns {Array<TraversalStep>} The steps that were taken while traversing the source code.
1194
+ */
1195
+ traverse() {
1196
+ // Because the AST doesn't mutate, we can cache the steps
1197
+ if (this.#steps) {
1198
+ return this.#steps;
1199
+ }
1200
+
1201
+ const steps = (this.#steps = []);
1202
+
1203
+ /*
1204
+ * This logic works for any AST, not just ESTree. Because ESLint has allowed
1205
+ * custom parsers to return any AST, we need to ensure that the traversal
1206
+ * logic works for any AST.
1207
+ */
1208
+ const emitter = createEmitter();
1209
+ let analyzer = {
1210
+ enterNode(node) {
1211
+ steps.push(
1212
+ new VisitNodeStep({
1213
+ target: node,
1214
+ phase: 1,
1215
+ args: [node, node.parent],
1216
+ }),
1217
+ );
1218
+ },
1219
+ leaveNode(node) {
1220
+ steps.push(
1221
+ new VisitNodeStep({
1222
+ target: node,
1223
+ phase: 2,
1224
+ args: [node, node.parent],
1225
+ }),
1226
+ );
1227
+ },
1228
+ emitter,
1229
+ };
1230
+
1231
+ /*
1232
+ * We do code path analysis for ESTree only. Code path analysis is not
1233
+ * necessary for other ASTs, and it's also not possible to do for other
1234
+ * ASTs because the necessary information is not available.
1235
+ *
1236
+ * Generally speaking, we can tell that the AST is an ESTree if it has a
1237
+ * Program node at the top level. This is not a perfect heuristic, but it
1238
+ * is good enough for now.
1239
+ */
1240
+ if (this.isESTree) {
1241
+ analyzer = new CodePathAnalyzer(analyzer);
1242
+
1243
+ CODE_PATH_EVENTS.forEach(eventName => {
1244
+ emitter.on(eventName, (...args) => {
1245
+ steps.push(
1246
+ new CallMethodStep({
1247
+ target: eventName,
1248
+ args,
1249
+ }),
1250
+ );
1251
+ });
1252
+ });
1253
+ }
1254
+
1255
+ /*
1256
+ * The actual AST traversal is done by the `Traverser` class. This class
1257
+ * is responsible for walking the AST and calling the appropriate methods
1258
+ * on the `analyzer` object, which is appropriate for the given AST.
1259
+ */
1260
+ Traverser.traverse(this.ast, {
1261
+ enter(node, parent) {
1262
+ // save the parent node on a property for backwards compatibility
1263
+ node.parent = parent;
1264
+
1265
+ analyzer.enterNode(node);
1266
+ },
1267
+ leave(node) {
1268
+ analyzer.leaveNode(node);
1269
+ },
1270
+ visitorKeys: this.visitorKeys,
1271
+ });
1272
+
1273
+ return steps;
1274
+ }
1204
1275
  }
1205
1276
 
1206
1277
  module.exports = SourceCode;