@inthedark/dxlint 0.0.0-stage → 0.1.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 (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 +29 -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,963 @@
1
+ # .ts/structure: @stylistic rules about the shape of code across lines:
2
+ # wrapping, comments' places, line lengths.
3
+
4
+ import .ts/parentheses
5
+ import .ts/structure/padding
6
+ import .ts/expression-fixes
7
+ import .ts/import-attributes
8
+ import .ts/precedence
9
+ import .ts/structure/declarations
10
+ import .ts/structure/lists
11
+ import .ts/structure/lines
12
+
13
+ define {
14
+
15
+ in ts_declarator let structure_destructure_left name.node
16
+ in ts_declarator let structure_destructure_right init.structure_unwrapped
17
+ in ts_declarator let structure_destructure_role "VariableDeclarator"
18
+ in ts_declarator let structure_destructure_eligible !contains(["using", "await using"], parent.kind)
19
+ in ts_declarator let structure_destructure_annotated type.node != null
20
+ in ts_assignment let structure_destructure_left left.structure_unwrapped
21
+ in ts_assignment let structure_destructure_right right.structure_unwrapped
22
+ in ts_assignment let structure_destructure_role "AssignmentExpression"
23
+ in ts_assignment let structure_destructure_eligible operator == "="
24
+ in ts_member, ts_index let structure_access_optional optional != null || object is (ts_member, ts_index) && object.structure_access_optional
25
+ in ts_member let structure_access_key property.node
26
+ in ts_index let structure_access_key index.structure_unwrapped
27
+
28
+ fn structure_destructure_enabled(settings, role, kind) =
29
+ if settings == null then true
30
+ else if settings.array != null || settings.object != null then get(settings, kind) == true
31
+ else { value settings; get role; get kind } == true
32
+
33
+ fn structure_typescript(path) = re_test(path, /\.(?:ts|tsx|mts|cts)$/)
34
+
35
+ fn structure_iterable(n) = if n.structure_unwrapped is (ts_array, ts_string, ts_template) then true
36
+ else structure_iterable_parts(n, value_parts(n))
37
+ fn structure_iterable_inventory(handle) = {
38
+ value members: value_children(handle, true, [])
39
+ value if members != null && all(members, edge => edge.member != null
40
+ && contains(["str", "num"], type_of(edge.member.key))) then false else null
41
+ }
42
+ fn structure_iterable_parts(n, parts) = if parts == null then null else if len(parts) == 0 then false else {
43
+ value arms: map(parts, arm => {
44
+ value builtin: any(arm.parts, part => contains(part.traits, "string") || contains(part.traits, "tuple")
45
+ || ts_value_array_part(part, n).array)
46
+ value unresolved: any(arm.parts, part => contains(part.traits, "any") || contains(part.traits, "parameter"))
47
+ value absent: any(arm.parts, part => any(["unknown", "null", "undefined", "void"], trait => contains(part.traits, trait)))
48
+ value { builtin: builtin, unresolved: unresolved, absent: absent, handle: arm.handle }
49
+ })
50
+ value if all(arms, arm => arm.builtin) then true
51
+ else structure_iterable_members(arms, value_lookup(n, { global: "Symbol", class: "value", path: ["iterator"] }))
52
+ }
53
+ fn structure_iterable_members(arms, lookup) = {
54
+ value states: map(zip(arms, lookup.alternatives ?? map(arms, arm => null)), pair => {
55
+ value arm: first(pair)
56
+ value member: last(pair)
57
+ value if arm.builtin || member.matched == true && any(member.sources, source => source.kind == "named") then true
58
+ else if arm.unresolved then null
59
+ else if arm.absent then false
60
+ else if lookup == null then structure_iterable_inventory(arm.handle)
61
+ else false
62
+ })
63
+ value if any(states, state => state == false) then false
64
+ else if all(states, state => state == true) then true
65
+ else null
66
+ }
67
+
68
+ in ts_declarator, ts_assignment rule es-prefer-destructuring {
69
+ option options ({ array?: boolean; object?: boolean; VariableDeclarator?: { array?: boolean; object?: boolean }; AssignmentExpression?: { array?: boolean; object?: boolean }; enforceForRenamedProperties?: boolean; enforceForDeclarationWithTypeAnnotation?: boolean })[] = [] "destructuring categories, per declaration or assignment, renamed-property and annotated-declaration enforcement"
70
+ when structure_destructure_eligible
71
+ let typed structure_typescript(file)
72
+ let enforcement { value options; slice 1; first }
73
+ when !(typed && is ts_declarator && structure_destructure_annotated) || enforcement.enforceForDeclarationWithTypeAnnotation == true
74
+ let settings first(options)
75
+ let array_enabled structure_destructure_enabled(settings, structure_destructure_role, "array")
76
+ let object_enabled structure_destructure_enabled(settings, structure_destructure_role, "object")
77
+ let access structure_destructure_right
78
+ when access is (ts_member, ts_index) && !access.structure_access_optional
79
+ when access.object.structure_unwrapped not ts_super && access.structure_access_key not ts_private_name
80
+ let key access.structure_access_key
81
+ let value key.literal_value
82
+ let array_index access is ts_index && key is ts_number && value.type == "number" && !contains(["Infinity", "-Infinity", "NaN"], value.value) && num(value.value) == { value value.value; num; round }
83
+ let renamed enforcement.enforceForRenamedProperties == true
84
+ when if array_index then array_enabled || typed && renamed && object_enabled else object_enabled
85
+ when array_index || renamed || structure_destructure_left is ts_identifier && (access is ts_member && structure_destructure_left.text == key.text || access is ts_index && key is ts_string && structure_destructure_left.text == value.value)
86
+ let iterable if typed && array_index then structure_iterable(access.object.node) else true
87
+ when iterable != false || renamed
88
+ let category if array_index && iterable != false then "array" else "object"
89
+ when if category == "array" then array_enabled else object_enabled
90
+ let receiver access.object.structure_unwrapped
91
+ let fixable is ts_declarator && !structure_destructure_annotated && structure_destructure_left is ts_identifier && access is ts_member && structure_destructure_left.text == key.text
92
+ && !{
93
+ value overlapping(ts_comment)
94
+ any (comment) comment.offset < receiver.offset || comment.end_offset > receiver.end_offset
95
+ }
96
+ warn "Use {category} destructuring."
97
+ fix if fixable as "{{" key.text "}} = " expression_fix_wrap(receiver.text, receiver.precedence < 1)
98
+ }
99
+
100
+ fn structure_dot_keyword(name) = contains(["abstract", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with"], name)
101
+ fn structure_property_info(subject, key) = {
102
+ value arms: filter(value_trait_parts(subject, ["null", "undefined"]) ?? [], arm => !all(arm.parts, part =>
103
+ contains(part.traits, "null") || contains(part.traits, "undefined")))
104
+ value lookups: map(arms, arm => value_lookup(arm.handle, key))
105
+ value declared: count(lookups) > 0 && all(lookups, lookup => lookup != null
106
+ && count(lookup.alternatives) > 0 && all(lookup.alternatives, alternative =>
107
+ any(alternative.sources, source => source.kind == "named")))
108
+ value sources: if declared then flat_map(lookups, lookup =>
109
+ flat_map(lookup.alternatives, alternative => filter(alternative.sources, source => source.kind == "named"))) else []
110
+ value flags: first(sources).flags
111
+ value {
112
+ declared: declared,
113
+ named: any(lookups, lookup => any(lookup.alternatives, alternative =>
114
+ any(alternative.sources, source => source.kind == "named"))),
115
+ visibility: if !declared then null else if contains(flags, "private_access") then "private"
116
+ else if contains(flags, "protected_access") then "protected" else "public",
117
+ }
118
+ }
119
+
120
+ @macro structure_dot_options {
121
+ option allowKeywords boolean = true "allow ES3 keywords in dot notation"
122
+ option allowPattern string = "" "property names exempt from dot notation"
123
+ option allowPrivateClassPropertyAccess boolean = false "allow bracket access to private properties"
124
+ option allowProtectedClassPropertyAccess boolean = false "allow bracket access to protected properties"
125
+ option allowIndexSignaturePropertyAccess boolean = false "allow bracket access through string-like index signatures"
126
+ }
127
+
128
+ in ts_index rule es-dot-notation:computed {
129
+ @structure_dot_options
130
+ let key index.structure_unwrapped
131
+ let literal key.literal_value
132
+ let template key is ts_template && count(key.expression) == 0
133
+ when key is (ts_string, ts_boolean, ts_null) || template
134
+ let name if template then key.static_name else literal.value
135
+ when re_test(name, /^[a-zA-Z_$][a-zA-Z0-9_$]*$/) && (allowKeywords || !structure_dot_keyword(name))
136
+ when allowPattern == "" || !re_test(name, allowPattern)
137
+ let allow_index allowIndexSignaturePropertyAccess || structure_typescript(file) && ts_compiler_option(node, "noPropertyAccessFromIndexSignature") == true
138
+ let property if allowPrivateClassPropertyAccess || allowProtectedClassPropertyAccess || allow_index
139
+ then structure_property_info(object.node, name) else null
140
+ when !(allowPrivateClassPropertyAccess && property.visibility == "private" || allowProtectedClassPropertyAccess && property.visibility == "protected")
141
+ when !allow_index || property.named == true || ts_value_has_trait(object.node, "unknown") == true
142
+ || ts_value_has_trait(object.node, "any") == true || value_has_member(object.node, name, null) == false
143
+ let open index.space_before.start - 1
144
+ let fixable { value overlapping(ts_comment, open, end_offset); count } == 0
145
+ let receiver object.structure_unwrapped
146
+ let separator if optional != null then "" else if receiver is ts_number && re_test(receiver.text, /^(?:0|0[0-7]*[89]\d*|[1-9](?:_?\d)*)$/) then " ." else "."
147
+ let trailing if space_after.text == "" && next_token is (ts_identifier, ts_keyword, ts_number, ts_boolean, ts_null) then " " else ""
148
+ let quoted if template then "`" name "`" else if literal.type == "string" then "\"" name "\"" else name
149
+ report at key
150
+ warn "[{quoted}] is better written in dot notation."
151
+ fix at { start: open, end: end_offset } if fixable as separator name trailing
152
+ }
153
+
154
+ in ts_member rule es-dot-notation:keyword {
155
+ @structure_dot_options
156
+ when !allowKeywords && structure_dot_keyword(property.text)
157
+ let dot property.space_before.start - 1
158
+ let fixable !(object is ts_identifier && object.text == "let" && optional == null) && { value overlapping(ts_comment, dot, property.offset); count } == 0
159
+ report at property.node
160
+ warn ".{property} is a syntax error."
161
+ fix at { start: dot, end: end_offset } if fixable as (if optional != null then "." else "") "[\"" property "\"]"
162
+ }
163
+
164
+ in ts_conditional let structure_ternary_jsx if parent is ts_conditional then parent.structure_ternary_jsx else parent is ts_jsx_expression
165
+ in ts_binary, ts_logical let structure_operator operator
166
+ in ts_conditional let structure_operator "?:"
167
+ in ts_binary, ts_logical let structure_operator_span { start: operator.offset, end: operator.end_offset }
168
+ in ts_conditional let structure_operator_span { start: (test.space_after).end, end: (test.space_after).end + 1 }
169
+ in ts_variable_declaration let structure_loop_declaration parent is (ts_for, ts_for_in, ts_for_of)
170
+ in ts_union, ts_intersection let structure_first_type_offset first(type.offset)
171
+ in ts_union, ts_intersection let structure_type_operators { value children.nodes; filter (item) item is ts_punctuator }
172
+ in ts_type_alias let structure_start offset
173
+ in ts_unary let structure_typeof operator == "typeof"
174
+ in ts_parenthesized let structure_typeof expression.structure_typeof.nonempty ?? false
175
+ in ts_parenthesized let structure_single_conditional expression is ts_conditional || expression is ts_parenthesized && expression.structure_single_conditional
176
+
177
+ @macro structure_mixed_operator_rule(name, span) {
178
+ in ts_binary, ts_logical rule name {
179
+ option groups string[][] = [["+", "-", "*", "/", "%", "**"], ["&", "|", "^", "~", "<<", ">>", ">>>"], ["==", "!=", "===", "!==", ">", ">=", "<", "<="], ["&&", "||"], ["in", "instanceof"]] "operator groups requiring parentheses"
180
+ option allowSamePrecedence boolean = true "allow mixtures with equal precedence"
181
+ when parent is (ts_binary, ts_logical, ts_conditional)
182
+ let child_operator structure_operator
183
+ let outer_operator parent.structure_operator
184
+ when child_operator != outer_operator
185
+ when !allowSamePrecedence || precedence != parent.precedence
186
+ when any(groups, operators => contains(operators, child_operator) && contains(operators, outer_operator))
187
+ let child_first is (ts_node.left, ts_node.test)
188
+ let left_operator if child_first then child_operator else outer_operator
189
+ let right_operator if child_first then outer_operator else child_operator
190
+ report at span
191
+ warn "Unexpected mix of '{left_operator}' and '{right_operator}'. Use parentheses to clarify the intended order of operations."
192
+ }
193
+ }
194
+
195
+ @structure_mixed_operator_rule(es-no-mixed-operators:child, structure_operator_span)
196
+ @structure_mixed_operator_rule(es-no-mixed-operators:parent, parent.structure_operator_span)
197
+
198
+ fn structure_declaration_issue(message, fix) = if message == null then null else { message: message, fix: fix }
199
+ fn structure_fix_edits(source, edits) = if count(edits) == 0 then null else {
200
+ value start: first(edits).start
201
+ value end: last(edits).end
202
+ value region: byte_slice(source, start, end)
203
+ value { start: start, end: end, text: expression_fix_edits(region, start, edits) }
204
+ }
205
+ fn structure_declaration_join(n, source) = {
206
+ value before: ts_token_neighbours(source, n.kind.node).before
207
+ value first: ts_token_record(token(n.kind.node, "first"))
208
+ value last: ts_token_record(token(n.kind.node, "last"))
209
+ value edits: [
210
+ { start: if before.text == ";" then before.start else before.end, end: before.end, text: "," },
211
+ { start: first.start, end: first.end, text: "" },
212
+ if first.start != last.start then { start: last.start, end: last.end, text: "" } else null,
213
+ ]
214
+ value edits
215
+ compact
216
+ ordered: sort_by (edit) edit.start
217
+ value structure_fix_edits(source, ordered)
218
+ }
219
+ fn structure_declaration_split_edit(declarator, source, kind, exported) = {
220
+ value comma: declarator.token_neighbours.after
221
+ value next: declarator.next.offset
222
+ value comments: overlapping(ts_comment, comma.end, next)
223
+ value after: first(comments).offset ?? next
224
+ value prefix: (if exported then "export " else "") kind
225
+ value separated: position(after).js_line > position(comma.end).js_line || count(comments) > 0
226
+ value {
227
+ start: comma.start,
228
+ end: if after != comma.end && separated then next else comma.end,
229
+ text: if after == comma.end then "; " prefix " "
230
+ else if separated then ";" byte_slice(source, comma.end, next) prefix " "
231
+ else "; " prefix,
232
+ }
233
+ }
234
+ fn structure_declaration_split(declarations, kind, exported, source) = {
235
+ value declarations
236
+ filter (declarator) declarator.token_neighbours.after.text == ","
237
+ edits: map (declarator) structure_declaration_split_edit(declarator, source, kind, exported)
238
+ value structure_fix_edits(source, edits)
239
+ }
240
+
241
+ fn structure_declaration_findings(info, settings, source) = {
242
+ value kind: info.kind
243
+ value common: if type_of(settings) == "str" then settings else if kind == "var" then settings.var else if kind == "let" then settings.let else if kind == "const" then settings.const else if kind == "using" then settings.using else settings.awaitUsing
244
+ value init_mode: settings.initialized.nonempty ?? common
245
+ value uninit_mode: settings.uninitialized.nonempty ?? common
246
+ value counts: info.counts
247
+ value total: counts.initialized + counts.uninitialized
248
+ value prior_counts: info.prior
249
+ value prior_init: prior_counts.initialized > 0 && (!settings.separateRequires || prior_counts.initialized > prior_counts.requires)
250
+ value prior_uninit: prior_counts.uninitialized > 0
251
+ value prior_required: settings.separateRequires && prior_counts.requires > 0
252
+ value both_always: init_mode == "always" && uninit_mode == "always"
253
+ value always_failed: both_always && (prior_init || prior_uninit) && counts.requires == 0 || uninit_mode == "always" && counts.uninitialized > 0 && prior_uninit || init_mode == "always" && counts.initialized > 0 && prior_init && counts.requires == 0 || prior_required && counts.requires > 0
254
+ value joining: always_failed && (both_always || init_mode == "always" && counts.initialized > 0
255
+ || uninit_mode == "always" && counts.uninitialized > 0 && !info.for_binding)
256
+ value previous_counts: if init_mode == "consecutive" || uninit_mode == "consecutive" || joining
257
+ then structure_declaration_previous(info.node) else null
258
+ value consecutive: previous_counts != null && !((counts.requires + previous_counts.requires > 0) && (total + previous_counts.initialized + previous_counts.uninitialized > counts.requires + previous_counts.requires))
259
+ value consecutive_message: if !consecutive then null else if init_mode == "consecutive" && uninit_mode == "consecutive" then "Combine this with the previous '" kind "' statement." else if init_mode == "consecutive" && counts.initialized > 0 && previous_counts.initialized > 0 then "Combine this with the previous '" kind "' statement with initialized variables." else if uninit_mode == "consecutive" && counts.uninitialized > 0 && previous_counts.uninitialized > 0 then "Combine this with the previous '" kind "' statement with uninitialized variables." else null
260
+ value split_message: if total < 2 || info.for_init then null else if init_mode == "never" && uninit_mode == "never" then "Split '" kind "' declarations into multiple statements." else if init_mode == "never" && counts.initialized > 0 then "Split initialized '" kind "' declarations into multiple statements." else if uninit_mode == "never" && counts.uninitialized > 0 then "Split uninitialized '" kind "' declarations into multiple statements." else null
261
+ value join_fix: if previous_counts != null && (consecutive_message != null || joining)
262
+ then structure_declaration_join(info.node, source) else null
263
+ value split_parent: if info.node.parent is ts_export then info.node.parent.parent.node else info.node.parent.node
264
+ value declaration_exported: info.node.parent is ts_export
265
+ value split_fix: if split_message != null && split_parent is (ts_program, ts_block, ts_case, ts_static_block) then structure_declaration_split(info.node.declaration.nodes, kind, declaration_exported, source) else null
266
+ value issues: compact([
267
+ if init_mode == "always" && settings.separateRequires && counts.requires > 0 && counts.requires < total then { message: "Split requires to be separated into a single block.", fix: null } else null,
268
+ structure_declaration_issue(consecutive_message, join_fix),
269
+ if always_failed && both_always then { message: "Combine this with the previous '" kind "' statement.", fix: join_fix } else null,
270
+ if always_failed && !both_always && init_mode == "always" && counts.initialized > 0 then { message: "Combine this with the previous '" kind "' statement with initialized variables.", fix: join_fix } else null,
271
+ if always_failed && !both_always && uninit_mode == "always" && counts.uninitialized > 0 && !info.for_binding then { message: "Combine this with the previous '" kind "' statement with uninitialized variables.", fix: join_fix } else null,
272
+ structure_declaration_issue(split_message, split_fix)
273
+ ])
274
+ value map(issues, issue => {node: info.node, message: issue.message, fix: issue.fix})
275
+ }
276
+
277
+ in structure_lexical_scope, ts_variable_declaration rule es-one-var {
278
+ option options ("always" | "never" | "consecutive" | { separateRequires?: boolean; var?: "always" | "never" | "consecutive"; let?: "always" | "never" | "consecutive"; const?: "always" | "never" | "consecutive"; using?: "always" | "never" | "consecutive"; awaitUsing?: "always" | "never" | "consecutive"; initialized?: "always" | "never" | "consecutive"; uninitialized?: "always" | "never" | "consecutive" })[] = [] "declaration grouping policy"
279
+ let settings { value options; first; nonempty } ?? "always"
280
+ let scoped if type_of(settings) == "str" then settings == "always"
281
+ else settings.separateRequires || contains([settings.var, settings.let, settings.const, settings.using, settings.awaitUsing, settings.initialized, settings.uninitialized], "always")
282
+ when if scoped then is structure_lexical_scope
283
+ else is ts_variable_declaration && (settings != "never" || count(node.declaration.nodes) > 1)
284
+ let entries if scoped then structure_declaration_entries
285
+ else [structure_declaration_entry(node, structure_declaration_counts(node), {initialized: 0, uninitialized: 0, requires: 0})]
286
+ each issue in flat_map(entries, info => structure_declaration_findings(info, settings, source)) {
287
+ report at issue.node
288
+ fix at issue.fix if issue.fix != null as issue.fix.text
289
+ warn "{issue.message}"
290
+ }
291
+ }
292
+
293
+ group structure_template ts_template, ts_template_type
294
+ group structure_literal ts_string, ts_regex
295
+ in ts_comment let structure_comment_span if parent is ts_jsx_expression && parent.expression == null && !re_test(parent.text, /[\n\r\u{2028}\u{2029}]/) then { start: parent.offset, end: parent.end_offset } else { start: offset, end: end_offset }
296
+ in ts_line let structure_line_content re_replace(text, /(?:\r\n|[\n\r\u{2028}\u{2029}])$/, "")
297
+ in ts_line let structure_line_end offset + byte_len(structure_line_content)
298
+ in ts_line let structure_line_comments { value overlapping(ts_comment); map (comment) comment.structure_comment_span }
299
+ in ts_line let structure_last_comment last(structure_line_comments)
300
+ in ts_line let structure_full_comment structure_last_comment != null && structure_last_comment.end >= structure_line_end
301
+ && (structure_last_comment.start <= offset || { value structure_line_content; byte_slice 0 (structure_last_comment.start - offset); trim } == "")
302
+ in ts_line let structure_line_template {
303
+ value overlapping(structure_template)
304
+ any (template) any(template.quasi.node, quasi => quasi.offset <= structure_line_end && quasi.end_offset >= offset)
305
+ }
306
+
307
+ fn expanded_line_length(parts, extra, width) = (if width == 0 then sum(parts) else {
308
+ value result: len(parts) - 1
309
+ value slice(parts, 0, result)
310
+ map (part) part - part % width + width
311
+ sum
312
+ } + last(parts)) - extra
313
+ fn structure_strip_comments(content, start, comments) = {
314
+ value comments
315
+ reversed: reverse
316
+ value initial_end: byte_len(content)
317
+ value reversed
318
+ end: fold initial_end (end, comment) if comment.start >= start && comment.start <= start + end && comment.end >= start + end
319
+ then {
320
+ value content
321
+ byte_slice 0 (comment.start - start)
322
+ trim_end
323
+ byte_len
324
+ } else end
325
+ value byte_slice(content, 0, end)
326
+ }
327
+
328
+ in ts_line rule es-max-len {
329
+ option options (number | { code?: number; comments?: number; tabWidth?: number; ignorePattern?: string; ignoreComments?: boolean; ignoreStrings?: boolean; ignoreUrls?: boolean; ignoreTemplateLiterals?: boolean; ignoreRegExpLiterals?: boolean; ignoreTrailingComments?: boolean })[] = [] "line limits, tab stops and exemptions"
330
+ let settings last(options)
331
+ let code_limit if { value options; first; type_of } == "num" then first(options) else settings.code.nonempty ?? 80
332
+ let second { value options; slice 1; first }
333
+ let tab_width if type_of(second) == "num" then second else settings.tabWidth.nonempty ?? 4
334
+ let comment_limit settings.comments.nonempty ?? 0
335
+ let strip_comments settings.ignoreTrailingComments || settings.ignoreComments
336
+ when !settings.ignoreComments || !structure_full_comment
337
+ when !settings.ignoreStrings || { value overlapping(ts_string); count } == 0
338
+ when !settings.ignoreTemplateLiterals || !structure_line_template
339
+ when !settings.ignoreRegExpLiterals || { value overlapping(ts_regex); count } == 0
340
+ let measured if strip_comments && !structure_full_comment then structure_strip_comments(structure_line_content, offset, structure_line_comments) else structure_line_content
341
+ when !settings.ignoreUrls || !re_test(measured, /[^:/?#]:\/\/[^?#]/)
342
+ when !settings.ignorePattern || !re_test(measured, settings.ignorePattern)
343
+ let parts { value measured; split "\t"; map (part) utf16_len(part) }
344
+ let length if !contains(measured, "\t") then len(measured) else { value extra: utf16_len(measured) - len(measured); value expanded_line_length(parts, extra, tab_width) }
345
+ let comment_applies comment_limit > 0 && structure_full_comment
346
+ let limit if comment_applies then comment_limit else code_limit
347
+ when length > limit
348
+ report at { start: offset, end: offset + byte_len(measured) }
349
+ warn if comment_applies then "This line has a comment length of {length}. Maximum allowed is {limit}." else "This line has a length of {length}. Maximum allowed is {limit}."
350
+ }
351
+
352
+ fn structure_is_directive(body) = re_test(body, /^(?:eslint|jscs|\/ <reference|\/ <amd-)/)
353
+ || re_test(body, /^(?:@ts-expect-error|@ts-ignore|@ts-nocheck|@ts-check|prettier-ignore)(?:[^\p{Alphabetic}\p{Number}_-]|$)/)
354
+ || re_test(body, /^(?:jshint|jslint|istanbul|global|globals|exported)\s/)
355
+ || re_test(body, /^(?:v8|c8)\s+ignore(?:[^\p{Alphabetic}\p{Number}_-]|$)/)
356
+ || re_test(body, /^node:coverage\s+(?:(?:disable|enable)(?:[^\p{Alphabetic}\p{Number}_-]|$)|ignore\s+next(?:[^\p{Alphabetic}\p{Number}_-]|$))/)
357
+ || re_test(body, /^webpack(?:ChunkName|FetchPriority|Mode|Exports|Include|Exclude|Prefetch|Preload|Ignore)\s*:/)
358
+
359
+ in ts_program layer text match structure_hashbang /\A#![^\n\r\u{2028}\u{2029}]*/
360
+ group structure_comment ts_comment, structure_hashbang
361
+ in structure_comment let structure_comment_body if is ts_block_comment then { value result: len(text) - 2; value slice(text, 2, result) } else slice(text, 2)
362
+ in structure_comment let structure_directive { value structure_comment_body; trim_start; structure_is_directive }
363
+ in ts_comment let structure_comment_beside { value js_line_start: position(offset).js_line_start; value byte_slice(source, js_line_start, offset); trim } != ""
364
+
365
+ in ts_line_comment rule es-line-comment-position {
366
+ option options ("above" | "beside" | { position?: "above" | "beside"; ignorePattern?: string; applyDefaultIgnorePatterns?: boolean })[] = [] "line comment position and exemptions"
367
+ let settings first(options)
368
+ let position if type_of(settings) == "str" then settings else settings.position.nonempty ?? "above"
369
+ let ignore_pattern settings.ignorePattern.nonempty ?? ""
370
+ let ignore_default settings.applyDefaultIgnorePatterns.nonempty ?? true
371
+ when !ignore_default || !structure_directive && !re_test(structure_comment_body, /^\s*falls?\s?through/)
372
+ when ignore_pattern == "" || !re_test(structure_comment_body, ignore_pattern)
373
+ when position == "above" && structure_comment_beside || position == "beside" && !structure_comment_beside
374
+ warn if position == "above" then "Expected comment to be above code." else "Expected comment to be beside code."
375
+ }
376
+
377
+ @macro structure_comment_style_options {
378
+ option mode "starred-block" | "bare-block" | "separate-lines" = "starred-block"
379
+ option checkJSDoc boolean = false "check JSDoc when requiring line comments"
380
+ option checkExclamation boolean = false "check exclamation comments when requiring line comments"
381
+ }
382
+ in ts_comment let structure_style_eligible !structure_directive && !structure_comment_beside
383
+ in ts_block_comment let structure_multiline structure_first_row.offset != structure_last_row.offset
384
+ in ts_block_comment let structure_block_first byte_slice(source, offset + 2, structure_first_row.structure_line_end)
385
+ in ts_block_comment let structure_block_last byte_slice(source, structure_last_row.offset, end_offset - 2)
386
+ in ts_block_comment let structure_block_middle {
387
+ value overlapping(ts_line)
388
+ filter (row) row.offset > structure_first_row.offset && row.offset < structure_last_row.offset
389
+ }
390
+ in ts_block_comment let structure_block_padded all(structure_block_middle, row => re_test(row.structure_line_content, /^\s/) && contains(row.structure_line_content, " "))
391
+ in ts_block_comment let structure_block_jsdoc starts_with(structure_block_first, "*") && { value structure_block_first; slice 1; trim } == "" && structure_block_padded && trim(structure_block_last) == ""
392
+ in ts_block_comment let structure_block_exclamation starts_with(structure_block_first, "!") && { value structure_block_first; slice 1; trim } == "" && structure_block_padded && trim(structure_block_last) == ""
393
+
394
+ fn structure_comment_indent(n, source) = byte_slice(source, n.structure_first_row.offset, n.offset)
395
+ fn structure_comment_split(body) = { value body; re_replace /\r\n|[\n\r\u{2028}\u{2029}]/ "\n"; split "\n" }
396
+ fn structure_comment_starred(lines) = { value lines; first; trim } == "" && { value lines; last; trim } == ""
397
+ && { value lines; slice 1 -1; all (line) re_test(line, /^\s*\*/) }
398
+ fn structure_comment_starred_lines(lines) = {
399
+ value lines
400
+ slice 1 -1
401
+ rows: map (line) re_replace(line, /^\s*$/, "")
402
+ value padded: all(rows, line => { value line; re_replace /^\s*\*/ ""; then (body) trim(body) == "" || starts_with(body, " ") })
403
+ value rows
404
+ map (line) if padded then re_replace(line, /^\s*\* ?/, "") else re_replace(line, /^\s*\*/, "")
405
+ }
406
+ fn structure_comment_bare_line(line) = {
407
+ value content: if trim(line) == "" then "" else line
408
+ value { prefix: re_find(content, /^\s*\*?\s*/), content: re_replace(content, /^\s*\*?\s*/, "") }
409
+ }
410
+ fn structure_comment_bare_lines(lines, indent) = {
411
+ value lines
412
+ rows: map (line) structure_comment_bare_line(line)
413
+ value width: len(indent) + 3
414
+ value rows
415
+ slice 1
416
+ filter (row) row.content != ""
417
+ map (row) width - len(row.prefix)
418
+ widest: max
419
+ value adjustment: max(widest, 0)
420
+ value rows
421
+ map (row) if len(row.prefix) > width then {
422
+ value begin: width - adjustment - len(row.prefix)
423
+ value slice(row.prefix, begin) row.content
424
+ } else row.content
425
+ }
426
+ fn structure_comment_block_lines(n, indent) = {
427
+ value lines: structure_comment_split(n.structure_comment_body)
428
+ value if structure_comment_starred(lines) then structure_comment_starred_lines(lines) else structure_comment_bare_lines(lines, indent)
429
+ }
430
+ fn structure_comment_group_lines(group) = {
431
+ value group
432
+ lines: map (comment) comment.structure_comment_body
433
+ value padded: all(lines, line => trim(line) == "" || starts_with(line, " "))
434
+ value lines
435
+ map (line) if padded then re_replace(line, /^ /, "") else line
436
+ }
437
+ fn structure_comment_format(lines, indent, mode) = {
438
+ value separator: "\n" indent (if mode == "bare-block" then " " else "")
439
+ value if mode == "starred-block" then {
440
+ value lines
441
+ map (line) indent " * " line
442
+ body: join "\n"
443
+ value "/*\n" body "\n" indent " */"
444
+ } else if mode == "bare-block" then "/* " join(lines, separator) " */"
445
+ else { value lines; map (line) "// " line; join separator }
446
+ }
447
+ in ts_block_comment let structure_style_reference {
448
+ value comment: node
449
+ value overlapping(ts_line)
450
+ filter (line) { value if line.offset == comment.structure_first_row.offset then comment.structure_block_first else if line.offset == comment.structure_last_row.offset then comment.structure_block_last else line.structure_line_content; trim } != ""
451
+ first
452
+ }
453
+ fn structure_comment_alignment(row, comment, source) = {
454
+ value reference: comment.structure_style_reference
455
+ value starred: re_test(row.structure_line_content, /^\s*\*/)
456
+ value leading: re_find(row.structure_line_content, /^\s*\*?/)
457
+ value original_prefix: re_find(reference.structure_line_content, /^\s*(?:\/?\*)?\s*/)
458
+ value padding: if contains(original_prefix, "*") then re_replace(original_prefix, /^\s*\/?\*/, "") else ""
459
+ value prefix_size: len(original_prefix)
460
+ value extra: slice(leading, prefix_size)
461
+ value missing: (if trim(row.structure_line_content) != "" && !re_test(reference.structure_line_content, /^\s*\/?\*\s/) then " " else "") extra padding
462
+ value prefix: structure_comment_indent(comment, source) " *"
463
+ value { start: row.offset, end: row.offset + byte_len(leading), text: prefix (if starred then "" else missing) }
464
+ }
465
+
466
+ in ts_block_comment rule es-multiline-comment-style:shape {
467
+ @structure_comment_style_options
468
+ when structure_style_eligible && structure_multiline
469
+ when if mode == "bare-block" then trim(structure_block_first) == "" && trim(structure_block_last) == ""
470
+ && all(structure_block_middle, row => { value row.structure_line_content; trim_start; starts_with "*" })
471
+ && !structure_block_jsdoc && !structure_block_exclamation
472
+ else mode == "separate-lines"
473
+ && { value source; byte_slice end_offset structure_last_row.structure_line_end; trim } == ""
474
+ && (checkJSDoc || !structure_block_jsdoc) && (checkExclamation || !structure_block_exclamation)
475
+ let style_indent structure_comment_indent(node, source)
476
+ let style_lines structure_comment_block_lines(node, style_indent)
477
+ let converted_lines if mode == "separate-lines" && (structure_block_jsdoc || structure_block_exclamation) then { value style_lines; slice 1 -1 } else style_lines
478
+ fix as structure_comment_format(converted_lines, style_indent, mode)
479
+ warn if mode == "bare-block" then "Expected a block comment without padding stars." else "Expected multiple line comments instead of a block comment."
480
+ }
481
+
482
+ in ts_block_comment rule es-multiline-comment-style:open {
483
+ @structure_comment_style_options
484
+ when mode == "starred-block" && structure_style_eligible && structure_multiline
485
+ when {
486
+ value if starts_with(structure_block_first, "*") || starts_with(structure_block_first, "!") then slice(structure_block_first, 1) else structure_block_first
487
+ trim
488
+ } != ""
489
+ let style_indent structure_comment_indent(node, source)
490
+ let opening_body if starts_with(structure_block_first, "*") || starts_with(structure_block_first, "!") then slice(structure_block_first, 1) else structure_block_first
491
+ let opening_space re_find(opening_body, /^\s*/)
492
+ let opening_end offset + (if opening_body != structure_block_first then 3 else 2)
493
+ fix at { start: opening_end, end: opening_end } as "\n" style_indent " *" (if opening_space == "" then " " else "")
494
+ report at offset
495
+ warn "Expected a linebreak after '/*'."
496
+ }
497
+
498
+ in ts_block_comment rule es-multiline-comment-style:close {
499
+ @structure_comment_style_options
500
+ when mode == "starred-block" && structure_style_eligible && structure_multiline && trim(structure_block_last) != ""
501
+ let style_indent structure_comment_indent(node, source)
502
+ fix at { start: end_offset - 2, end: end_offset } as "\n" style_indent " */"
503
+ report at { start: end_offset - 2, end: end_offset }
504
+ warn "Expected a linebreak before '*/'."
505
+ }
506
+
507
+ in ts_line rule es-multiline-comment-style:alignment {
508
+ @structure_comment_style_options
509
+ when mode == "starred-block"
510
+ let comment {
511
+ value overlapping(ts_block_comment)
512
+ filter (comment) comment.structure_style_eligible && comment.offset < offset
513
+ first
514
+ }
515
+ when comment != null
516
+ let prefix byte_slice(source, comment.structure_first_row.offset, comment.offset) " *"
517
+ when !starts_with(text, prefix)
518
+ let alignment_fix structure_comment_alignment(node, comment, source)
519
+ fix at alignment_fix as alignment_fix.text
520
+ report at offset
521
+ warn if { value text; trim_start; starts_with "*" } then "Expected this line to be aligned with the start of the comment." else "Expected a '*' at the start of this line."
522
+ }
523
+
524
+ fn structure_line_comments_join(source, previous, next) = previous != null && next != null
525
+ && previous is ts_line_comment && next is ts_line_comment
526
+ && previous.structure_style_eligible && next.structure_style_eligible && previous.end_offset <= next.offset
527
+ && position(next.offset).js_line == position(previous.end_offset).js_line + 1
528
+ && { value source; byte_slice previous.end_offset next.offset; trim } == ""
529
+ in ts_line_comment let structure_previous_comment last(structure_previous_row.structure_row_comments)
530
+ in ts_line_comment let structure_next_comment first(structure_next_row.structure_row_comments)
531
+ in ts_line_comment let structure_joins_previous structure_line_comments_join(source, structure_previous_comment, node)
532
+ in ts_line_comment let structure_joins_next structure_line_comments_join(source, node, structure_next_comment)
533
+ in ts_line_comment let structure_group_unsafe contains(structure_comment_body, "*/")
534
+ || {
535
+ value if structure_joins_next then structure_next_comment.node else null
536
+ walk (comment) if comment.structure_joins_next then comment.structure_next_comment.node else null
537
+ any (comment) contains(comment.structure_comment_body, "*/")
538
+ }
539
+ in ts_line_comment rule es-multiline-comment-style:consecutive {
540
+ @structure_comment_style_options
541
+ when mode != "separate-lines" && structure_style_eligible && !structure_joins_previous && structure_joins_next && !structure_group_unsafe
542
+ let style_group { value node; walk (comment) if comment.structure_joins_next then comment.structure_next_comment.node else null }
543
+ let style_lines structure_comment_group_lines(style_group)
544
+ let style_indent structure_comment_indent(node, source)
545
+ let style_end last(style_group).end_offset
546
+ fix at { start: offset, end: style_end } if mode != "starred-block" || !any(style_lines, line => starts_with(line, "/")) as structure_comment_format(style_lines, style_indent, mode)
547
+ warn "Expected a block comment instead of consecutive line comments."
548
+ }
549
+
550
+ in ts_question, ts_colon rule es-multiline-ternary {
551
+ option mode "always" | "always-multiline" | "never" = "always"
552
+ option ignoreJSX boolean = false "ignore conditionals in JSX expressions"
553
+ when parent is ts_conditional
554
+ when !ignoreJSX || !parent.structure_ternary_jsx
555
+ when mode != "always-multiline" || re_test(parent_text, /[\n\r\u{2028}\u{2029}]/)
556
+ let broken space_before.line_break || space_after.line_break
557
+ when mode == "never" && broken || mode != "never" && !broken
558
+ let operands if text == "?" then "test and consequent" else "consequent and alternate"
559
+ let ternary_edits {
560
+ value [
561
+ if mode != "never" || space_before.line_break then { start: space_before.start, end: offset, text: if mode == "never" then "" else "\n" } else null,
562
+ if mode == "never" && space_after.line_break then { start: end_offset, end: space_after.end, text: "" } else null,
563
+ ]
564
+ compact
565
+ }
566
+ let ternary_fix structure_fix_edits(source, ternary_edits)
567
+ fix at ternary_fix if expression_fix_clean(parent.node) as ternary_fix.text
568
+ report at if text == "?" then parent.test.node else parent.consequent.node
569
+ warn if mode == "never" then "Unexpected newline between {operands} of ternary expression." else "Expected newline between {operands} of ternary expression."
570
+ }
571
+
572
+ in ts_line match structure_tab_run /\t+/
573
+ in structure_tab_run rule es-no-tabs {
574
+ option allowIndentationTabs boolean = false "allow tabs at the start of a line"
575
+ when !allowIndentationTabs || !{ value parent.text; byte_slice 0 (offset - parent.offset); re_test /^[ \t]*$/ }
576
+ warn "Unexpected tab character."
577
+ }
578
+
579
+ in ts_line rule es-no-mixed-spaces-and-tabs {
580
+ option mode "default" | "smart-tabs" = "default"
581
+ let indentation re_find(structure_line_content, /^[ \t]+/)
582
+ let mixed if indentation == null then null
583
+ else if mode == "smart-tabs" then re_find(indentation, /^\t* +\t/)
584
+ else re_find(indentation, /^(?: +\t|\t+ )/)
585
+ when mixed != null
586
+ let boundary offset + (len(mixed) - 2)
587
+ when !{ value overlapping(ts_comment); any (comment) comment.offset < boundary && boundary < comment.end_offset }
588
+ when !{ value overlapping(structure_literal); any (literal) literal.offset < boundary && boundary < literal.end_offset }
589
+ when !{
590
+ value overlapping(structure_template)
591
+ any (template) any(template.quasi.node, quasi => quasi.offset < boundary && boundary < quasi.end_offset)
592
+ }
593
+ report at { start: boundary, end: boundary + 2 }
594
+ warn "Mixed spaces and tabs."
595
+ }
596
+
597
+ in ts_program rule es-unicode-bom {
598
+ option mode "always" | "never" = "never"
599
+ when mode == "always" && !bom || mode == "never" && bom
600
+ fix at { start: 0, end: if mode == "never" then 3 else 0 } as if mode == "always" then "\u{feff}" else ""
601
+ report at 0
602
+ warn if mode == "always" then "Expected Unicode BOM (Byte Order Mark)." else "Unexpected Unicode BOM (Byte Order Mark)."
603
+ }
604
+
605
+ in ts_regex rule es-wrap-regex {
606
+ when is (ts_member.object, ts_index.object)
607
+ fix as "(" text ")"
608
+ warn "Wrap the regexp literal in parens to disambiguate the slash."
609
+ }
610
+
611
+ in ts_declarator rule es-one-var-declaration-per-line {
612
+ option mode "initializations" | "always" = "initializations"
613
+ when previous_sibling != null && !re_test(previous_gap, /[\n\r\u{2028}\u{2029}]/)
614
+ when !parent.structure_loop_declaration
615
+ when mode == "always" || init != null || previous.init != null
616
+ fix at { start: offset, end: offset } as "\n"
617
+ warn "Expected variable declaration to be on a new line."
618
+ }
619
+
620
+ in ts_binary rule es-eqeqeq {
621
+ option options ("always" | "smart" | "allow-null" | { null: "always" | "never" | "ignore" })[] = [] "equality mode and null policy"
622
+ let mode { value options; first; nonempty } ?? "always"
623
+ let null_mode { value last(options).null; nonempty } ?? "always"
624
+ let left_literal left.literal_value
625
+ let right_literal right.literal_value
626
+ let null_check left_literal.type == "null" || right_literal.type == "null"
627
+ let same_literal_type left_literal != null && right_literal != null && left_literal.type == right_literal.type
628
+ let left_typeof left.structure_typeof.nonempty ?? false
629
+ let right_typeof right.structure_typeof.nonempty ?? false
630
+ let loose operator == "==" || operator == "!="
631
+ let strict_null (operator == "===" || operator == "!==") && null_check && mode == "always" && null_mode == "never"
632
+ when strict_null || loose && (!null_check || mode == "always" && null_mode == "always") && (mode != "smart" || !(same_literal_type || left_typeof || right_typeof))
633
+ let expected if strict_null then { value result: len(operator) - 1; value slice(operator, 0, result) } else operator "="
634
+ let fixable same_literal_type && left_literal.type != "regex" || loose && (left_typeof && right_literal.type == "string" || right_typeof && left_literal.type == "string")
635
+ let operator_span { start: operator.offset, end: operator.end_offset }
636
+ report at operator_span
637
+ warn "Expected '{expected}' and instead saw '{operator}'."
638
+ fix at operator_span if fixable as expected
639
+ }
640
+
641
+ fn operand_cached_indent(records, row, fallback) = { value records; filter (prior) prior.right_line == row; last }.expected ?? fallback
642
+ fn operand_assignment(token) = contains(["=", "+=", "-=", "*=", "/=", "%=", "**=", "<<=", ">>=", ">>>=", "|=", "^=", "&=", "&&=", "||=", "??="], token)
643
+ fn operand_add(info) =
644
+ contains(["break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", "else", "export", "extends", "finally", "for", "function", "if", "import", "in", "let", "new", "return", "super", "switch", "throw", "try", "var", "void", "while", "with"], info.first)
645
+ || info.type_keyword || contains([":", "[", "(", "<"], info.last) || operand_assignment(info.last)
646
+ || contains(["[", "(", "{{", "=>", ":"], info.before) || operand_assignment(info.before)
647
+
648
+ in ts_program rule es-indent-binary-ops {
649
+ option mode number | "tab" = 2 "indent width or tabs"
650
+ let width if mode == "tab" then 1 else mode
651
+ let unit if mode == "tab" then "\t" else " "
652
+ let measurements {
653
+ value binary_operand_indents
654
+ sort_by (info) info.right_line
655
+ fold ([]) (state, info) {
656
+ value closes_more: info.last == ")" && info.parens < 0
657
+ value chain: closes_more && contains([".", "?."], info.first)
658
+ value add: operand_add(info)
659
+ value subtract: closes_more && !contains(["]", ")", "}}"], info.first) && !chain
660
+ value left: if chain then operand_cached_indent(state, info.root_line, info.root) else operand_cached_indent(state, info.left_line, info.left)
661
+ value state [{
662
+ offset: info.offset,
663
+ end: info.offset + byte_len(info.right),
664
+ right_line: info.right_line,
665
+ actual: operand_cached_indent(state, info.right_line, info.right),
666
+ expected: if add && !subtract then left repeat(unit, width) else if subtract && !add then { value left; slice width } else left,
667
+ }]
668
+ }
669
+ }
670
+ each info in measurements {
671
+ when info.actual != info.expected
672
+ report at info.offset
673
+ fix at { start: info.offset, end: info.end } as info.expected
674
+ warn "Expected indentation of " len(info.expected) " " (if mode == "tab" then "tab" else "space") (if len(info.expected) == 1 then "" else "s")
675
+ }
676
+ }
677
+
678
+ fn structure_operator_gap(gap, keep) = if keep || trim(gap) != "" then gap else re_replace(gap, /\r\n|[\n\r\u{2028}\u{2029}]/, "")
679
+ fn structure_operator_fix(before, after, next, policy, operator, before_comment, after_comment) =
680
+ if before.line_break != after.line_break && policy != "none" && before.comments && after.comments then null
681
+ else {
682
+ value start: if policy == "after" && before_comment is ts_block_comment then before_comment.end_offset else before.start
683
+ value end: if policy == "before" && after_comment is ts_block_comment then after_comment.offset else after.end
684
+ value before_text: byte_slice(before.text, start - before.start)
685
+ value after_text: byte_slice(after.text, 0, end - after.start)
686
+ value swap: before.line_break != after.line_break && policy != "none"
687
+ value left: if swap then after_text else structure_operator_gap(before_text, policy == "before")
688
+ value right: if swap then before_text else structure_operator_gap(after_text, policy == "after")
689
+ value if left == before_text && right == after_text then null else {
690
+ start: start,
691
+ end: end,
692
+ text: left operator right (if right == "" && contains(["+", "-"], operator) && next.text == operator then " " else ""),
693
+ }
694
+ }
695
+
696
+ in ts_binary.operator, ts_logical.operator, ts_assignment.operator, ts_equals, ts_question, ts_colon,
697
+ ts_union.structure_type_operators, ts_intersection.structure_type_operators rule es-operator-linebreak {
698
+ option mode "before" | "after" | "none" | null = null "operator side of a line break"
699
+ option overrides { [key: string]: "before" | "after" | "none" | "ignore" } = {} "operator-specific placement"
700
+ when parent is (ts_binary, ts_logical, ts_assignment)
701
+ || parent is (ts_conditional, ts_conditional_type) && contains(["?", ":"], text)
702
+ || parent is (ts_declarator, ts_class_property, ts_import_equals, ts_type_alias, ts_type_parameter, ts_enum_member) && text == "="
703
+ || parent is (ts_union, ts_intersection) && contains(["|", "&"], text) && offset > parent.structure_first_type_offset
704
+ let placement { value overrides; get text; nonempty } ?? if mode == null && contains(["?", ":"], text) then "before" else mode.nonempty ?? "after"
705
+ let operator_previous token(node, "before")
706
+ let operator_next token(node, "after")
707
+ let operator_before_break operator_previous != null && position(operator_previous.end).js_line != position(offset).js_line
708
+ let operator_after_break operator_next != null && position(end_offset).js_line != position(operator_next.start).js_line
709
+ when operator_before_break || operator_after_break
710
+ let next_text if parent is ts_type_alias then byte_slice(parent_text, operator_next.start - parent.structure_start) else null
711
+ let policy if placement != "ignore" && parent is ts_type_alias && re_test(next_text, /^[|&]/) then "after" else placement
712
+ when policy != "ignore"
713
+ when operator_before_break && operator_after_break || policy == "before" && !operator_before_break || policy == "after" && !operator_after_break || policy == "none"
714
+ let space_before ts_token_space(source, node, true)
715
+ let space_after ts_token_space(source, node, false)
716
+ let operator_before_comment { value overlapping(ts_comment, space_before.start, offset); last }
717
+ let operator_after_comment { value overlapping(ts_comment, end_offset, space_after.end); first }
718
+ let operator_fix structure_operator_fix(space_before, space_after, operator_next, policy, text, operator_before_comment, operator_after_comment)
719
+ fix at operator_fix if operator_fix != null as operator_fix.text
720
+ warn if operator_before_break && operator_after_break then "Bad line breaking before and after '{text}'." else if policy == "before" then "'{text}' should be placed at the beginning of the line." else if policy == "after" then "'{text}' should be placed at the end of the line." else "There should be no line break before or after '{text}'."
721
+ }
722
+
723
+ group core_ts_list ts_array, ts_array_pattern, ts_object, ts_object_pattern, ts_function, ts_arrow,
724
+ ts_call, ts_new, ts_if, ts_import, ts_export, ts_enum, ts_function_type, ts_interface,
725
+ ts_type_literal, ts_tuple_type, ts_type_reference, ts_method, ts_class
726
+ group core_list core_ts_list
727
+ in ts_import, ts_export let structure_attribute_lists overlapping(ts_import_attributes, offset, end_offset)
728
+ in core_ts_list let core_list_layout
729
+ if is (ts_import, ts_export) then filter(list_layout, site => !contains(["ImportDeclaration", "ExportNamedDeclaration"], site.kind) || !any(structure_attribute_lists, attributes => site.offset >= attributes.open.offset && site.end <= attributes.close.end_offset))
730
+ else list_layout
731
+
732
+ group structure_declaration_parent ts_program, ts_block, ts_if, ts_for, ts_for_in, ts_for_of,
733
+ ts_while, ts_do_while, ts_labeled, ts_with, ts_export
734
+ group structure_suppress_statement_entry ts_do_while, ts_for, ts_for_in, ts_for_of, ts_if, ts_labeled, ts_while
735
+ @macro register_max_statements_counted(target, eligible) {
736
+ group max_statements_counted target
737
+ in target let max_statements_counted_here eligible
738
+ }
739
+ @register_max_statements_counted(ts_break, true)
740
+ @register_max_statements_counted(ts_class, parent is structure_declaration_parent)
741
+ @register_max_statements_counted(ts_continue, true)
742
+ @register_max_statements_counted(ts_debugger, true)
743
+ @register_max_statements_counted(ts_do_while, true)
744
+ @register_max_statements_counted(ts_expression_statement, true)
745
+ @register_max_statements_counted(ts_for_in, true)
746
+ @register_max_statements_counted(ts_for_of, true)
747
+ @register_max_statements_counted(ts_for, true)
748
+ @register_max_statements_counted(ts_function, parent is structure_declaration_parent)
749
+ @register_max_statements_counted(ts_if, true)
750
+ @register_max_statements_counted(ts_import, true)
751
+ @register_max_statements_counted(ts_labeled, true)
752
+ @register_max_statements_counted(ts_return, true)
753
+ @register_max_statements_counted(ts_switch, true)
754
+ @register_max_statements_counted(ts_throw, true)
755
+ @register_max_statements_counted(ts_try, true)
756
+ @register_max_statements_counted(ts_variable_declaration, true)
757
+ @register_max_statements_counted(ts_while, true)
758
+ @register_max_statements_counted(ts_with, true)
759
+ @register_max_statements_counted(ts_export, true)
760
+ in max_statements_counted let max_statements_entry_line if parent is structure_suppress_statement_entry && not ts_node.alternate || parent is ts_export && parent.operator != "*" then null else position(offset).js_line
761
+ in max_statements_counted let max_statements_exit_offset offset + {
762
+ value {
763
+ value text
764
+ trim_end
765
+ trim_end ";"
766
+ trim_end
767
+ nonempty
768
+ } ?? text
769
+ byte_len
770
+ }
771
+ in max_statements_counted let max_statements_exit_line position(max_statements_exit_offset).js_line
772
+
773
+ in ts_line rule es-max-statements-per-line {
774
+ option max int(1..) = 1 "maximum statements on one line"
775
+ let row position(offset).js_line
776
+ let statements { value overlapping(max_statements_counted); filter (statement) statement.max_statements_counted_here.nonempty ?? true }
777
+ let tally {
778
+ value statements
779
+ fold ({ count: 0, first_entry: null, first_exit: null, at_limit: null, after_limit: null }) (state, statement) if statement.max_statements_entry_line != row && statement.max_statements_exit_line != row then state else {
780
+ count: state.count + (if statement.max_statements_entry_line == row then 1 else 0),
781
+ first_entry: if statement.max_statements_entry_line == row then state.first_entry.nonempty ?? statement.offset else state.first_entry,
782
+ first_exit: if statement.max_statements_exit_line == row then { value state.first_exit.nonempty ?? statement.max_statements_exit_offset; min statement.max_statements_exit_offset } else state.first_exit,
783
+ at_limit: if statement.max_statements_entry_line == row && state.count + 1 == max then statement.offset else state.at_limit,
784
+ after_limit: if statement.max_statements_entry_line == row && state.count == max then statement.offset else state.after_limit,
785
+ }
786
+ }
787
+ let carried if tally.first_exit != null && (tally.first_entry == null || tally.first_exit <= tally.first_entry) then 1 else 0
788
+ when tally.count + carried > max
789
+ report at if carried == 1 then tally.at_limit else tally.after_limit
790
+ warn "This line has {tally.count + carried} statements. Maximum allowed is {max}."
791
+ }
792
+
793
+ fn structure_gap_parts(source, from, to, comments) = {
794
+ value comments
795
+ filter (comment) comment.offset < to && comment.end_offset > from
796
+ fold ({ cursor: from, code: false }) (state, comment) {
797
+ cursor: min(comment.end_offset, to),
798
+ code: state.code || {
799
+ value comment.offset
800
+ max: max from
801
+ value byte_slice(source, state.cursor, max)
802
+ trim
803
+ } != "",
804
+ }
805
+ }
806
+ fn structure_gap_has_code(source, to, parts) = parts.code || { value source; byte_slice parts.cursor to; trim } != ""
807
+
808
+ in ts_line let structure_row_comments overlapping(structure_comment)
809
+ in ts_line let structure_row_blocked trim(text) == "" || any(structure_row_comments, comment =>
810
+ position(comment.offset).js_line == position(offset).js_line || position(comment.end_offset).js_line == position(offset).js_line)
811
+ in structure_comment let structure_first_row { value overlapping(ts_line, offset, offset + 1); first }
812
+ in structure_comment let structure_last_row { value overlapping(ts_line, end_offset - 1, end_offset); first }
813
+ in structure_comment let structure_previous_row if structure_first_row.offset > 0 then { value overlapping(ts_line, structure_first_row.offset - 1, structure_first_row.offset); first } else null
814
+ in structure_comment let structure_next_row { value overlapping(ts_line, structure_last_row.end_offset, structure_last_row.end_offset + 1); first }
815
+ in structure_comment let structure_inline {
816
+ value source
817
+ parts: structure_gap_parts structure_first_row.offset offset structure_first_row.structure_row_comments
818
+ value structure_gap_has_code(source, offset, parts)
819
+ }
820
+ || {
821
+ value source
822
+ parts_2: structure_gap_parts end_offset structure_last_row.end_offset structure_last_row.structure_row_comments
823
+ value structure_gap_has_code(source, structure_last_row.end_offset, parts_2)
824
+ }
825
+
826
+ in ts_class let structure_boundary_targets ["Block", "Class"]
827
+ in ts_block let structure_boundary_targets if parent is ts_namespace then ["Module"] else ["Block"]
828
+ in ts_static_block, ts_switch, ts_case let structure_boundary_targets ["Block"]
829
+ in ts_object, ts_object_pattern let structure_boundary_targets ["Object"]
830
+ in ts_array, ts_array_pattern let structure_boundary_targets ["Array"]
831
+ in ts_interface let structure_boundary_targets ["Interface"]
832
+ in ts_type_literal let structure_boundary_targets ["Type"]
833
+ in ts_enum let structure_boundary_targets ["Enum"]
834
+ in ts_class, ts_interface, ts_enum, ts_static_block, ts_switch let structure_boundary_start opener.ts_open_brace.offset
835
+ in ts_block, ts_case, ts_object, ts_object_pattern, ts_array, ts_array_pattern, ts_type_literal let structure_boundary_start offset
836
+
837
+ @macro structure_comment_gap(name, before) {
838
+ in structure_comment rule name {
839
+ option beforeBlockComment boolean = true "blank line before block comments"
840
+ option afterBlockComment boolean = false "blank line after block comments"
841
+ option beforeLineComment boolean = false "blank line before line comments"
842
+ option afterLineComment boolean = false "blank line after line comments"
843
+ option afterHashbangComment boolean = false "blank line after hashbangs"
844
+ option applyDefaultIgnorePatterns boolean = true "ignore directive comments"
845
+ option ignorePattern string = "" "ignore matching comment bodies"
846
+ option allowBlockStart boolean = false "allow comments at block starts"
847
+ option allowBlockEnd boolean = false "allow comments at block ends"
848
+ option allowClassStart boolean | null = null "override class start exceptions"
849
+ option allowClassEnd boolean | null = null "override class end exceptions"
850
+ option allowObjectStart boolean = false "allow comments at object starts"
851
+ option allowObjectEnd boolean = false "allow comments at object ends"
852
+ option allowArrayStart boolean = false "allow comments at array starts"
853
+ option allowArrayEnd boolean = false "allow comments at array ends"
854
+ option allowInterfaceStart boolean = false "allow comments at interface starts"
855
+ option allowInterfaceEnd boolean = false "allow comments at interface ends"
856
+ option allowTypeStart boolean = false "allow comments at type starts"
857
+ option allowTypeEnd boolean = false "allow comments at type ends"
858
+ option allowEnumStart boolean = false "allow comments at enum starts"
859
+ option allowEnumEnd boolean = false "allow comments at enum ends"
860
+ option allowModuleStart boolean = false "allow comments at module starts"
861
+ option allowModuleEnd boolean = false "allow comments at module ends"
862
+ let allowedStart {
863
+ value [if allowClassStart == true then "Class" else null, if allowObjectStart then "Object" else null, if allowArrayStart then "Array" else null, if allowInterfaceStart then "Interface" else null, if allowTypeStart then "Type" else null, if allowEnumStart then "Enum" else null, if allowModuleStart then "Module" else null]
864
+ compact
865
+ }
866
+ let allowedEnd {
867
+ value [if allowClassEnd == true then "Class" else null, if allowObjectEnd then "Object" else null, if allowArrayEnd then "Array" else null, if allowInterfaceEnd then "Interface" else null, if allowTypeEnd then "Type" else null, if allowEnumEnd then "Enum" else null, if allowModuleEnd then "Module" else null]
868
+ compact
869
+ }
870
+ when if before then (if is ts_block_comment then beforeBlockComment else beforeLineComment)
871
+ else (if is structure_hashbang then afterHashbangComment else if is ts_block_comment then afterBlockComment else afterLineComment)
872
+ when !(applyDefaultIgnorePatterns && structure_directive) && (ignorePattern == "" || !re_test(structure_comment_body, ignorePattern))
873
+ when !structure_inline
874
+ let row if before then structure_first_row else structure_last_row
875
+ let adjacent if before then structure_previous_row else structure_next_row
876
+ when adjacent != null && !adjacent.structure_row_blocked
877
+ when !any(row.structure_row_comments, comment => if before
878
+ then comment.offset < offset && position(comment.end_offset).js_line == position(offset).js_line
879
+ else comment.offset >= end_offset && position(comment.offset).js_line == position(end_offset).js_line)
880
+ let boundary parent.structure_boundary_start != null && parent.structure_boundary_start <= offset
881
+ && (if before then position(parent.structure_boundary_start).js_line + 1 == position(offset).js_line
882
+ else position(parent.end_offset).js_line == position(end_offset).js_line + 1)
883
+ let targets if boundary then parent.structure_boundary_targets.nonempty ?? [] else []
884
+ when !any(targets, target => { value if before then allowedStart else allowedEnd; contains target })
885
+ when !(contains(targets, "Block") && (if before then allowBlockStart else allowBlockEnd) && !(contains(targets, "Class") && (if before then allowClassStart else allowClassEnd) == false))
886
+ warn if before then "Expected line before comment." else "Expected line after comment."
887
+ let comment_fix_site if before then row.offset else end_offset
888
+ fix at { start: comment_fix_site, end: comment_fix_site } as "\n"
889
+ }
890
+ }
891
+ @structure_comment_gap(es-lines-around-comment:before, true)
892
+ @structure_comment_gap(es-lines-around-comment:after, false)
893
+
894
+ in ts_parenthesized let structure_unwrapped_end expression.structure_unwrapped_end.nonempty ?? expression.end_offset
895
+ in ts_parenthesized let structure_unwrapped_text expression.structure_unwrapped_text.nonempty ?? expression.text
896
+ in ts_parenthesized let structure_chain_info expression.structure_chain_info
897
+ in ts_parenthesized let structure_call_depth expression.structure_call_depth
898
+ in ts_call, ts_new let structure_call_depth (callee.structure_chain_info).depth.nonempty ?? 1
899
+
900
+ @macro structure_chain_owner(kind, property_start, property_text, is_computed) {
901
+ in kind let structure_chain_info {
902
+ depth: 1 + (object.structure_call_depth.nonempty ?? 0),
903
+ offset: (object.space_after).end,
904
+ end: end_offset,
905
+ gap: {
906
+ value default: object.structure_unwrapped_end.nonempty ?? object.end_offset
907
+ value byte_slice(source, default, property_start)
908
+ },
909
+ property: property_text,
910
+ computed: is_computed,
911
+ optional: optional != null,
912
+ }
913
+ }
914
+ @structure_chain_owner(ts_member, property.offset, property.structure_unwrapped_text.nonempty ?? property.text, false)
915
+ @structure_chain_owner(ts_index, index.offset, index.structure_unwrapped_text.nonempty ?? index.text, true)
916
+
917
+ in ts_call rule es-newline-per-chained-call {
918
+ option ignoreChainWithDepth number = 2 "allow chains up to this depth"
919
+ let info callee.structure_chain_info
920
+ when info != null && info.depth > ignoreChainWithDepth && !re_test(info.gap, /[\n\r\u{2028}\u{2029}]/)
921
+ fix at { start: info.offset, end: info.offset } as "\n"
922
+ report at { start: info.offset, end: info.end }
923
+ let first_line { value info.property; re_find /^[^\n\r\u{2028}\u{2029}]*/; nonempty } ?? ""
924
+ let prefix if info.optional then (if info.computed then "?.[" else "?.") else (if info.computed then "[" else ".")
925
+ let suffix if info.computed && len(first_line) == len(info.property) then "]" else ""
926
+ let callee_name prefix first_line suffix
927
+ warn "Expected line break before `{callee_name}`."
928
+ }
929
+
930
+ in ts_arrow.body rule es-implicit-arrow-linebreak {
931
+ option mode "beside" | "below" = "beside"
932
+ when not ts_block
933
+ let broken space_before.line_break
934
+ when mode == "beside" && broken || mode == "below" && !broken
935
+ fix at { start: if mode == "below" then offset else space_before.start, end: offset } if mode == "below" || !space_before.comments as if mode == "below" then "\n" else " "
936
+ warn if mode == "beside" then "Expected no linebreak before this expression." else "Expected a linebreak before this expression."
937
+ }
938
+
939
+ in ts_arrow rule es-no-confusing-arrow {
940
+ option allowParens boolean = true "parentheses disambiguate a conditional body"
941
+ option onlyOneSimpleParam boolean = false "check only single identifier parameters"
942
+ when body is ts_conditional || !allowParens && body is ts_parenthesized && body.structure_single_conditional
943
+ when !onlyOneSimpleParam || count(parameter) == 1 && { value parameter; first; re_test /^[A-Za-z_$][\w$]*$/ }
944
+ fix at body.node if allowParens as "(" body.text ")"
945
+ warn "Arrow function used ambiguously with a conditional expression."
946
+ }
947
+
948
+ in ts_if.consequent, ts_if.alternate, ts_while.body, ts_do_while.body, ts_for.body, ts_for_in.body, ts_for_of.body rule es-nonblock-statement-body-position {
949
+ option mode "beside" | "below" | "any" = "beside"
950
+ option overrides { if?: "beside" | "below" | "any"; else?: "beside" | "below" | "any"; while?: "beside" | "below" | "any"; do?: "beside" | "below" | "any"; for?: "beside" | "below" | "any" } = {} "position by statement"
951
+ let kind if parent is ts_if then (if is ts_if.alternate then "else" else "if") else if parent is ts_while then "while" else if parent is ts_do_while then "do" else "for"
952
+ let style {
953
+ value (if kind == "if" then overrides.if else if kind == "else" then overrides.else else if kind == "while" then overrides.while else if kind == "do" then overrides.do else overrides.for)
954
+ nonempty
955
+ } ?? mode
956
+ let broken space_before.line_break
957
+ when not ts_block && !(kind == "else" && is ts_if)
958
+ when style != "any" && (style == "beside" && broken || style == "below" && !broken)
959
+ fix at { start: if style == "below" then offset else space_before.start, end: offset } if style == "below" || !space_before.comments as if style == "below" then "\n" else " "
960
+ warn if style == "beside" then "Expected no linebreak before this statement." else "Expected a linebreak before this statement."
961
+ }
962
+
963
+ }