@inthedark/dxlint 0.0.0-stage → 0.1.1

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 (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -2
  3. package/bin/dxlint +15 -0
  4. package/bin/dxlint-linux-x64 +0 -0
  5. package/bin/dxlint-win32-x64.exe +0 -0
  6. package/package.json +40 -4
  7. package/rules/conventions.dx +34 -0
  8. package/rules/correctness.dx +33 -0
  9. package/rules/es-no-semi.dx +11 -0
  10. package/rules/format.dx +49 -0
  11. package/rules/recommended.dx +51 -0
  12. package/rules/suspicious.dx +31 -0
  13. package/rules/types.dx +21 -0
  14. package/std/c.dx +23 -0
  15. package/std/core/lines.dx +51 -0
  16. package/std/core/lists.dx +76 -0
  17. package/std/core.dx +30 -0
  18. package/std/css.dx +17 -0
  19. package/std/dx.dx +31 -0
  20. package/std/json.dx +104 -0
  21. package/std/markdown.dx +11 -0
  22. package/std/python.dx +21 -0
  23. package/std/rust/format.dx +316 -0
  24. package/std/rust.dx +34 -0
  25. package/std/toml.dx +19 -0
  26. package/std/ts/arithmetic.dx +87 -0
  27. package/std/ts/assertions.dx +477 -0
  28. package/std/ts/calls.dx +246 -0
  29. package/std/ts/comments.dx +10 -0
  30. package/std/ts/conditions.dx +531 -0
  31. package/std/ts/constants.dx +335 -0
  32. package/std/ts/deprecation.dx +185 -0
  33. package/std/ts/directives.dx +60 -0
  34. package/std/ts/effects.dx +219 -0
  35. package/std/ts/enums.dx +233 -0
  36. package/std/ts/expression-fixes.dx +67 -0
  37. package/std/ts/expressions.dx +270 -0
  38. package/std/ts/flow.dx +2 -0
  39. package/std/ts/format.dx +755 -0
  40. package/std/ts/globals.dx +106 -0
  41. package/std/ts/grammar.dx +488 -0
  42. package/std/ts/helpers.dx +102 -0
  43. package/std/ts/import-attributes.dx +88 -0
  44. package/std/ts/import-no-deprecated.dx +73 -0
  45. package/std/ts/indentation.dx +442 -0
  46. package/std/ts/jsx.dx +646 -0
  47. package/std/ts/layout/blocks.dx +281 -0
  48. package/std/ts/layout/calls.dx +79 -0
  49. package/std/ts/layout/expressions.dx +213 -0
  50. package/std/ts/layout/literals.dx +114 -0
  51. package/std/ts/layout/separators.dx +184 -0
  52. package/std/ts/layout/shared.dx +203 -0
  53. package/std/ts/layout/text.dx +142 -0
  54. package/std/ts/layout/types.dx +182 -0
  55. package/std/ts/layout.dx +9 -0
  56. package/std/ts/license.dx +54 -0
  57. package/std/ts/literal-text.dx +3 -0
  58. package/std/ts/literals.dx +95 -0
  59. package/std/ts/methods.dx +69 -0
  60. package/std/ts/modules.dx +268 -0
  61. package/std/ts/names.dx +348 -0
  62. package/std/ts/naming.dx +169 -0
  63. package/std/ts/narrowing.dx +96 -0
  64. package/std/ts/parentheses.dx +262 -0
  65. package/std/ts/precedence.dx +25 -0
  66. package/std/ts/problems/calls.dx +34 -0
  67. package/std/ts/problems/classes.dx +39 -0
  68. package/std/ts/problems/control-flow.dx +257 -0
  69. package/std/ts/problems/declarations.dx +67 -0
  70. package/std/ts/problems/duplicates.dx +70 -0
  71. package/std/ts/problems/expressions.dx +154 -0
  72. package/std/ts/problems/helpers.dx +22 -0
  73. package/std/ts/problems/imports.dx +43 -0
  74. package/std/ts/problems/non-null.dx +78 -0
  75. package/std/ts/problems/numeric-precision.dx +66 -0
  76. package/std/ts/problems/regexp.dx +66 -0
  77. package/std/ts/problems/statements.dx +36 -0
  78. package/std/ts/problems/typescript.dx +55 -0
  79. package/std/ts/problems/whitespace.dx +40 -0
  80. package/std/ts/problems.dx +20 -0
  81. package/std/ts/regex/checks.dx +249 -0
  82. package/std/ts/regex/findings.dx +209 -0
  83. package/std/ts/regex/grammar.dx +80 -0
  84. package/std/ts/regex/suggestions.dx +94 -0
  85. package/std/ts/regex/values.dx +378 -0
  86. package/std/ts/regex.dx +5 -0
  87. package/std/ts/restrictions/comments.dx +76 -0
  88. package/std/ts/restrictions.dx +811 -0
  89. package/std/ts/scopes.dx +68 -0
  90. package/std/ts/scoping/atomic.dx +40 -0
  91. package/std/ts/scoping/bindings.dx +126 -0
  92. package/std/ts/scoping/constness.dx +72 -0
  93. package/std/ts/scoping/globals.dx +52 -0
  94. package/std/ts/scoping/loops.dx +86 -0
  95. package/std/ts/scoping/member-uses.dx +111 -0
  96. package/std/ts/scoping/receivers.dx +107 -0
  97. package/std/ts/scoping/shadowing.dx +202 -0
  98. package/std/ts/scoping/usage.dx +393 -0
  99. package/std/ts/scoping.dx +842 -0
  100. package/std/ts/selectors.dx +10 -0
  101. package/std/ts/spreads.dx +97 -0
  102. package/std/ts/structure/declarations.dx +52 -0
  103. package/std/ts/structure/lines.dx +79 -0
  104. package/std/ts/structure/lists.dx +144 -0
  105. package/std/ts/structure/padding.dx +258 -0
  106. package/std/ts/structure.dx +963 -0
  107. package/std/ts/style/classes.dx +157 -0
  108. package/std/ts/style/comments.dx +75 -0
  109. package/std/ts/style/declarations.dx +82 -0
  110. package/std/ts/style/expressions.dx +125 -0
  111. package/std/ts/style/imports.dx +44 -0
  112. package/std/ts/style/ordering.dx +192 -0
  113. package/std/ts/style/returns.dx +152 -0
  114. package/std/ts/style/signatures.dx +308 -0
  115. package/std/ts/style/type-exports.dx +62 -0
  116. package/std/ts/style/type-imports.dx +166 -0
  117. package/std/ts/style/types.dx +474 -0
  118. package/std/ts/style.dx +29 -0
  119. package/std/ts/suggestions/accessors.dx +138 -0
  120. package/std/ts/suggestions/builtins.dx +110 -0
  121. package/std/ts/suggestions/comparisons.dx +41 -0
  122. package/std/ts/suggestions/computed-keys.dx +20 -0
  123. package/std/ts/suggestions/constructors.dx +54 -0
  124. package/std/ts/suggestions/control-flow.dx +204 -0
  125. package/std/ts/suggestions/curly.dx +124 -0
  126. package/std/ts/suggestions/directives.dx +79 -0
  127. package/std/ts/suggestions/errors.dx +106 -0
  128. package/std/ts/suggestions/expressions.dx +230 -0
  129. package/std/ts/suggestions/functions.dx +260 -0
  130. package/std/ts/suggestions/globals.dx +187 -0
  131. package/std/ts/suggestions/helpers.dx +17 -0
  132. package/std/ts/suggestions/object-shorthand.dx +148 -0
  133. package/std/ts/suggestions/object-spread.dx +96 -0
  134. package/std/ts/suggestions/regexp.dx +59 -0
  135. package/std/ts/suggestions/renames.dx +50 -0
  136. package/std/ts/suggestions/strings.dx +47 -0
  137. package/std/ts/suggestions/templates.dx +104 -0
  138. package/std/ts/suggestions.dx +24 -0
  139. package/std/ts/template-fixes.dx +22 -0
  140. package/std/ts/tokens.dx +124 -0
  141. package/std/ts/type-specifiers.dx +93 -0
  142. package/std/ts/typed/assignments.dx +154 -0
  143. package/std/ts/typed/awaitability.dx +157 -0
  144. package/std/ts/typed/boolean-comparisons.dx +82 -0
  145. package/std/ts/typed/defaults.dx +97 -0
  146. package/std/ts/typed/enum-consistency.dx +68 -0
  147. package/std/ts/typed/enums.dx +52 -0
  148. package/std/ts/typed/errors.dx +32 -0
  149. package/std/ts/typed/floating-promises.dx +147 -0
  150. package/std/ts/typed/for-in-arrays.dx +21 -0
  151. package/std/ts/typed/includes.dx +102 -0
  152. package/std/ts/typed/nullish-coalescing.dx +149 -0
  153. package/std/ts/typed/optional-chains.dx +317 -0
  154. package/std/ts/typed/promises.dx +205 -0
  155. package/std/ts/typed/qualifiers.dx +108 -0
  156. package/std/ts/typed/readonly-members.dx +159 -0
  157. package/std/ts/typed/readonly-parameters.dx +121 -0
  158. package/std/ts/typed/redundant-constituents.dx +171 -0
  159. package/std/ts/typed/regexp-exec.dx +79 -0
  160. package/std/ts/typed/rejection-handlers.dx +91 -0
  161. package/std/ts/typed/return-await.dx +116 -0
  162. package/std/ts/typed/return-this.dx +67 -0
  163. package/std/ts/typed/strict-void-return.dx +121 -0
  164. package/std/ts/typed/string-boundaries.dx +153 -0
  165. package/std/ts/typed/switches.dx +147 -0
  166. package/std/ts/typed/template-expressions.dx +127 -0
  167. package/std/ts/typed/template-values.dx +88 -0
  168. package/std/ts/typed/transfers.dx +106 -0
  169. package/std/ts/typed/type-arguments.dx +337 -0
  170. package/std/ts/typed/type-constituents.dx +143 -0
  171. package/std/ts/typed/type-conversions.dx +82 -0
  172. package/std/ts/typed/type-parameters.dx +280 -0
  173. package/std/ts/typed/unbound-method.dx +167 -0
  174. package/std/ts/typed/unsafe-calls.dx +73 -0
  175. package/std/ts/typed/unsafe-members.dx +36 -0
  176. package/std/ts/typed/void-expressions.dx +101 -0
  177. package/std/ts/typed.dx +585 -0
  178. package/std/ts/values/arguments.dx +13 -0
  179. package/std/ts/values/enums.dx +104 -0
  180. package/std/ts/values/inspection.dx +274 -0
  181. package/std/ts/values.dx +1217 -0
  182. package/std/ts.dx +77 -0
  183. package/std/whitespace.dx +37 -0
@@ -0,0 +1,842 @@
1
+ # .ts/scoping: eslint core and typescript-eslint rules about declarations,
2
+ # scopes and names (shadowing, use before definition, `this`),
3
+ # on the native TypeScript layers and bindings.
4
+
5
+ import .ts/naming
6
+ import .ts/expression-fixes
7
+ import .ts/scoping/receivers
8
+ import .ts/scoping/bindings
9
+ import .ts/scoping/atomic
10
+ import .ts/scoping/loops
11
+ import .ts/scoping/constness
12
+ import .ts/scoping/globals
13
+ import .ts/scoping/shadowing
14
+ import .ts/scoping/usage
15
+
16
+ define {
17
+
18
+ in ts_assignment rule es-require-atomic-updates {
19
+ option allowProperties boolean = false
20
+ let updates scoping_atomic_updates(node)
21
+ each updated in filter(updates, u => !u.property || !allowProperties) {
22
+ warn if updated.property then "Possible race condition: `{updated.value}` might be assigned based on an outdated state of `{updated.name}`." else "Possible race condition: `{updated.name}` might be reassigned based on an outdated value of `{updated.name}`."
23
+ }
24
+ }
25
+
26
+ in ts_this rule es-no-invalid-this {
27
+ option capIsConstructor boolean = true
28
+ let context scoping_receiver_context
29
+ when !context.valid && !(capIsConstructor && context.constructor)
30
+ warn "Unexpected 'this'."
31
+ }
32
+
33
+ in ts_this let arrow_bound_this true
34
+ in ts_parenthesized forward arrow_bound_this from expression
35
+ in ts_call let arrow_bind_lexical count(argument) == 1 && first([] argument.arrow_bound_this) == true
36
+ in ts_node let arrow_bound_callback when {
37
+ parent is ts_parenthesized then parent.arrow_bound_callback
38
+ is ts_call.callee && parent.arrow_callback_receiver != null then parent.arrow_bind_lexical
39
+ else then null
40
+ }
41
+ # Null: not a callback; boolean: its receiver is lexical.
42
+ in ts_node let arrow_callback_receiver when {
43
+ is (ts_call.argument, ts_new.argument) then false
44
+ parent is (ts_parenthesized, ts_logical) || parent is ts_conditional && not ts_conditional.test then parent.arrow_callback_receiver
45
+ is ts_member.object && parent.property == "bind" then parent.arrow_bound_callback
46
+ else then null
47
+ }
48
+ in ts_node let arrow_callback_barrier
49
+ if is (ts_function, ts_method) then false
50
+ else is ts_super || is ts_meta_property && starts_with(text, "new") || any(children.arrow_callback_barrier, b => b == true)
51
+ fn arrow_callback_fix(source, n, lexical) = {
52
+ value parameters: n.parameter.nodes
53
+ value names: [] n.parameter.name
54
+ value distinct: unique(names)
55
+ value duplicates: count(distinct) != count(names)
56
+ value unsafe: n.body.scoping_receiver_read == true && !lexical || duplicates || first(names) == "this" || count(n.type_parameter) != 0
57
+ value wrapped: {
58
+ value n
59
+ walk (current) if current.parent is ts_parenthesized then current.parent.node else null
60
+ last
61
+ }
62
+ value member: wrapped.parent.node
63
+ value bound: if lexical && member is ts_member && member.property == "bind"
64
+ && member.parent is ts_call && member.parent.arrow_bind_lexical then member.parent.node else null
65
+ value target: bound ?? n
66
+ value function_tokens: overlapping(ts_keyword, n.offset, n.body.offset)
67
+ value keyword: { value function_tokens; find (token) token.text == "function" }
68
+ value opening_from: n.name.end_offset ?? keyword.end_offset
69
+ value opening_until: first(parameters).offset ?? n.body.offset
70
+ value opening_tokens: overlapping(ts_open_paren, opening_from, opening_until)
71
+ value opening: first(opening_tokens)
72
+ value before_body: n.body.node.token_neighbours.before
73
+ value comments: overlapping(ts_comment, keyword.end_offset, opening.offset)
74
+ value prefix: if count(comments) == 0 then [{ start: keyword.offset, end: opening.offset, text: "" }]
75
+ else ([{ start: keyword.offset, end: keyword.end_offset, text: "" }] (if n.name != null then [{ start: n.name.offset, end: n.name.end_offset, text: "" }] else []))
76
+ value edits: prefix [{ start: before_body.end, end: before_body.end, text: " =>" }] (if bound != null then [{ start: wrapped.end_offset, end: bound.end_offset, text: "" }] else [])
77
+ value replacement: expression_fix_edits(target.text, target.offset, edits)
78
+ value shape: { kind: "ts_arrow", precedence: if bound != null && wrapped is ts_parenthesized then 20 else 1 }
79
+ value parens: expression_fix_parens(source, target, shape)
80
+ value async_break: contains([] n.modifier, "async") && { value source; byte_slice keyword.offset opening.offset; re_test /[\n\r\u{2028}\u{2029}]/ }
81
+ value if unsafe || lexical && bound == null || keyword == null || opening == null || before_body == null
82
+ || async_break || parens == null || !expression_fix_retains_comments(target, replacement) then null
83
+ else { start: target.offset, end: target.end_offset, text: expression_fix_wrap(replacement, parens > 0) }
84
+ }
85
+ in ts_function rule es-prefer-arrow-callback {
86
+ option allowNamedFunctions boolean = false
87
+ option allowUnboundThis boolean = true
88
+ # Cheap checks first: the barrier scan walks the whole body.
89
+ let callback arrow_callback_receiver
90
+ when callback != null && !(allowNamedFunctions && name != null)
91
+ when !contains([] modifier, "*") && (name.uses.nonempty ?? 0) + (name.self_uses.nonempty ?? 0) + (name.writes.nonempty ?? 0) == 0 && !function_uses_arguments(node)
92
+ when !allowUnboundThis || body.scoping_receiver_read != true || callback
93
+ when !any(children.arrow_callback_barrier, b => b == true)
94
+ warn "Unexpected function expression."
95
+ let edit arrow_callback_fix(source, node, callback)
96
+ fix at (edit ?? { start: offset, end: end_offset }) if edit != null as edit.text
97
+ }
98
+
99
+ in ts_identifier rule es-no-unmodified-loop-condition {
100
+ option checkConditionalExpressions boolean = false
101
+ when scoping_condition_unmodified(node, checkConditionalExpressions)
102
+ warn "'{text}' is not modified in this loop."
103
+ }
104
+
105
+ in ts_function, ts_arrow rule es-no-loop-func {
106
+ let captured scoping_loop_captured(node)
107
+ when { value captured.nonempty ?? []; len } > 0
108
+ let captured_names { value captured; map (name) "'" name "'"; join ", " }
109
+ warn "Function declared in a loop contains unsafe references to variable(s) {captured_names}."
110
+ }
111
+
112
+ in ts_node let scoping_outer_parens if parent is ts_parenthesized then parent.scoping_outer_parens else node
113
+ in ts_node let scoping_callback_destination when {
114
+ parent is (ts_parenthesized, ts_logical, ts_conditional, ts_return) then parent.scoping_callback_destination
115
+ parent is ts_call then { call: parent.node, argument: node }
116
+ else then null
117
+ }
118
+ in ts_return let scoping_callback_destination {
119
+ value owner: containing(ts_function_like).scoping_outer_parens
120
+ value if owner.parent is ts_call && owner.parent.callee.node == owner then owner.parent.scoping_callback_destination else null
121
+ }
122
+ in ts_function, ts_arrow let scoping_callback_method {
123
+ value destination: if contains([] modifier, "*") then null else scoping_callback_destination
124
+ value callee: destination.call.callee.scoping_unparen_node.nonempty ?? destination.call.callee.node
125
+ value method: callee.static_name
126
+ value object: callee.object.scoping_unparen_node.nonempty ?? callee.object.node
127
+ value factory: scoping_array_factory(callee)
128
+ value arguments: [] destination.call.argument.nodes
129
+ value index: if factory then 1 else 0
130
+ value next_index: index + 1
131
+ value asynchronous: contains([] modifier, "async")
132
+ value when {
133
+ destination == null || body == null || callee not (ts_member, ts_index)
134
+ || { value arguments; slice index next_index; first } != destination.argument then null
135
+ factory && (method == "fromAsync" || !asynchronous)
136
+ || !asynchronous && contains(["every", "filter", "find", "findLast", "findIndex", "findLastIndex", "flatMap", "forEach", "map", "reduce", "reduceRight", "some", "sort", "toSorted"], method) then method
137
+ else then null
138
+ }
139
+ }
140
+ in ts_function, ts_arrow let scoping_callback_label
141
+ if contains(["from", "fromAsync"], scoping_callback_method) then "Array." scoping_callback_method else "Array.prototype." scoping_callback_method
142
+ in ts_function, ts_arrow rule es-array-callback-return {
143
+ option allowImplicit boolean = false
144
+ option checkForEach boolean = false
145
+ option allowVoid boolean = false
146
+ when scoping_callback_method != null && scoping_callback_method != "forEach" && body is ts_block && flow_normal(body.node) == true
147
+ report at function_head_site(node)
148
+ warn if count(scoping_own_returns) > 0 then "{scoping_callback_label}() expects a value to be returned at the end of {function_name}." else "{scoping_callback_label}() expects a return value from {function_name}."
149
+ }
150
+ in ts_function, ts_arrow rule es-array-callback-return:return {
151
+ option allowImplicit boolean = false
152
+ option checkForEach boolean = false
153
+ option allowVoid boolean = false
154
+ when scoping_callback_method != null && body is ts_block
155
+ each returned in filter(scoping_own_returns, r => if scoping_callback_method == "forEach" then checkForEach && r.argument != null && !(allowVoid && r.argument.scoping_void_expression == true) else !allowImplicit && r.argument == null) {
156
+ report at returned
157
+ warn if scoping_callback_method == "forEach" then "{scoping_callback_label}() expects no useless return value from {function_name}." else "{scoping_callback_label}() expects a return value from {function_name}."
158
+ }
159
+ }
160
+ in ts_arrow rule es-array-callback-return:concise {
161
+ option allowImplicit boolean = false
162
+ option checkForEach boolean = false
163
+ option allowVoid boolean = false
164
+ when scoping_callback_method == "forEach" && checkForEach && body not ts_block && !(allowVoid && body.scoping_void_expression == true)
165
+ report at function_head_site(node)
166
+ warn "{scoping_callback_label}() expects no useless return value from {function_name}."
167
+ }
168
+
169
+ fn scoping_const_declaration(n) = {
170
+ value n
171
+ walk (current) if current is ts_variable_declaration then null else current.parent.node
172
+ last
173
+ }
174
+ in ts_variable_declaration let scoping_const_bindings if kind != "let" then [] else {
175
+ value owner: node
176
+ value declaration.nodes
177
+ flat_map (item) overlapping(ts_identifier, item.name.offset, item.name.end_offset)
178
+ filter (item) scoping_const_declaration(item) == owner && item.ts_name_declared
179
+ }
180
+ in ts_variable_declaration let scoping_const_fix {
181
+ value bindings: filter(scoping_const_bindings, item => item.declaration.node == item && item.declaration_kind == "let")
182
+ value {
183
+ eligible: kind == "let" && count(bindings) > 0
184
+ && (is (ts_for_in.left, ts_for_of.left) || all(declaration.nodes, item => item.init != null))
185
+ && all(bindings, item => item.scoping_const_state != null && item.scoping_const_state.all),
186
+ before: any(bindings, item => item.scoping_const_state.before)
187
+ }
188
+ }
189
+ in ts_variable_declaration.scoping_const_bindings rule es-prefer-const {
190
+ option destructuring "any" | "all" = "any"
191
+ option ignoreReadBeforeAssign boolean = false
192
+ let binding scoping_const_state
193
+ when binding != null && !(ignoreReadBeforeAssign && binding.before)
194
+ when destructuring != "all" || binding.all && !(ignoreReadBeforeAssign && binding.all_before)
195
+ report at binding.offset
196
+ warn "'{text}' is never reassigned. Use 'const' instead."
197
+ let variable scoping_const_declaration(node)
198
+ # Reserve the declaration so another first-pass edit cannot invalidate const.
199
+ fix at variable if variable.scoping_const_fix.eligible && !(ignoreReadBeforeAssign && variable.scoping_const_fix.before) as
200
+ byte_slice(variable.text, 0, variable.kind.offset - variable.offset) "const" byte_slice(variable.text, variable.kind.end_offset - variable.offset)
201
+ }
202
+
203
+ in ts_function let scoping_function_context {
204
+ value destination: scoping_outer_parens.parent.node
205
+ value declaration: destination is (ts_program, ts_block, ts_export, ts_if, ts_labeled, ts_case, ts_while, ts_for)
206
+ value target: destination.left.node ?? destination.parent.key.node
207
+ value inferred: when {
208
+ destination is (ts_declarator, ts_parameter) then destination.name is ts_identifier
209
+ destination is (ts_property, ts_class_property) then true
210
+ destination is (ts_assignment, ts_assignment_pattern) then target is ts_identifier
211
+ destination is ts_pattern_property then destination.key is ts_identifier
212
+ else then false
213
+ }
214
+ value if destination == null then null else {
215
+ eligible: !declaration || destination is ts_export && destination.kind == "default",
216
+ expression: !declaration, named: name != null, recursive: count(name.references) > 0,
217
+ inferred: inferred, generator: contains(modifier, "*")
218
+ }
219
+ }
220
+ in ts_function rule es-func-names {
221
+ option mode "always" | "as-needed" | "never" = "always"
222
+ option generators "always" | "as-needed" | "never" | null = null "override policy for generators"
223
+ let context scoping_function_context
224
+ let policy if context.generator && generators != null then generators else mode
225
+ when context.eligible && !context.recursive
226
+ when policy == "never" && context.named && context.expression || policy != "never" && !context.named && (policy != "as-needed" || !context.inferred)
227
+ report at function_head_site(node)
228
+ warn if policy == "never" then "Unexpected named {function_name}." else "Unexpected unnamed {function_name}."
229
+ }
230
+
231
+ in ts_parenthesized let scoping_unparen_node expression.scoping_unparen_node.nonempty ?? expression.node
232
+ in ts_parenthesized let scoping_unparen_text expression.scoping_unparen_text.nonempty ?? expression
233
+ in ts_identifier let scoping_assignment_name text
234
+ in ts_member let scoping_assignment_name static_name
235
+ in ts_index let scoping_assignment_name
236
+ if (index.scoping_unparen_node.nonempty ?? index.node) is ts_string then static_name else null
237
+ in ts_member, ts_index let scoping_assignment_property true
238
+ in ts_parenthesized forward scoping_assignment_name, scoping_assignment_property from expression
239
+ in ts_member, ts_index let scoping_property_object object.scoping_unparen_text.nonempty ?? object
240
+ in ts_member, ts_index let scoping_descriptor_method
241
+ if scoping_property_object == "Object" || scoping_property_object == "Reflect" && static_name == "defineProperty" then static_name else null
242
+ in ts_parenthesized forward scoping_descriptor_method from expression
243
+ in ts_string let scoping_string_value literal_value.value
244
+ in ts_parenthesized forward scoping_string_value from expression
245
+ in ts_call let scoping_descriptor_kind callee.scoping_descriptor_method
246
+ in ts_call let scoping_descriptor_name { value [] argument.scoping_string_value; slice 1 2; first }
247
+ in ts_object let scoping_descriptors parent is ts_call && contains(["defineProperties", "create"], parent.scoping_descriptor_kind)
248
+ in ts_property let scoping_descriptor_holder
249
+ if parent.scoping_descriptors then {name: if key is ts_identifier then key else null} else null
250
+ in ts_object let scoping_descriptor_context
251
+ if parent is ts_call && parent.scoping_descriptor_kind == "defineProperty" then {name: parent.scoping_descriptor_name}
252
+ else parent.scoping_descriptor_holder
253
+ in ts_computed_key let scoping_match_key_node expression.node
254
+ in ts_declarator let scoping_name_destination
255
+ {target: if name is ts_identifier then name else null, property: false, commonjs: false, offset: offset}
256
+ in ts_assignment let scoping_name_destination
257
+ {target: left.scoping_assignment_name, property: left.scoping_assignment_property,
258
+ commonjs: left.static_name == "exports" && left.scoping_property_object == "module", offset: offset}
259
+ in ts_property, ts_class_property let scoping_name_key_node key.scoping_match_key_node.nonempty ?? key.node
260
+ # ESLint names string keys only when they are identifier names for the file's
261
+ # edition; its ES5 tables predate modern Unicode, so pre-2015 stays ASCII.
262
+ in ts_property, ts_class_property let scoping_name_target when {
263
+ key.node is ts_computed_key then if scoping_name_key_node is (ts_identifier, ts_string) then static_name else null
264
+ scoping_name_key_node is ts_identifier then static_name
265
+ scoping_name_key_node is ts_string && (if ecma_version >= 2015 then ts_identifier_name_valid(static_name) else is_identifier(static_name)) then static_name
266
+ else then null
267
+ }
268
+ in ts_property, ts_class_property let scoping_name_destination
269
+ {target: scoping_name_target,
270
+ property: true, commonjs: false, descriptor: if static_name == "value" then parent.scoping_descriptor_context else null, offset: offset}
271
+ in ts_parenthesized let scoping_name_destination parent.scoping_name_destination
272
+ in ts_function rule es-func-name-matching {
273
+ option mode "always" | "never" = "always"
274
+ option considerPropertyDescriptor boolean = false
275
+ option includeCommonJSModuleExports boolean = false
276
+ let matched parent.scoping_name_destination
277
+ let descriptor_call considerPropertyDescriptor && matched.descriptor != null
278
+ let target if descriptor_call then matched.descriptor.name else matched.target
279
+ when name != null && matched != null && target != null
280
+ when includeCommonJSModuleExports || !matched.commonjs
281
+ when descriptor_call || ts_identifier_name_valid(target)
282
+ when if mode == "always" then target != name else target == name
283
+ report at matched.offset
284
+ warn "Function name `{name}` should{if mode == 'never' then ' not' else ''} match {if matched.property then 'property' else 'variable'} name `{target}`."
285
+ }
286
+
287
+ in ts_node let scoping_camelcase_assignment_target when {
288
+ is ts_assignment.left && parent.flow_destructuring_target != true then true
289
+ is (ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression, ts_non_null.expression,
290
+ ts_type_assertion.expression, ts_instantiation.expression)
291
+ || flow_destructuring_target == true && parent.flow_destructuring_target == true then parent.scoping_camelcase_assignment_target
292
+ else then false
293
+ }
294
+ in ts_object let scoping_camelcase_import_options
295
+ parent is ts_import_call
296
+ || parent is ts_property && parent.parent is ts_object && parent.parent.parent is ts_import_call
297
+ fn scoping_camelcase_candidate(n) = {
298
+ value name: if n is ts_private_name then slice(n.text, 1) else n.text
299
+ value body: re_replace(name, /^_+|_+$/, "")
300
+ value if !contains(body, "_") || upper(body) == body then null else {
301
+ value wrapped: n.scoping_outer_parens
302
+ value property: n is (ts_property.key, ts_method.key, ts_class_property.key)
303
+ && !(n.parent.parent is ts_object && n.parent.parent.flow_destructuring_target == true)
304
+ || n is ts_member.property && n.parent.scoping_camelcase_assignment_target == true
305
+ value own: n.declaration.node == n
306
+ value kind: n.declaration_kind ?? n.reference_kind
307
+ value imported: kind == "import"
308
+ value external: n.resolved == "global"
309
+ value binding: contains(["var", "let", "const", "using", "parameter", "catch", "function", "function_name", "class", "class_name", "import"], kind) || external
310
+ value label: n is (ts_labeled.label, ts_break.label, ts_continue.label)
311
+ value exported: n is (ts_export_specifier.exported, ts_export.namespace)
312
+ value skipped: !own && (wrapped.parent is (ts_call, ts_new)
313
+ || wrapped is (ts_assignment_pattern.right, ts_parameter.init))
314
+ value value_name: if n.parent is ts_assignment_pattern then n.parent.node else n
315
+ value original: value_name.parent is (ts_property, ts_pattern_property)
316
+ && value_name.parent.key is ts_identifier && value_name.parent.key.text == name
317
+ || value_name.parent is ts_import_specifier
318
+ && re_replace(value_name.parent.imported.text, /^['"]|['"]$/, "") == name
319
+ value if n is ts_property.key && n.parent.parent.scoping_camelcase_import_options == true
320
+ || !(property || label || exported) && (skipped || !binding) then null
321
+ else { name: name, property: property, private: n is ts_private_name,
322
+ import: imported && own, original: original, global: external && contains(["readonly", "writable"], n.global), binding: binding }
323
+ }
324
+ }
325
+ in ts_identifier, ts_private_name rule es-camelcase {
326
+ option allow string[] = [] "names or regular expressions allowed"
327
+ option ignoreDestructuring boolean = false
328
+ option ignoreGlobals boolean = false
329
+ option ignoreImports boolean = false
330
+ option properties "always" | "never" = "always"
331
+ let candidate scoping_camelcase_candidate(node)
332
+ when candidate != null && !any(allow, pattern => candidate.name == pattern || re_test(candidate.name, pattern))
333
+ when candidate.property && properties == "always" || candidate.binding && !(ignoreGlobals && candidate.global) && !(ignoreImports && candidate.import && candidate.original) && !(ignoreDestructuring && candidate.original && !candidate.import) || !candidate.property && !candidate.binding
334
+ warn if candidate.private then "#{candidate.name} is not in camel case." else "Identifier '{candidate.name}' is not in camel case."
335
+ }
336
+
337
+ in ts_block let scoping_block_meaningful { value [] body.scoping_following_meaningful; first; nonempty } ?? false
338
+ in ts_return let scoping_bare_return argument == null
339
+ in ts_statement let scoping_meaningful
340
+ if is ts_block then scoping_block_meaningful
341
+ else !(is (ts_function, ts_break)) && !scoping_bare_return
342
+ in ts_statement let scoping_following_meaningful scoping_meaningful || next.scoping_following_meaningful
343
+ in ts_statement let scoping_following_break is ts_break || next.scoping_following_break
344
+ in ts_case let scoping_case_meaningful any(body.nodes, statement => statement.scoping_meaningful == true) || next.scoping_case_meaningful == true
345
+ in ts_case let scoping_case_exit first([] body.scoping_following_break) || next.node == null || next.scoping_case_meaningful != true
346
+ in ts_statement, ts_case let scoping_return_tail when {
347
+ parent is ts_function_like then true
348
+ parent is ts_loop || is ts_try.finalizer then false
349
+ parent is (ts_block, ts_case, ts_program) then
350
+ !next.scoping_following_meaningful && (parent not ts_case || parent.scoping_case_exit) && parent.scoping_return_tail
351
+ else then parent.scoping_return_tail
352
+ }
353
+ in ts_program let scoping_return_tail ts_source_type(node) == "commonjs" || ts_environment(node, "globalReturn") == true
354
+ in ts_node let scoping_return_fix_owner
355
+ if parent is (ts_function_like, ts_program) then parent.node
356
+ else parent.scoping_return_fix_owner
357
+ in ts_return rule es-no-useless-return {
358
+ when argument == null && reachable(node) == true && scoping_return_tail
359
+ warn "Unnecessary return statement."
360
+ let owner scoping_return_fix_owner
361
+ # Reserve control-flow owner so overlapping edits wait for the next pass.
362
+ fix at owner if parent is (ts_block, ts_case, ts_program, ts_static_block) && expression_fix_clean(node) as
363
+ byte_slice(owner.text, 0, offset - owner.offset) byte_slice(owner.text, end_offset - owner.offset)
364
+ }
365
+
366
+ fn scoping_return_void(subject, asynchronous) = {
367
+ value arms: walk(value_parts(subject) ?? [], arm => if asynchronous then
368
+ flat_map(arm.parts, part => if part.carrier_known && part.carrier.description.kind == "named"
369
+ && contains(["Promise", "PromiseLike"], part.carrier.description.name)
370
+ && count(part.carrier.arguments) > 0 then value_parts(first(part.carrier.arguments)) ?? [] else [])
371
+ else [])
372
+ value any(arms, arm => any(arm.parts, part => contains(part.traits, "void")))
373
+ }
374
+ in ts_program let scoping_return_void_annotation false
375
+ in ts_function, ts_arrow, ts_method let scoping_return_void_annotation
376
+ return_type != null && {
377
+ value asynchronous: contains([] modifier, "async")
378
+ value scoping_return_void(return_type.node, asynchronous)
379
+ }
380
+ in ts_program, ts_function, ts_arrow, ts_method let scoping_return_entry if is ts_program then node else body.node
381
+ in ts_node let scoping_return_syntax count(descendants(ts_return)) > 0
382
+ in ts_program, ts_function, ts_arrow, ts_method let scoping_own_returns if scoping_return_entry.scoping_return_syntax != true then [] else {
383
+ value flow_declared_effects(scoping_return_entry, "returned")
384
+ filter (effect) effect.site is ts_return
385
+ map (effect) effect.site
386
+ sort_by (returned) returned.offset
387
+ }
388
+ in ts_program, ts_function, ts_arrow, ts_method let scoping_consistent_returns
389
+ if re_test(file, /\.(ts|tsx|mts|cts)$/) && any(scoping_own_returns, returned => returned.argument == null)
390
+ && any(scoping_own_returns, returned => returned.argument != null) && scoping_return_void_annotation == true
391
+ then filter(scoping_own_returns, returned => returned.argument != null) else scoping_own_returns
392
+ in ts_node let scoping_return_undefined
393
+ is ts_identifier && text == "undefined" || scoping_void_expression == true
394
+ || re_test(file, /\.(ts|tsx|mts|cts)$/) && ts_value_trait(value_constituent_traits(node, ["undefined"]), "undefined") && ts_value_has_void(node) == false
395
+ in ts_parenthesized let scoping_return_undefined expression.scoping_return_undefined
396
+ fn scoping_return_specified(returned, unspecified) = returned.argument != null && !(unspecified && returned.argument.scoping_return_undefined == true)
397
+ in ts_program, ts_function, ts_arrow, ts_method let scoping_return_name function_name.nonempty ?? "program"
398
+ in ts_program, ts_function, ts_arrow, ts_method let scoping_return_capitalized
399
+ { value scoping_return_name; slice 0 1; upper } { value scoping_return_name; slice 1 }
400
+ in ts_program, ts_function, ts_arrow, ts_method let scoping_return_head when {
401
+ is ts_arrow then operator.offset
402
+ is ts_method then key.offset
403
+ is ts_function then name.offset.nonempty ?? offset
404
+ else then offset
405
+ }
406
+ in ts_function let scoping_return_uppercase name != null && re_test(name, /^\p{Uppercase}/)
407
+ in ts_program, ts_function, ts_arrow, ts_method let scoping_return_constructor
408
+ function_name == "constructor" || scoping_return_uppercase == true
409
+ in ts_program, ts_function, ts_arrow, ts_method rule es-consistent-return {
410
+ option treatUndefinedAsUnspecified boolean = false "treat undefined and void as bare returns"
411
+ let returns scoping_consistent_returns
412
+ let initial first(returns)
413
+ let expected scoping_return_specified(initial, treatUndefinedAsUnspecified)
414
+ each returned in filter(returns, r => r != initial && scoping_return_specified(r, treatUndefinedAsUnspecified) != expected) {
415
+ report at returned
416
+ warn if expected then "{scoping_return_capitalized} expected a return value." else "{scoping_return_capitalized} expected no return value."
417
+ }
418
+ }
419
+ in ts_program, ts_function, ts_arrow, ts_method rule es-consistent-return:fallthrough {
420
+ option treatUndefinedAsUnspecified boolean = false "treat undefined and void as bare returns"
421
+ let initial first(scoping_consistent_returns)
422
+ when scoping_return_specified(initial, treatUndefinedAsUnspecified)
423
+ when !scoping_return_constructor && flow_normal(scoping_return_entry) == true
424
+ report at when {
425
+ is ts_program then scoping_return_head
426
+ is ts_function && node.name != null then { start: node.name.offset, end: node.name.end_offset }
427
+ else then function_head_site(node)
428
+ }
429
+ warn "Expected to return a value at the end of {scoping_return_name}."
430
+ }
431
+
432
+ in ts_node let underscore_declarator when {
433
+ is ts_declarator.name then parent.node
434
+ parent is (ts_array_pattern, ts_object_pattern) || is (ts_pattern_property.value, ts_assignment_pattern.left, ts_rest.argument)
435
+ || is ts_pattern_property.key && (parent.value == null || parent.value is ts_assignment_pattern && parent.value.left == null) then parent.underscore_declarator
436
+ else then null
437
+ }
438
+ in ts_node let underscore_pattern when {
439
+ parent is ts_array_pattern then "array"
440
+ parent is ts_object_pattern then "object"
441
+ parent is (ts_pattern_property, ts_assignment_pattern, ts_rest) then parent.underscore_pattern
442
+ else then null
443
+ }
444
+ in ts_parameter let underscore_parameter_position if parent is (ts_function, ts_arrow, ts_method) then {start: offset, end: parent.end_offset} else null
445
+ in ts_rest let underscore_parameter_position if is ts_parameter.name then parent.underscore_parameter_position else null
446
+ in ts_method let underscore_method_position if parent is ts_class || kind != "get" && kind != "set" then offset else null
447
+ in ts_identifier, ts_private_name let underscore_candidate when {
448
+ underscore_declarator != null then {context: underscore_pattern.nonempty ?? "variable"}
449
+ is ts_function.name && declaration_kind == "function" then {context: "function"}
450
+ is (ts_parameter.name, ts_rest.argument) && parent.underscore_parameter_position != null then {context: "parameter"}
451
+ is ts_method.key && parent.underscore_method_position != null then {context: "method"}
452
+ is ts_class_property.key then {context: "field"}
453
+ is ts_identifier && is (ts_member.property, ts_index.index) then {context: "member"}
454
+ else then null
455
+ }
456
+ fn underscore_this_constructor(object) = object is ts_member && object.property == "constructor" && (object.object.scoping_unparen_node.nonempty ?? object.object.node) is ts_this
457
+ in ts_identifier, ts_private_name rule es-no-underscore-dangle {
458
+ option allow string[] = [] "names allowed to have dangling underscores"
459
+ option allowAfterSuper boolean = false
460
+ option allowAfterThis boolean = false
461
+ option allowAfterThisConstructor boolean = false
462
+ option allowFunctionParams boolean = true
463
+ option allowInArrayDestructuring boolean = true
464
+ option allowInObjectDestructuring boolean = true
465
+ option enforceInClassFields boolean = false
466
+ option enforceInMethodNames boolean = false
467
+ let bare_name if is ts_private_name then slice(text, 1) else text
468
+ when bare_name != "_" && (starts_with(bare_name, "_") || ends_with(bare_name, "_")) && !contains(allow, bare_name)
469
+ let candidate underscore_candidate
470
+ when candidate != null
471
+ when !(allowFunctionParams && candidate.context == "parameter")
472
+ when !(allowInArrayDestructuring && candidate.context == "array")
473
+ when !(allowInObjectDestructuring && candidate.context == "object")
474
+ when (enforceInClassFields || candidate.context != "field") && (enforceInMethodNames || candidate.context != "method")
475
+ let receiver if candidate.context == "member" then parent.object.scoping_unparen_node.nonempty ?? parent.object.node else null
476
+ when !(candidate.context == "member" && (bare_name == "__proto__" || allowAfterThis && receiver is ts_this || allowAfterSuper && receiver is ts_super || allowAfterThisConstructor && underscore_this_constructor(receiver)))
477
+ report at node
478
+ warn "Unexpected dangling '_' in '{text}'."
479
+ }
480
+
481
+ in ts_identifier let scoping_promise_callee text == "Promise" && resolved == "global" && global != "off"
482
+ in ts_unary let scoping_void_expression operator == "void"
483
+ in ts_parenthesized forward scoping_promise_callee, scoping_void_expression from expression
484
+ in ts_parenthesized let scoping_expression_offset expression.scoping_expression_offset.nonempty ?? expression.offset
485
+ in ts_new let scoping_promise_new callee.scoping_promise_callee
486
+ in ts_function, ts_arrow, ts_parenthesized let scoping_executor_argument
487
+ if parent is ts_parenthesized then parent.scoping_executor_argument
488
+ else is ts_new.argument && index == 0 && parent.scoping_promise_new
489
+ in ts_statement, ts_arrow, ts_method, ts_static_block, ts_program, ts_case let scoping_executor_body
490
+ when {
491
+ is ts_function_like then scoping_executor_argument.nonempty ?? false
492
+ is (ts_program, ts_static_block) then false
493
+ else then parent.scoping_executor_body
494
+ }
495
+
496
+ in ts_return let scoping_executor_result
497
+ if scoping_executor_body && argument != null then
498
+ { void: argument.scoping_void_expression, start: offset,
499
+ end: if byte_slice(source, end_offset, end_offset + 1) == ";" then end_offset + 1 else end_offset }
500
+ else null
501
+ in ts_arrow let scoping_executor_result
502
+ if scoping_executor_argument && body not ts_block then
503
+ { void: body.scoping_void_expression,
504
+ start: body.scoping_expression_offset.nonempty ?? body.offset,
505
+ end: unparenthesised_node(body).end_offset } else null
506
+ in ts_return, ts_arrow rule es-no-promise-executor-return {
507
+ option allowVoid boolean = false "allow explicit void return expressions"
508
+ let returned scoping_executor_result
509
+ when returned != null && !(allowVoid && returned.void)
510
+ report at { start: returned.start, end: returned.end }
511
+ warn "Return values from promise executor functions cannot be read."
512
+ }
513
+
514
+ in ts_identifier rule es-block-scoped-var {
515
+ each definition in scoping_outside_definitions(node) {
516
+ warn "'{text}' declared on line {definition.line} column {definition.column} is used outside of binding context."
517
+ }
518
+ }
519
+
520
+ in ts_statement, ts_arrow, ts_method, ts_static_block, ts_program, ts_case let scoping_declaration_body
521
+ when {
522
+ is ts_program then "program"
523
+ is ts_static_block then "class static block body"
524
+ is ts_function_like then "function body"
525
+ else then parent.scoping_declaration_body
526
+ }
527
+ in ts_program, ts_static_block let scoping_body_root true
528
+ in ts_block let scoping_body_root parent is (ts_function_like, ts_static_block)
529
+ in ts_block let scoping_static_body parent is ts_static_block
530
+ fn scoping_var_step(prior, variable, prefix, static_body) =
531
+ when {
532
+ prior == 2 then 2
533
+ variable then 1
534
+ prior == 0 && prefix && !static_body then 0
535
+ else then 2
536
+ }
537
+ in ts_expression_statement let scoping_var_directive expression is ts_string
538
+ in ts_export let scoping_export_variable declaration is ts_variable_declaration
539
+ in ts_statement let scoping_var_prefix {
540
+ value previous.scoping_var_prefix.nonempty ?? 0
541
+ scoping_var_step (is ts_variable_declaration || scoping_export_variable) (is ts_import || scoping_var_directive) (parent is ts_static_block || parent.scoping_static_body)
542
+ }
543
+ in ts_export let scoping_export_top parent.scoping_body_root && scoping_var_prefix != 2
544
+ in ts_variable_declaration rule es-vars-on-top {
545
+ when kind == "var"
546
+ when !(if parent is ts_export then parent.scoping_export_top else parent.scoping_body_root && scoping_var_prefix != 2)
547
+ warn "All 'var' declarations must be at the top of the function scope."
548
+ }
549
+
550
+ in ts_function rule es-no-inner-declarations {
551
+ option mode "functions" | "both" = "functions"
552
+ option blockScopedFunctions "allow" | "disallow" = "allow"
553
+ when parent is (ts_program, ts_block, ts_export, ts_if, ts_labeled, ts_while, ts_do_while, ts_for, ts_for_in, ts_for_of, ts_case)
554
+ when !parent.scoping_body_root && parent not ts_export
555
+ when !(blockScopedFunctions == "allow" && ecma_version >= 2015 && parent.scoping_strict_code)
556
+ warn "Move function declaration to { parent.scoping_declaration_body.nonempty ?? 'program' } root."
557
+ }
558
+ in ts_variable_declaration rule es-no-inner-declarations:variable {
559
+ option mode "functions" | "both" = "functions"
560
+ option blockScopedFunctions "allow" | "disallow" = "allow"
561
+ when mode == "both" && kind == "var" && !parent.scoping_body_root && parent not ts_export
562
+ warn "Move variable declaration to { parent.scoping_declaration_body.nonempty ?? 'program' } root."
563
+ }
564
+
565
+ in ts_identifier rule es-no-redeclare {
566
+ option builtinGlobals boolean = true "also report built-in global redeclarations"
567
+ option ignoreDeclarationMerge boolean = true "allow typescript declaration merging"
568
+ when scoping_lexical_declaration
569
+ let redeclared scoping_redeclaration_context(node)
570
+ when redeclared != null && (redeclared.duplicate || redeclared.comment || builtinGlobals && redeclared.builtin)
571
+ when !(ignoreDeclarationMerge && redeclared.safe_merge && !redeclared.comment && !(builtinGlobals && redeclared.builtin))
572
+ warn when {
573
+ builtinGlobals && redeclared.builtin then "'{text}' is already defined as a built-in global variable."
574
+ redeclared.comment then "'{text}' is already defined by a variable declaration."
575
+ else then "'{text}' is already defined."
576
+ }
577
+ }
578
+ in ts_program rule es-no-redeclare:global {
579
+ option builtinGlobals boolean = true "also report built-in global redeclarations"
580
+ option ignoreDeclarationMerge boolean = true "allow typescript declaration merging"
581
+ let redeclarations scoping_global_mentions(node)
582
+ each redeclaration in filter(redeclarations, d => d.duplicate || d.syntax || builtinGlobals && d.builtin) {
583
+ report at { start: redeclaration.offset, end: redeclaration.offset + byte_len(redeclaration.name) }
584
+ warn when {
585
+ builtinGlobals && redeclaration.builtin then "'{redeclaration.name}' is already defined as a built-in global variable."
586
+ redeclaration.syntax then "'{redeclaration.name}' is already defined by a variable declaration."
587
+ else then "'{redeclaration.name}' is already defined."
588
+ }
589
+ }
590
+ }
591
+
592
+ in ts_this let scoping_captures_this true
593
+ in ts_parenthesized forward scoping_captures_this from expression
594
+ in ts_assignment let scoping_assigns_this operator == "=" && right.scoping_captures_this
595
+ in ts_identifier let scoping_initialised_alias parent is ts_declarator && parent.init != null
596
+ in ts_identifier let scoping_alias_initialised
597
+ any(declarations, d => d.scope == scope && d.scoping_initialised_alias)
598
+ || any(declarations, d => any(d.references, r => r.scope == scope && r.writer.scoping_assigns_this))
599
+ in ts_declarator let scoping_alias_context
600
+ if name is ts_identifier then {
601
+ name: name, this: init.scoping_captures_this, valid: init.scoping_captures_this, value: init != null,
602
+ assigned: init != null || name.scoping_alias_initialised
603
+ } else null
604
+ in ts_assignment let scoping_alias_context
605
+ if left is ts_identifier then {
606
+ name: left, this: right.scoping_captures_this, valid: scoping_assigns_this, value: right != null, assigned: right != null
607
+ } else null
608
+ in ts_declarator, ts_assignment rule es-consistent-this {
609
+ option names string[] = ["that"] "names used to capture this"
610
+ let alias scoping_alias_context
611
+ when alias != null && { value if len(names) == 0 then ["that"] else names; contains alias.name }
612
+ when alias.value && !alias.valid || !alias.assigned
613
+ warn "Designated alias '{alias.name}' is not assigned to 'this'."
614
+ }
615
+ in ts_declarator, ts_assignment rule es-consistent-this:unexpected {
616
+ option names string[] = ["that"] "names used to capture this"
617
+ let alias scoping_alias_context
618
+ when alias != null && alias.this && !{ value if len(names) == 0 then ["that"] else names; contains alias.name }
619
+ warn "Unexpected alias '{alias.name}' for 'this'."
620
+ }
621
+
622
+ in ts_program.binding_declarations rule es-no-shadow {
623
+ option allow string[] = [] "names allowed to shadow"
624
+ option builtinGlobals boolean = false "also check configured global names"
625
+ option hoist "all" | "functions" | "never" | "types" | "functions-and-types" = "functions"
626
+ option ignoreOnInitialization boolean = false "allow callback declarations in initializers"
627
+ option ignoreTypeValueShadow boolean = true "allow type and value namespace shadowing"
628
+ option ignoreFunctionTypeParameterNameValueShadow boolean = true "allow parameter names in function types"
629
+ when !contains(allow, identifier_name)
630
+ let shadowed scoping_shadow_context(node, builtinGlobals)
631
+ when shadowed != null
632
+ when !(ignoreTypeValueShadow && shadowed.type_value)
633
+ when !(ignoreFunctionTypeParameterNameValueShadow && shadowed.function_type)
634
+ when !(ignoreOnInitialization && shadowed.initializing)
635
+ when !shadowed.early || hoist == "all" || (hoist == "functions" || hoist == "functions-and-types") && shadowed.kind == "function" || (hoist == "types" || hoist == "functions-and-types") && contains(["type", "interface"], shadowed.kind)
636
+ report at if parent is ts_parameter then parent.node else node
637
+ warn when {
638
+ shadowed.enum_member then "Enum members are added to the enum scope, so references to '{text}' in enum member initializers resolve to this member instead of the declaration in the upper scope on line {shadowed.line} column {shadowed.column}."
639
+ shadowed.global then "'{text}' is already a global variable."
640
+ else then "'{text}' is already declared in the upper scope on line {shadowed.line} column {shadowed.column}."
641
+ }
642
+ }
643
+
644
+ in ts_identifier rule es-no-use-before-define {
645
+ option mode "always" | "nofunc" = "always"
646
+ option functions boolean = true "check function declarations"
647
+ option classes boolean = true "check class declarations across execution contexts"
648
+ option variables boolean = true "check variables across execution contexts"
649
+ option enums boolean = true "check enum declarations"
650
+ option typedefs boolean = true "check type declarations"
651
+ option allowNamedExports boolean = false "allow export specifiers before declarations"
652
+ option ignoreTypeReferences boolean = true "allow type references before declarations"
653
+ when !(ignoreTypeReferences && access == "type")
654
+ let definition scoping_definition_context(node)
655
+ when definition.before && !(allowNamedExports && definition.export)
656
+ when !((!functions || mode == "nofunc") && (definition.kind == "function" || definition.kind == "function_name"))
657
+ when !(definition.separate && (!classes && (definition.kind == "class" || definition.kind == "class_name") || !variables && contains(["var", "let", "const", "using"], definition.kind)))
658
+ when !(!enums && definition.kind == "enum")
659
+ when !(!typedefs && contains(["type", "interface"], definition.kind))
660
+ warn "'{text}' was used before it was defined."
661
+ }
662
+
663
+ fn implicit_global_label(kind) = if kind == "function" || kind == "class" then kind else "'" kind "'"
664
+ in ts_variable_declaration, ts_function, ts_class rule es-no-implicit-globals {
665
+ option lexicalBindings boolean = false "also top-level let, const and class declarations"
666
+ let declarations scoping_global_bindings(node)
667
+ each global_declaration in filter(declarations, d => !d.exported && contains(["var", "let", "const", "function", "class"], d.kind) && d.setting != "writable" && (lexicalBindings || d.kind == "var" || d.kind == "function")) {
668
+ report at { start: global_declaration.offset, end: global_declaration.end }
669
+ warn when {
670
+ global_declaration.setting == "readonly" then "Unexpected redeclaration of read-only global variable."
671
+ global_declaration.kind == "var" || global_declaration.kind == "function" then "Unexpected {implicit_global_label(global_declaration.kind)} declaration in the global scope, wrap in an IIFE for a local variable, assign as global property for a global variable."
672
+ else then "Unexpected {implicit_global_label(global_declaration.kind)} declaration in the global scope, wrap in a block or in an IIFE."
673
+ }
674
+ }
675
+ }
676
+ in ts_identifier rule es-no-implicit-globals:leak {
677
+ option lexicalBindings boolean = false "also top-level let, const and class declarations"
678
+ when access == "write"
679
+ let setting ts_global_setting(scoping_program, identifier_name)
680
+ when ts_source_type(scoping_program) != "module" && !scoping_strict_code && resolved == "global" && (setting == null || setting == "off")
681
+ report at { start: scoping_pattern_write_owner.offset, end: end_offset }
682
+ warn "Global variable leak, declare the variable if it is intended to be local."
683
+ }
684
+ in ts_identifier rule es-no-implicit-globals:readonly {
685
+ option lexicalBindings boolean = false "also top-level let, const and class declarations"
686
+ when access == "write"
687
+ when ts_source_type(scoping_program) != "module" && resolved == "global"
688
+ && ts_global_setting(scoping_program, identifier_name) == "readonly"
689
+ report at { start: scoping_pattern_write_owner.offset, end: end_offset }
690
+ warn "Unexpected assignment to read-only global variable."
691
+ }
692
+
693
+ in ts_variable_declaration rule es-no-case-declarations {
694
+ when parent is ts_case && kind != "var"
695
+ warn "Unexpected lexical declaration in case block."
696
+ }
697
+ in ts_function, ts_class rule es-no-case-declarations:declaration {
698
+ when parent is ts_case
699
+ warn "Unexpected lexical declaration in case block."
700
+ }
701
+
702
+ in ts_node let scoping_pattern_write_owner
703
+ if flow_destructuring_target == true && parent.flow_destructuring_target == true then parent.scoping_pattern_write_owner else node
704
+ in ts_node let scoping_parameter_property_write
705
+ when {
706
+ is (ts_member.property, ts_index.index, ts_conditional.test, ts_call.argument, ts_new.argument) then null
707
+ parent is (ts_member, ts_index) && parent.write_site != null then parent.scoping_pattern_write_owner
708
+ else then parent.scoping_parameter_property_write
709
+ }
710
+ in ts_identifier rule es-no-param-reassign {
711
+ option props boolean = false "also assignments to parameter properties"
712
+ option ignorePropertyModificationsFor string[] = [] "parameter names whose properties may change"
713
+ option ignorePropertyModificationsForRegex string[] = [] "patterns for exempt parameter names"
714
+ when (access == "write" || access == "read_write") && reference_kind == "parameter"
715
+ warn "Assignment to function parameter '{text}'."
716
+ }
717
+ in ts_identifier rule es-no-param-reassign:property {
718
+ option props boolean = false "also assignments to parameter properties"
719
+ option ignorePropertyModificationsFor string[] = [] "parameter names whose properties may change"
720
+ option ignorePropertyModificationsForRegex string[] = [] "patterns for exempt parameter names"
721
+ let modification scoping_parameter_property_write
722
+ when props && reference_kind == "parameter" && modification != null && !contains(ignorePropertyModificationsFor, identifier_name)
723
+ when !any(declaration.references, reference => reference.offset < offset && reference.scoping_parameter_property_write == modification)
724
+ when !any(ignorePropertyModificationsForRegex, pattern => re_test(identifier_name, pattern))
725
+ warn "Assignment to property of function parameter '{text}'."
726
+ }
727
+
728
+ in ts_return rule es-no-constructor-return {
729
+ when argument != null && enclosing_function_name == "constructor"
730
+ warn "Unexpected return statement in constructor."
731
+ }
732
+
733
+ in ts_variable_declaration {
734
+ let scoping_loop_head is (ts_for.init, ts_for_in.left, ts_for_of.left)
735
+ let scoping_iteration_binding is (ts_for_in.left, ts_for_of.left)
736
+ }
737
+ in ts_declarator rule es-init-declarations {
738
+ option mode "always" | "never" = "always"
739
+ option ignoreForLoopInit boolean = false "allow initialized declarations in loop initializers"
740
+ when name.declaration_kind != null && name.ambient != true && mode == "always" && init == null
741
+ when !parent.scoping_iteration_binding
742
+ warn "Variable '{name}' should be initialized on declaration."
743
+ }
744
+ in ts_declarator rule es-init-declarations:never {
745
+ option mode "always" | "never" = "always"
746
+ option ignoreForLoopInit boolean = false "allow initialized declarations in loop initializers"
747
+ when name.declaration_kind != null && name.ambient != true && mode == "never"
748
+ when parent.kind != "const" && name.declaration_kind != "using"
749
+ when init != null || parent.scoping_iteration_binding
750
+ when !(ignoreForLoopInit && parent.scoping_loop_head)
751
+ warn "Variable '{name}' should not be initialized on declaration."
752
+ }
753
+
754
+ in ts_method rule es-class-methods-use-this {
755
+ option exceptMethods string[] = [] "method names that need not use this"
756
+ option enforceForClassFields boolean = true "also check function-valued class fields"
757
+ option ignoreOverrideMethods boolean = false "ignore overriding methods"
758
+ option ignoreClassesWithImplements "all" | "public-fields" | null = null "ignore methods implementing interfaces"
759
+ option ignoreClassesThatImplementAnInterface boolean | "public-fields" = false "typescript-eslint interface exception"
760
+ when parent is ts_class && body != null && !contains(modifier, "static") && static_name != "constructor"
761
+ when !scoping_receiver_used && !{ value default: static_name.nonempty ?? key; value contains(exceptMethods, default) }
762
+ when !(ignoreOverrideMethods && contains(modifier, "override"))
763
+ when !((ignoreClassesWithImplements == "all" || ignoreClassesThatImplementAnInterface == true) && parent.implements != null)
764
+ when !((ignoreClassesWithImplements == "public-fields" || ignoreClassesThatImplementAnInterface == "public-fields") && parent.implements != null && !contains(modifier, "private") && !contains(modifier, "protected") && !starts_with(key, "#"))
765
+ report at function_head_site(node)
766
+ warn "Expected 'this' to be used by class {function_name}."
767
+ }
768
+ in ts_class_property rule es-class-methods-use-this:field {
769
+ option exceptMethods string[] = [] "method names that need not use this"
770
+ option enforceForClassFields boolean = true "also check function-valued class fields"
771
+ option ignoreOverrideMethods boolean = false "ignore overriding methods"
772
+ option ignoreClassesWithImplements "all" | "public-fields" | null = null "ignore methods implementing interfaces"
773
+ option ignoreClassesThatImplementAnInterface boolean | "public-fields" = false "typescript-eslint interface exception"
774
+ when enforceForClassFields && parent is ts_class && !contains(modifier, "static")
775
+ when init.function_name != null && !scoping_receiver_used
776
+ when !{ value default: static_name.nonempty ?? key; value contains(exceptMethods, default) }
777
+ when !(ignoreOverrideMethods && contains(modifier, "override"))
778
+ when !((ignoreClassesWithImplements == "all" || ignoreClassesThatImplementAnInterface == true) && parent.implements != null)
779
+ when !((ignoreClassesWithImplements == "public-fields" || ignoreClassesThatImplementAnInterface == "public-fields") && parent.implements != null && !contains(modifier, "private") && !contains(modifier, "protected") && !starts_with(key, "#"))
780
+ report at if contains(modifier, "accessor") && init is ts_arrow then init.operator.node else function_head_site(init.node)
781
+ warn if contains(modifier, "accessor") then "Expected 'this' to be used by class arrow function." else "Expected 'this' to be used by class {init.function_name}."
782
+ }
783
+
784
+ in ts_identifier let scoping_cap_name text
785
+ in ts_member, ts_index let scoping_cap_name static_name
786
+ in ts_parenthesized let scoping_cap_name expression.scoping_cap_name
787
+ in ts_identifier let scoping_builtin_constructor global == "readonly" || global == "writable"
788
+ in ts_member, ts_index let scoping_builtin_constructor (object == "globalThis" || object == "window" || object == "self" || object == "global") && (object.global == "readonly" || object.global == "writable")
789
+ in ts_parenthesized let scoping_builtin_constructor expression.scoping_builtin_constructor
790
+ in ts_member, ts_index let scoping_is_property true
791
+ in ts_parenthesized let scoping_is_property expression.scoping_is_property
792
+ in ts_identifier let scoping_global_date text == "Date" && resolved == "global" && global != "off"
793
+ in ts_identifier let scoping_global_object contains(["global", "globalThis", "self", "window"], text) && resolved == "global" && contains(["readonly", "writable"], global)
794
+ in ts_member, ts_index let scoping_global_date static_name == "Date" && object.scoping_global_object
795
+ in ts_parenthesized forward scoping_global_date, scoping_global_object from expression
796
+ in ts_member, ts_index let scoping_utc_call static_name == "UTC" && object.scoping_global_date
797
+ in ts_parenthesized forward scoping_utc_call from expression
798
+ in ts_identifier, ts_function, ts_method let scoping_callable_head offset
799
+ in ts_arrow let scoping_callable_head operator.offset
800
+ in ts_member let scoping_callable_head property.scoping_expression_offset.nonempty ?? property.offset
801
+ in ts_index let scoping_callable_head index.scoping_expression_offset.nonempty ?? index.offset
802
+ in ts_parenthesized let scoping_callable_head expression.scoping_callable_head.nonempty ?? expression.offset
803
+ in ts_identifier, ts_function, ts_method let scoping_callable_end end_offset
804
+ in ts_arrow let scoping_callable_end operator.end_offset
805
+ in ts_member let scoping_callable_end property.scoping_expression_end.nonempty ?? property.end_offset
806
+ in ts_index let scoping_callable_end index.scoping_expression_end.nonempty ?? index.end_offset
807
+ in ts_parenthesized let scoping_callable_end expression.scoping_callable_end.nonempty ?? expression.end_offset
808
+ in ts_parenthesized let scoping_expression_end expression.scoping_expression_end.nonempty ?? expression.end_offset
809
+ in ts_call.callee rule es-new-cap {
810
+ option capIsNew boolean = true "require new for capitalised calls"
811
+ option newIsCap boolean = true "require capitalised constructors"
812
+ option capIsNewExceptions string[] = [] "capitalised functions that may be called"
813
+ option newIsCapExceptions string[] = [] "lowercase functions that may be constructed"
814
+ option capIsNewExceptionPattern regex | null = null "pattern for callable capitalised functions"
815
+ option newIsCapExceptionPattern regex | null = null "pattern for constructible lowercase functions"
816
+ option properties boolean = true "check member calls"
817
+ when capIsNew && scoping_cap_name != null && re_test(scoping_cap_name, /^\p{Lu}/)
818
+ when properties || scoping_is_property != true
819
+ when !contains(capIsNewExceptions, scoping_cap_name) && !contains(capIsNewExceptions, text)
820
+ when capIsNewExceptionPattern == null || !re_test(text, capIsNewExceptionPattern)
821
+ when !(scoping_builtin_constructor && contains(["Array", "Boolean", "Date", "Error", "Function", "Number", "Object", "RegExp", "String", "Symbol", "BigInt"], scoping_cap_name))
822
+ when !scoping_utc_call
823
+ report at { start: scoping_callable_head, end: scoping_callable_end }
824
+ warn "A function with a name starting with an uppercase letter should only be used as a constructor."
825
+ }
826
+ in ts_new.callee rule es-new-cap:constructor {
827
+ option capIsNew boolean = true "require new for capitalised calls"
828
+ option newIsCap boolean = true "require capitalised constructors"
829
+ option capIsNewExceptions string[] = [] "capitalised functions that may be called"
830
+ option newIsCapExceptions string[] = [] "lowercase functions that may be constructed"
831
+ option capIsNewExceptionPattern regex | null = null "pattern for callable capitalised functions"
832
+ option newIsCapExceptionPattern regex | null = null "pattern for constructible lowercase functions"
833
+ option properties boolean = true "check member calls"
834
+ when newIsCap && scoping_cap_name != null && re_test(scoping_cap_name, /^\p{Ll}/)
835
+ when properties || scoping_is_property != true
836
+ when !contains(newIsCapExceptions, scoping_cap_name) && !contains(newIsCapExceptions, text)
837
+ when newIsCapExceptionPattern == null || !re_test(text, newIsCapExceptionPattern)
838
+ report at { start: scoping_callable_head, end: scoping_callable_end }
839
+ warn "A constructor name should not start with a lowercase letter."
840
+ }
841
+
842
+ }