@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,755 @@
1
+ # .ts/format: syntax-owned rules and textual edges.
2
+ # Native layout policy also lives in .ts/layout.
3
+ # Rule names are `es-` and the eslint rule name without its plugin prefix;
4
+ # `family:variant` rules are parts of one eslint rule. All are off until
5
+ # turned on: `rule es-semi:always`, `rule es-quotes:double { set style "double" }`.
6
+
7
+ import .ts/import-attributes
8
+ import .ts/expression-fixes
9
+
10
+ define {
11
+
12
+ # ---- blank lines ---------------------------------------------------------
13
+
14
+ # No blank lines between a header and its single-statement body.
15
+ in ts_if.consequent, ts_while.body, ts_for.body, ts_for_in.body, ts_for_of.body rule es-no-blank-lines-in-optional-blocks warn "Avoid blank lines between a conditional or loop statement and its single non-block statement." {
16
+ when not ts_block && !space_before.comments && space_before.blank_lines > 0
17
+ let optional_header { value space_before.text; re_find /^[ \t]*/; nonempty } ?? ""
18
+ let optional_indent { value space_before.text; re_find /[ \t]*\z/; nonempty } ?? ""
19
+ report change
20
+ fix space_before as optional_header @newline optional_indent
21
+ }
22
+
23
+ # `declare global { var x }` is the only way to declare a global.
24
+ in ts_namespace let format_global_namespace kind == "global"
25
+ in ts_node let format_global_scope (format_global_namespace.nonempty ?? false) || (parent.format_global_scope.nonempty ?? false)
26
+ in ts_program let format_script_global ts_source_type(node) == "script" && source_environment.root_function != true
27
+ # Dynamic names prevent a binding-preservation proof.
28
+ in ts_program, ts_function_like, ts_static_block let format_lexical_dynamic
29
+ { value descendants(ts_with); count } > 0
30
+ || {
31
+ value descendants(ts_call)
32
+ any (call) call.callee.structure_unwrapped is ts_identifier && call.callee.structure_unwrapped.identifier_name == "eval"
33
+ }
34
+ fn format_lexical_container(n) = {
35
+ value n.parent.node
36
+ walk (current) if current is (ts_program, ts_block, ts_for, ts_for_in, ts_for_of) then null else current.parent.node
37
+ last
38
+ }
39
+ fn format_lexical_loop(n) = {
40
+ value n.parent.node
41
+ walk (current) if current is (ts_program, ts_function_like, ts_static_block) then null else current.parent.node
42
+ any (current) current is (ts_for, ts_for_in, ts_for_of, ts_while, ts_do_while)
43
+ }
44
+ fn format_lexical_catch(binding) = {
45
+ value binding.parent.node
46
+ walk (current) if current is (ts_program, ts_function_like, ts_static_block) then null else current.parent.node
47
+ any (current) current is ts_try && current.parameter.nonempty
48
+ && binding.offset >= current.handler.offset && binding.end_offset <= current.handler.end_offset
49
+ && {
50
+ value overlapping(ts_identifier, current.parameter.offset, current.parameter.end_offset)
51
+ any (identifier) identifier.identifier_name == binding.identifier_name
52
+ }
53
+ }
54
+ fn format_lexical_declarator(n) = {
55
+ value n.parent.node
56
+ walk (current) if current is ts_declarator then null else current.parent.node
57
+ last
58
+ }
59
+ fn format_lexical_called_after(reference, binding, declarator, container) = {
60
+ value function: {
61
+ value reference
62
+ walk (current) if current is ts_function_like then null else current.parent.node
63
+ last
64
+ }
65
+ value calls: filter(function.name.references, call => !scoping_within(call, function))
66
+ value function is ts_function && ts_declared_statement(function)
67
+ && count(calls) > 0 && all(calls, call => call.scoping_outer_parens is ts_call.callee
68
+ && call.scoping_execution_scope.id == binding.scoping_binding_execution_scope.id
69
+ && call.offset >= declarator.end_offset && scoping_within(call, container))
70
+ }
71
+ fn format_lexical_binding(binding, declarator, container) = {
72
+ value initializer: declarator.init.structure_unwrapped
73
+ value recursive: initializer is (ts_function, ts_arrow) && !format_lexical_loop(declarator)
74
+ value !contains(["let", "arguments"], binding.identifier_name)
75
+ && count(binding.declarations) == 1
76
+ && binding.scope.format_script_global != true
77
+ && !binding.scope.format_lexical_dynamic
78
+ && !format_lexical_catch(binding)
79
+ && all(binding.references, reference => reference.scoping_definition_use != null
80
+ && !reference.scoping_definition_use.before
81
+ && (!reference.scoping_definition_use.separate
82
+ || recursive && reference.offset >= initializer.offset && reference.end_offset <= initializer.end_offset
83
+ || format_lexical_called_after(reference, binding, declarator, container))
84
+ && reference.offset >= container.offset && reference.end_offset <= container.end_offset)
85
+ }
86
+ in ts_variable_declaration let format_lexical_bindings {
87
+ value container: format_lexical_container(node)
88
+ value count(declaration) > 0 && all(declaration.nodes, declarator => {
89
+ value bindings: {
90
+ value overlapping(ts_identifier, declarator.name.offset, declarator.name.end_offset)
91
+ filter (binding) binding.declaration.node == binding && binding.declaration_kind == "var"
92
+ && format_lexical_declarator(binding) == declarator
93
+ }
94
+ value count(bindings) > 0 && all(bindings, binding => format_lexical_binding(binding, declarator, container))
95
+ })
96
+ }
97
+ in ts_variable_declaration let format_lexical_initialized all(declaration.nodes, declarator => count(declarator.init.nodes) > 0)
98
+ in ts_variable_declaration.kind rule es-no-var warn "Unexpected var, use let or const instead." {
99
+ when text == "var"
100
+ when ts_namespace.format_global_scope != true
101
+ let owner parent.node
102
+ let body if owner.parent is ts_export then owner.parent.parent.node else owner.parent.node
103
+ let fixable owner.ecma_version >= 2015
104
+ && (body is (ts_program, ts_block) || owner is (ts_for.init, ts_for_in.left, ts_for_of.left))
105
+ && (!format_lexical_loop(owner) || owner is (ts_for_in.left, ts_for_of.left) || owner.format_lexical_initialized)
106
+ && owner.format_lexical_bindings
107
+ fix if fixable as "let"
108
+ }
109
+
110
+ # ---- spacing ---------------------------------------------------------------
111
+
112
+ in ts_open_paren rule es-space-in-parens:after {
113
+ option mode "always" | "never" = "never" "spacing inside parentheses"
114
+ option exceptions ("{}" | "[]" | "()" | "empty")[] = [] "invert spacing for nested delimiters"
115
+ let paren_edge token(node, "after")
116
+ when mode != "never" || count(exceptions) > 0 || paren_edge.start != end_offset
117
+ let paren_gap if paren_edge == null then null else byte_slice(source, end_offset, paren_edge.start)
118
+ let paren_space { value paren_gap; re_find /^\s*/; nonempty } ?? ""
119
+ when count(exceptions) > 0 || (mode == "always") == (paren_space == "")
120
+ let paren_next when {
121
+ trim(paren_gap) != "" then ""
122
+ next_token is ts_open_brace then "{{}}"
123
+ next_token is ts_open_bracket then "[]"
124
+ next_token is ts_open_paren then "()"
125
+ next_token is ts_close_paren then "empty"
126
+ else then ""
127
+ }
128
+ let paren_wants if count(exceptions) == 0 then mode == "always" else (mode == "always") != contains(exceptions, paren_next)
129
+ when if paren_space != "" then !re_test(paren_space, /[\n\r\u{2028}\u{2029}]/) && !{ value paren_gap; trim_start; starts_with "//" } && !paren_wants else paren_wants && (paren_next != "empty" || contains(exceptions, "empty"))
130
+ report at if paren_space != "" then { start: end_offset, end: end_offset + byte_len(paren_space) } else node
131
+ fix space_after as if paren_wants then " " else ""
132
+ warn if paren_space != "" then "There should be no space after this paren." else "There must be a space after this paren."
133
+ }
134
+ in ts_close_paren rule es-space-in-parens:before {
135
+ option mode "always" | "never" = "never" "spacing inside parentheses"
136
+ option exceptions ("{}" | "[]" | "()" | "empty")[] = [] "invert spacing for nested delimiters"
137
+ let paren_edge token(node, "before")
138
+ when mode != "never" || count(exceptions) > 0 || paren_edge.end != offset
139
+ let paren_gap if paren_edge == null then null else byte_slice(source, paren_edge.end, offset)
140
+ let paren_space { value paren_gap; re_find /\s*$/; nonempty } ?? ""
141
+ when count(exceptions) > 0 || (mode == "always") == (paren_space == "")
142
+ let paren_previous when {
143
+ trim(paren_gap) != "" then ""
144
+ previous_token is ts_close_brace then "{{}}"
145
+ previous_token is ts_close_bracket then "[]"
146
+ previous_token is ts_close_paren then "()"
147
+ previous_token is ts_open_paren then "empty"
148
+ else then ""
149
+ }
150
+ let paren_wants if count(exceptions) == 0 then mode == "always" else (mode == "always") != contains(exceptions, paren_previous)
151
+ when if paren_space != "" then !re_test(paren_space, /[\n\r\u{2028}\u{2029}]/) && !paren_wants else paren_wants && (paren_previous != "empty" || contains(exceptions, "empty"))
152
+ report at if paren_space != "" then { start: offset - byte_len(paren_space), end: offset } else node
153
+ fix space_before as if paren_wants then " " else ""
154
+ warn if paren_space != "" then "There should be no space before this paren." else "There must be a space before this paren."
155
+ }
156
+
157
+ fn format_gap_text(gap, spaced, comments) = (if spaced then " " else "") {
158
+ value comments
159
+ filter (comment) comment.offset >= gap.start && comment.end_offset <= gap.end
160
+ map (comment) comment.text
161
+ join ""
162
+ }
163
+ in ts_array, ts_array_pattern rule es-array-bracket-spacing {
164
+ option style "always" | "never" = "never" "spacing inside array brackets"
165
+ option singleValue boolean | null = null "override spacing around a single element"
166
+ option objectsInArrays boolean | null = null "override spacing beside object elements"
167
+ option arraysInArrays boolean | null = null "override spacing beside nested arrays"
168
+ let array_spaced style == "always"
169
+ when !(array_spaced && count(element) == 0)
170
+ let array_open opener.ts_open_bracket.node
171
+ let array_close closer.ts_close_bracket.node
172
+ let array_single singleValue != null && singleValue != array_spaced && count(element) == 1
173
+ let array_objects objectsInArrays != null && objectsInArrays != array_spaced
174
+ let array_arrays arraysInArrays != null && arraysInArrays != array_spaced
175
+ let array_first first(element.node)
176
+ let array_last last(element.node)
177
+ let array_open_wants array_spaced != (array_single || array_objects && array_first is (ts_object, ts_object_pattern) || array_arrays && array_first is (ts_array, ts_array_pattern))
178
+ let array_close_wants array_spaced != (array_single || array_objects && array_last is (ts_object, ts_object_pattern) || array_arrays && array_last is (ts_array, ts_array_pattern))
179
+ let array_edges [
180
+ { open: true, gap: array_open.space_after, wanted: array_open_wants, position: if array_open_wants then array_open.offset else array_open.end_offset },
181
+ { open: false, gap: array_close.space_before, wanted: array_close_wants, position: if array_close_wants then array_close.offset else array_close.space_before.start }
182
+ ]
183
+ let array_bad filter(array_edges, edge => !edge.gap.line_break && edge.gap.spaced != edge.wanted && (edge.open || edge.gap.start != array_open.end_offset))
184
+ let array_comments overlapping(ts_comment)
185
+ let array_fixed {
186
+ value array_bad
187
+ edits: map (edge) { start: edge.gap.start, end: edge.gap.end, text: format_gap_text(edge.gap, edge.wanted, array_comments) }
188
+ value expression_fix_edits(text, offset, edits)
189
+ }
190
+ each array_edge in array_bad {
191
+ report at array_edge.position
192
+ fix as array_fixed
193
+ warn if array_edge.wanted then (if array_edge.open then "A space is required after '['." else "A space is required before ']'.") else (if array_edge.open then "There should be no space after '['." else "There should be no space before ']'.")
194
+ }
195
+ }
196
+ in ts_dot, ts_punctuator, ts_member.optional rule es-dot-location {
197
+ @ts_token_context
198
+ option style "object" | "property" = "property" "line for a member-access dot"
199
+ when text == "." || text == "?."
200
+ when parent is (ts_member, ts_qualified_name, ts_import_type, ts_meta_property, ts_jsx_name)
201
+ when if style == "object" then position(offset).js_line != position(token(node, "before").end).js_line
202
+ else position(end_offset).js_line != position(token(node, "after").start).js_line
203
+ let dot_number_space text == "." && re_test(token_neighbours.before.text, /^(?:0|0[0-7]*[89]\d*|[1-9](?:_?\d)*)$/)
204
+ fix at {
205
+ start: if style == "object" then space_before.start else offset,
206
+ end: if style == "object" then end_offset else space_after.end,
207
+ } as if style == "object" then (if dot_number_space then " " else "") text space_before.text else space_after.text text
208
+ warn if style == "object" then "Expected dot to be on same line as object." else "Expected dot to be on same line as property."
209
+ }
210
+
211
+ @macro format_template_curly {
212
+ @ts_token_context
213
+ option mode "always" | "never" = "never" "spacing inside template substitutions"
214
+ let template_before { value space_before.text; re_find /\s*$/; nonempty } ?? ""
215
+ let template_after { value space_after.text; re_find /^\s*/; nonempty } ?? ""
216
+ }
217
+ in ts_template.quasi rule es-template-curly-spacing:after warn "Unexpected space(s) after '${{'." {
218
+ @format_template_curly
219
+ when mode == "never" && ends_with(text, "${{") && template_after != "" && !re_test(template_after, /[\n\r\u{2028}\u{2029}]/)
220
+ report at { start: end_offset, end: end_offset + byte_len(template_after) }
221
+ fix space_after as ""
222
+ }
223
+ in ts_template.quasi rule es-template-curly-spacing:before warn "Unexpected space(s) before '}}'." {
224
+ @format_template_curly
225
+ when mode == "never" && starts_with(text, "}}") && template_before != "" && !re_test(template_before, /[\n\r\u{2028}\u{2029}]/)
226
+ report at { start: offset - byte_len(template_before), end: offset }
227
+ fix space_before as ""
228
+ }
229
+ in ts_template.quasi rule es-template-curly-spacing:missing-after warn "Expected space(s) after '${{'." {
230
+ @format_template_curly
231
+ when mode == "always" && ends_with(text, "${{") && template_after == ""
232
+ report at { start: end_offset - 2, end: end_offset }
233
+ fix space_after as " "
234
+ }
235
+ in ts_template.quasi rule es-template-curly-spacing:missing-before warn "Expected space(s) before '}}'." {
236
+ @format_template_curly
237
+ when mode == "always" && starts_with(text, "}}") && template_before == ""
238
+ report at { start: offset, end: offset + 1 }
239
+ fix space_before as " "
240
+ }
241
+
242
+ @macro format_template_tag {
243
+ option style "always" | "never" = "never" "spacing after a template tag"
244
+ let tag_gap ts_token_space(source, quasi.node, true)
245
+ let tag_comments { value overlapping(ts_comment); filter (comment) comment.offset >= tag_gap.start && comment.end_offset <= tag_gap.end }
246
+ let tag_line_comment any(tag_comments, comment => comment is ts_line_comment)
247
+ }
248
+ in ts_tagged_template rule es-template-tag-spacing warn "Unexpected space between template tag and template literal." {
249
+ @format_template_tag
250
+ when style == "never" && tag_gap.spaced && !tag_line_comment
251
+ report at { start: tag_gap.start, end: tag_gap.end }
252
+ fix as {
253
+ value edits: [{ start: tag_gap.start, end: tag_gap.end, text: format_gap_text(tag_gap, false, tag_comments) }]
254
+ value expression_fix_edits(text, offset, edits)
255
+ }
256
+ }
257
+ in ts_tagged_template rule es-template-tag-spacing:comment warn "Unexpected space between template tag and template literal." {
258
+ @format_template_tag
259
+ when style == "never" && tag_gap.spaced && tag_line_comment
260
+ report at { start: tag_gap.start, end: tag_gap.end }
261
+ }
262
+ in ts_tagged_template rule es-template-tag-spacing:missing warn "Missing space between template tag and template literal." {
263
+ @format_template_tag
264
+ when style == "always" && !tag_gap.spaced
265
+ report at { start: offset, end: quasi.offset }
266
+ fix as {
267
+ value edits: [{ start: tag_gap.start, end: tag_gap.end, text: format_gap_text(tag_gap, true, tag_comments) }]
268
+ value expression_fix_edits(text, offset, edits)
269
+ }
270
+ }
271
+
272
+ # The nearest syntax or comment token owns the report position.
273
+ in ts_arrow, ts_function_type let format_arrow_comments overlapping(ts_comment)
274
+ @macro format_arrow_spacing {
275
+ @ts_token_context
276
+ option before boolean = true "require space before the arrow"
277
+ option after boolean = true "require space after the arrow"
278
+ when parent is ts_arrow || parent is ts_function_type && text == "=>"
279
+ let arrow_before { value space_before.text; re_find /\s*$/; nonempty } ?? ""
280
+ let arrow_after { value space_after.text; re_find /^\s*/; nonempty } ?? ""
281
+ let arrow_previous_comment {
282
+ value parent.format_arrow_comments
283
+ filter (comment) comment.offset >= space_before.start && comment.end_offset <= offset
284
+ last
285
+ }
286
+ let arrow_next_comment {
287
+ value parent.format_arrow_comments
288
+ filter (comment) comment.offset >= end_offset && comment.end_offset <= space_after.end
289
+ first
290
+ }
291
+ let arrow_previous arrow_previous_comment.offset.nonempty ?? token_neighbours.before.start
292
+ let arrow_next arrow_next_comment.offset.nonempty ?? token_neighbours.after.start
293
+ }
294
+ in ts_arrow.operator, ts_punctuator rule es-arrow-spacing:before warn "Missing space before =>." {
295
+ @format_arrow_spacing
296
+ when before && arrow_before == ""
297
+ report at arrow_previous
298
+ fix space_before as " "
299
+ }
300
+ in ts_arrow.operator, ts_punctuator rule es-arrow-spacing:after warn "Missing space after =>." {
301
+ @format_arrow_spacing
302
+ when after && arrow_after == ""
303
+ report at arrow_next
304
+ fix space_after as " "
305
+ }
306
+ in ts_arrow.operator, ts_punctuator rule es-arrow-spacing:unexpected-before warn "Unexpected space before =>." {
307
+ @format_arrow_spacing
308
+ when !before && arrow_before != ""
309
+ report at arrow_previous
310
+ fix space_before as ""
311
+ }
312
+ in ts_arrow.operator, ts_punctuator rule es-arrow-spacing:unexpected-after warn "Unexpected space after =>." {
313
+ @format_arrow_spacing
314
+ when !after && arrow_after != ""
315
+ report at arrow_next
316
+ fix space_after as ""
317
+ }
318
+
319
+ @macro format_arrow_parens {
320
+ option style "always" | "as-needed" = "always" "parentheses around one arrow parameter"
321
+ option requireForBlockBody boolean = false "keep parentheses with a block body"
322
+ }
323
+ in ts_parameter let format_arrow_plain name is ts_identifier && optional == null && type == null && init == null && count(modifier) == 0 && count(decorator) == 0
324
+ in ts_arrow.parameter rule es-arrow-parens:missing {
325
+ @format_arrow_parens
326
+ let arrow_previous token_neighbours.before
327
+ when count(parent.parameter) == 1
328
+ when arrow_previous.text != "(" || arrow_previous.start < parent.offset
329
+ let arrow_block style == "as-needed" && requireForBlockBody && parent.body is ts_block
330
+ when style == "always" || arrow_block
331
+ fix as "(" text ")"
332
+ warn if arrow_block then "Expected parentheses around arrow function argument having a body with curly braces." else "Expected parentheses around arrow function argument."
333
+ }
334
+ in ts_arrow rule es-arrow-parens {
335
+ @format_arrow_parens
336
+ when style == "as-needed"
337
+ let arrow_block requireForBlockBody && body is ts_block
338
+ when count(parameter) == 1 && !arrow_block && return_type == null && count(type_parameter) == 0
339
+ let arrow_parameter first(parameter.node)
340
+ when arrow_parameter.format_arrow_plain
341
+ let arrow_open opener.ts_open_paren.node
342
+ when arrow_open != null && arrow_open.offset < arrow_parameter.offset
343
+ let arrow_close arrow_open.paired_token
344
+ when !{
345
+ value overlapping(ts_comment)
346
+ any (comment) comment.offset >= arrow_open.end_offset && comment.end_offset <= arrow_close.start
347
+ }
348
+ let arrow_prefix byte_slice(text, 0, arrow_open.offset - offset)
349
+ let arrow_separate arrow_open.space_before.text == "" && re_test(arrow_open.token_neighbours.before.text, /[\p{L}\p{N}_$]$/)
350
+ report at arrow_parameter
351
+ fix as arrow_prefix (if arrow_separate then " " else "") arrow_parameter.text byte_slice(text, arrow_close.end - offset)
352
+ warn if requireForBlockBody then "Unexpected parentheses around single function argument having a body with no curly braces." else "Unexpected parentheses around single function argument."
353
+ }
354
+
355
+ # Object-like braces, including named module lists and import attributes.
356
+ group format_object_curly ts_object, ts_object_pattern, ts_import, ts_export, ts_mapped_type, ts_type_literal, ts_interface, ts_enum
357
+ in ts_object let format_object_kind "ObjectExpression"
358
+ in ts_object_pattern let format_object_kind "ObjectPattern"
359
+ in ts_import let format_object_kind "ImportDeclaration"
360
+ in ts_export let format_object_kind if operator == "*" then "ExportAllDeclaration" else "ExportNamedDeclaration"
361
+ in ts_mapped_type let format_object_kind "TSMappedType"
362
+ in ts_type_literal let format_object_kind "TSTypeLiteral"
363
+ in ts_interface let format_object_kind "TSInterfaceBody"
364
+ in ts_enum let format_object_kind "TSEnumBody"
365
+ in ts_object let format_object_empty count(property) == 0
366
+ in ts_object_pattern let format_object_empty count(property) == 0 && rest == null
367
+ in ts_import, ts_export let format_object_empty count(specifier) == 0
368
+ in ts_mapped_type let format_object_empty false
369
+ in ts_type_literal, ts_interface, ts_enum let format_object_empty count(member) == 0
370
+ in ts_property, ts_pattern_property let format_curly_tail value.format_curly_tail.nonempty ?? value.node
371
+ in ts_property_signature, ts_index_signature, ts_type_operator let format_curly_tail type.format_curly_tail.nonempty ?? type.node
372
+ in ts_function_type let format_curly_tail return_type.format_curly_tail.nonempty ?? return_type.node
373
+ in ts_enum_member let format_curly_tail init.format_curly_tail.nonempty ?? init.node
374
+ in ts_spread, ts_rest let format_curly_tail argument.format_curly_tail.nonempty ?? argument.node
375
+ in ts_assignment_pattern let format_curly_tail right.format_curly_tail.nonempty ?? right.node
376
+ in ts_as, ts_satisfies let format_curly_tail type.format_curly_tail.nonempty ?? type.node
377
+ in ts_mapped_type let format_object_tail type.format_curly_tail.nonempty ?? type.node
378
+ @macro format_object_curly_options {
379
+ option style "always" | "never" = "never" "spaces inside object-like braces"
380
+ option arraysInObjects boolean | null = null "override spacing beside array values and index signatures"
381
+ option objectsInObjects boolean | null = null "override spacing beside object values"
382
+ option overrides { [node: string]: "always" | "never" } = {} "spacing by syntax kind"
383
+ option emptyObjects "ignore" | "always" | "never" = "ignore" "spacing inside empty braces"
384
+ when parent is format_object_curly
385
+ let curly_attributes parent is (ts_import, ts_export) && parent.from != null && offset > parent.from.end_offset
386
+ let curly_kind if curly_attributes then "ImportAttributes" else parent.format_object_kind
387
+ let curly_spaced ({ value overrides; get curly_kind; nonempty } ?? style) == "always"
388
+ let curly_arrays arraysInObjects != null && arraysInObjects != (style == "always")
389
+ let curly_objects objectsInObjects != null && objectsInObjects != (style == "always")
390
+ let curly_empty if curly_attributes then (if is ts_open_brace then next_token is ts_close_brace else previous_token is ts_open_brace) else parent.format_object_empty
391
+ }
392
+ in ts_open_brace rule es-object-curly-spacing:open {
393
+ @format_object_curly_options
394
+ when !curly_empty
395
+ let curly_gap re_find(ts_token_gap_text(source, node, false), /^\s*/)
396
+ when !re_test(curly_gap, /[\n\r\u{2028}\u{2029}]/)
397
+ let curly_required if curly_arrays && (parent is ts_mapped_type || first(parent.member.node) is ts_index_signature) then !curly_spaced else curly_spaced
398
+ when (curly_gap != "") != curly_required
399
+ when curly_required || !space_after.comments || position(offset).js_line == position(paired_token.start).js_line
400
+ report at if curly_required then offset else end_offset
401
+ fix space_after as if curly_required then " " else ""
402
+ warn if curly_required then "A space is required after '\{'." else "There should be no space after '\{'."
403
+ }
404
+ in ts_close_brace rule es-object-curly-spacing:close {
405
+ @format_object_curly_options
406
+ when !curly_empty
407
+ let curly_gap re_find(ts_token_gap_text(source, node, true), /\s*$/)
408
+ when !re_test(curly_gap, /[\n\r\u{2028}\u{2029}]/)
409
+ let curly_tail when {
410
+ parent is (ts_object, ts_object_pattern) then { value parent.property.format_curly_tail; last; nonempty } ?? last(parent.property.node)
411
+ parent is ts_mapped_type then parent.format_object_tail
412
+ else then { value parent.member.format_curly_tail; last; nonempty } ?? last(parent.member.node)
413
+ }
414
+ let curly_required if (curly_arrays && previous_token is ts_close_bracket && curly_tail is (ts_array, ts_tuple_type) || curly_objects && previous_token is ts_close_brace && curly_tail is (ts_object, ts_object_pattern, ts_mapped_type, ts_type_literal)) && !space_before.comments then !curly_spaced else curly_spaced
415
+ when (curly_gap != "") != curly_required
416
+ report at if curly_required then offset else offset - byte_len(curly_gap)
417
+ fix space_before as if curly_required then " " else ""
418
+ warn if curly_required then "A space is required before '\}'." else "There should be no space before '\}'."
419
+ }
420
+ in ts_open_brace rule es-object-curly-spacing:empty {
421
+ @format_object_curly_options
422
+ when curly_empty && emptyObjects != "ignore" && !space_after.comments && !space_after.line_break && next_token is ts_close_brace
423
+ let curly_wanted if emptyObjects == "always" then " " else ""
424
+ when space_after.text != curly_wanted
425
+ report at end_offset
426
+ fix space_after as curly_wanted
427
+ warn if emptyObjects == "always" then "A space is required in empty '{curly_kind}'." else "There should be no space in empty '{curly_kind}'."
428
+ }
429
+
430
+ # Keys and annotation colons belong to their property or member.
431
+ group format_key_owner ts_property, ts_pattern_property, ts_property_signature, ts_index_signature, ts_class_property, ts_import_attribute
432
+ group key_spacing_containers ts_object, ts_object_pattern, ts_type_literal, ts_interface, ts_class, ts_import_attributes
433
+ in key_spacing_containers let key_spacing_enabled true
434
+ in ts_parenthesized, ts_computed_key let format_key_text expression.format_key_text.nonempty ?? expression.text
435
+ in ts_property, ts_pattern_property, ts_property_signature, ts_class_property, ts_import_attribute let format_key_label if key is ts_computed_key then key.format_key_text else key.literal_value.value.nonempty ?? key.text
436
+ in ts_index_signature let format_key_label byte_slice(text, 0, opener.ts_open_bracket.paired_token.end - offset)
437
+ in ts_property, ts_pattern_property let format_key_computed key is ts_computed_key
438
+ in ts_property, ts_pattern_property, ts_property_signature, ts_index_signature, ts_class_property let format_key_colon { value descendants(ts_colon); filter (c) c.parent.node == node; first }
439
+ in ts_import_attribute let format_key_colon colon.node
440
+ in format_key_owner let format_key_comments overlapping(ts_comment) overlapping(ts_import_attribute_comment)
441
+ in format_key_owner let format_key_width { value text; byte_slice 0 (format_key_colon.token_neighbours.before.end - offset); graphemes }
442
+ in format_key_owner let format_key_column {
443
+ value js_line_start: position(format_key_colon.token_neighbours.before.end).js_line_start
444
+ value byte_slice(source, js_line_start, format_key_colon.token_neighbours.before.end)
445
+ graphemes
446
+ }
447
+ in format_key_owner let format_key_colon_line position(format_key_colon.offset).js_line
448
+ in format_key_owner let format_key_value_line position(format_key_colon.token_neighbours.after.start).js_line
449
+ in format_key_owner let format_key_before_comment if format_key_colon.space_before.comments then {
450
+ value format_key_comments
451
+ filter (c) c.offset >= format_key_colon.token_neighbours.before.end && c.end_offset <= format_key_colon.offset
452
+ last
453
+ } else null
454
+ in format_key_owner let format_key_after_comment if format_key_colon.space_after.comments then {
455
+ value format_key_comments
456
+ filter (c) c.offset >= format_key_colon.end_offset && c.end_offset <= format_key_colon.space_after.end
457
+ first
458
+ } else null
459
+ in format_key_owner let format_key_before { value format_key_colon.space_before.text; re_find /\s*$/; nonempty } ?? ""
460
+ in format_key_owner let format_key_after { value format_key_colon.space_after.text; re_find /^\s*/; nonempty } ?? ""
461
+ in ts_colon, ts_import_attribute_colon let format_key_single_line parent.parent.format_key_single_line
462
+ in ts_colon, ts_import_attribute_colon let format_key_typed parent.parent.format_key_typed
463
+ in format_key_owner let format_key_site if format_key_colon == null then null else {
464
+ owner: node,
465
+ colon: format_key_colon,
466
+ typed: is (ts_property_signature, ts_index_signature, ts_class_property),
467
+ before: format_key_before,
468
+ after: format_key_after
469
+ }
470
+ in ts_object, ts_object_pattern let format_key_members map(property.node, p => p) map(rest.node, p => p)
471
+ in ts_type_literal, ts_interface, ts_class let format_key_members member.node
472
+ in ts_import_attributes let format_key_members descendants(ts_import_attribute)
473
+ in key_spacing_containers let format_key_typed is (ts_type_literal, ts_interface, ts_class)
474
+ in ts_object, ts_object_pattern, ts_type_literal, ts_interface, ts_class let format_key_single_line { value opener.ts_open_brace.offset.nonempty ?? offset; position }.js_line == position(end_offset).js_line
475
+ in ts_import_attributes let format_key_single_line position(open.offset).js_line == position(close.offset).js_line
476
+ in key_spacing_containers let format_key_anchors map(format_key_members, m => {
477
+ node: m,
478
+ start: m.offset,
479
+ end: m.end_offset,
480
+ start_line: position(m.offset).js_line,
481
+ end_line: position(m.end_offset).js_line,
482
+ site: m.format_key_site
483
+ })
484
+ in key_spacing_containers let format_key_group_comments overlapping(ts_comment) overlapping(ts_import_attribute_comment)
485
+ fn format_key_bridge(previous, current, comments, typed) = when {
486
+ current == null || previous == null then false
487
+ (if typed then current.site.owner.format_key_colon_line.nonempty ?? current.start_line else current.site.owner.format_key_value_line.nonempty ?? current.start_line) - previous.start_line <= 1 then true
488
+ else then {
489
+ value to: if typed then current.site.owner.format_key_colon_line.nonempty ?? current.start_line else current.site.owner.format_key_value_line.nonempty ?? current.start_line
490
+ value comments
491
+ comments_2: filter (c) c.offset >= previous.end && c.end_offset <= current.start
492
+ value format_key_comment_bridge(previous.start_line, to, comments_2)
493
+ }
494
+ }
495
+ fn format_key_comment_end(chain, to) = chain.connected && to - chain.line <= 1
496
+ fn format_key_comment_bridge(from, to, comments) = count(comments) > 0 && {
497
+ value comments
498
+ fold ({ line: from, connected: true }) (state, comment) {
499
+ line: position(comment.end_offset).js_line,
500
+ connected: state.connected && position(comment.offset).js_line - state.line <= 1
501
+ }
502
+ format_key_comment_end to
503
+ }
504
+ fn format_key_add_group(groups, item) = slice(groups, 0, -1) [last(groups) [item]]
505
+ fn format_key_group_step(state, item, comments, typed) = when {
506
+ state.previous == null then { previous: item, groups: [[item]], unaligned: [] }
507
+ typed && item.start_line == state.previous.start_line then {
508
+ previous: item,
509
+ groups: if { value state.groups; last; last }.node == state.previous.node then slice(state.groups, 0, -1) [{ value state.groups; last; slice 0 -1 }] else state.groups,
510
+ unaligned: state.unaligned [item] (if { value state.groups; last; last }.node == state.previous.node then [state.previous] else [])
511
+ }
512
+ { value state.groups; last; last }.node == state.previous.node && format_key_bridge(state.previous, item, comments, typed) then {
513
+ previous: item, groups: format_key_add_group(state.groups, item), unaligned: state.unaligned
514
+ }
515
+ else then { previous: item, groups: state.groups [[item]], unaligned: state.unaligned }
516
+ }
517
+ fn format_key_group_rows(state, typed) = {
518
+ value state.groups map(state.unaligned, item => [item])
519
+ map (g) {
520
+ rows: filter(g, a => a.site != null),
521
+ multiline: { value g; filter (a) a.site != null; first }.start_line != { value g; filter (a) a.site != null; last }.end_line,
522
+ aligned: if typed then count(g) > 1 else { value g; filter (a) a.site != null; count } > 1 && { value g; filter (a) a.site != null; first }.start_line != { value g; filter (a) a.site != null; last }.end_line,
523
+ width: if typed then 0 else { value [0] { value g; filter (a) a.site != null; map (a) a.site.owner.format_key_width }; max },
524
+ column: if typed then { value [0] { value g; filter (a) a.site != null; map (a) a.site.owner.format_key_column }; max } else 0
525
+ }
526
+ }
527
+ in key_spacing_containers let format_key_groups {
528
+ value format_key_anchors
529
+ fold ({ previous: null, groups: [], unaligned: [] }) (state, item) format_key_group_step(state, item, format_key_group_comments, format_key_typed)
530
+ format_key_group_rows format_key_typed
531
+ }
532
+ schema format_key_alignment { on?: "colon" | "value"; mode?: "strict" | "minimum"; beforeColon?: boolean; afterColon?: boolean }
533
+ schema format_key_line_policy { mode?: "strict" | "minimum"; beforeColon?: boolean; afterColon?: boolean; align?: "colon" | "value" | format_key_alignment }
534
+ fn format_key_policy(options) = { before: if options.beforeColon.nonempty ?? false then 1 else 0, after: if options.afterColon.nonempty ?? true then 1 else 0, mode: options.mode.nonempty ?? "strict" }
535
+ fn format_key_alignment_policy(align, line) = if align == null then null else {
536
+ on: if type_of(align) == "str" then align else align.on.nonempty ?? "colon",
537
+ before: if align.beforeColon.nonempty ?? line.before == 1 then 1 else 0,
538
+ after: if align.afterColon.nonempty ?? line.after == 1 then 1 else 0,
539
+ mode: align.mode.nonempty ?? line.mode
540
+ }
541
+ fn format_key_rows(groups) = { value groups; fold ([]) (rows, group) rows map(group.rows, row => { site: row.site, group: group }) }
542
+ fn format_key_expected(site, group, policy, alignment, typed) = if alignment == null || !group.aligned then {
543
+ before: policy.before, after: policy.after,
544
+ before_mode: if alignment != null && !typed && group.multiline then alignment.mode else policy.mode,
545
+ after_mode: if alignment != null && !typed && group.multiline then alignment.mode else policy.mode
546
+ } else {
547
+ before: if alignment.on == "colon" then (if typed then group.column - site.owner.format_key_column else group.width - site.owner.format_key_width) + alignment.before else alignment.before,
548
+ after: if alignment.on == "value" then (if typed then group.column - site.owner.format_key_column + alignment.before - utf16_len(site.before) else group.width - site.owner.format_key_width) + alignment.after else alignment.after,
549
+ before_mode: if typed && alignment.on == "colon" then "strict" else alignment.mode,
550
+ after_mode: if typed && alignment.on == "value" then "strict" else alignment.mode
551
+ }
552
+ fn format_key_wrong(actual, wanted, mode, typed) = if mode == "minimum" then actual < wanted || !typed && wanted == 0 && actual > 0 else actual != wanted
553
+ fn format_key_fixed_gap(gap, wanted, keep_end, insert_end) = when {
554
+ utf16_len(gap) >= wanted then if keep_end then { value result: utf16_len(gap) - wanted; value slice(gap, result) } else slice(gap, 0, wanted)
555
+ insert_end then gap { value result_2: wanted - utf16_len(gap); value repeat(" ", result_2) }
556
+ else then { value result_3: wanted - utf16_len(gap); value repeat(" ", result_3) } gap
557
+ }
558
+ fn format_key_fix(site, before, wanted) = when {
559
+ before then { start: site.colon.offset - byte_len(site.before), end: site.colon.offset, text: format_key_fixed_gap(site.before, wanted, !site.typed, site.typed) }
560
+ wanted < 0 then {
561
+ start: site.colon.offset - byte_len(site.before), end: site.colon.end_offset + byte_len(site.after),
562
+ text: { value result: utf16_len(site.before) + wanted; value slice(site.before, 0, result) } ":"
563
+ }
564
+ else then { start: site.colon.end_offset, end: site.colon.end_offset + byte_len(site.after), text: format_key_fixed_gap(site.after, wanted, site.typed, !site.typed) }
565
+ }
566
+ @macro format_key_options {
567
+ option beforeColon boolean = false "space before colons"
568
+ option afterColon boolean = true "space after colons"
569
+ option mode "strict" | "minimum" = "strict" "exact or minimum spacing"
570
+ option singleLine format_key_line_policy | null = null "single-line container spacing"
571
+ option multiLine format_key_line_policy | null = null "multiline container spacing"
572
+ option align "colon" | "value" | format_key_alignment | null = null "align multiline keys or values"
573
+ let key_base { beforeColon: beforeColon, afterColon: afterColon, mode: mode, align: align }
574
+ let key_line if singleLine == null && multiLine == null then { before: if beforeColon then 1 else 0, after: if afterColon then 1 else 0, mode: mode } else {
575
+ value if format_key_single_line then singleLine.nonempty ?? key_base else multiLine.nonempty ?? key_base
576
+ format_key_policy
577
+ }
578
+ let key_multi { value multiLine.nonempty ?? key_base; format_key_policy }
579
+ # Typed nested alignment resets spacing defaults, but inherits line mode.
580
+ let key_alignment when {
581
+ align == null && multiLine.align == null then null
582
+ type_of(align) == "record" then { value {}; line: format_key_policy; value format_key_alignment_policy(align, line) }
583
+ else then {
584
+ value align_2: if format_key_typed then multiLine.align.nonempty ?? align else (multiLine.nonempty ?? key_base).align
585
+ value line_2: if format_key_typed && type_of(multiLine.align) == "record" then { before: 0, after: 1, mode: key_multi.mode } else key_multi
586
+ value format_key_alignment_policy(align_2, line_2)
587
+ }
588
+ }
589
+ }
590
+ @macro format_key_aligned_entries {
591
+ when key_spacing_enabled
592
+ @format_key_options
593
+ when key_alignment != null && !format_key_single_line
594
+ let key_aligned_entries {
595
+ value format_key_groups
596
+ format_key_rows
597
+ map (entry) {
598
+ site: entry.site,
599
+ policy: format_key_expected(entry.site, entry.group, key_line, key_alignment, format_key_typed)
600
+ }
601
+ }
602
+ }
603
+ @macro format_key_aligned_entry {
604
+ when !spacing_entry.site.typed || position(spacing_entry.site.owner.offset).js_line == spacing_entry.site.owner.format_key_colon_line
605
+ }
606
+ @macro format_key_individual {
607
+ when parent is format_key_owner && parent.parent is key_spacing_containers && parent.parent.key_spacing_enabled
608
+ @format_key_options
609
+ when key_alignment == null || format_key_single_line
610
+ when !(parent is (ts_property_signature, ts_index_signature, ts_class_property) && position(parent.offset).js_line != position(offset).js_line)
611
+ let spacing_entry { site: parent.format_key_site, policy: key_line }
612
+ }
613
+ @macro format_key_before_report {
614
+ report at when {
615
+ spacing_entry.site.typed then { start: spacing_entry.site.owner.offset, end: spacing_entry.site.owner.end_offset }
616
+ utf16_len(spacing_entry.site.before) > spacing_entry.policy.before then { start: spacing_entry.site.colon.offset - byte_len(spacing_entry.site.before), end: spacing_entry.site.colon.offset }
617
+ else then { start: spacing_entry.site.owner.format_key_before_comment.offset.nonempty ?? spacing_entry.site.colon.token_neighbours.before.start, end: spacing_entry.site.owner.format_key_before_comment.end_offset.nonempty ?? spacing_entry.site.colon.token_neighbours.before.end }
618
+ }
619
+ warn if utf16_len(spacing_entry.site.before) > spacing_entry.policy.before then "Extra space after {if spacing_entry.site.owner.format_key_computed == true then 'computed ' else ''}key '{spacing_entry.site.owner.format_key_label}'." else "Missing space after {if spacing_entry.site.owner.format_key_computed == true then 'computed ' else ''}key '{spacing_entry.site.owner.format_key_label}'."
620
+ }
621
+ @macro format_key_after_report {
622
+ report at when {
623
+ spacing_entry.site.typed then { start: spacing_entry.site.owner.offset, end: spacing_entry.site.owner.end_offset }
624
+ utf16_len(spacing_entry.site.after) > spacing_entry.policy.after then { start: spacing_entry.site.colon.offset, end: spacing_entry.site.colon.end_offset + byte_len(spacing_entry.site.after) }
625
+ else then { start: spacing_entry.site.owner.format_key_after_comment.offset.nonempty ?? spacing_entry.site.colon.token_neighbours.after.start, end: spacing_entry.site.owner.format_key_after_comment.end_offset.nonempty ?? spacing_entry.site.colon.token_neighbours.after.end }
626
+ }
627
+ warn if utf16_len(spacing_entry.site.after) > spacing_entry.policy.after then "Extra space before value for {if spacing_entry.site.owner.format_key_computed == true then 'computed ' else ''}key '{spacing_entry.site.owner.format_key_label}'." else "Missing space before value for {if spacing_entry.site.owner.format_key_computed == true then 'computed ' else ''}key '{spacing_entry.site.owner.format_key_label}'."
628
+ }
629
+ in ts_colon, ts_import_attribute_colon rule es-key-spacing {
630
+ @format_key_individual
631
+ let key_gap { value space_before.text; re_find /\s*$/; nonempty } ?? ""
632
+ when {
633
+ value key_gap
634
+ utf16_len
635
+ format_key_wrong key_line.before key_line.mode (parent is (ts_property_signature, ts_index_signature, ts_class_property))
636
+ }
637
+ when key_line.before == 0 || !re_test(key_gap, /[\n\r\u{2028}\u{2029}]/)
638
+ fix space_before as format_key_fixed_gap(key_gap, key_line.before, !format_key_typed, format_key_typed)
639
+ @format_key_before_report
640
+ }
641
+ in ts_colon, ts_import_attribute_colon rule es-key-spacing:after {
642
+ @format_key_individual
643
+ let key_gap { value space_after.text; re_find /^\s*/; nonempty } ?? ""
644
+ when {
645
+ value key_gap
646
+ utf16_len
647
+ format_key_wrong key_line.after key_line.mode (parent is (ts_property_signature, ts_index_signature, ts_class_property))
648
+ }
649
+ when key_line.after == 0 || !re_test(key_gap, /[\n\r\u{2028}\u{2029}]/)
650
+ fix space_after as format_key_fixed_gap(key_gap, key_line.after, format_key_typed, !format_key_typed)
651
+ @format_key_after_report
652
+ }
653
+ in key_spacing_containers rule es-key-spacing:aligned-before {
654
+ @format_key_aligned_entries
655
+ each spacing_entry in key_aligned_entries {
656
+ @format_key_aligned_entry
657
+ when {
658
+ value spacing_entry.site.before
659
+ utf16_len
660
+ format_key_wrong spacing_entry.policy.before spacing_entry.policy.before_mode spacing_entry.site.typed
661
+ }
662
+ when spacing_entry.policy.before == 0 || !re_test(spacing_entry.site.before, /[\n\r\u{2028}\u{2029}]/)
663
+ fix at format_key_fix(spacing_entry.site, true, spacing_entry.policy.before) as {
664
+ value key_fix: format_key_fix(spacing_entry.site, true, spacing_entry.policy.before)
665
+ value key_fix.text
666
+ }
667
+ @format_key_before_report
668
+ }
669
+ }
670
+ in key_spacing_containers rule es-key-spacing:aligned-after {
671
+ @format_key_aligned_entries
672
+ each spacing_entry in key_aligned_entries {
673
+ @format_key_aligned_entry
674
+ when {
675
+ value spacing_entry.site.after
676
+ utf16_len
677
+ format_key_wrong spacing_entry.policy.after spacing_entry.policy.after_mode spacing_entry.site.typed
678
+ }
679
+ when spacing_entry.policy.after == 0 || !re_test(spacing_entry.site.after, /[\n\r\u{2028}\u{2029}]/)
680
+ fix at format_key_fix(spacing_entry.site, false, spacing_entry.policy.after) as {
681
+ value key_fix: format_key_fix(spacing_entry.site, false, spacing_entry.policy.after)
682
+ value key_fix.text
683
+ }
684
+ @format_key_after_report
685
+ }
686
+ }
687
+
688
+ # ---- keywords ---------------------------------------------------------------
689
+
690
+ schema format_keyword_policy { before?: boolean; after?: boolean }
691
+ @macro format_keyword_options {
692
+ option before boolean = true "space before keywords"
693
+ option after boolean = true "space after keywords"
694
+ option overrides { [keyword: string]: format_keyword_policy } = {} "spacing by keyword"
695
+ let keyword_value when {
696
+ is ts_for_of.modifier then "for"
697
+ parent is ts_variable_declaration && starts_with(text, "await") then "await"
698
+ else then text
699
+ }
700
+ let keyword_label if parent is ts_for_of && parent.modifier != null && (text == "for" || is ts_for_of.modifier) then "for await" else keyword_value
701
+ let keyword_module parent is (ts_import, ts_export, ts_import_specifier, ts_export_specifier) && contains(["import", "export", "from", "as", "type"], text)
702
+ let keyword_statement parent is (ts_debugger, ts_with, ts_break, ts_continue, ts_return, ts_throw, ts_try, ts_if, ts_switch, ts_case, ts_do_while, ts_for, ts_for_in, ts_for_of, ts_while, ts_static_block) && contains(["debugger", "with", "break", "continue", "return", "throw", "try", "catch", "finally", "if", "else", "switch", "case", "default", "do", "for", "in", "of", "while", "static", "await"], text) && !(text == "await" && parent is ts_for_of && is ts_keyword)
703
+ let keyword_modifier parent is (ts_method, ts_class_property) && (contains(["get", "set", "async", "accessor"], text) && offset < parent.key.offset || text == "static" && offset == parent.offset)
704
+ let keyword_around keyword_statement || keyword_module || keyword_modifier || is ts_variable_declaration.kind || parent is ts_class && text == "extends" || parent is ts_export && text == "default" || parent is (ts_import, ts_export) && text == "with" || parent is (ts_as, ts_satisfies) && contains(["as", "satisfies"], text) || is (ts_debugger, ts_break, ts_continue, ts_return) && contains(["debugger", "break", "continue", "return"], text)
705
+ let keyword_leading parent is (ts_function, ts_arrow, ts_class) && contains(["function", "async", "class"], text) && offset == parent.offset || parent is ts_unary && contains(["typeof", "void", "delete"], text) || parent is (ts_new, ts_await, ts_yield) && contains(["new", "await", "yield"], text) || is (ts_super, ts_this) || is ts_yield && text == "yield"
706
+ }
707
+ in ts_keyword, ts_variable_declaration.kind, ts_try.kind, ts_unary.operator, ts_function.modifier, ts_arrow.modifier, ts_method.modifier, ts_method.kind, ts_class_property.modifier, ts_import.kind, ts_export.kind, ts_for_of.modifier, ts_super, ts_this, ts_debugger, ts_break, ts_continue, ts_return, ts_yield rule es-keyword-spacing:before {
708
+ @ts_token_context
709
+ @format_keyword_options
710
+ # Cheapest and most selective first: most keywords are spaced as wanted.
711
+ let keyword_required if len(overrides) == 0 then before else { value get(overrides, keyword_value).before; nonempty } ?? before
712
+ let keyword_before_text token(node, "before").text
713
+ when keyword_around || keyword_leading
714
+ when space_before != null && space_before.spaced != keyword_required
715
+ when !(contains(["in", "of"], text) && previous_token is ts_private_name)
716
+ when !(keyword_before_text == ">" && ts_binary.operator == ">" && offset == ts_binary.right.offset)
717
+ when !space_before.line_break && (keyword_required || !space_before.comments)
718
+ when previous_token is (ts_regex, ts_string, ts_template, ts_private_name) || {
719
+ value result: if keyword_module then /^[)\]}>*]$/ else /^[)\]}>]$/
720
+ value re_test(keyword_before_text, result)
721
+ }
722
+ when !re_test(keyword_before_text, /\$\{$/)
723
+ report at if keyword_required then { start: offset, end: end_offset } else { start: space_before.start, end: space_before.end }
724
+ fix space_before as if keyword_required then " " else ""
725
+ warn if keyword_required then "Expected space(s) before \"{keyword_label}\"." else "Unexpected space(s) before \"{keyword_label}\"."
726
+ }
727
+ in ts_keyword, ts_variable_declaration.kind, ts_try.kind, ts_method.modifier, ts_method.kind, ts_class_property.modifier, ts_import.kind, ts_export.kind, ts_for_of.modifier, ts_debugger, ts_break, ts_continue, ts_return rule es-keyword-spacing:after {
728
+ @ts_token_context
729
+ @format_keyword_options
730
+ # Cheapest and most selective first: most keywords are spaced as wanted.
731
+ let keyword_required if len(overrides) == 0 then after else { value get(overrides, keyword_value).after; nonempty } ?? after
732
+ let keyword_after_text token(node, "after").text
733
+ when keyword_around
734
+ when space_after != null && space_after.spaced != keyword_required
735
+ when !(parent is ts_try && text == "catch" && parent.parameter != null)
736
+ when !(parent is ts_import_specifier && text == "as" || parent is ts_import && text == "as")
737
+ when !(parent is ts_variable_declaration && starts_with(text, "await"))
738
+ when !space_after.line_break && (keyword_required || !space_after.comments)
739
+ when next_token is (ts_regex, ts_string, ts_template, ts_private_name) || {
740
+ value result: if keyword_module then /^[{*]$/ else /^(?:[(\[{<~!]|\+\+?|--?)$/
741
+ value re_test(keyword_after_text, result)
742
+ }
743
+ when !re_test(keyword_after_text, /^\}/)
744
+ report at when {
745
+ !keyword_required then { start: space_after.start, end: space_after.end }
746
+ is ts_for_of.modifier then parent.offset
747
+ else then { start: offset, end: end_offset }
748
+ }
749
+ fix space_after as if keyword_required then " " else ""
750
+ warn if keyword_required then "Expected space(s) after \"{keyword_label}\"." else "Unexpected space(s) after \"{keyword_label}\"."
751
+ }
752
+
753
+ import .ts/flow
754
+
755
+ }