@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,308 @@
1
+ # .ts/style/signatures: internal part of .ts/style.
2
+ import .ts/expression-fixes
3
+
4
+ define {
5
+
6
+ fn signature_key_shape(key, name) = when {
7
+ key is ts_private_name then "private"
8
+ key is (ts_identifier, ts_string, ts_number, ts_bigint, ts_boolean, ts_null) then if starts_with(name, '"') then "quoted" else "normal"
9
+ else then "expression"
10
+ }
11
+ in ts_function let overload_identity if name != null then { name: name, shape: "normal", static: null, call: false } else null
12
+ in ts_method, ts_method_signature let overload_identity { name: style_member_name, shape: { value key.member_key_node.nonempty ?? key.node; signature_key_shape style_member_name }, static: if is ts_method then is_static else null, call: false }
13
+ in ts_call_signature let overload_identity { name: if kind == "new" then "new" else "call", shape: "normal", static: null, call: kind != "new" }
14
+ in ts_export forward overload_identity from declaration
15
+ in ts_function, ts_method, ts_method_signature, ts_call_signature, ts_export let overload_entry { identity: overload_identity, start: offset }
16
+ in ts_program, ts_block, ts_class, ts_interface, ts_type_literal let overload_entries [] children.overload_entry
17
+ fn earlier_overload(entries, identity, start) = any(entries, e => e.start < start && e.identity == identity)
18
+ fn overload_report_site(n) = {
19
+ value terminator: n.token_neighbours.after
20
+ value bodiless: n is (ts_method_signature, ts_call_signature) || n.body == null
21
+ value separated: bodiless && terminator != null && terminator.text == ";" && terminator.start == n.end_offset
22
+ value { start: n.offset, end: if separated then terminator.end else n.end_offset }
23
+ }
24
+ in ts_function, ts_method, ts_method_signature, ts_call_signature, ts_export rule es-adjacent-overload-signatures {
25
+ when parent is (ts_program, ts_block, ts_class, ts_interface, ts_type_literal)
26
+ when overload_identity != null && overload_identity != previous.overload_identity
27
+ when earlier_overload(parent.overload_entries, overload_identity, offset)
28
+ let identity overload_identity
29
+ report at overload_report_site(node)
30
+ warn "All {if identity.static == true then 'static ' else ''}{identity.name} signatures should be adjacent."
31
+ }
32
+
33
+ # `new (): I` in interface `I` (type arguments aside), and `constructor()`
34
+ # signatures in interfaces and type literals.
35
+ in ts_call_signature rule es-no-misused-new {
36
+ when parent is ts_interface && kind == "new" && { value return_type.nonempty ?? ""; re_replace /\s*<[\s\S]*$/ "" } == ts_interface.name
37
+ report at { start: offset, end: signature_source_end }
38
+ warn "Interfaces cannot be constructed, only classes."
39
+ }
40
+ in ts_method_signature rule es-no-misused-new:constructor {
41
+ when key == "constructor"
42
+ report at { start: offset, end: signature_source_end }
43
+ warn "Interfaces cannot be constructed, only classes."
44
+ }
45
+ in ts_method rule es-no-misused-new:class {
46
+ when key == "new" && body == null && return_type == ts_class.name
47
+ warn "Class cannot have method named `new`."
48
+ }
49
+
50
+ # ---- functions -----------------------------------------------------------------------
51
+ in ts_function_type let is_function_type kind != "new"
52
+ in ts_property_signature let signature_callable_node type.node
53
+ in ts_method_signature, ts_property_signature let signature_source_key
54
+ (if contains(modifier, "readonly") then "readonly " else "") key.text (if optional != null then "?" else "")
55
+ in ts_method_signature, ts_property_signature, ts_call_signature let signature_source_end
56
+ if contains([";", ","], token_neighbours.after.text) then token_neighbours.after.end else end_offset
57
+ in ts_method_signature, ts_property_signature let signature_source_delimiter
58
+ if contains([";", ","], token_neighbours.after.text) then token_neighbours.after.text else ""
59
+ group signature_separator ts_semicolon, ts_comma
60
+ in ts_method_signature let signature_source_next {
61
+ value delimiters: overlapping(signature_separator, end_offset, signature_source_end)
62
+ value delimiter: first(delimiters)
63
+ value delimiter.token_neighbours.after.start ?? token_neighbours.after.start
64
+ }
65
+ fn signature_callable_source(source, n) = {
66
+ value last_type: last(n.type_parameter.nodes)
67
+ value first_parameter: first(n.parameter.nodes)
68
+ value from: last_type.end_offset ?? n.key.end_offset ?? n.offset
69
+ value until: first_parameter.offset ?? n.return_type.offset ?? n.end_offset
70
+ value opening_tokens: overlapping(ts_open_paren, from, until)
71
+ value opening: first(opening_tokens)
72
+ value parameters: if count(n.parameter) == 0 then "()" else byte_slice(source, opening.offset, opening.paired_token.end)
73
+ value first_type: first(n.type_parameter.nodes)
74
+ value closing_tokens: if last_type != null then overlapping(ts_close_angle, last_type.end_offset, opening.offset) else []
75
+ value closing: first(closing_tokens)
76
+ value type_parameters: if first_type != null then byte_slice(source, first_type.token_neighbours.before.start, closing.end_offset) else ""
77
+ value { parameters: type_parameters parameters, result: n.return_type.text ?? "any", return_colon: n.signature_return_colon }
78
+ }
79
+ in ts_call_signature let signature_return_colon return_type.node.token_neighbours.before
80
+
81
+ # Only a call signature: a function type says it better.
82
+ in style_tree_node let function_this_offset when {
83
+ is ts_type_literal then null
84
+ is ts_type_keyword && text == "this" then offset
85
+ else then { value children.function_this_offset; compact; first }
86
+ }
87
+ in ts_type_reference let function_base name == "Function"
88
+ in ts_interface let function_type_container { name: name, kind: "Interface", eligible: count(member) == 1 && (count(extends) == 0 || count(extends) == 1 && (first([] extends) == "Function" || first([] extends.function_base) == true)), this: function_this_offset }
89
+ in ts_type_literal let function_type_container { name: "", kind: "Type literal", eligible: count(member) == 1, this: null }
90
+ in ts_interface let function_type_header_end {
91
+ value last_parameter: last(type_parameter.nodes)
92
+ value first_member: first(member.nodes)
93
+ value angle_tokens: if last_parameter != null then overlapping(ts_close_angle, last_parameter.end_offset, first_member.offset) else []
94
+ value first(angle_tokens).end_offset ?? name.end_offset
95
+ }
96
+ in ts_interface let function_type_name_start name.offset
97
+ fn function_type_comments(n) = {
98
+ value before: overlapping(ts_comment, n.token_neighbours.before.end, n.offset)
99
+ value after: overlapping(ts_comment, n.signature_source_end, n.parent.end_offset)
100
+ value before after
101
+ }
102
+ fn function_type_comment_prefix(comment, member, exported) = comment.text (if exported || starts_with(comment.text, "//") || position(comment.offset).js_line != position(member.offset).js_line then "\n" else " ")
103
+ fn function_type_fix(source, n, info) = {
104
+ value owner: n.parent.node
105
+ value exported: owner.parent is ts_export
106
+ value colon: signature_callable_source(source, n).return_colon
107
+ value raw: byte_slice(source, n.offset, colon.start) " =>" byte_slice(source, colon.end, n.end_offset)
108
+ value comments: function_type_comments(n)
109
+ value exported_comments: exported && count(comments) > 0
110
+ value body: if owner is ts_interface then {
111
+ value name: byte_slice(source, owner.function_type_name_start, owner.function_type_header_end)
112
+ value ending: if n.token_neighbours.after.text == ";" then ";" else ""
113
+ value "type " name " = " raw ending
114
+ } else {
115
+ value shape: { kind: "ts_function_type", precedence: 3 }
116
+ value required: expression_fix_parens(source, owner, shape)
117
+ value if required == null then null else expression_fix_wrap(raw, required > 0)
118
+ }
119
+ value replacement: if exported_comments then {
120
+ value prefix: { value comments; map (comment) function_type_comment_prefix(comment, n, true); join "" }
121
+ value prefix byte_slice(source, owner.parent.offset, owner.offset) body
122
+ } else { value comments; fold (body) (text, comment) function_type_comment_prefix(comment, n, false) text }
123
+ value start: if exported_comments then owner.parent.offset else owner.offset
124
+ value span: { offset: start, end_offset: owner.end_offset, text: byte_slice(source, start, owner.end_offset) }
125
+ value if info.this != null || exported && owner.parent.kind == "default" || body == null
126
+ || !expression_fix_retains_comments(span, replacement) then null
127
+ else { start: start, end: owner.end_offset, text: replacement }
128
+ }
129
+ in ts_call_signature rule es-prefer-function-type {
130
+ let container parent.function_type_container
131
+ when return_type != null && container.eligible == true
132
+ let this_offset container.this
133
+ report at if this_offset != null then { start: this_offset, end: this_offset + 4 } else offset
134
+ warn if container.this != null then "`this` refers to the function type '" container.name "', did you intend to use a generic `this` parameter like `<Self>(this: Self, ...) => Self` instead?" else container.kind " only has a call signature, you should use a function type instead."
135
+ let edit function_type_fix(source, node, container)
136
+ fix at { start: edit.start ?? offset, end: edit.end ?? offset } if edit != null as edit.text
137
+ }
138
+ fn signature_return_has_this(n) = n.return_type != null && {
139
+ value nodes: walk([n.return_type.node], child => child.children.nodes)
140
+ value any(nodes, child => child is ts_type_keyword && child.text == "this")
141
+ }
142
+ fn signature_in_namespace(n) = {
143
+ value ancestors: walk([n], ancestor => ancestor.parent.node)
144
+ value any(ancestors, ancestor => ancestor is ts_namespace)
145
+ }
146
+ in ts_interface, ts_type_literal let signature_method_groups {
147
+ value member.nodes
148
+ filter (member) member is ts_method_signature && member.kind != "get" && member.kind != "set"
149
+ group_by (member) member.signature_source_key
150
+ }
151
+ in ts_method_signature let signature_method_group {
152
+ value key: signature_source_key
153
+ value parent.signature_method_groups
154
+ find (group) group.key == key
155
+ then (group) group.items
156
+ }
157
+ in ts_method_signature let signature_property_edit {
158
+ value representative: first(signature_method_group)
159
+ value if representative == node then signature_property_replacement(source, node) else representative.signature_property_edit
160
+ }
161
+ fn signature_property_replacement(source, owner) = {
162
+ value key: owner.signature_source_key
163
+ value methods: owner.signature_method_group
164
+ value first_method: first(methods)
165
+ value last_method: last(methods)
166
+ value end: last_method.signature_source_end
167
+ value start: first_method.offset
168
+ value input: byte_slice(source, start, end)
169
+ value signatures: map(methods, method => signature_callable_source(source, method))
170
+ value functions: map(signatures, signature => signature.parameters " => " signature.result)
171
+ value combined: if count(methods) == 1 then first(functions) else { value functions; map (signature) "(" signature ")"; join " & " }
172
+ value edits: map(methods, method => if method.offset == last_method.offset then { start: method.offset, end: end, text: key ": " combined method.signature_source_delimiter } else { start: method.offset, end: method.signature_source_next, text: "" })
173
+ value replacement: expression_fix_edits(input, start, edits)
174
+ value span: { offset: start, end_offset: end, text: input }
175
+ value if !signature_in_namespace(owner) && !any(methods, method => signature_return_has_this(method)) && expression_fix_retains_comments(span, replacement) then { start: start, end: end, text: replacement } else null
176
+ }
177
+ fn signature_method_replacement(source, owner) = {
178
+ value callable: signature_callable_source(source, owner.signature_callable_node)
179
+ value replacement: owner.signature_source_key callable.parameters ": " callable.result owner.signature_source_delimiter
180
+ value end: owner.signature_source_end
181
+ value input: byte_slice(source, owner.offset, end)
182
+ value span: { offset: owner.offset, end_offset: end, text: input }
183
+ value if !contains(owner.modifier, "readonly") && expression_fix_retains_comments(span, replacement) then { start: owner.offset, end: end, text: replacement } else null
184
+ }
185
+ in ts_method_signature, ts_property_signature let signature_report_end {
186
+ value next: token(node, "after")
187
+ value if next != null && contains([";", ","], next.text) then next.end else end_offset
188
+ }
189
+ in ts_method_signature rule es-method-signature-style {
190
+ option mode ("method" | "property") = "property" "Preferred method signature notation."
191
+ when mode == "property" && kind != "get" && kind != "set"
192
+ report at { start: offset, end: signature_report_end }
193
+ warn "Shorthand method signature is forbidden. Use a function property instead."
194
+ let replacement signature_property_edit
195
+ fix at { start: replacement.start ?? offset, end: replacement.end ?? offset } if replacement != null as replacement.text
196
+ }
197
+ in ts_property_signature rule es-method-signature-style:property {
198
+ option mode ("method" | "property") = "property" "Preferred method signature notation."
199
+ when mode == "method" && type.is_function_type == true
200
+ report at { start: offset, end: signature_report_end }
201
+ warn "Function property signature is forbidden. Use a method shorthand instead."
202
+ let replacement signature_method_replacement(source, node)
203
+ fix at { start: replacement.start ?? offset, end: replacement.end ?? offset } if replacement != null as replacement.text
204
+ }
205
+
206
+ in ts_parenthesized_type let unified_inner_type type.unified_bare_type
207
+ in ts_parenthesized let unified_inner_type expression.unified_bare_type
208
+ in style_tree_node let unified_bare_type unified_inner_type.nonempty ?? text
209
+ in ts_identifier, ts_this let unified_parameter_name text
210
+ in ts_rest let unified_parameter_name argument.unified_parameter_name
211
+ in ts_parameter let unified_parameter { index: index, offset: offset, end: end_offset, line: line, name: name.unified_parameter_name, rest: name is ts_rest, optional: optional != null, type: type.unified_bare_type, raw_type: type, union: type.unified_union_members.nonempty ?? [] }
212
+ in ts_type_parameter let unified_type_parameter { name: name, constraint: constraint.unified_bare_type }
213
+ in ts_type_parameter let unified_type_name name
214
+ in ts_type_reference let unified_outer_type name
215
+ in ts_array_type, ts_parenthesized_type let unified_outer_type type.unified_outer_type
216
+ in ts_parameter let unified_outer_type type.unified_outer_type
217
+ in ts_union let unified_union_children { value [] type.unified_union_members; fold ([]) (members, child) members child }
218
+ in ts_parenthesized_type let unified_union_children type.unified_union_members
219
+ in ts_parenthesized let unified_union_children expression.unified_union_members
220
+ in style_tree_node let unified_union_members unified_union_children.nonempty ?? [{ text: text, wrap: is (ts_function_type, ts_conditional_type) }]
221
+ fn unified_union(members) = {
222
+ value members
223
+ fold ([]) (unique_members, member) if any(unique_members, previous_member => previous_member.text == member.text) then unique_members else unique_members [member]
224
+ map (member) if member.wrap then "(" member.text ")" else member.text
225
+ join " | "
226
+ }
227
+ fn unified_doc(prefix) = if re_test(prefix, /\/\*[\s\S]*?\*\/[ \t\r\n]*(?:\/\/[^\n]*(?:\n|$)[ \t\r\n]*)*$/) then re_replace(prefix, /[\s\S]*\/\*([\s\S]*?)\*\/[ \t\r\n]*(?:\/\/[^\n]*(?:\n|$)[ \t\r\n]*)*$/, "$1") else null
228
+ in ts_export let unified_doc unified_doc(text_before)
229
+ in ts_function, ts_method, ts_method_signature, ts_call_signature let unified_doc if parent is ts_export then parent.unified_doc else unified_doc(text_before)
230
+ in style_tree_node let unified_after_line line + { value text; split "\n"; count } - 1 + { value space_after.text.nonempty ?? ""; split "\n"; count } - 1
231
+ in style_tree_node let unified_next_offset space_after.end
232
+ in ts_method let unified_head { offset: key.unified_next_offset, line: key.unified_after_line }
233
+ in ts_function let unified_key if body == null then name.nonempty ?? "default" else null
234
+ in ts_method let unified_key if body == null && kind != "get" && kind != "set" && key != null then { kind: kind_name(key.node), name: key, static: contains([] modifier, "static") } else null
235
+ in ts_method_signature let unified_key if kind != "get" && kind != "set" && key != null then { kind: kind_name(key.node), name: key, static: false } else null
236
+ in ts_call_signature let unified_key kind.nonempty ?? "call"
237
+ in ts_function, ts_method, ts_method_signature, ts_call_signature let unified_signature if unified_key == null then null else { key: unified_key, node: node, offset: offset, head: unified_head.nonempty ?? { offset: offset, line: line }, parameters: [] parameter.unified_parameter, types: [] type_parameter.unified_type_parameter, result: return_type.unified_bare_type, outer: [] parameter.unified_outer_type }
238
+ in ts_export let unified_signature declaration.unified_signature
239
+ in ts_program, ts_block, ts_class, ts_interface, ts_type_literal let unified_signatures [] children.unified_signature
240
+ in ts_export let unified_signatures parent.unified_signatures
241
+ in ts_program, ts_class, ts_interface, ts_type_literal let unified_scope true
242
+ in ts_block let unified_scope parent is ts_namespace
243
+ in ts_export let unified_scope parent.unified_scope
244
+ in ts_class, ts_interface let unified_outer_names [] type_parameter.unified_type_name
245
+ in ts_program, ts_block, ts_type_literal let unified_outer_names []
246
+ in ts_export let unified_outer_names parent.unified_outer_names
247
+ fn unified_parameter_at(signature, at) = { value signature.parameters; slice at (at + 1); first }
248
+ fn unified_sigils(a, b) = a.rest == b.rest && a.optional == b.optional
249
+ fn unified_difference(a, b) = if unified_sigils(a, b) && !a.rest then { offset: b.offset, end: b.end, other_line: a.line, ending: " taking `" unified_union(a.union b.union) "`." } else null
250
+ fn unified_equal_length(a, b, differences) = when {
251
+ count(differences) == 0 then { offset: b.head.offset, end: b.node.end_offset, other_line: a.head.line, ending: " with identical parameters." }
252
+ count(differences) == 1 then {
253
+ value differences
254
+ first_differences: first
255
+ value at: first(differences).index
256
+ value unified_parameter_at: unified_parameter_at(b, at)
257
+ value unified_difference(first_differences, unified_parameter_at)
258
+ }
259
+ else then null
260
+ }
261
+ fn unified_shorter(short, long) = if last(short.parameters).rest == true || !all(short.parameters, p => p.type == unified_parameter_at(long, p.index).type) || !{
262
+ value result: count(short.parameters) + 1
263
+ value long.parameters
264
+ parameters_count: count
265
+ value slice(long.parameters, result, parameters_count)
266
+ all (p) p.optional || p.rest
267
+ } then null else { offset: last(long.parameters).offset, end: last(long.parameters).end, other_line: short.head.line, ending: " with " (if last(long.parameters).rest then "a rest" else "an optional") " parameter." }
268
+ fn unified_parameter_difference(a, b) = when {
269
+ count(a.parameters) == count(b.parameters) then {
270
+ value a.parameters
271
+ differences: filter (p) !{ value b; unified_parameter_at_3: unified_parameter_at p.index; value unified_sigils(p, unified_parameter_at_3) } || p.type != unified_parameter_at(b, p.index).type
272
+ value unified_equal_length(a, b, differences)
273
+ }
274
+ (first(a.parameters).name == "this") != (first(b.parameters).name == "this") then null
275
+ count(a.parameters) < count(b.parameters) then unified_shorter(a, b)
276
+ else then unified_shorter(b, a)
277
+ }
278
+ fn unified_comparison(a, b, outer_names) = if a.result != b.result || any(a.outer, name => contains(outer_names, name)) != any(b.outer, name => contains(outer_names, name)) || a.types != b.types || first(a.parameters).name == "this" && first(a.parameters).raw_type == "void" || first(b.parameters).name == "this" && first(b.parameters).raw_type == "void" then null else unified_parameter_difference(a, b)
279
+ fn unified_names(a, b) = {
280
+ value b.parameters
281
+ parameters_count: count
282
+ value slice(a.parameters, 0, parameters_count)
283
+ all (p) p.rest != unified_parameter_at(b, p.index).rest || p.name == unified_parameter_at(b, p.index).name
284
+ }
285
+ fn unified_pair(a, b, outer_names, group_count) = { difference: unified_comparison(a, b, outer_names), a: a, b: b, count: group_count }
286
+ group unified_signature_node ts_function, ts_method, ts_method_signature, ts_call_signature
287
+ in unified_signature_node rule es-unified-signatures {
288
+ option ignoreDifferentlyNamedParameters boolean = false "Ignore overloads with different parameter names."
289
+ option ignoreOverloadsWithDifferentJSDoc boolean = false "Ignore overloads with different block comments."
290
+ let signature unified_signature
291
+ when signature != null && parent.unified_scope == true
292
+ let group filter(parent.unified_signatures, candidate => candidate.key == signature.key)
293
+ let pairs {
294
+ value group
295
+ filter (candidate) candidate.offset > signature.offset
296
+ map (candidate) {
297
+ value group
298
+ group_count_2: count
299
+ value unified_pair(signature, candidate, parent.unified_outer_names, group_count_2)
300
+ }
301
+ }
302
+ each pair in filter(pairs, p => p.difference != null && (!ignoreDifferentlyNamedParameters || unified_names(p.a, p.b)) && (!ignoreOverloadsWithDifferentJSDoc || p.a.node.unified_doc == p.b.node.unified_doc)) {
303
+ report at { start: pair.difference.offset, end: pair.difference.end }
304
+ warn "{if pair.count == 2 then 'These overloads' else 'This overload and the one on line ' pair.difference.other_line} can be combined into one signature{pair.difference.ending}"
305
+ }
306
+ }
307
+
308
+ }
@@ -0,0 +1,62 @@
1
+ import .ts/expression-fixes
2
+ import .ts/style/type-imports
3
+ import .ts/modules
4
+
5
+ define {
6
+ in ts_export_specifier {
7
+ let export_key ts_module_name(local.node)
8
+ let export_source js_string(parent.from.text)
9
+ let export_classes name_classes(local.node, export_key, export_source, null)
10
+ let export_erased_namespace parent.from == null && {
11
+ value declarations: name_binding(local.node).declarations
12
+ value any(declarations, site => site.parent is ts_namespace)
13
+ && all(declarations, site => site.parent is (ts_interface, ts_type_alias)
14
+ || site.parent is ts_namespace && !ts_namespace_runtime(site.parent.node))
15
+ }
16
+ let export_type kind == null && (export_classes.value == false || export_erased_namespace)
17
+ && (export_classes.type == true || export_classes.query == true)
18
+ let export_value kind == null && export_classes.value == true && !export_erased_namespace
19
+ let export_name local.literal_value.value ?? local.text
20
+ let export_alias { value children.nodes; filter (item) item is ts_export_specifier.exported; first }
21
+ }
22
+ in ts_export {
23
+ let export_types { value specifier.nodes; filter (item) item.export_type }
24
+ let export_values { value specifier.nodes; filter (item) item.export_value }
25
+ let export_inline { value specifier.nodes; filter (item) item.kind == "type" }
26
+ let export_names { value export_types; map (item) item.export_name }
27
+ let export_source js_string(from.text)
28
+ let export_module if operator == "*" then module_exports(node, export_source) else null
29
+ let export_star operator == "*" && export_module != null && all(export_module, item => item.classes.value == false)
30
+ }
31
+ fn export_specifier_text(item) = if item.export_alias != null then item.local.text " as " item.export_alias.text else item.local.text
32
+ fn export_policy_text(source, n, inline) = if n.operator == "*" then {
33
+ value edits: [{ start: n.operator.offset, end: n.operator.offset, text: "type " }]
34
+ value expression_fix_edits(n.text, n.offset, edits)
35
+ } else if count(n.export_values) == 0 then {
36
+ value removals: map(n.export_inline, item => import_remove_type(source, item))
37
+ value edits: [{ start: n.first_token.end, end: n.first_token.end, text: " type" }] removals
38
+ value expression_fix_edits(n.text, n.offset, edits)
39
+ } else if inline then {
40
+ value edits: map(n.export_types, item => { start: item.offset, end: item.offset, text: "type " })
41
+ value expression_fix_edits(n.text, n.offset, edits)
42
+ } else {
43
+ value open: { value overlapping(ts_open_brace, n.first_token.end, n.end_offset); first }
44
+ value close: open.paired_token
45
+ value types: { value n.export_types n.export_inline; map (item) export_specifier_text(item); join ", " }
46
+ value values: { value n.export_values; map (item) export_specifier_text(item); join ", " }
47
+ value edits: [{ start: open.end_offset, end: close.start, text: " " values " " }]
48
+ value body: expression_fix_edits(n.text, n.offset, edits)
49
+ value source_suffix: if n.from != null then " from '" n.from.literal_value.value "'" else ""
50
+ value newline: if contains(source, "\r\n") then "\r\n" else "\n"
51
+ value "export type {{ " types " }}" source_suffix ";" newline body
52
+ }
53
+ in ts_export rule es-consistent-type-exports {
54
+ option fixMixedExportsWithInlineTypeSpecifier boolean = false "Use inline type specifiers in mixed exports."
55
+ when kind == null && (count(export_types) > 0 || export_star)
56
+ let replacement export_policy_text(source, node, fixMixedExportsWithInlineTypeSpecifier)
57
+ warn if count(export_values) == 0 then "All exports in the declaration are only used as types. Use `export type`."
58
+ else if count(export_types) == 1 then "Type export {first(export_names)} is not a value and should be exported using `export type`."
59
+ else "Type exports {word_list(export_names)} are not values and should be exported using `export type`."
60
+ fix at node if expression_fix_retains_comments(node, replacement) as replacement
61
+ }
62
+ }
@@ -0,0 +1,166 @@
1
+ # .ts/style/type-imports: import binding policy and atomic byte edits.
2
+ import .ts/expression-fixes
3
+ import .ts/modules
4
+ import .ts/import-attributes
5
+ import .ts/scoping/usage
6
+
7
+ define {
8
+
9
+ fn word_list(xs) = if count(xs) == 1 then first(xs) else { value xs; slice 0 -1; join ', ' } " and " last(xs)
10
+ in ts_program let import_decorator_metadata ts_compiler_option(node, "experimentalDecorators") == true && ts_compiler_option(node, "emitDecoratorMetadata") == true && { value descendants(ts_decorator); count } > 0
11
+ in ts_identifier {
12
+ let import_use if value_uses != null && value_uses > 0 then "value"
13
+ else if scoping_program.ts_usage_jsx_present && ts_usage_jsx_used(name_binding(node), scoping_program) then "value"
14
+ else if uses == null then null else if uses == 0 then "unused" else if value_uses == 0 then "type" else "value"
15
+ let type_only_name if import_use == "type" then '"' text '"' else null
16
+ }
17
+ in ts_import_specifier {
18
+ let import_use if kind != null then "inline" else if local != null then local.import_use else imported.import_use
19
+ let type_only_name if kind != null then null else if local != null then local.type_only_name else imported.type_only_name
20
+ }
21
+ in ts_import {
22
+ let import_uses compact([default_import.import_use, namespace.import_use] specifier.import_use)
23
+ let type_only_names compact([default_import.type_only_name, namespace.type_only_name] specifier.type_only_name)
24
+ let import_type_named kind == "type" && default_import == null && namespace == null && import_named_open != null
25
+ let import_has_attributes { value overlapping(ts_import_attributes, offset, end_offset); count } > 0
26
+ }
27
+ fn import_remove_type(source, n) = {
28
+ value gap: byte_slice(source, n.kind.end_offset, n.end_offset)
29
+ value whitespace: re_find(gap, /^\s*/)
30
+ value { start: n.kind.offset, end: n.kind.end_offset + byte_len(whitespace), text: "" }
31
+ }
32
+ fn import_inline_edits(items) = map(items, item => { start: item.offset, end: item.offset, text: "type " })
33
+ fn import_whole_edits(source, n) = {
34
+ value inline: filter(n.specifier.nodes, item => item.kind == "type")
35
+ value removals: map(inline, item => import_remove_type(source, item))
36
+ value [{ start: n.first_token.end, end: n.first_token.end, text: " type" }] removals
37
+ }
38
+ fn import_named_run(n, first_item, last_item, items) = {
39
+ value before: first_item.token_neighbours.before
40
+ value after: last_item.token_neighbours.after
41
+ value {
42
+ start: if before.text == "," then before.start else before.end,
43
+ end: if (first_item == first(items) || last_item == last(items)) && after.text == "," then after.end else last_item.end_offset,
44
+ text_start: before.end,
45
+ text_end: after.start
46
+ }
47
+ }
48
+ fn import_named_runs(n, items, moving) = if count(moving) == 0 then [] else if count(items) == count(moving) then [{
49
+ start: n.default_import.token_neighbours.after.start,
50
+ end: n.import_named_close.end,
51
+ text_start: n.import_named_open.end_offset,
52
+ text_end: n.import_named_close.start
53
+ }] else {
54
+ value starts: filter(moving, item => item.previous.import_use != "type")
55
+ value ends: filter(moving, item => item.next.import_use != "type")
56
+ value starts
57
+ zip ends (a, b) import_named_run(n, a, b, items)
58
+ }
59
+ fn import_named_text(source, runs) = { value runs; map (run) byte_slice(source, run.text_start, run.text_end); join "," }
60
+ fn import_named_removals(runs) = map(runs, run => { start: run.start, end: run.end, text: "" })
61
+ fn import_default_remove(source, n) = {
62
+ value comma: n.default_import.token_neighbours.after
63
+ value gap: byte_slice(source, comma.end, n.from.offset)
64
+ value whitespace: re_find(gap, /^\s*/)
65
+ value { start: n.default_import.offset, end: comma.end + byte_len(whitespace), text: "" }
66
+ }
67
+ fn import_default_text(source, n) = {
68
+ value byte_slice(source, n.default_import.offset, n.default_import.token_neighbours.after.start)
69
+ trim_end
70
+ }
71
+ in ts_node let import_merge_groups {
72
+ value node.body.nodes
73
+ filter (other) other is ts_import && other.import_type_named && !other.import_has_attributes
74
+ group_by (other) other.from.literal_value.value
75
+ }
76
+ fn import_merge_target(n) = {
77
+ value n.parent.import_merge_groups
78
+ find (group) group.key == n.from.literal_value.value
79
+ then (group) { value group.items; find (other) other != n }
80
+ }
81
+ fn import_merge_edit(target, text) = {
82
+ value before: target.import_named_close
83
+ value needs_comma: count(target.specifier) > 0 && last(target.specifier.nodes).token_neighbours.after.text != ","
84
+ value { start: before.start, end: before.start, text: (if needs_comma then "," else "") text }
85
+ }
86
+ fn import_split_edits(source, n, style) = {
87
+ value items: n.specifier.nodes
88
+ value moving: filter(items, item => item.import_use == "type")
89
+ value runs: import_named_runs(n, items, moving)
90
+ value named_text: import_named_text(source, runs)
91
+ value newline: if contains(source, "\r\n") then "\r\n" else "\n"
92
+ value target: if style == "separate-type-imports" && count(moving) > 0 then import_merge_target(n) else null
93
+ value named_prefix: if count(moving) == 0 || target != null then "" else
94
+ (if style == "inline-type-imports" then {
95
+ value moving
96
+ map (item) "type " item.text
97
+ join ", "
98
+ then (text) "import {{" text "}} from " n.from.text ";" newline
99
+ } else "import type {{" named_text "}} from " n.from.text ";" newline)
100
+ value namespace_type: n.namespace.import_use == "type"
101
+ value namespace_text: if namespace_type then { value byte_slice(source, n.default_import.token_neighbours.after.end, n.from.token_neighbours.before.start); trim } else ""
102
+ value namespace_prefix: if namespace_type then "import type " namespace_text " from " n.from.text ";" newline else ""
103
+ value all_type: !contains(n.import_uses, "value") && !contains(n.import_uses, "unused") && !contains(n.import_uses, "inline")
104
+ value default_type: n.default_import.import_use == "type"
105
+ value default_prefix: if default_type && !all_type then "import type " import_default_text(source, n) " from " n.from.text ";" newline else ""
106
+ value prefix: named_prefix namespace_prefix default_prefix
107
+ value edits: ((if prefix != "" then [{ start: n.offset, end: n.offset, text: prefix }] else [])
108
+ import_named_removals(runs)
109
+ (if target != null then [import_merge_edit(target, named_text)] else [])
110
+ (if namespace_type then [{ start: n.default_import.token_neighbours.after.start, end: n.namespace.end_offset, text: "" }] else [])
111
+ (if default_type then (if all_type then [{ start: n.first_token.end, end: n.first_token.end, text: " type" }] else [import_default_remove(source, n)]) else []))
112
+ value edits
113
+ }
114
+ fn import_policy_edits(source, n, style, preference) = if preference == "no-type-imports" then [import_remove_type(source, n)]
115
+ else if n.namespace != null && n.default_import == null then import_whole_edits(source, n)
116
+ else if n.default_import != null && count(n.specifier) == 0 && n.namespace == null then {
117
+ value edits: import_whole_edits(source, n)
118
+ value edits (if n.import_named_open != null then [{ start: n.default_import.token_neighbours.after.start, end: n.import_named_close.end, text: "" }] else [])
119
+ }
120
+ else if style == "inline-type-imports" && (n.default_import == null || n.default_import.import_use != "type") && n.namespace == null then {
121
+ value moving: filter(n.specifier.nodes, item => item.import_use == "type")
122
+ value import_inline_edits(moving)
123
+ }
124
+ else if n.default_import == null && n.namespace == null && all(n.specifier.nodes, item => item.import_use == "type") then import_whole_edits(source, n)
125
+ else import_split_edits(source, n, style)
126
+ fn import_policy_fix(source, n, style, preference) =
127
+ if preference == "type-imports" && n.import_has_attributes then null else {
128
+ value edits: import_policy_edits(source, n, style, preference)
129
+ value from: { value edits; map (edit) edit.start; min }
130
+ value until: { value edits; map (edit) edit.end; max }
131
+ value start: min(n.offset, from)
132
+ value end: max(n.end_offset, until)
133
+ value region: byte_slice(source, start, end)
134
+ value text: expression_fix_edits(region, start, edits)
135
+ value owner: { offset: start, end_offset: end, text: region }
136
+ value if count(edits) == 0 || !expression_fix_retains_comments(owner, text) then null else { target: { start: start, end: end }, text: text }
137
+ }
138
+
139
+ in ts_import rule es-consistent-type-imports {
140
+ option disallowTypeAnnotations boolean = true "Disallow import() type annotations."
141
+ option fixStyle ("inline-type-imports" | "separate-type-imports") = "separate-type-imports" "Preferred fix style."
142
+ option prefer ("type-imports" | "no-type-imports") = "type-imports" "Preferred import kind."
143
+ when prefer == "no-type-imports" && kind == "type" || prefer == "type-imports" && kind == null && contains(import_uses, "type")
144
+ when prefer == "no-type-imports" || !ts_program.import_decorator_metadata
145
+ let import_fix import_policy_fix(source, node, fixStyle, prefer)
146
+ warn if prefer == "no-type-imports" then "Use an `import` instead of an `import type`." else if !contains(import_uses, "value") && !contains(import_uses, "unused") then "All imports in the declaration are only used as types. Use `import type`." else "Imports {word_list(type_only_names)} are only used as type."
147
+ fix at import_fix.target if import_fix != null as import_fix.text
148
+ }
149
+ in ts_import_specifier rule es-consistent-type-imports:inline {
150
+ option disallowTypeAnnotations boolean = true "Disallow import() type annotations."
151
+ option fixStyle ("inline-type-imports" | "separate-type-imports") = "separate-type-imports" "Preferred fix style."
152
+ option prefer ("type-imports" | "no-type-imports") = "type-imports" "Preferred import kind."
153
+ when prefer == "no-type-imports" && kind == "type"
154
+ let edit import_remove_type(source, node)
155
+ warn "Use an `import` instead of an `import type`."
156
+ fix at { start: edit.start, end: edit.end } as ""
157
+ }
158
+ in ts_import_type rule es-consistent-type-imports:annotation {
159
+ option disallowTypeAnnotations boolean = true "Disallow import() type annotations."
160
+ option fixStyle ("inline-type-imports" | "separate-type-imports") = "separate-type-imports" "Preferred fix style."
161
+ option prefer ("type-imports" | "no-type-imports") = "type-imports" "Preferred import kind."
162
+ when disallowTypeAnnotations
163
+ warn "`import()` type annotations are forbidden."
164
+ }
165
+
166
+ }