@inthedark/dxlint 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -2
  3. package/bin/dxlint +15 -0
  4. package/bin/dxlint-linux-x64 +0 -0
  5. package/bin/dxlint-win32-x64.exe +0 -0
  6. package/package.json +40 -4
  7. package/rules/conventions.dx +34 -0
  8. package/rules/correctness.dx +33 -0
  9. package/rules/es-no-semi.dx +11 -0
  10. package/rules/format.dx +49 -0
  11. package/rules/recommended.dx +51 -0
  12. package/rules/suspicious.dx +31 -0
  13. package/rules/types.dx +21 -0
  14. package/std/c.dx +23 -0
  15. package/std/core/lines.dx +51 -0
  16. package/std/core/lists.dx +76 -0
  17. package/std/core.dx +30 -0
  18. package/std/css.dx +17 -0
  19. package/std/dx.dx +31 -0
  20. package/std/json.dx +104 -0
  21. package/std/markdown.dx +11 -0
  22. package/std/python.dx +21 -0
  23. package/std/rust/format.dx +316 -0
  24. package/std/rust.dx +34 -0
  25. package/std/toml.dx +19 -0
  26. package/std/ts/arithmetic.dx +87 -0
  27. package/std/ts/assertions.dx +477 -0
  28. package/std/ts/calls.dx +246 -0
  29. package/std/ts/comments.dx +10 -0
  30. package/std/ts/conditions.dx +531 -0
  31. package/std/ts/constants.dx +335 -0
  32. package/std/ts/deprecation.dx +185 -0
  33. package/std/ts/directives.dx +60 -0
  34. package/std/ts/effects.dx +219 -0
  35. package/std/ts/enums.dx +233 -0
  36. package/std/ts/expression-fixes.dx +67 -0
  37. package/std/ts/expressions.dx +270 -0
  38. package/std/ts/flow.dx +2 -0
  39. package/std/ts/format.dx +755 -0
  40. package/std/ts/globals.dx +106 -0
  41. package/std/ts/grammar.dx +488 -0
  42. package/std/ts/helpers.dx +102 -0
  43. package/std/ts/import-attributes.dx +88 -0
  44. package/std/ts/import-no-deprecated.dx +73 -0
  45. package/std/ts/indentation.dx +442 -0
  46. package/std/ts/jsx.dx +646 -0
  47. package/std/ts/layout/blocks.dx +281 -0
  48. package/std/ts/layout/calls.dx +79 -0
  49. package/std/ts/layout/expressions.dx +213 -0
  50. package/std/ts/layout/literals.dx +114 -0
  51. package/std/ts/layout/separators.dx +184 -0
  52. package/std/ts/layout/shared.dx +203 -0
  53. package/std/ts/layout/text.dx +142 -0
  54. package/std/ts/layout/types.dx +182 -0
  55. package/std/ts/layout.dx +9 -0
  56. package/std/ts/license.dx +54 -0
  57. package/std/ts/literal-text.dx +3 -0
  58. package/std/ts/literals.dx +95 -0
  59. package/std/ts/methods.dx +69 -0
  60. package/std/ts/modules.dx +268 -0
  61. package/std/ts/names.dx +348 -0
  62. package/std/ts/naming.dx +169 -0
  63. package/std/ts/narrowing.dx +96 -0
  64. package/std/ts/parentheses.dx +262 -0
  65. package/std/ts/precedence.dx +25 -0
  66. package/std/ts/problems/calls.dx +34 -0
  67. package/std/ts/problems/classes.dx +39 -0
  68. package/std/ts/problems/control-flow.dx +257 -0
  69. package/std/ts/problems/declarations.dx +67 -0
  70. package/std/ts/problems/duplicates.dx +70 -0
  71. package/std/ts/problems/expressions.dx +154 -0
  72. package/std/ts/problems/helpers.dx +22 -0
  73. package/std/ts/problems/imports.dx +43 -0
  74. package/std/ts/problems/non-null.dx +78 -0
  75. package/std/ts/problems/numeric-precision.dx +66 -0
  76. package/std/ts/problems/regexp.dx +66 -0
  77. package/std/ts/problems/statements.dx +36 -0
  78. package/std/ts/problems/typescript.dx +55 -0
  79. package/std/ts/problems/whitespace.dx +40 -0
  80. package/std/ts/problems.dx +20 -0
  81. package/std/ts/regex/checks.dx +249 -0
  82. package/std/ts/regex/findings.dx +209 -0
  83. package/std/ts/regex/grammar.dx +80 -0
  84. package/std/ts/regex/suggestions.dx +94 -0
  85. package/std/ts/regex/values.dx +378 -0
  86. package/std/ts/regex.dx +5 -0
  87. package/std/ts/restrictions/comments.dx +76 -0
  88. package/std/ts/restrictions.dx +811 -0
  89. package/std/ts/scopes.dx +68 -0
  90. package/std/ts/scoping/atomic.dx +40 -0
  91. package/std/ts/scoping/bindings.dx +126 -0
  92. package/std/ts/scoping/constness.dx +72 -0
  93. package/std/ts/scoping/globals.dx +52 -0
  94. package/std/ts/scoping/loops.dx +86 -0
  95. package/std/ts/scoping/member-uses.dx +111 -0
  96. package/std/ts/scoping/receivers.dx +107 -0
  97. package/std/ts/scoping/shadowing.dx +202 -0
  98. package/std/ts/scoping/usage.dx +393 -0
  99. package/std/ts/scoping.dx +842 -0
  100. package/std/ts/selectors.dx +10 -0
  101. package/std/ts/spreads.dx +97 -0
  102. package/std/ts/structure/declarations.dx +52 -0
  103. package/std/ts/structure/lines.dx +79 -0
  104. package/std/ts/structure/lists.dx +144 -0
  105. package/std/ts/structure/padding.dx +258 -0
  106. package/std/ts/structure.dx +963 -0
  107. package/std/ts/style/classes.dx +157 -0
  108. package/std/ts/style/comments.dx +75 -0
  109. package/std/ts/style/declarations.dx +82 -0
  110. package/std/ts/style/expressions.dx +125 -0
  111. package/std/ts/style/imports.dx +44 -0
  112. package/std/ts/style/ordering.dx +192 -0
  113. package/std/ts/style/returns.dx +152 -0
  114. package/std/ts/style/signatures.dx +308 -0
  115. package/std/ts/style/type-exports.dx +62 -0
  116. package/std/ts/style/type-imports.dx +166 -0
  117. package/std/ts/style/types.dx +474 -0
  118. package/std/ts/style.dx +29 -0
  119. package/std/ts/suggestions/accessors.dx +138 -0
  120. package/std/ts/suggestions/builtins.dx +110 -0
  121. package/std/ts/suggestions/comparisons.dx +41 -0
  122. package/std/ts/suggestions/computed-keys.dx +20 -0
  123. package/std/ts/suggestions/constructors.dx +54 -0
  124. package/std/ts/suggestions/control-flow.dx +204 -0
  125. package/std/ts/suggestions/curly.dx +124 -0
  126. package/std/ts/suggestions/directives.dx +79 -0
  127. package/std/ts/suggestions/errors.dx +106 -0
  128. package/std/ts/suggestions/expressions.dx +230 -0
  129. package/std/ts/suggestions/functions.dx +260 -0
  130. package/std/ts/suggestions/globals.dx +187 -0
  131. package/std/ts/suggestions/helpers.dx +17 -0
  132. package/std/ts/suggestions/object-shorthand.dx +148 -0
  133. package/std/ts/suggestions/object-spread.dx +96 -0
  134. package/std/ts/suggestions/regexp.dx +59 -0
  135. package/std/ts/suggestions/renames.dx +50 -0
  136. package/std/ts/suggestions/strings.dx +47 -0
  137. package/std/ts/suggestions/templates.dx +104 -0
  138. package/std/ts/suggestions.dx +24 -0
  139. package/std/ts/template-fixes.dx +22 -0
  140. package/std/ts/tokens.dx +124 -0
  141. package/std/ts/type-specifiers.dx +93 -0
  142. package/std/ts/typed/assignments.dx +154 -0
  143. package/std/ts/typed/awaitability.dx +157 -0
  144. package/std/ts/typed/boolean-comparisons.dx +82 -0
  145. package/std/ts/typed/defaults.dx +97 -0
  146. package/std/ts/typed/enum-consistency.dx +68 -0
  147. package/std/ts/typed/enums.dx +52 -0
  148. package/std/ts/typed/errors.dx +32 -0
  149. package/std/ts/typed/floating-promises.dx +147 -0
  150. package/std/ts/typed/for-in-arrays.dx +21 -0
  151. package/std/ts/typed/includes.dx +102 -0
  152. package/std/ts/typed/nullish-coalescing.dx +149 -0
  153. package/std/ts/typed/optional-chains.dx +317 -0
  154. package/std/ts/typed/promises.dx +205 -0
  155. package/std/ts/typed/qualifiers.dx +108 -0
  156. package/std/ts/typed/readonly-members.dx +159 -0
  157. package/std/ts/typed/readonly-parameters.dx +121 -0
  158. package/std/ts/typed/redundant-constituents.dx +171 -0
  159. package/std/ts/typed/regexp-exec.dx +79 -0
  160. package/std/ts/typed/rejection-handlers.dx +91 -0
  161. package/std/ts/typed/return-await.dx +116 -0
  162. package/std/ts/typed/return-this.dx +67 -0
  163. package/std/ts/typed/strict-void-return.dx +121 -0
  164. package/std/ts/typed/string-boundaries.dx +153 -0
  165. package/std/ts/typed/switches.dx +147 -0
  166. package/std/ts/typed/template-expressions.dx +127 -0
  167. package/std/ts/typed/template-values.dx +88 -0
  168. package/std/ts/typed/transfers.dx +106 -0
  169. package/std/ts/typed/type-arguments.dx +337 -0
  170. package/std/ts/typed/type-constituents.dx +143 -0
  171. package/std/ts/typed/type-conversions.dx +82 -0
  172. package/std/ts/typed/type-parameters.dx +280 -0
  173. package/std/ts/typed/unbound-method.dx +167 -0
  174. package/std/ts/typed/unsafe-calls.dx +73 -0
  175. package/std/ts/typed/unsafe-members.dx +36 -0
  176. package/std/ts/typed/void-expressions.dx +101 -0
  177. package/std/ts/typed.dx +585 -0
  178. package/std/ts/values/arguments.dx +13 -0
  179. package/std/ts/values/enums.dx +104 -0
  180. package/std/ts/values/inspection.dx +274 -0
  181. package/std/ts/values.dx +1217 -0
  182. package/std/ts.dx +77 -0
  183. package/std/whitespace.dx +37 -0
@@ -0,0 +1,281 @@
1
+ # .ts/layout/blocks: blocks layout rules.
2
+
3
+ import .ts/layout/shared
4
+
5
+ define {
6
+
7
+ @macro layout_block_space_options {
8
+ option style "always" | "never" = "always" "spaces inside non-empty blocks"
9
+ }
10
+
11
+ @macro layout_switch_colon_options {
12
+ option before boolean = false "space before case colon"
13
+ option after boolean = true "space after case colon"
14
+ }
15
+
16
+ in ts_block, ts_static_block, ts_switch, ts_enum let layout_block_line_comment { value opener.ts_open_brace.space_after.text; trim_start; starts_with "//" }
17
+
18
+ in ts_block let layout_block_function parent is (ts_function, ts_arrow, ts_method)
19
+ in ts_block let layout_block_case parent is ts_case
20
+ in ts_block let layout_block_module parent is ts_namespace
21
+ in ts_block let layout_brace_excluded parent is (ts_program, ts_block, ts_case)
22
+ in ts_block let layout_brace_keyword parent is (ts_if, ts_try)
23
+ in ts_block, ts_static_block, ts_switch, ts_class let layout_brace_single opener.ts_open_brace.line == closer.ts_close_brace.line
24
+
25
+ @macro layout_horizontal_space "[^\\S\\n\\r\\u{{2028}}\\u{{2029}}]"
26
+ fn layout_padding_attached_start(gap) = if !contains(gap, "/") then "" else { value gap; re_find ("^(?:" @layout_horizontal_space "*" @layout_comment_pattern ")*"); nonempty } ?? ""
27
+ fn layout_padding_attached_end(gap) = if !contains(gap, "/") then "" else { value gap; re_find ("(?:" @layout_comment_pattern @layout_horizontal_space "*)*$"); nonempty } ?? ""
28
+ fn layout_padding_last_attached(prefix) = {
29
+ value pattern: "^(?:" @layout_horizontal_space "*(" @layout_comment_pattern "))*$"
30
+ value re_replace(prefix, pattern, "$1")
31
+ }
32
+ fn layout_padding_last_comment(gap) = { value pattern: "^(?:\\s*(" @layout_comment_pattern "))*\\s*$"; value re_replace(gap, pattern, "$1") }
33
+ fn layout_padding_top_space(gap) = {
34
+ value gap
35
+ layout_padding_attached_start
36
+ len: len
37
+ value slice(gap, len)
38
+ layout_comma_space false
39
+ }
40
+ fn layout_padding_tail_space(gap) = {
41
+ value result: if contains(gap, "//") && { value gap; layout_padding_last_comment; starts_with "//" } then /(?:\r\n|[\n\r\u{2028}\u{2029}])\s*$/ else /\s*$/
42
+ value re_find(gap, result)
43
+ nonempty
44
+ } ?? ""
45
+ fn layout_padding_bottom_space(gap) = {
46
+ value result: len(gap) - { value gap; layout_padding_attached_end; len }
47
+ value slice(gap, 0, result)
48
+ layout_padding_tail_space
49
+ }
50
+ fn layout_line_breaks(gap) = re_count(gap, /\r\n|[\n\r\u{2028}\u{2029}]/)
51
+ fn layout_padding_reduced(gap) = "\n" { value gap; re_find /[^\n\r\u{2028}\u{2029}]*$/ }
52
+ fn layout_padding_mode(style, blocks, switches, classes, owner) = if owner.layout_block_module.nonempty ?? false then null else if style != null then style else if blocks == null && switches == null && classes == null then "always" else if owner is ts_class then classes else if owner is ts_switch then switches else blocks
53
+ @macro layout_padding_options {
54
+ option style "always" | "never" | "start" | "end" | null = null "padding at block boundaries"
55
+ option blocks "always" | "never" | "start" | "end" | null = null "statement block padding"
56
+ option switches "always" | "never" | "start" | "end" | null = null "switch padding"
57
+ option classes "always" | "never" | "start" | "end" | null = null "class padding"
58
+ option allowSingleLineBlocks boolean = false "allow single-line blocks"
59
+ }
60
+
61
+ in ts_block, ts_static_block let layout_padding_content body != null
62
+ in ts_switch let layout_padding_content case != null
63
+ in ts_class let layout_padding_content member != null
64
+ in ts_block, ts_static_block, ts_switch, ts_class {
65
+ let layout_padding_open_prefix layout_padding_attached_start(opener.ts_open_brace.space_after.text)
66
+ let layout_padding_close_suffix layout_padding_attached_end(closer.ts_close_brace.space_before.text)
67
+ let layout_padding_single opener.ts_open_brace.line + layout_line_breaks(layout_padding_open_prefix) == closer.ts_close_brace.line - layout_line_breaks(layout_padding_close_suffix)
68
+ }
69
+
70
+ in ts_block, ts_static_block, ts_switch, ts_class rule es-padded-blocks:top {
71
+ @layout_padding_options
72
+ when opener.ts_open_brace.offset != null
73
+ when layout_padding_content
74
+ let padding_mode layout_padding_mode(style, blocks, switches, classes, node)
75
+ when padding_mode != null
76
+ when !(allowSingleLineBlocks && layout_padding_single)
77
+ let padding_space layout_padding_top_space(opener.ts_open_brace.space_after.text)
78
+ let padding_padded layout_line_breaks(padding_space) >= 2
79
+ when padding_padded != (padding_mode == "always" || padding_mode == "start")
80
+ let padding_start opener.ts_open_brace.end_offset + byte_len(layout_padding_open_prefix)
81
+ let padding_report_start if layout_padding_open_prefix == "" then opener.ts_open_brace.offset else opener.ts_open_brace.end_offset + byte_len(layout_padding_open_prefix) - { value layout_padding_open_prefix; layout_padding_last_attached; byte_len }
82
+ report at { start: padding_report_start, end: padding_start + byte_len(padding_space) }
83
+ warn if padding_padded then "Block must not be padded by blank lines." else "Block must be padded by blank lines."
84
+ fix at { start: padding_start, end: padding_start + (if padding_padded then byte_len(padding_space) else 0) } as if padding_padded then layout_padding_reduced(padding_space) else "\n"
85
+ }
86
+
87
+ in ts_block, ts_static_block, ts_switch, ts_class rule es-padded-blocks:bottom {
88
+ @layout_padding_options
89
+ when closer.ts_close_brace.offset != null
90
+ when layout_padding_content
91
+ let padding_mode layout_padding_mode(style, blocks, switches, classes, node)
92
+ when padding_mode != null
93
+ when !(allowSingleLineBlocks && layout_padding_single)
94
+ let padding_space layout_padding_bottom_space(closer.ts_close_brace.space_before.text)
95
+ let padding_padded layout_line_breaks(padding_space) >= 2
96
+ when padding_padded != (padding_mode == "always" || padding_mode == "end")
97
+ let padding_end closer.ts_close_brace.offset - byte_len(layout_padding_close_suffix)
98
+ report at { start: padding_end - byte_len(padding_space), end: closer.ts_close_brace.offset }
99
+ warn if padding_padded then "Block must not be padded by blank lines." else "Block must be padded by blank lines."
100
+ fix at { start: padding_end - (if padding_padded then byte_len(padding_space) else 0), end: padding_end } as if padding_padded then layout_padding_reduced(padding_space) else "\n"
101
+ }
102
+
103
+ in ts_code match layout_class_line_gap /[\r\n](?:[^\S\r\n]*[\r\n])+/
104
+ in layout_class_line_gap let layout_class_blank re_count(text, /\r\n|[\r\n]/) > 1
105
+ in ts_semicolon let layout_member_before token_neighbours.before
106
+ in ts_semicolon let layout_member_preceding_end {
107
+ value default: { value overlapping(ts_comment, layout_member_before.end, offset).end_offset; last; nonempty } ?? 0
108
+ value max(layout_member_before.end, default)
109
+ }
110
+
111
+ group layout_member_gap_token ts_token, ts_comment
112
+ fn layout_member_line_run(parts, start, lines) = {
113
+ value result: {
114
+ value parts
115
+ fold ({ end: start, active: true }) (state, part) {
116
+ end: if state.active && position(part.start).line - position(state.end).line <= lines then part.end else state.end,
117
+ active: state.active && position(part.start).line - position(state.end).line <= lines
118
+ }
119
+ }
120
+ value result.end
121
+ }
122
+ fn layout_member_gap_fix(source, start, end, padded) = {
123
+ value parts: {
124
+ value overlapping(layout_member_gap_token, start, end)
125
+ filter (token) token.offset >= start && token.end_offset <= end
126
+ sort_by (token) token.offset
127
+ map (token) { start: token.offset, end: token.end_offset }
128
+ }
129
+ value checked: {
130
+ value parts
131
+ fold ({ end: start, clean: true }) (state, part) {
132
+ end: part.end,
133
+ clean: state.clean && { value source; byte_slice state.end part.start; re_test /^\s*$/ }
134
+ }
135
+ }
136
+ value clean: checked.clean && { value source; byte_slice checked.end end; re_test /^\s*$/ }
137
+ value before: layout_member_line_run(parts, start, 1)
138
+ value reversed: {
139
+ value parts
140
+ reverse
141
+ fold ({ start: end, active: true }) (state, part) {
142
+ start: if state.active && position(state.start).line - position(part.end).line <= 1 then part.start else state.start,
143
+ active: state.active && position(state.start).line - position(part.end).line <= 1
144
+ }
145
+ }
146
+ value after: reversed.start
147
+ value between: any(parts, part => part.start >= before && part.end <= after)
148
+ value insertion: layout_member_line_run(parts, start, 0)
149
+ value gap: if before < after then byte_slice(source, before, after) else ""
150
+ value if !clean || between || padded && (before >= after || !re_test(gap, /^\s*$/)) then null
151
+ else if padded then { start: before, end: after, text: "\n" }
152
+ else { start: insertion, end: insertion, text: "\n" }
153
+ }
154
+ in ts_class.member rule es-lines-between-class-members {
155
+ option style "always" | "never" = "always" "blank lines between class members"
156
+ option enforce { blankLine: "always" | "never"; prev: "method" | "field" | "*"; next: "method" | "field" | "*" }[] = [] "policy by neighbouring member kinds"
157
+ option exceptAfterSingleLine boolean = false "skip after single-line members"
158
+ option exceptAfterOverload boolean = true "skip overload signatures"
159
+ let skip_overloads exceptAfterOverload && (count(enforce) == 0 && style == "always" || any(enforce, e => e.blankLine == "always" && e.prev != "field" && e.next != "field"))
160
+ let member_skipped skip_overloads && is ts_method && ts_method.body == null
161
+ let member_tail last_token
162
+ let retained_member if member_skipped then previous.retained_member else {
163
+ start: offset,
164
+ kind: if is ts_method then "method" else if is ts_class_property then "field" else "*",
165
+ tail: member_tail,
166
+ before_tail: if member_tail is ts_semicolon then { value overlapping(ts_semicolon, offset, end_offset).layout_member_preceding_end; last } else null
167
+ }
168
+ let prior_member previous.retained_member
169
+ when !member_skipped && prior_member != null
170
+ let gap_moved_semicolon prior_member.tail.text == ";" && position(prior_member.before_tail).line != position(prior_member.tail.start).line && position(prior_member.tail.end).line == position(first_token.start).line
171
+ let gap_start if gap_moved_semicolon then prior_member.before_tail else prior_member.tail.end
172
+ let gap_end if gap_moved_semicolon then prior_member.tail.start else first_token.start
173
+ let gap_padded { value overlapping(layout_class_line_gap, gap_start, gap_end).layout_class_blank; contains true }
174
+ let gap_mode if count(enforce) == 0 then style else find_last(enforce, e => (e.prev == "*" || e.prev == prior_member.kind) && (e.next == "*" || e.next == retained_member.kind)).blankLine
175
+ let gap_single_line position(prior_member.start).line == position(gap_start).line
176
+ when gap_mode == "never" && gap_padded || gap_mode == "always" && !gap_padded && !(exceptAfterSingleLine && gap_single_line)
177
+ report at offset
178
+ warn if gap_padded then "Unexpected blank line between class members." else "Expected blank line between class members."
179
+ let edit layout_member_gap_fix(source, gap_start, gap_end, gap_padded)
180
+ fix at (edit ?? { start: gap_start, end: gap_end }) if edit != null as edit.text
181
+ }
182
+
183
+ in ts_open_brace rule es-space-before-blocks {
184
+ option style "always" | "never" = "always" "spaces before blocks"
185
+ option functions "always" | "never" | "off" | null = null "function body policy"
186
+ option keywords "always" | "never" | "off" | null = null "statement body policy"
187
+ option classes "always" | "never" | "off" | null = null "class body policy"
188
+ option modules "always" | "never" | "off" | null = null "module body policy"
189
+ when parent is (ts_block, ts_switch, ts_class, ts_interface, ts_enum)
190
+ when !space_before.line_break
191
+ when functions != null || keywords != null || classes != null || modules != null || space_before.spaced != (style == "always")
192
+ when previous_token != null
193
+ let block_class parent is (ts_class, ts_interface, ts_enum)
194
+ let block_function parent.layout_block_function.nonempty ?? false
195
+ let block_previous token_neighbours.before.text
196
+ when block_previous != "=>"
197
+ when !(contains(["try", "do", "else", "finally", "catch", "static"], block_previous) && !block_class && !block_function)
198
+ when !(block_previous == ":" && (parent.layout_block_case.nonempty ?? false))
199
+ let block_mode if block_class then classes.nonempty ?? style else if block_function then functions.nonempty ?? style else if parent.layout_block_module.nonempty ?? false then modules.nonempty ?? style else keywords.nonempty ?? style
200
+ when block_mode != "off" && space_before.spaced != (block_mode == "always")
201
+ fix at { start: if block_mode == "always" then offset else token_neighbours.before.end, end: offset } if block_mode == "always" || !space_before.comments as if block_mode == "always" then " " else ""
202
+ warn if space_before.spaced then "Unexpected space before opening brace." else "Missing space before opening brace."
203
+ }
204
+
205
+ in ts_open_brace rule es-block-spacing:after {
206
+ @layout_block_space_options
207
+ when parent is (ts_block, ts_static_block, ts_switch, ts_enum)
208
+ when space_after.comments || next_token not ts_close_brace
209
+ when !(style == "never" && parent.layout_block_line_comment)
210
+ let block_space layout_comma_space(space_after.text, false)
211
+ when layout_same_line(block_space) && (block_space != "") != (style == "always")
212
+ report at if block_space != "" then { start: end_offset, end: end_offset + byte_len(block_space) } else { start: offset, end: end_offset }
213
+ fix space_after as if style == "always" then " " else ""
214
+ warn if style == "always" then "Requires a space after '{{'." else "Unexpected space(s) after '{{'."
215
+ }
216
+
217
+ in ts_close_brace rule es-block-spacing:before {
218
+ @layout_block_space_options
219
+ when parent is (ts_block, ts_static_block, ts_switch, ts_enum)
220
+ when space_before.comments || previous_token not ts_open_brace
221
+ when !(style == "never" && parent.layout_block_line_comment)
222
+ let block_space layout_comma_space(space_before.text, true)
223
+ when layout_same_line(block_space) && (block_space != "") != (style == "always")
224
+ report at if block_space != "" then { start: offset - byte_len(block_space), end: offset } else { start: offset, end: end_offset }
225
+ fix space_before as if style == "always" then " " else ""
226
+ warn if style == "always" then "Requires a space before '}}'." else "Unexpected space(s) before '}}'."
227
+ }
228
+
229
+ in ts_colon rule es-switch-colon-spacing:before {
230
+ @layout_switch_colon_options
231
+ when parent is ts_case && previous_token != null
232
+ when !space_before.line_break && before != space_before.spaced
233
+ fix at { start: space_before.start, end: if before then space_before.start else offset } if !space_before.comments as if before then " " else ""
234
+ warn if before then "Expected space(s) before this colon." else "Unexpected space(s) before this colon."
235
+ }
236
+
237
+ in ts_colon rule es-switch-colon-spacing:after {
238
+ @layout_switch_colon_options
239
+ when parent is ts_case && next_token != null && next_token not ts_close_brace
240
+ when !space_after.line_break && after != space_after.spaced
241
+ fix at { start: end_offset, end: if after then end_offset else space_after.end } if !space_after.comments as if after then " " else ""
242
+ warn if after then "Expected space(s) after this colon." else "Unexpected space(s) after this colon."
243
+ }
244
+
245
+ @macro layout_brace_options {
246
+ option style "1tbs" | "stroustrup" | "allman" = "1tbs" "brace layout"
247
+ option allowSingleLine boolean = false "allow single-line block bodies"
248
+ when parent is (ts_block, ts_static_block, ts_switch, ts_class)
249
+ when !(parent.layout_brace_excluded.nonempty ?? false)
250
+ }
251
+ in ts_open_brace rule es-brace-style:open {
252
+ @layout_brace_options
253
+ when previous_token != null
254
+ when if style == "allman" then !space_before.line_break && !(allowSingleLine && parent.layout_brace_single) else space_before.line_break
255
+ fix at { start: if style == "allman" then offset else token_neighbours.before.end, end: offset } if style == "allman" || !space_before.comments as if style == "allman" then "\n" else " "
256
+ warn if style == "allman" then "Opening curly brace appears on the same line as controlling statement." else "Opening curly brace does not appear on the same line as controlling statement."
257
+ }
258
+ in ts_open_brace rule es-brace-style:body {
259
+ @layout_brace_options
260
+ when next_token not ts_close_brace
261
+ when !space_after.line_break && !(allowSingleLine && parent.layout_brace_single)
262
+ fix at { start: end_offset, end: end_offset } as "\n"
263
+ warn "Statement inside of curly braces should be on next line."
264
+ }
265
+ in ts_close_brace rule es-brace-style:close {
266
+ @layout_brace_options
267
+ when previous_token not ts_open_brace
268
+ when !space_before.line_break && !(allowSingleLine && parent.layout_brace_single)
269
+ fix at { start: offset, end: offset } as "\n"
270
+ warn "Closing curly brace should be on the same line as opening curly brace or on the line after the previous block."
271
+ }
272
+ in ts_close_brace rule es-brace-style:keyword {
273
+ @layout_brace_options
274
+ when parent.layout_brace_keyword.nonempty ?? false
275
+ when contains(["else", "catch", "finally"], token(node, "after").text)
276
+ when space_after.line_break == (style == "1tbs")
277
+ fix at { start: end_offset, end: if style == "1tbs" then token(node, "after").start else end_offset } if style != "1tbs" || !space_after.comments as if style == "1tbs" then " " else "\n"
278
+ warn if style == "1tbs" then "Closing curly brace does not appear on the same line as the subsequent block." else "Closing curly brace appears on the same line as the subsequent block."
279
+ }
280
+
281
+ }
@@ -0,0 +1,79 @@
1
+ # .ts/layout/calls: calls layout rules.
2
+
3
+ import .ts/layout/shared
4
+ import .ts/format
5
+
6
+ define {
7
+
8
+ in ts_open_paren rule es-space-before-function-paren {
9
+ option style "always" | "never" = "always" "spaces before function parameter lists"
10
+ option named "always" | "never" | "ignore" | null = null "named function policy"
11
+ option anonymous "always" | "never" | "ignore" | null = null "anonymous function policy"
12
+ option asyncArrow "always" | "never" | "ignore" | null = null "async arrow policy"
13
+ option catch "always" | "never" | "ignore" | null = null "catch policy"
14
+ when parent is (ts_function, ts_method, ts_arrow, ts_try) && previous_token != null
15
+ let function_named parent is ts_method || parent.name != null
16
+ when !(parent is ts_function && !function_named && contains(parent.modifier, "*"))
17
+ when parent not ts_arrow || token_neighbours.before.text == "async"
18
+ when parent not ts_try || parent.parameter != null
19
+ let paren_mode if parent is ts_try then catch.nonempty ?? style else if parent is ts_arrow then asyncArrow.nonempty ?? style else if function_named then named.nonempty ?? style else anonymous.nonempty ?? style
20
+ when paren_mode != "ignore" && !(paren_mode == "always" && !function_named && parent.type_parameter != null)
21
+ when space_before.spaced != (paren_mode == "always")
22
+ report at if space_before.spaced then { start: space_before.start, end: offset } else { start: offset, end: offset + 1 }
23
+ warn if space_before.spaced then "Unexpected space before function parentheses." else "Missing space before function parentheses."
24
+ let paren_gap { start: token_neighbours.before.end, end: offset }
25
+ let paren_comments { value overlapping(ts_comment, paren_gap.start, paren_gap.end); filter (comment) paren_gap.start <= comment.offset && comment.end_offset <= paren_gap.end }
26
+ let paren_fixed format_gap_text(paren_gap, paren_mode == "always", paren_comments)
27
+ let original { offset: paren_gap.start, end_offset: paren_gap.end, text: byte_slice(source, paren_gap.start, paren_gap.end) }
28
+ fix at paren_gap if !any(paren_comments, comment => starts_with(comment.text, "//")) && expression_fix_retains_comments(original, paren_fixed) as paren_fixed
29
+ }
30
+
31
+
32
+ in ts_call, ts_new let layout_invocation_end { value type_argument.end_offset; last; nonempty } ?? callee.end_offset
33
+ in ts_import_call let layout_invocation_end offset + 6
34
+ in ts_call let layout_invocation_optional optional != null
35
+ in ts_new, ts_import_call let layout_invocation_optional false
36
+ fn layout_invocation_chain(gap, allow_newlines, before, after) = {
37
+ value joined: if allow_newlines then gap else re_replace(gap, /\r\n|[\n\r\u{2028}\u{2029}]/, " ")
38
+ value prefix: if before then (if re_test(joined, /^\s/) then joined else " " joined) else trim_start(joined)
39
+ value if after then (if re_test(prefix, /\s$/) then prefix else prefix " ") else trim_end(prefix)
40
+ }
41
+ in ts_open_paren rule es-function-call-spacing {
42
+ option mode "always" | "never" = "never" "spacing before invocation parentheses"
43
+ option allowNewlines boolean = false "allow line breaks before invocation parentheses"
44
+ option chain_before boolean = true "space before optional invocation punctuation"
45
+ option chain_after boolean = true "space after optional invocation punctuation"
46
+ when parent is (ts_call, ts_new, ts_import_call)
47
+ when offset >= parent.layout_invocation_end
48
+ let invocation_optional parent.layout_invocation_optional
49
+ let invocation_prefix if invocation_optional then ts_token_space(source, parent.optional.node, true) else space_before
50
+ let invocation_spaced invocation_prefix.spaced || space_before.spaced
51
+ let bad_newline mode == "always" && !allowNewlines && (invocation_prefix.line_break || space_before.line_break)
52
+ let bad_space if mode == "never" then invocation_spaced
53
+ else if invocation_optional then invocation_prefix.spaced != chain_before || space_before.spaced != chain_after
54
+ else !invocation_spaced
55
+ when bad_newline || bad_space
56
+ report at { start: invocation_prefix.start, end: offset }
57
+ warn if bad_newline then "Unexpected newline between function name and paren."
58
+ else if mode == "never" || invocation_optional && (!chain_before && invocation_prefix.spaced || !chain_after && space_before.spaced) then "Unexpected whitespace between function name and paren."
59
+ else "Missing space between function name and paren."
60
+ let invocation_insert mode == "always" && !invocation_optional && !invocation_spaced
61
+ let invocation_gap byte_slice(source, invocation_prefix.start, offset)
62
+ let invocation_fix if mode == "never" then (if invocation_optional then "?." else "")
63
+ else if invocation_optional then layout_invocation_chain(invocation_gap, allowNewlines, chain_before, chain_after) else " "
64
+ fix at { start: if invocation_insert then offset else invocation_prefix.start, end: offset } if invocation_insert || !(invocation_prefix.comments || space_before.comments) as invocation_fix
65
+ }
66
+
67
+ # Empty constructor argument lists belong to this new expression, not its callee.
68
+ in ts_new rule es-new-parens {
69
+ option style "always" | "never" = "always" "empty constructor parentheses"
70
+ let constructor_open opener.ts_open_paren.offset
71
+ let constructor_close closer.ts_close_paren.offset
72
+ when count(argument) == 0
73
+ when (constructor_open != null && constructor_close != null && callee.end_offset < end_offset) != (style == "always")
74
+ report at offset
75
+ fix as if style == "always" then text "()" else "(" byte_slice(text, 0, constructor_open - offset) byte_slice(text, constructor_open - offset + 1, constructor_close - offset) byte_slice(text, constructor_close - offset + 1) ")"
76
+ warn if style == "always" then "Missing '()' invoking a constructor." else "Unnecessary '()' invoking a constructor with no arguments."
77
+ }
78
+
79
+ }
@@ -0,0 +1,213 @@
1
+ # .ts/layout/expressions: expressions layout rules.
2
+
3
+ import .ts/layout/shared
4
+ import .ts/expression-fixes
5
+
6
+ define {
7
+
8
+ @macro layout_computed_space_options {
9
+ option style "always" | "never" = "never" "spaces inside computed property brackets"
10
+ option enforceForClassMembers boolean = true "check class member keys"
11
+ }
12
+ group computed_spacing_owners ts_computed_key, ts_index, ts_indexed_access
13
+ in ts_computed_key, ts_index, ts_indexed_access let computed_spacing_enabled true
14
+
15
+ in ts_method let layout_class_method parent is ts_class
16
+ in ts_computed_key let layout_computed_class parent is ts_class_property || (parent.layout_class_method.nonempty ?? false)
17
+ in ts_rest, ts_spread let layout_spread_property parent is (ts_object, ts_object_pattern)
18
+
19
+ in ts_function let generator_star_named name != null
20
+ in ts_method let generator_star_shorthand parent is ts_object
21
+ @macro layout_yield_star_options {
22
+ @ts_token_context
23
+ option mode "before" | "after" | "both" | "neither" = "after" "spacing around yield star"
24
+ option before boolean | null = null "override space before star"
25
+ option after boolean | null = null "override space after star"
26
+ }
27
+ @macro layout_generator_star_options {
28
+ @ts_token_context
29
+ option before boolean = true "space before generator star"
30
+ option after boolean = false "space after generator star"
31
+ when text == "*"
32
+ let star_before_space layout_comma_space(space_before.text, true)
33
+ let star_after_space layout_comma_space(space_after.text, false)
34
+ each star_before in [true, false] {
35
+ when if star_before then previous_token != null && (parent not ts_method || offset != token_neighbours.parent_start) else next_token != null
36
+ when ((if star_before then star_before_space else star_after_space) != "") != (if star_before then before else after)
37
+ fix at { start: if star_before then offset - byte_len(star_before_space) else end_offset, end: if star_before then offset else end_offset + byte_len(star_after_space) } as if (if star_before then before else after) then " " else ""
38
+ warn when {
39
+ star_before then if star_before_space != "" then "Unexpected space before *." else "Missing space before *."
40
+ star_after_space != "" then "Unexpected space after *."
41
+ else then "Missing space after *."
42
+ }
43
+ }
44
+ }
45
+
46
+ group infix_spacing_owners ts_binary, ts_logical, ts_assignment, ts_union, ts_intersection,
47
+ ts_conditional, ts_conditional_type, ts_assignment_pattern, ts_declarator, ts_parameter,
48
+ ts_class_property, ts_enum_member, ts_type_alias, ts_type_parameter
49
+ in infix_spacing_owners let infix_spacing_enabled true
50
+ in ts_binary.operator, ts_logical.operator, ts_assignment.operator, ts_equals, ts_question, ts_colon, ts_punctuator rule es-space-infix-ops {
51
+ @ts_token_context
52
+ option int32Hint boolean = false "allow terminal |0 conversion"
53
+ let infix_types parent is (ts_union, ts_intersection)
54
+ when parent is infix_spacing_owners && parent.infix_spacing_enabled
55
+ when parent is (ts_binary, ts_logical, ts_assignment)
56
+ || infix_types && contains(["|", "&"], text)
57
+ || parent is (ts_conditional, ts_conditional_type) && contains(["?", ":"], text)
58
+ || parent is (ts_assignment_pattern, ts_declarator, ts_parameter, ts_class_property, ts_enum_member, ts_type_alias, ts_type_parameter) && text == "="
59
+ when !(int32Hint && parent is (ts_binary, ts_logical, ts_assignment, ts_assignment_pattern) && ends_with(parent_text, "|0"))
60
+ when !(space_before.spaced && space_after.spaced)
61
+ warn "Operator '{text}' must be spaced."
62
+ fix as (if !space_before.spaced then " " else "") text (if !space_after.spaced then " " else "")
63
+ }
64
+
65
+ in ts_parenthesized let layout_property_name expression.layout_property_name.nonempty ?? expression
66
+ in ts_parenthesized_type let layout_property_name type.layout_property_name.nonempty ?? type
67
+ in ts_member let layout_property_name property
68
+ in ts_index, ts_indexed_access let layout_property_name index.layout_property_name.nonempty ?? index
69
+ in ts_qualified_name let layout_property_name right
70
+ in ts_member let layout_property_spaced {
71
+ value dots: if node.optional != null then 2 else 1
72
+ value property.offset - object.end_offset > dots
73
+ && (object.space_after.spaced || property.space_before.spaced)
74
+ }
75
+ in ts_qualified_name let layout_property_spaced right.offset - left.end_offset > 1
76
+ && (left.space_after.spaced || right.space_before.spaced)
77
+ in ts_member let layout_property_same_line position(object.end_offset).line == position(property.offset).line
78
+ in ts_index let layout_property_same_line position(object.end_offset).line == position(index.offset).line
79
+ fn layout_property_gap_clean(start, end) = { value overlapping(ts_comment, start, end); count } == 0
80
+ in ts_member, ts_qualified_name rule es-no-whitespace-before-property {
81
+ when layout_property_spaced
82
+ when layout_property_same_line.nonempty ?? true
83
+ let property_left if is ts_member then object.node else left.node
84
+ let property_right if is ts_member then property.node else right.node
85
+ let property_decimal unparenthesised_node(property_left)
86
+ let property_fixable layout_property_gap_clean(property_left.end_offset, property_right.offset)
87
+ && !(is ts_member && node.optional == null && property_decimal is ts_number && re_test(property_decimal.text, /^(?:0|0[0-7]*[89]\d*|[1-9](?:_?\d)*)$/))
88
+ fix at { start: property_left.end_offset, end: property_right.offset } if property_fixable as if is ts_member && node.optional != null then "?." else "."
89
+ warn "Unexpected whitespace before property {layout_property_name}."
90
+ }
91
+ in ts_index, ts_indexed_access rule es-no-whitespace-before-property:computed {
92
+ when opener.ts_open_bracket.offset > object.end_offset
93
+ when object.space_after.spaced || opener.ts_open_bracket.space_before.spaced
94
+ when layout_property_same_line.nonempty ?? true
95
+ let property_fix_end opener.ts_open_bracket.offset
96
+ fix at { start: object.end_offset, end: property_fix_end } if layout_property_gap_clean(object.end_offset, property_fix_end) as if is ts_index && node.optional != null then "?." else ""
97
+ warn "Unexpected whitespace before property {layout_property_name}."
98
+ }
99
+ in ts_dot rule es-no-whitespace-before-property:import {
100
+ when parent is ts_import_type && parent.qualifier != null
101
+ when space_before.spaced || space_after.spaced
102
+ report at parent.offset
103
+ let property_fix_start token_neighbours.before.end
104
+ fix at { start: property_fix_start, end: parent.qualifier.offset } if layout_property_gap_clean(property_fix_start, parent.qualifier.offset) as "."
105
+ warn "Unexpected whitespace before property {parent.qualifier}."
106
+ }
107
+
108
+ in ts_open_bracket rule es-computed-property-spacing:after {
109
+ @layout_computed_space_options
110
+ when parent is computed_spacing_owners && parent.computed_spacing_enabled
111
+ when enforceForClassMembers || !(parent is ts_computed_key) || !(parent.layout_computed_class.nonempty ?? false)
112
+ let computed_space layout_comma_space(space_after.text, false)
113
+ when layout_same_line(computed_space) && (computed_space != "") != (style == "always")
114
+ report at if computed_space != "" then { start: end_offset, end: end_offset + byte_len(computed_space) } else { start: offset, end: end_offset }
115
+ fix space_after as if style == "always" then " " else ""
116
+ warn if style == "always" then "A space is required after '['." else "There should be no space after '['."
117
+ }
118
+
119
+ in ts_close_bracket rule es-computed-property-spacing:before {
120
+ @layout_computed_space_options
121
+ when parent is computed_spacing_owners && parent.computed_spacing_enabled
122
+ when enforceForClassMembers || !(parent is ts_computed_key) || !(parent.layout_computed_class.nonempty ?? false)
123
+ let computed_space layout_comma_space(space_before.text, true)
124
+ when layout_same_line(computed_space) && (computed_space != "") != (style == "always")
125
+ report at if computed_space != "" then { start: offset - byte_len(computed_space), end: offset } else { start: offset, end: end_offset }
126
+ fix space_before as if style == "always" then " " else ""
127
+ warn if style == "always" then "A space is required before ']'." else "There should be no space before ']'."
128
+ }
129
+
130
+ in ts_yield.operator rule es-yield-star-spacing:before {
131
+ @layout_yield_star_options
132
+ when text == "*" && previous_token != null
133
+ when space_before.spaced != (before.nonempty ?? mode == "before" || mode == "both")
134
+ let required before.nonempty ?? mode == "before" || mode == "both"
135
+ fix at { start: if required then offset else space_before.start, end: offset } if required || !space_before.comments as if required then " " else ""
136
+ warn if space_before.spaced then "Unexpected space before *." else "Missing space before *."
137
+ }
138
+
139
+ in ts_yield.operator rule es-yield-star-spacing:after {
140
+ @layout_yield_star_options
141
+ when text == "*" && next_token != null
142
+ when space_after.spaced != (after.nonempty ?? mode == "after" || mode == "both")
143
+ let required after.nonempty ?? mode == "after" || mode == "both"
144
+ fix at { start: end_offset, end: if required then end_offset else space_after.end } if required || !space_after.comments as if required then " " else ""
145
+ warn if space_after.spaced then "Unexpected space after *." else "Missing space after *."
146
+ }
147
+
148
+ in ts_function.modifier rule es-generator-star-spacing:named {
149
+ when parent.generator_star_named
150
+ @layout_generator_star_options
151
+ }
152
+ in ts_function.modifier rule es-generator-star-spacing:anonymous {
153
+ when !parent.generator_star_named
154
+ @layout_generator_star_options
155
+ }
156
+ in ts_method.modifier rule es-generator-star-spacing:method {
157
+ when !parent.generator_star_shorthand
158
+ @layout_generator_star_options
159
+ }
160
+ in ts_method.modifier rule es-generator-star-spacing:shorthand {
161
+ when parent.generator_star_shorthand
162
+ @layout_generator_star_options
163
+ }
164
+
165
+ in ts_unary, ts_update let layout_unary_prefix operator.offset == offset
166
+ in ts_new, ts_await, ts_yield let layout_unary_prefix true
167
+ in ts_non_null let layout_unary_prefix false
168
+ in ts_unary, ts_update, ts_await, ts_yield let layout_unary_operand argument.node
169
+ in ts_new let layout_unary_operand callee.node
170
+ in ts_non_null let layout_unary_operand expression.node
171
+ in ts_yield let layout_unary_delegate operator == "*"
172
+ in ts_unary let layout_unary_double_bang operator == "!" && argument is ts_unary && argument.operator == "!"
173
+ in ts_unary, ts_update, ts_new, ts_await, ts_yield, ts_non_null rule es-space-unary-ops {
174
+ option words boolean = true "spaces after word operators"
175
+ option nonwords boolean = false "spaces around symbolic unary operators"
176
+ option overrides { [key: string]: boolean } = {} "spacing by operator"
177
+ when layout_unary_operand != null && layout_unary_delegate != true
178
+ let unary_prefix layout_unary_prefix
179
+ let unary_token if unary_prefix then first_token else last_token
180
+ let unary_argument if unary_prefix then layout_unary_operand.first_token else layout_unary_operand.last_token
181
+ let unary_operator unary_token.text
182
+ let unary_word contains(["typeof", "void", "delete", "new", "await", "yield"], unary_operator)
183
+ let unary_separated if unary_prefix then unary_token.end != unary_argument.start else unary_argument.end != unary_token.start
184
+ let unary_joinable unary_argument not (ts_identifier, ts_keyword, ts_this, ts_super, ts_boolean, ts_null, ts_yield, ts_number, ts_bigint)
185
+ let unary_override if is ts_non_null then "ts-non-null" else unary_operator
186
+ let wants_space { value overrides; get unary_override; nonempty } ?? { value overrides; get unary_operator; nonempty } ?? if unary_word then words else nonwords
187
+ when wants_space != unary_separated
188
+ when !(wants_space && layout_unary_double_bang == true) && !(unary_word && !wants_space && !unary_joinable)
189
+ warn when {
190
+ unary_word then if wants_space then "Unary word operator '{unary_operator}' must be followed by whitespace." else "Unexpected space after unary word operator '{unary_operator}'."
191
+ wants_space then if unary_prefix then "Unary operator '{unary_operator}' must be followed by whitespace." else "Space is required before unary operator '{unary_operator}'."
192
+ unary_prefix then "Unexpected space after unary operator '{unary_operator}'."
193
+ else then "Unexpected space before unary operator '{unary_operator}'."
194
+ }
195
+ let left_token if unary_prefix then unary_token else unary_argument
196
+ let right_token if unary_prefix then unary_argument else unary_token
197
+ let gap byte_slice(source, left_token.end, right_token.start)
198
+ let joinable expression_fix_separator(left_token.text, right_token.text) == ""
199
+ fix at { start: left_token.end, end: right_token.start } if re_test(gap, /^\s*$/) && (wants_space || joinable) as if wants_space then " " else ""
200
+ }
201
+
202
+ in ts_ellipsis rule es-rest-spread-spacing {
203
+ option style "always" | "never" = "never" "spacing after rest and spread"
204
+ when parent is (ts_rest, ts_spread) && next_token != null
205
+ let spread_space layout_comma_space(space_after.text, false)
206
+ when (spread_space != "") != (style == "always")
207
+ let spread_kind (if parent is ts_rest then "rest" else "spread") (if parent.layout_spread_property then " property" else "")
208
+ report at if style == "always" then { start: offset, end: end_offset } else { start: end_offset, end: space_after.end }
209
+ fix space_after as if style == "always" then " " else ""
210
+ warn if style == "always" then "Expected whitespace after {spread_kind} operator." else "Unexpected whitespace after {spread_kind} operator."
211
+ }
212
+
213
+ }