eslint 9.21.0 → 9.23.0

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