@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,335 @@
1
+ import .ts/globals
2
+ import .ts/expression-fixes
3
+
4
+ define {
5
+
6
+ fn ts_runtime_binding_init(n) = {
7
+ value binding: if n is ts_identifier then name_binding(n) else null
8
+ value declaration: binding.node
9
+ value owner: declaration.parent.node
10
+ value if binding == null || count(binding.declarations) != 1 || owner not ts_declarator
11
+ || owner.name.node != declaration || !contains(["const", "let", "var"], declaration.declaration_kind)
12
+ || !all(binding.references, reference => reference.access != null
13
+ && !contains(["write", "read_write"], reference.access))
14
+ then null else owner.init.node
15
+ }
16
+ fn ts_runtime_quasi(q) = {
17
+ value body: slice(q.text, 1, if re_test(q.text, /\$\{$/) then -2 else -1)
18
+ value js_string_parts("`" body "`").text
19
+ }
20
+ fn ts_runtime_template(n, seen) = {
21
+ value expressions: n.expression.nodes
22
+ value quasis: n.quasi.nodes
23
+ value pieces: map(quasis, quasi => {
24
+ value index: index_of(quasis, quasi)
25
+ value text: ts_runtime_quasi(quasi)
26
+ value expression: if index < count(expressions) then {
27
+ value expressions
28
+ drop index
29
+ first
30
+ } else null
31
+ value substitution: if expression != null then ts_runtime_compute(expression, seen) else null
32
+ value if type_of(text) != "str" || expression != null && substitution == null then null
33
+ else text (if expression != null then js_to_string(substitution) else "")
34
+ })
35
+ value if count(quasis) != count(expressions) + 1 || any(pieces, piece => type_of(piece) != "str")
36
+ then null else { type: "string", value: join(pieces, "") }
37
+ }
38
+ fn ts_runtime_unary(n, seen) = {
39
+ value operand: ts_runtime_compute(n.argument.node, seen)
40
+ value when {
41
+ operand == null then null
42
+ operand.type == "bigint" then
43
+ if n.operator != "-" then null else {
44
+ type: "bigint",
45
+ value: when {
46
+ operand.value == "0" then "0"
47
+ starts_with(operand.value, "-") then slice(operand.value, 1)
48
+ else then "-{operand.value}"
49
+ }
50
+ }
51
+ else then {
52
+ value number: js_number_unary(n.operator.text, js_number(operand))
53
+ value if number == null then null else { type: "number", value: number }
54
+ }
55
+ }
56
+ }
57
+ fn ts_runtime_binary(n, seen) = {
58
+ value left: ts_runtime_compute(n.left.node, seen)
59
+ value right: ts_runtime_compute(n.right.node, seen)
60
+ value when {
61
+ left == null || right == null || !contains(["+", "-", "*", "/", "%", "**"], n.operator.text) then null
62
+ n.operator == "+" && (left.type == "string" || right.type == "string")
63
+ then { type: "string", value: js_to_string(left) js_to_string(right) }
64
+ left.type == "bigint" || right.type == "bigint" then null
65
+ else then {
66
+ value number: js_number_binary(n.operator.text, js_number(left), js_number(right))
67
+ value if number == null then null else { type: "number", value: number }
68
+ }
69
+ }
70
+ }
71
+ fn ts_runtime_index(n, seen) = {
72
+ value object: ts_runtime_compute(n.object.node, seen)
73
+ value index: ts_runtime_compute(n.index.node, seen)
74
+ value number: if index != null && index.type == "number" then index.value else null
75
+ value if n.optional != null || object.type != "string" || number == null
76
+ || number < 0 || number % 1 != 0 || number >= utf16_len(object.value) then null
77
+ else { type: "string", value: js_char_at(object.value, number) }
78
+ }
79
+ fn ts_runtime_compute(n, seen) =
80
+ if n == null || contains(seen, n) then null else {
81
+ value path: seen [n]
82
+ value when {
83
+ n is ts_string then {
84
+ value decoded: js_string(n.text)
85
+ value if type_of(decoded) != "str" then null else { type: "string", value: decoded }
86
+ }
87
+ n is ts_number then {
88
+ type: "number", value: js_number({ type: "number", value: n.text })
89
+ }
90
+ n is ts_bigint then {
91
+ value decoded: js_bigint(n.text)
92
+ value if decoded == null then null else { type: "bigint", value: decoded }
93
+ }
94
+ n is ts_boolean then { type: "boolean", value: n.text == "true" }
95
+ n is ts_null then { type: "null", value: null }
96
+ n is (ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion)
97
+ then ts_runtime_compute(n.expression.node, path)
98
+ n is ts_identifier then ts_runtime_compute(ts_runtime_binding_init(n), path)
99
+ n is ts_template && n not ts_tagged_template.quasi then ts_runtime_template(n, path)
100
+ n is ts_sequence then ts_runtime_compute(last(n.expression.nodes), path)
101
+ n is ts_assignment && n.operator == "=" then ts_runtime_compute(n.right.node, path)
102
+ n is ts_unary then ts_runtime_unary(n, path)
103
+ n is ts_binary then ts_runtime_binary(n, path)
104
+ n is ts_index then ts_runtime_index(n, path)
105
+ else then null
106
+ }
107
+ }
108
+ fn ts_runtime_constant(n) = ts_runtime_compute(n, [])
109
+ fn ts_runtime_string(n) = {
110
+ value constant: ts_runtime_constant(n)
111
+ value if constant == null then null else js_to_string(constant)
112
+ }
113
+ fn ts_runtime_number(n) = {
114
+ value constant: ts_runtime_constant(n)
115
+ value if constant == null || !contains(["number", "boolean", "bigint"], constant.type)
116
+ then null else js_number(constant)
117
+ }
118
+
119
+ in ts_node let static_string ts_runtime_string(node)
120
+ in ts_node let static_number ts_runtime_number(node)
121
+
122
+ group ts_constant_expression ts_string, ts_number, ts_boolean, ts_null, ts_regex, ts_bigint, ts_arrow, ts_function, ts_class, ts_object, ts_hole, ts_template, ts_array, ts_unary, ts_binary, ts_logical, ts_new, ts_assignment, ts_sequence, ts_spread, ts_call, ts_identifier, ts_parenthesized, ts_update, ts_conditional
123
+ group ts_constant_literal ts_string, ts_number, ts_boolean, ts_null, ts_regex, ts_bigint
124
+
125
+ fn ts_constant_global(n, key) = n is ts_identifier && n.text == key && contains(["readonly", "writable"], n.global)
126
+ fn ts_constant_call(n, names) = {
127
+ value callee: unparenthesised_node(n.callee.node)
128
+ value n is ts_call && callee is ts_identifier && contains(names, callee.text)
129
+ && ts_constant_global(callee, callee.text)
130
+ }
131
+ fn ts_constant_boolean_call(n) = {
132
+ value argument: first(n.argument.nodes)
133
+ value ts_constant_call(n, ["Boolean"]) && (argument == null || argument.constant_truthiness == true)
134
+ }
135
+ fn ts_constant_answer(n, boolean) = when {
136
+ n == null then false
137
+ boolean then n.constant_truthiness == true
138
+ else then n.ts_constant_value == true
139
+ }
140
+ fn ts_constant_quasi(q) = {
141
+ value cooked: ts_runtime_quasi(q)
142
+ value type_of(cooked) != "str" || cooked != ""
143
+ }
144
+ fn ts_constant_compute(n, boolean) = when {
145
+ n is (ts_constant_literal, ts_arrow, ts_function, ts_class, ts_object, ts_hole) then true
146
+ n is ts_parenthesized then ts_constant_answer(n.expression.node, boolean)
147
+ n is ts_template then boolean && any(n.quasi.nodes, quasi => ts_constant_quasi(quasi))
148
+ || all(n.expression.nodes, expression => ts_constant_answer(expression, false))
149
+ n is ts_array then boolean || all(n.element.nodes, element => ts_constant_answer(element, false))
150
+ n is ts_unary then n.operator == "void" || n.operator == "typeof" && boolean
151
+ || ts_constant_answer(n.argument.node, n.operator == "!")
152
+ n is ts_binary then n.operator != "in" && ts_constant_answer(n.left.node, false) && ts_constant_answer(n.right.node, false)
153
+ n is ts_logical then {
154
+ value left: ts_constant_answer(n.left.node, boolean)
155
+ value right: ts_constant_answer(n.right.node, boolean)
156
+ value left && (right || ts_constant_identity(n.left.node, n.operator))
157
+ || boolean && right && ts_constant_identity(n.right.node, n.operator)
158
+ }
159
+ n is ts_new then boolean
160
+ n is ts_assignment then n.operator == "=" && ts_constant_answer(n.right.node, boolean)
161
+ || boolean && contains(["||=", "&&="], n.operator) && {
162
+ value operator: slice(n.operator, 0, 2)
163
+ value ts_constant_identity(n.right.node, operator)
164
+ }
165
+ n is ts_sequence then { value tail: last(n.expression.nodes); value ts_constant_answer(tail, boolean) }
166
+ n is ts_spread then ts_constant_answer(n.argument.node, boolean)
167
+ n is ts_call then ts_constant_boolean_call(n)
168
+ else then ts_constant_global(n, "undefined")
169
+ }
170
+
171
+ fn ts_constant_literal_truth(n) = when {
172
+ n is ts_boolean then n.text == "true"
173
+ n is ts_null then false
174
+ n is ts_string then len(n.text) > 2
175
+ n is ts_regex then true
176
+ n is ts_number then { value number: js_number(n.text); value number != null && number != 0 && number == number }
177
+ n is ts_bigint then { value body: re_replace(n.text, /n+$/, ""); value number: js_number(body); value number != null && number != 0 }
178
+ else then null
179
+ }
180
+ fn ts_constant_identity(n, operator) = {
181
+ value n
182
+ walk (current) {
183
+ value bare: unparenthesised_node(current)
184
+ value when {
185
+ bare is ts_logical && bare.operator == operator then [bare.left.node, bare.right.node]
186
+ bare is ts_assignment && contains(["||=", "&&="], bare.operator)
187
+ && slice(bare.operator, 0, 2) == operator then bare.right.node
188
+ else then null
189
+ }
190
+ }
191
+ any (current) {
192
+ value bare: unparenthesised_node(current)
193
+ value truth: ts_constant_literal_truth(bare)
194
+ value if bare is ts_constant_literal then operator == "||" && truth == true || operator == "&&" && truth == false
195
+ else bare is ts_unary && operator == "&&" && bare.operator == "void"
196
+ }
197
+ }
198
+ fn ts_constant_null(n) = {
199
+ value bare: unparenthesised_node(n)
200
+ value bare is ts_null || ts_constant_global(bare, "undefined") || bare is ts_unary && bare.operator == "void"
201
+ }
202
+ fn ts_constant_nullish(n, non_nullish) = {
203
+ value terminal: {
204
+ value {node: unparenthesised_node(n), non_nullish: non_nullish}
205
+ walk (current) {
206
+ value bare: current.node
207
+ value next: when {
208
+ bare == null || current.non_nullish && ts_constant_null(bare) then null
209
+ bare is ts_logical && bare.operator == "??" || bare is ts_assignment && bare.operator == "=" then bare.right.node
210
+ bare is ts_sequence then last(bare.expression.nodes)
211
+ else then null
212
+ }
213
+ value if next == null then null else {
214
+ node: unparenthesised_node(next), non_nullish: current.non_nullish || bare is ts_logical,
215
+ }
216
+ }
217
+ last
218
+ }
219
+ value bare: terminal.node
220
+ value when {
221
+ bare == null || terminal.non_nullish && ts_constant_null(bare) then false
222
+ bare is (ts_constant_literal, ts_object, ts_array, ts_arrow, ts_function, ts_class, ts_new, ts_template, ts_update, ts_binary, ts_unary) then true
223
+ bare is ts_call then ts_constant_call(bare, ["Boolean", "String", "Number", "Symbol", "BigInt"])
224
+ bare is ts_assignment then !contains(["=", "||=", "&&=", "??="], bare.operator)
225
+ else then ts_constant_global(bare, "undefined")
226
+ }
227
+ }
228
+ fn ts_constant_static_boolean(n) = {
229
+ value bare: unparenthesised_node(n)
230
+ value bare is ts_boolean || ts_constant_boolean_call(bare)
231
+ || bare is ts_unary && bare.operator == "!" && ts_constant_answer(bare.argument.node, true)
232
+ }
233
+ fn ts_constant_boolean_comparison(n, strict) = {
234
+ value bare: {
235
+ value unparenthesised_node(n)
236
+ walk (current) when {
237
+ current is ts_assignment && current.operator == "=" then unparenthesised_node(current.right.node)
238
+ current is ts_sequence then unparenthesised_node(last(current.expression.nodes))
239
+ else then null
240
+ }
241
+ last
242
+ }
243
+ value when {
244
+ bare == null then false
245
+ bare is (ts_object, ts_class, ts_arrow, ts_function) then true
246
+ bare is ts_array then strict || count(bare.element.nodes) == 0
247
+ || { value bare.element.nodes; filter (element) element not (ts_hole, ts_spread); count } > 1
248
+ bare is ts_unary then when {
249
+ bare.operator == "!" then ts_constant_answer(bare.argument.node, true)
250
+ strict then bare.operator != "delete"
251
+ else then contains(["void", "typeof"], bare.operator)
252
+ }
253
+ bare is ts_binary then strict && contains(["+", "-", "*", "/", "%", "|", "^", "&", "**", "<<", ">>", ">>>"], bare.operator)
254
+ bare is (ts_new, ts_update) then strict
255
+ bare is ts_template then strict || count(bare.expression.nodes) == 0
256
+ bare is ts_call then ts_constant_boolean_call(bare)
257
+ || strict && ts_constant_call(bare, ["String", "Number", "BigInt", "Symbol"])
258
+ bare is ts_assignment then strict && !contains(["=", "||=", "&&=", "??="], bare.operator)
259
+ else then ts_constant_global(bare, "undefined") || bare is ts_constant_literal
260
+ }
261
+ }
262
+ fn ts_constant_new(n) = {
263
+ value branches: {
264
+ value unparenthesised_node(n)
265
+ walk (current) when {
266
+ current is ts_assignment && current.operator == "=" then unparenthesised_node(current.right.node)
267
+ current is ts_sequence then unparenthesised_node(last(current.expression.nodes))
268
+ current is ts_conditional then map([current.consequent.node, current.alternate.node], branch => unparenthesised_node(branch))
269
+ else then null
270
+ }
271
+ }
272
+ value count(branches) > 0 && all(branches, bare => {
273
+ value when {
274
+ bare is (ts_object, ts_array, ts_arrow, ts_function, ts_class, ts_regex) then true
275
+ bare is ts_new then {
276
+ value callee: unparenthesised_node(bare.callee.node)
277
+ value program: ts_module_program(bare)
278
+ value callee is ts_identifier && ts_global_builtin_setting(program, callee.text) == "readonly"
279
+ && ts_constant_global(callee, callee.text)
280
+ }
281
+ bare is ts_assignment then bare.operator == "=" && bare.right != null
282
+ bare is ts_sequence then count(bare.expression.nodes) > 0
283
+ bare is ts_conditional then bare.consequent != null && bare.alternate != null
284
+ else then false
285
+ }
286
+ })
287
+ }
288
+ fn ts_constant_operand(a, b, operator) = contains(["==", "!=", "===", "!=="], operator)
289
+ && (ts_constant_null(a) && ts_constant_nullish(b, false)
290
+ || ts_constant_static_boolean(a) && {
291
+ value strict: contains(["===", "!=="], operator)
292
+ value ts_constant_boolean_comparison(b, strict)
293
+ })
294
+ fn ts_constant_static_literal(n) = {
295
+ value bare: unparenthesised_node(n)
296
+ value bare is ts_constant_literal || ts_constant_global(bare, "undefined")
297
+ || bare is ts_template && count(bare.expression.nodes) == 0
298
+ || bare is ts_unary && contains(["-", "+", "~"], bare.operator) && bare.argument is ts_constant_literal
299
+ }
300
+ fn ts_constant_report(kind, n, detail) = {
301
+ value bare: unparenthesised_node(n)
302
+ value {kind: kind, node: bare, detail: detail}
303
+ }
304
+ fn ts_constant_compare(n) = {
305
+ value left: n.left.node
306
+ value right: n.right.node
307
+ value operator: n.operator
308
+ value when {
309
+ n is ts_logical then when {
310
+ contains(["&&", "||"], operator) && ts_constant_answer(left, true) then ts_constant_report("constantShortCircuit", left, "truthiness")
311
+ operator == "??" && ts_constant_nullish(left, false) then ts_constant_report("constantShortCircuit", left, "nullishness")
312
+ else then null
313
+ }
314
+ n not ts_binary then null
315
+ ts_constant_operand(left, right, operator) then ts_constant_report("constantBinaryOperand", right, "left")
316
+ ts_constant_operand(right, left, operator) then ts_constant_report("constantBinaryOperand", left, "right")
317
+ contains(["===", "!=="], operator) && ts_constant_new(left) then ts_constant_report("alwaysNew", left, "")
318
+ contains(["===", "!=="], operator) && ts_constant_new(right) then ts_constant_report("alwaysNew", right, "")
319
+ contains(["==", "!="], operator) && ts_constant_new(left) && ts_constant_new(right) then ts_constant_report("bothAlwaysNew", left, "")
320
+ else then null
321
+ }
322
+ }
323
+ fn ts_constant_relational(n) = n is ts_binary && contains(["<", "<=", ">", ">="], n.operator)
324
+ && ts_constant_static_literal(n.left.node) && ts_constant_static_literal(n.right.node)
325
+
326
+ in ts_constant_expression {
327
+ let ts_constant_value ts_constant_compute(node, false)
328
+ let constant_truthiness ts_constant_compute(node, true)
329
+ }
330
+ in ts_binary, ts_logical {
331
+ let constant_comparison ts_constant_compare(node)
332
+ let constant_relation ts_constant_relational(node)
333
+ }
334
+
335
+ }
@@ -0,0 +1,185 @@
1
+ # Declaration documentation policy.
2
+
3
+ import .ts/type-specifiers
4
+ import .ts/constants
5
+
6
+ define {
7
+ fn deprecation_tag(doc) =
8
+ if doc == null || !re_test(doc, /(?m)^@deprecated\b/) then null
9
+ else {reason: {
10
+ value doc
11
+ re_find /(?m)^@deprecated[^\n]*(?:\n[^@\n][^\n]*)*/
12
+ re_replace /^@deprecated[ \t]*/ ''
13
+ trim
14
+ }}
15
+ fn deprecation_first(docs) = {
16
+ value docs
17
+ map (doc) deprecation_tag(doc)
18
+ filter (reason) reason != null
19
+ first
20
+ }
21
+ fn deprecation_leading_doc(leading) = {
22
+ value doc: if leading == null then null else re_find(leading, /\/\*\*(?:[^*]|\*+[^*/])*\*\/\s*$/)
23
+ value if doc == null then null else {
24
+ value doc
25
+ re_replace /^\s*\/\*\*/ ""
26
+ re_replace /\*\/\s*$/ ""
27
+ re_replace /\r\n?/ "\n"
28
+ re_replace /(?m)^[ \t]*\*?[ \t]?/ ""
29
+ trim
30
+ }
31
+ }
32
+ fn deprecation_doc(n, site) = {
33
+ value owners: walk([site], current => {
34
+ value parent: site_parent(n, current)
35
+ value if parent != null && (parent.kind == "ts_export"
36
+ || current.kind == "ts_identifier" && parent.kind == "ts_declarator"
37
+ || current.kind == "ts_declarator" && parent.kind == "ts_variable_declaration")
38
+ then parent else null
39
+ })
40
+ value first(filter(map(owners, owner => deprecation_leading_doc(site_text(n, owner).leading)),
41
+ doc => doc != null))
42
+ }
43
+ # The documentation an imported name carries on the way: the export
44
+ # specifier or `export default` that publishes it, then its declaration.
45
+ fn import_remote_site(n, site) = {
46
+ value parent: site_parent(n, site)
47
+ value if parent == null then true else !contains(["ts_import", "ts_import_specifier"], parent.kind)
48
+ }
49
+ fn import_deprecation_doc(n, site) = {
50
+ value owners: walk([site], current => site_parent(n, current))
51
+ value first(filter(map(owners, owner => deprecation_leading_doc(site_text(n, owner).leading)),
52
+ doc => doc != null))
53
+ }
54
+ fn import_remote_sites(n, sites) = filter(sites ?? [], site => import_remote_site(n, site))
55
+ # `export { /** @deprecated */ X }`: the specifier carries the documentation,
56
+ # not `X`'s declaration. Found among the statements of the declaring file.
57
+ fn import_specifier_docs(n, site, name) = {
58
+ value root: last(walk([site], current => site_parent(n, current)))
59
+ value exports: filter(site_field(n, root, "body") ?? [], statement => statement.kind == "ts_export")
60
+ value specifiers: flat_map(exports, statement => site_field(n, statement, "specifier") ?? [])
61
+ value named: filter(specifiers, specifier => {
62
+ value exported: first(site_field(n, specifier, "exported") ?? []) ?? first(site_field(n, specifier, "local") ?? [])
63
+ value exported != null && site_text(n, exported).text == name
64
+ })
65
+ value map(named, specifier => deprecation_leading_doc(site_text(n, specifier).leading))
66
+ }
67
+ fn import_deprecation_tag(n) = {
68
+ value values: import_remote_sites(n, name_definitions(n, "value"))
69
+ value types: if count(values) > 0 then [] else import_remote_sites(n, name_definitions(n, "type"))
70
+ value sites: if count(values) > 0 then values else types
71
+ value imported: name_binding(n).node.imported_name ?? n.text
72
+ value deprecation_first(flat_map(reverse(sites), site => [import_deprecation_doc(n, site)] import_specifier_docs(n, site, imported)))
73
+ }
74
+ fn deprecation_local_doc(n) = {
75
+ value owner: find(walk([] n.declaration, current =>
76
+ if current is (ts_declarator, ts_identifier) then current.parent.node else null),
77
+ current => current is ts_variable_declaration)
78
+ value if owner == null then null else deprecation_leading_doc(owner.gap_before ?? owner.text_before)
79
+ }
80
+ fn deprecation_subject(n) = if n is (ts_member.property, ts_qualified_name.right) then n.parent.node
81
+ else if n.deprecation_shorthand then n.declaration else n
82
+ fn deprecation_pattern_source(pattern) = {
83
+ value path: walk(pattern, current =>
84
+ if current is (ts_pattern_property.value, ts_property.value) then current.parent.parent.node
85
+ else if current is (ts_array_pattern.element, ts_array.element, ts_assignment_pattern.left, ts_parenthesized.expression)
86
+ then current.parent.node else null)
87
+ value root: last(path)
88
+ value source: if root is ts_declarator.name then root.parent.init.node
89
+ else if root is ts_parameter.name then root.parent.type.node ?? root.parent.init.node
90
+ else if root is ts_assignment.left then root.parent.right.node else null
91
+ value fold(reverse(path), source, (receiver, current) =>
92
+ if current is (ts_pattern_property.value, ts_property.value) then
93
+ value_lookup(receiver, ts_value_key(current.parent.key.node)).member
94
+ else if current is (ts_array_pattern.element, ts_array.element) then
95
+ value_lookup(receiver, index_of(current.parent.element.nodes, current)).member
96
+ else receiver)
97
+ }
98
+ fn deprecation_symbols(n) = {
99
+ value subject: deprecation_subject(n)
100
+ value pattern: subject.deprecation_pattern_key
101
+ value member: subject is (ts_member, ts_index) || pattern
102
+ value binding: if member then null else name_binding(subject)
103
+ value key: if pattern then ts_value_key(subject)
104
+ else if subject is ts_index then subject.static_name ?? value_key(subject.index.node)
105
+ else if subject is ts_member then subject.static_name else binding.key
106
+ value name: if type_of(key) == "str" then key else if subject is ts_index then ts_runtime_string(subject.index.node) else null
107
+ value receiver: if pattern then deprecation_pattern_source(subject.parent.parent.node) else subject.object.node
108
+ value lookup: if member && name != null then value_lookup(receiver, name) else null
109
+ value declaration: if member then null else declaration_identity(subject)
110
+ value sites: if member then unique(flat_map(lookup.alternatives ?? [], alternative =>
111
+ map(alternative.sources ?? [], source => source.site)))
112
+ else if declaration != null then [declaration] else []
113
+ value complete: name != null && (if member then lookup.alternatives != null
114
+ && count(lookup.alternatives) > 0 && all(lookup.alternatives, alternative =>
115
+ alternative.matched && alternative.sources != null && count(alternative.sources) > 0)
116
+ else binding != null && binding.node.imported_name == null && declaration != null)
117
+ value {name: name, sites: sites, complete: complete}
118
+ }
119
+ fn deprecation_value_allowed(entries, n) = if count(entries) == 0 then false else {
120
+ value symbols: deprecation_symbols(n)
121
+ value directory: if any(entries, entry => entry.from == "file") then program_directory(n) else null
122
+ value states: flat_map(entries, entry => map(symbols.sites, site =>
123
+ type_specifier_site(entry, n, site, symbols.name, directory)))
124
+ value if any(states, state => state == true) then true
125
+ else if !symbols.complete || any(states, state => state == null) then null else false
126
+ }
127
+ fn deprecation_call(n) = {
128
+ value chain: walk([n], current => if current.parent is (ts_parenthesized, ts_non_null, ts_instantiation)
129
+ then current.parent.node else null)
130
+ value callee: find(chain, current => current is (ts_call.callee, ts_new.callee, ts_tagged_template.tag))
131
+ value callee.parent.node
132
+ }
133
+ fn deprecation_reason(n) = {
134
+ value subject: deprecation_subject(n)
135
+ value call: deprecation_call(subject)
136
+ value selection: if call != null then call_selection(call) else null
137
+ value roots: value_sources(subject)
138
+ value sites: walk(roots.sites ?? [], site =>
139
+ if contains(["ts_declarator", "ts_parameter"], site.kind) then []
140
+ else site_sources(subject, site).sites ?? [])
141
+ value members: if subject is (ts_member, ts_index) || subject.deprecation_pattern_key then deprecation_symbols(subject).sites else []
142
+ value origin: declaration_identity(subject)
143
+ value declarations: unique([] members (if origin != null then [origin] else []) sites)
144
+ value owners: if call == null then declarations else filter(roots.sites ?? [], site =>
145
+ !contains(["ts_function", "ts_arrow", "ts_method", "ts_function_type", "ts_method_signature", "ts_call_signature"], site.kind))
146
+ value signatures: if call != null && selection != null && !selection.uncertain && count(selection.selected) == 1
147
+ then selection.selected else []
148
+ value docs: map(unique([] signatures owners), site => deprecation_doc(subject, site))
149
+ # A namespace import's member: the module's export specifiers may carry
150
+ # the tag (`imported.X` with `export { /** @deprecated */ X }`).
151
+ value namespace: subject is ts_member && subject.object is ts_identifier
152
+ && name_binding(subject.object.node).node is ts_import.namespace
153
+ value published: if namespace then flat_map(members, site => import_specifier_docs(subject, site, deprecation_symbols(subject).name)) else []
154
+ value own: deprecation_first([deprecation_local_doc(subject)] docs published)
155
+ # A name bound by an import is deprecated where its module publishes it.
156
+ value imported: if own == null && subject is ts_identifier && name_binding(subject).node.deprecation_import == true
157
+ then import_deprecation_tag(subject) else null
158
+ value own ?? imported
159
+ }
160
+
161
+ in ts_node let deprecation_import
162
+ if is (ts_import, ts_import_equals) then true
163
+ else if is (ts_program, ts_block, ts_function, ts_arrow, ts_class, ts_interface) then false
164
+ else parent.deprecation_import == true
165
+ in ts_node let deprecation_pattern_key is ts_pattern_property.key || is ts_property.key && parent.flow_destructuring_target
166
+ in ts_node let deprecation_shorthand is ts_property.key && parent.value.node == null && !deprecation_pattern_key
167
+ in ts_node let deprecation_excluded
168
+ deprecation_import || uses != null && !deprecation_pattern_key || is (ts_function.name, ts_class.name, ts_interface.name, ts_type_alias.name, ts_enum.name, ts_namespace.name, ts_method.key, ts_class_property.key, ts_property_signature.key, ts_method_signature.key) || is ts_property.key && !deprecation_shorthand && !deprecation_pattern_key || is (ts_enum_member.name, ts_parameter.name, ts_type_parameter.name, ts_import_equals.name, ts_export_specifier.exported, ts_export.namespace, ts_jsx_closing.name, ts_index.index)
169
+ in ts_index let deprecation_key index.node
170
+ in ts_node let deprecation_export_name if is ts_export_specifier.local then parent.exported.node ?? node else null
171
+ in ts_node let no_deprecated_checked true
172
+
173
+ in ts_identifier, ts_private_name, ts_super, ts_jsx_attribute.name, ts_index, ts_pattern_property.key, ts_property.key rule es-no-deprecated {
174
+ option allow (string | type_specifier_file | type_specifier_lib | type_specifier_package)[] = [] "Allowed deprecated symbols"
175
+ when !deprecation_excluded
176
+ when deprecation_export_name == null || deprecation_export_name is ts_identifier
177
+ let computed is ts_index
178
+ let tag deprecation_reason(node)
179
+ when tag != null
180
+ when no_deprecated_checked
181
+ when deprecation_value_allowed(allow, node) == false
182
+ report at if computed then deprecation_key else deprecation_export_name ?? node
183
+ warn "`" (deprecation_export_name.identifier_name ?? deprecation_symbols(node).name ?? identifier_name) "` is deprecated." (if tag.reason == "" then "" else " " tag.reason)
184
+ }
185
+ }
@@ -0,0 +1,60 @@
1
+ import .ts/globals
2
+
3
+ in ts_node let ts_directive_program parent.ts_directive_program
4
+ in ts_program let ts_directive_program node
5
+ in ts_node let ecma_version parent.ecma_version
6
+ in ts_program let ecma_version ts_ecma_version(node)
7
+ in ts_node let source_environment parent.source_environment
8
+ in ts_program let source_environment {
9
+ value program: node
10
+ value {
11
+ implied_strict: ts_environment(program, "impliedStrict") == true,
12
+ root_function: ts_source_type(program) == "commonjs"
13
+ || ts_source_type(program) == "script" && ts_environment(program, "globalReturn") == true
14
+ }
15
+ }
16
+ fn ts_directive_body(n) = if n is ts_program then n
17
+ else if n is (ts_function, ts_method, ts_arrow, ts_namespace) && n.body is ts_block then n.body.node
18
+ else null
19
+ in ts_node let ts_directive_prologue {
20
+ value body: ts_directive_body(node)
21
+ value state: {
22
+ value body.body.nodes
23
+ fold ({ active: true, statements: [] }) (state, statement)
24
+ if state.active && statement is ts_expression_statement && statement.expression is ts_string
25
+ then { active: true, statements: state.statements [statement] }
26
+ else { active: false, statements: state.statements }
27
+ }
28
+ value state.statements
29
+ }
30
+ in ts_node let ts_strict_directives {
31
+ value ts_directive_prologue
32
+ filter (statement) contains(["'use strict'", '"use strict"'], statement.expression.text)
33
+ }
34
+ in ts_node let scoping_strict_code {
35
+ value program: ts_directive_program
36
+ value ts_source_type(program) == "module"
37
+ || source_environment.implied_strict && ecma_version >= 5
38
+ || is (ts_class, ts_static_block)
39
+ || ecma_version >= 5 && !(is ts_namespace) && count(ts_strict_directives) > 0
40
+ || parent.scoping_strict_code == true
41
+ }
42
+ in ts_node let directive_statement false
43
+ in ts_expression_statement let directive_statement {
44
+ value scope: if parent is ts_program then parent.node
45
+ else if parent is ts_block then parent.parent.node else null
46
+ value expression is ts_string
47
+ && contains(scope.ts_directive_prologue, node)
48
+ }
49
+ in ts_node let directive_scope {
50
+ value ancestors: walk(parent.node, ancestor => ancestor.parent.node)
51
+ value program: ts_directive_program
52
+ value {
53
+ directives: ts_strict_directives,
54
+ inherited: if parent.node != null then parent.scoping_strict_code
55
+ else ts_source_type(program) == "module" || source_environment.implied_strict && ecma_version >= 5,
56
+ parent_function: any(ancestors, ancestor => ancestor is (ts_function, ts_method, ts_arrow)),
57
+ class_body: any(ancestors, ancestor => ancestor is ts_class.member) || is ts_class.member,
58
+ simple_parameters: all(parameter.nodes, parameter => parameter.name is ts_identifier && parameter.init == null)
59
+ }
60
+ }