@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,268 @@
1
+ import .ts/comments
2
+
3
+ @macro ts_module_profile(profile, order, condition) {
4
+ module_resolution profile {
5
+ config {
6
+ files ["tsconfig.json", "jsconfig.json"]
7
+ format jsonc
8
+ source_files "files"
9
+ include "include"
10
+ exclude "exclude"
11
+ extends "extends"
12
+ references "references[].path"
13
+ options "compilerOptions"
14
+ paths "paths"
15
+ base "baseUrl"
16
+ }
17
+ extensions [".ts", ".tsx", ".d.ts", ".mts", ".cts", ".d.mts", ".d.cts"]
18
+ substitutions {
19
+ ".js" [".ts", ".tsx", ".d.ts"]
20
+ ".jsx" [".tsx", ".d.ts"]
21
+ ".mjs" [".mts", ".d.mts"]
22
+ ".cjs" [".cts", ".d.cts"]
23
+ }
24
+ candidates ["{{dir}}/{{name}}{{ext}}", "{{dir}}/{{name}}/index{{ext}}"]
25
+ packages {
26
+ directories ["node_modules"]
27
+ metadata "package.json"
28
+ entries ["types", "typings"]
29
+ exports "exports"
30
+ imports "imports"
31
+ scoped_prefix "@"
32
+ type_directory "@types"
33
+ type_join "__"
34
+ }
35
+ conditions ["types", condition, "default"]
36
+ ambient_scopes [ambient_module]
37
+ star_excludes ["default", "="]
38
+ search order
39
+ globals {
40
+ libraries "{{package_dir}}/typescript/lib/lib.{{name:lower}}.d.ts"
41
+ library_option "lib"
42
+ default_libraries ["es2023", "dom"]
43
+ types_option "types"
44
+ type_roots_option "typeRoots"
45
+ type_directories ["node_modules/@types"]
46
+ project_files ["**/*.ts", "**/*.tsx", "**/*.mts", "**/*.cts"]
47
+ references true
48
+ # A file publishes a global only by spelling its name (or escaping it,
49
+ # or forwarding through a reference); ambient modules need a keyword.
50
+ name_hints ["\\u", "<reference"]
51
+ ambient_hints ["module", "namespace"]
52
+ }
53
+ when resolution.config.module == "commonjs" || resolution.config.module == 1 {
54
+ conditions ["types", "require", "default"]
55
+ }
56
+ when resolution.config.moduleResolution == "classic" || resolution.config.moduleResolution == 1 {
57
+ search [relative, config_paths, config_base, ambient]
58
+ }
59
+ }
60
+ }
61
+ @ts_module_profile(ts, [relative, config_paths, config_base, ambient, packages], "import")
62
+ @ts_module_profile(ts_require, [relative, config_paths, config_base, ambient, packages], "require")
63
+ @ts_module_profile(ts_augmentation, [relative, config_paths, config_base, packages], "import")
64
+
65
+ fn ts_compiler_option(n, name) = {
66
+ value setting: program_option(n, name)
67
+ value if setting == true || setting == false then setting
68
+ else if contains(["alwaysStrict", "noImplicitAny", "noImplicitThis", "strictBindCallApply",
69
+ "strictBuiltinIteratorReturn", "strictFunctionTypes", "strictNullChecks",
70
+ "strictPropertyInitialization", "useUnknownInCatchVariables"], name)
71
+ then program_option(n, "strict") == true else null
72
+ }
73
+
74
+ fn ts_module_name(n) = js_string(n.text) ?? js_string('"' n.text '"')
75
+ fn ts_module_program(n) = {
76
+ value walk([n], p => p.parent.node)
77
+ find (p) p is ts_program
78
+ }
79
+ fn ts_module_external(n) = n.ts_module_external_file
80
+ fn ts_module_names(n) = if n.name.node is ts_qualified_name then {
81
+ value [n.name.node]
82
+ walk (current) if current is ts_qualified_name then [current.left.node, current.right.node] else []
83
+ filter (current) current is ts_identifier
84
+ sort_by (current) current.offset
85
+ } else [n.name.node]
86
+ fn ts_module_require_source(n) = { value argument: first(n.value.argument.nodes); value js_string(argument.text) }
87
+ fn ts_module_global(n) = n.parent.node is ts_program && !n.parent.ts_module_external_file
88
+ || n.parent.node is ts_block && n.parent.parent.node is ts_namespace && n.parent.parent.kind == "global"
89
+ fn ts_module_public(n) = n is ts_export.declaration && n.parent.kind != "default"
90
+ || n.parent.node is ts_block && n.parent.parent.node is ts_namespace && n.parent.parent.kind != "global"
91
+ && (n.parent.parent.name.node is ts_string || n.parent.parent.ts_name_ambient)
92
+ fn ts_module_primitives(n) = {
93
+ value n
94
+ value descendants(ts_type_keyword)
95
+ filter (keyword) contains(["any", "unknown", "never", "void", "undefined", "null",
96
+ "boolean", "number", "bigint", "string", "symbol", "object", "intrinsic"], keyword.text)
97
+ unique_by (keyword) keyword.text
98
+ }
99
+
100
+ in ts_program {
101
+ let ts_module_external_file ends_with(file, ".mjs") || ends_with(file, ".mts")
102
+ || ends_with(file, ".cjs") || ends_with(file, ".cts")
103
+ || any(body.nodes, item => item is (ts_import, ts_export, ts_export_assignment)
104
+ || item is ts_import_equals && item.value.node is ts_call)
105
+ uses module_resolution ts
106
+ globals first type ts_module_primitives(node) named ["any", "unknown", "never", "void", "undefined", "null",
107
+ "boolean", "number", "bigint", "string", "symbol", "object", "intrinsic"]
108
+ }
109
+ in ts_type_query { resolves query name }
110
+
111
+ in ts_import {
112
+ when kind != "type" {
113
+ resolves value, type, query default_import.node in module js_string(from.text) as "default"
114
+ resolves value, type, query namespace.node in module js_string(from.text) as "*"
115
+ }
116
+ when kind == "type" {
117
+ resolves type, query default_import.node in module js_string(from.text) as "default" asking all
118
+ resolves type, query namespace.node in module js_string(from.text) as "*" asking all
119
+ }
120
+ }
121
+ in ts_import_specifier {
122
+ when kind != "type" && parent.kind != "type" {
123
+ resolves value, type, query local.node ?? imported.node in module js_string(parent.from.text) as ts_module_name(imported.node)
124
+ }
125
+ when kind == "type" || parent.kind == "type" {
126
+ resolves type, query local.node ?? imported.node in module js_string(parent.from.text) as ts_module_name(imported.node) asking all
127
+ }
128
+ }
129
+ in ts_import_equals {
130
+ let ts_module_alias_key ts_module_name(value.property.node)
131
+ when value.node is ts_member {
132
+ defines (scope module, namespace from value.object.definition).[ts_module_alias_key] ?? (scope statics from value.object.definition).[ts_module_alias_key]
133
+ unwraps definition
134
+ }
135
+ when value.node not (ts_call, ts_member) {
136
+ defines value.definition
137
+ unwraps definition
138
+ }
139
+ when value.node is ts_call && value.callee.text == "require" {
140
+ uses module_resolution ts_require
141
+ when kind != "type" {
142
+ resolves value, type, query name.node in module ts_module_require_source(node) as "="
143
+ }
144
+ when kind == "type" {
145
+ resolves type, query name.node in module ts_module_require_source(node) as "=" asking all
146
+ }
147
+ }
148
+ }
149
+
150
+ in ts_export_specifier {
151
+ when parent.from == null {
152
+ when kind != "type" && parent.kind != "type" {
153
+ exports value, type, query local.node as ts_module_name(exported.node ?? local.node)
154
+ }
155
+ when kind == "type" || parent.kind == "type" {
156
+ exports type, query local.node as ts_module_name(exported.node ?? local.node) asking all
157
+ }
158
+ }
159
+ when parent.from != null {
160
+ when kind != "type" && parent.kind != "type" {
161
+ resolves value, type, query local.node in module js_string(parent.from.text) as ts_module_name(local.node)
162
+ exports value, type, query ts_module_name(local.node) from module js_string(parent.from.text) as ts_module_name(exported.node ?? local.node)
163
+ }
164
+ when kind == "type" || parent.kind == "type" {
165
+ resolves type, query local.node in module js_string(parent.from.text) as ts_module_name(local.node) asking all
166
+ exports type, query ts_module_name(local.node) from module js_string(parent.from.text) as ts_module_name(exported.node ?? local.node) asking all
167
+ }
168
+ }
169
+ }
170
+ in ts_export {
171
+ when operator == "*" && namespace == null {
172
+ when kind != "type" { exports all value, type, query from module js_string(from.text) }
173
+ when kind == "type" { exports all type, query from module js_string(from.text) asking all }
174
+ }
175
+ when operator == "*" && namespace != null {
176
+ when kind != "type" { exports value, type, query "*" from module js_string(from.text) as ts_module_name(namespace.node) }
177
+ when kind == "type" { exports type, query "*" from module js_string(from.text) as ts_module_name(namespace.node) asking all }
178
+ }
179
+ when kind == "default" {
180
+ when declaration.node is ts_class { exports value, type, query declaration.node as "default" }
181
+ when declaration.node is ts_function { exports value, query declaration.node as "default" }
182
+ when declaration.node is ts_interface { exports type declaration.node as "default" }
183
+ when declaration == null && expression.node not ts_identifier { exports value, query expression.node as "default" }
184
+ # A name exports whatever it declares (`class X {}; export default X`).
185
+ when declaration == null && expression.node is ts_identifier { exports value, type, query expression.node as "default" }
186
+ }
187
+ }
188
+ in ts_export_assignment { exports value, type, query expression.node as "=" }
189
+
190
+ in ts_variable_declaration {
191
+ when ts_module_public(node) { exports value, query declaration.nodes }
192
+ when ts_module_global(node) { globals value, query declaration.nodes }
193
+ }
194
+ in ts_function {
195
+ merges value, query
196
+ when ts_module_public(node) { exports value, query name.node }
197
+ when ts_module_global(node) { globals value, query name.node }
198
+ }
199
+ in ts_class, ts_enum {
200
+ merges value, type, query
201
+ when ts_module_public(node) { exports value, type, query name.node }
202
+ when ts_module_global(node) { globals value, type, query name.node }
203
+ }
204
+ in ts_interface, ts_type_alias {
205
+ when ts_module_public(node) { exports type name.node }
206
+ when ts_module_global(node) { globals type name.node }
207
+ }
208
+ in ts_interface { merges type }
209
+ in ts_namespace {
210
+ merges value, type, query
211
+ when name.node is ts_string {
212
+ when !ts_program.ts_module_external_file { scope ambient_module named js_string(name.text) }
213
+ when ts_program.ts_module_external_file {
214
+ uses module_resolution ts_augmentation
215
+ scope ambient_module named js_string(name.text) from module js_string(name.text)
216
+ }
217
+ }
218
+ when name != null && !(name.node is ts_string) {
219
+ scope namespace named ts_module_names(node)
220
+ when ts_module_public(node) {
221
+ exports type name.node
222
+ when ts_namespace_runtime(node) { exports value, query name.node }
223
+ }
224
+ when ts_module_global(node) {
225
+ globals type name.node
226
+ when ts_namespace_runtime(node) { globals value, query name.node }
227
+ }
228
+ }
229
+ }
230
+
231
+ define {
232
+ in .js layer ts_module_leading_trivia match ts_module_leading_trivia /\A/ (/[\s\u{FEFF}]+/ or ts_module_leading_line_comment or ts_module_leading_block_comment)+
233
+ in ts_module_leading_trivia {
234
+ match ts_module_leading_line_comment @core_line_comment_pattern
235
+ match ts_module_leading_block_comment @core_block_comment_pattern
236
+ }
237
+ in ts_module_leading_line_comment {
238
+ match ts_reference_path /^\/\/\/\s*<reference\s+path\s*=\s*["'][^"']+["'][^>]*\/>/
239
+ match ts_reference_types /^\/\/\/\s*<reference\s+types\s*=\s*["'][^"']+["'][^>]*\/>/
240
+ match ts_reference_lib /^\/\/\/\s*<reference\s+lib\s*=\s*["'][^"']+["'][^>]*\/>/
241
+ }
242
+ # The name a binding imports: "default", "*", the decoded specifier name, else null.
243
+ in ts_node let imported_name {
244
+ value binding: unparenthesised_node(node).declaration
245
+ value if binding is ts_import.default_import then "default"
246
+ else if binding is ts_import.namespace then "*"
247
+ else if binding.parent is ts_import_specifier then ts_module_name(binding.parent.imported.node)
248
+ else null
249
+ }
250
+ }
251
+ in ts_reference_path, ts_reference_types, ts_reference_lib let ts_module_reference
252
+ re_replace(text, /^\/\/\/\s*<reference\s+(?:path|types|lib)\s*=\s*["']([^"']+)["'][\s\S]*$/, "$1")
253
+ in ts_reference_path let ts_module_path_reference {
254
+ value path: re_replace(ts_module_reference, /\\/, "/")
255
+ value if starts_with(path, "./") || starts_with(path, "../") || starts_with(path, "/")
256
+ || re_test(path, /^[A-Za-z]:\//) then path else "./{path}"
257
+ }
258
+ in ts_reference_types let ts_module_types_reference "@types/{ts_module_reference}"
259
+ in ts_reference_lib let ts_module_lib_reference "typescript/lib/lib.{ts_module_reference}.d.ts"
260
+ in ts_reference_path {
261
+ globals all value, type, query from module ts_module_path_reference
262
+ }
263
+ in ts_reference_types {
264
+ globals all value, type, query from module ts_module_types_reference
265
+ }
266
+ in ts_reference_lib {
267
+ globals all value, type, query from module ts_module_lib_reference
268
+ }
@@ -0,0 +1,348 @@
1
+ # .ts/names: JavaScript and TypeScript scopes, declarations and references.
2
+ import .ts/directives
3
+ import .ts/effects
4
+
5
+ in ts_node let identifier_name null
6
+ in ts_identifier, ts_private_name let identifier_name
7
+ if !contains(text, "\\") then text else js_string('"' text '"') ?? text
8
+
9
+ fn ts_identifier_name_valid(name) = re_test(name, /^[\p{ID_Start}_$][\p{ID_Continue}$\u{200c}\u{200d}]*$/)
10
+
11
+ fn ts_arguments_owner(n) =
12
+ if n.identifier_name != "arguments" then null else {
13
+ value scopes: name_scopes(n, "reference")
14
+ value rows: map(scopes, scope => {
15
+ value declarations: name_scope_declarations(n, scope.id, "arguments")
16
+ value {
17
+ known: declarations != null,
18
+ declarations: filter(declarations, declaration => contains(declaration.classes, "value"))
19
+ }
20
+ })
21
+ value visible: find(rows, row => !row.known || count(row.declarations) > 0)
22
+ value if scopes != null && visible != null && visible.known
23
+ && all(visible.declarations, declaration => declaration.node == declaration.owner
24
+ && declaration.node is (ts_function, ts_method, ts_program))
25
+ then first(visible.declarations).owner else null
26
+ }
27
+
28
+ # Where a function or class is a declaration (else an expression).
29
+ fn ts_declared_statement(node) = node is (ts_program.body, ts_block.body, ts_case.body, ts_export.declaration, ts_labeled.body)
30
+ group ts_namespace_runtime_nodes ts_variable_declaration, ts_function, ts_class, ts_enum, ts_empty, ts_export_assignment, ts_export
31
+ in ts_block let ts_namespace_runtime_entries descendants(ts_namespace_runtime_nodes)
32
+ fn ts_namespace_runtime(n) = {
33
+ value n.body.ts_namespace_runtime_entries ?? []
34
+ any (item) !(item is ts_export)
35
+ || item.declaration.node == null
36
+ || item.declaration.node is ts_import_equals
37
+ }
38
+
39
+ # Scopes; several on one node nest, outermost first. A function's body
40
+ # shares the function's scope (parameters and body declarations together,
41
+ # as TypeScript binds them); a function expression's name sits just outside.
42
+ in ts_program {
43
+ scope module, function, block
44
+ # CommonJS and `globalReturn` scripts run inside a function.
45
+ when source_environment.root_function {
46
+ scope function, block
47
+ declares value, query "arguments" in nearest function hoisted
48
+ }
49
+ }
50
+ in ts_function { when !ts_declared_statement(node) { scope function_name } }
51
+ in ts_function, ts_arrow, ts_method, ts_static_block { scope function, block }
52
+ in ts_function_type, ts_method_signature, ts_call_signature { scope function }
53
+ in ts_block {
54
+ when !(parent is (ts_function, ts_arrow, ts_method, ts_static_block) || is (ts_try.handler, ts_namespace.body)) { scope block }
55
+ # `namespace N { var x }`: `var` lands in the namespace body.
56
+ when is ts_namespace.body { scope function, block }
57
+ # A catch clause: its parameter (on the `try`) and body share one scope.
58
+ when is ts_try.handler {
59
+ scope catch, block
60
+ declares value, query parent.parameter in nearest catch hoisted
61
+ }
62
+ }
63
+ in ts_for, ts_for_in, ts_for_of, ts_switch { scope block }
64
+ in ts_class { scope class }
65
+ in ts_interface, ts_type_alias { scope type_parameters }
66
+ # `infer X` names reach the true branch only.
67
+ in ts_conditional_type { scope conditional within check, extends, consequent }
68
+ in ts_mapped_type { scope mapped }
69
+
70
+ # Declarations. Patterns pass a declaration on to the names they bind.
71
+ in ts_variable_declaration {
72
+ when kind == "var" { declares value, query declaration in nearest function hoisted }
73
+ when kind != "var" { declares value, query declaration in nearest block hoisted }
74
+ }
75
+ in ts_declarator { targets name }
76
+ in ts_object_pattern { targets property, rest }
77
+ in ts_pattern_property {
78
+ when flow_shorthand_default { targets key }
79
+ when !flow_shorthand_default { targets value ?? key }
80
+ }
81
+ in ts_array_pattern { targets element }
82
+ in ts_assignment_pattern { targets left }
83
+ in ts_rest { targets argument }
84
+
85
+ in ts_function, ts_arrow, ts_method, ts_function_type, ts_method_signature, ts_call_signature {
86
+ declares value, query parameter in nearest function hoisted
87
+ }
88
+ in ts_parameter { targets name }
89
+ in ts_function, ts_method { declares value, query "arguments" in nearest function hoisted }
90
+ # A declaration's name lands in the enclosing block; an expression's only
91
+ # inside it.
92
+ in ts_function {
93
+ when ts_declared_statement(node) { declares value, query name in enclosing block hoisted }
94
+ when !ts_declared_statement(node) { declares value, query name in nearest function_name hoisted }
95
+ }
96
+ in ts_class {
97
+ when ts_declared_statement(node) { declares value, type, query name in enclosing block hoisted }
98
+ when !ts_declared_statement(node) { declares value, type, query name in nearest class hoisted }
99
+ }
100
+ in ts_interface, ts_type_alias { declares type name in nearest block hoisted }
101
+ in ts_enum { declares value, type, query name in nearest block hoisted }
102
+ in ts_namespace {
103
+ declares value, type, query name in nearest block hoisted
104
+ }
105
+ # `namespace A.B {}` declares `A`.
106
+ in ts_qualified_name { targets left }
107
+ in ts_type_parameter { declares type name in nearest function, class, type_parameters, conditional, mapped hoisted }
108
+ # `infer X` declares for the whole conditional.
109
+ in ts_infer { declares type type_parameter.name in nearest conditional hoisted }
110
+
111
+ in ts_import {
112
+ declares type, query default_import in nearest module hoisted
113
+ declares type, query namespace in nearest module hoisted
114
+ declares type, query specifier in nearest module hoisted
115
+ when kind != "type" {
116
+ declares value default_import in nearest module hoisted
117
+ declares value namespace in nearest module hoisted
118
+ declares value specifier where it.parent.kind != "type" in nearest module hoisted
119
+ }
120
+ }
121
+ in ts_import_specifier { targets local ?? imported }
122
+ in ts_import_equals {
123
+ declares type, query name in nearest block hoisted
124
+ when kind != "type" { declares value name in nearest block hoisted }
125
+ }
126
+
127
+ # References: identifiers, except where they only name something (member
128
+ # and key names, labels, the far side of a rename).
129
+ in ts_decorator let ts_name_decorator_method when {
130
+ parent is ts_method then parent.node
131
+ parent is ts_parameter && parent.parent is ts_method then parent.parent.node
132
+ else then null
133
+ }
134
+ in ts_identifier {
135
+ let ts_name_outside_method containing(ts_decorator).ts_name_decorator_method
136
+ when !(is (ts_member.property, ts_method.key, ts_class_property.key, ts_property_signature.key, ts_method_signature.key,
137
+ ts_enum_member.name, ts_pattern_property.key, ts_import_specifier.imported, ts_export_specifier.local,
138
+ ts_export_specifier.exported, ts_labeled.label, ts_break.label, ts_continue.label, ts_jsx_attribute.name,
139
+ ts_tuple_member.name, ts_qualified_name.right, ts_type_parameter.name, ts_type_alias.name,
140
+ ts_interface.name, ts_import.default_import, ts_import.namespace, ts_export.namespace, ts_import_specifier.local, ts_import_equals.name)
141
+ || is ts_property.key && parent.value != null && !parent.flow_shorthand_default
142
+ # `[key: string]: T` names nothing.
143
+ || is ts_parameter.name && parent is ts_index_signature.parameter) {
144
+ when ts_name_outside_method != null { resolves value outside ts_name_outside_method }
145
+ when ts_name_outside_method == null { resolves value }
146
+ }
147
+ }
148
+ in ts_type_reference { resolves type name }
149
+ # `export { x }` uses `x`; `export { x } from "m"` does not.
150
+ in ts_export_specifier { when parent.from == null { resolves value, type local } }
151
+ in ts_qualified_name { resolves value, type left }
152
+ in ts_export { resolves value, type expression }
153
+ in ts_export_assignment { resolves value, type expression }
154
+
155
+ in ts_assignment { when operator == "=" { targets left } }
156
+ in ts_array { targets element }
157
+ in ts_object { targets property }
158
+ in ts_property {
159
+ when flow_shorthand_default { targets key }
160
+ when !flow_shorthand_default { targets value ?? key }
161
+ }
162
+ in ts_spread { targets argument }
163
+ in ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion { targets expression }
164
+
165
+ group name_type_syntax ts_type_reference, ts_type_keyword, ts_type_parameter, ts_union, ts_intersection,
166
+ ts_array_type, ts_parenthesized_type, ts_qualified_name, ts_type_operator, ts_indexed_access,
167
+ ts_conditional_type, ts_infer, ts_function_type, ts_type_literal, ts_property_signature,
168
+ ts_method_signature, ts_call_signature, ts_index_signature, ts_tuple_type, ts_tuple_member,
169
+ ts_type_query, ts_import_type, ts_literal_type, ts_template_type, ts_type_predicate, ts_mapped_type
170
+ in ts_node let name_type_access is name_type_syntax || parent.name_type_access == true
171
+ in ts_node let name_write_owner when {
172
+ is (ts_for_in.left, ts_for_of.left) then parent.node
173
+ is ts_declarator.name then when {
174
+ parent.init != null then parent.node
175
+ parent.parent is (ts_for_in.left, ts_for_of.left) then parent.parent.parent.node
176
+ else then null
177
+ }
178
+ is ts_assignment.left then
179
+ if parent.flow_destructuring_target == true then parent.name_write_owner else parent.node
180
+ is ts_update.argument || is ts_unary.argument && parent.operator == "delete" && is (ts_member, ts_index)
181
+ then parent.node
182
+ is (ts_array.element, ts_array_pattern.element, ts_object.property, ts_object_pattern.property,
183
+ ts_object_pattern.rest, ts_rest.argument, ts_spread.argument, ts_assignment_pattern.left,
184
+ ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression, ts_non_null.expression,
185
+ ts_type_assertion.expression, ts_instantiation.expression, ts_property.value, ts_pattern_property.value,
186
+ ts_property.key, ts_pattern_property.key) && flow_destructuring_target == true then parent.name_write_owner
187
+ is (ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression,
188
+ ts_non_null.expression, ts_type_assertion.expression, ts_instantiation.expression) then parent.name_write_owner
189
+ else then null
190
+ }
191
+ group name_write_source ts_assignment, ts_update, ts_for_in, ts_for_of, ts_declarator
192
+ in name_write_source let name_write_identifiers {
193
+ value owner: node
194
+ value if is (ts_assignment, ts_for_in, ts_for_of) then node.left.node
195
+ else if is ts_update then node.argument.node else node.name.node
196
+ walk (item) item.children.nodes
197
+ filter (item) item is ts_identifier && item.name_write_owner == owner
198
+ unique
199
+ }
200
+ in ts_identifier, ts_member, ts_index, ts_array, ts_object, ts_array_pattern, ts_object_pattern let write_site name_write_owner.offset
201
+ in ts_identifier let name_syntax_access {
202
+ value writer: name_write_owner
203
+ value when {
204
+ writer is ts_update || writer is ts_assignment && writer.operator != "=" then "read_write"
205
+ writer != null then "write"
206
+ name_type_access || is ts_export_specifier.local && (parent.kind == "type" || parent.parent.kind == "type") then "type"
207
+ else then "read"
208
+ }
209
+ }
210
+ in ts_identifier let access {
211
+ value classes: name_role_classes(node, "reference")
212
+ value if classes == null then null else {
213
+ value syntax: name_syntax_access
214
+ value if syntax == "read" && !contains(classes, "value") then "type" else syntax
215
+ }
216
+ }
217
+
218
+ fn ts_name_lexical(classes) = any(classes, class => contains(["value", "type", "query"], class))
219
+ in ts_identifier let ts_name_declared ts_name_lexical(name_role_classes(node, "declaration"))
220
+ in ts_identifier let ts_name_referenced ts_name_lexical(name_role_classes(node, "reference"))
221
+ in ts_identifier let ts_name_binding when {
222
+ ts_name_declared then name_binding(node)
223
+ ts_name_referenced && scope_declaration != null then name_binding(scope_declaration)
224
+ else then null
225
+ }
226
+ in ts_identifier let ts_name_site {
227
+ value binding: if ts_name_declared then ts_name_binding else null
228
+ value scope: if binding != null then first(name_scopes(node, "declaration")) else null
229
+ value if scope == null then null else {
230
+ value name_scope_declarations(node, scope.id, binding.key)
231
+ find (site) site.node == node
232
+ }.owner
233
+ }
234
+ fn ts_name_parameter(n, owner) = {
235
+ value n
236
+ walk (current) if current != owner then current.parent.node else null
237
+ any (current) current is ts_parameter
238
+ }
239
+ fn ts_name_site_kind(n, owner) = when {
240
+ owner is ts_variable_declaration then if contains(["var", "let", "const"], owner.kind) then owner.kind else "using"
241
+ owner is (ts_function, ts_arrow, ts_method, ts_function_type, ts_call_signature, ts_method_signature)
242
+ && ts_name_parameter(n, owner) then "parameter"
243
+ owner is ts_function then if ts_declared_statement(owner) then "function" else "function_name"
244
+ owner is ts_class then if ts_declared_statement(owner) then "class" else "class_name"
245
+ owner is ts_block && owner.parent is ts_try then "catch"
246
+ owner is ts_interface then "interface"
247
+ owner is ts_type_alias then "type"
248
+ owner is ts_enum then "enum"
249
+ owner is ts_namespace then "namespace"
250
+ owner is (ts_type_parameter, ts_infer) then "type_parameter"
251
+ owner is (ts_import, ts_import_equals) then "import"
252
+ else then ""
253
+ }
254
+ in ts_identifier let ts_name_kind if ts_name_site != null then ts_name_site_kind(node, ts_name_site) else null
255
+
256
+ in ts_node let declaration null
257
+ in ts_node let declarations []
258
+ in ts_node let references []
259
+ in ts_node let scope null
260
+ in ts_node let declaration_kind null
261
+ in ts_node let declaration_kinds null
262
+ in ts_node let reference_kind null
263
+ in ts_identifier let declaration ts_name_binding.node
264
+ in ts_identifier let declarations ts_name_binding.declarations ?? []
265
+ in ts_identifier let references
266
+ if ts_name_declared && ts_name_binding.node == node then ts_name_binding.references else []
267
+ in ts_identifier let scope when {
268
+ ts_name_declared then first(name_scopes(node, "declaration")).owner
269
+ ts_name_referenced then {
270
+ value name_scopes(node, "reference")
271
+ find (scope) any(scope.classes, class => contains(["module", "function", "block", "function_name", "class", "catch", "type_parameters", "conditional", "mapped"], class))
272
+ }.owner
273
+ else then null
274
+ }
275
+ in ts_identifier let declaration_kind
276
+ if ts_name_declared && ts_name_binding.node == node then ts_name_kind else null
277
+ in ts_identifier let declaration_kinds
278
+ if ts_name_declared then map(ts_name_binding.declarations, site => site.ts_name_kind) else null
279
+ in ts_identifier let reference_kind
280
+ if !ts_name_declared && ts_name_referenced then ts_name_binding.node.ts_name_kind else null
281
+ in ts_node let binding_declarations []
282
+ in ts_program let binding_declarations {
283
+ value descendants(ts_identifier)
284
+ filter (site) site.ts_name_declared || site is ts_enum_member.name
285
+ sort_by (site) site.offset
286
+ }
287
+ in ts_node let writer name_write_owner
288
+ in ts_node let conditional_ancestor when {
289
+ is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_conditional.test) then parent.node
290
+ parent is (ts_function, ts_arrow, ts_method) then null
291
+ else then parent.conditional_ancestor
292
+ }
293
+
294
+ in ts_node let ts_name_ambient contains([] node.modifier, "declare") || parent.ts_name_ambient == true
295
+ fn ts_name_self_owner(n) = when {
296
+ n.ts_name_kind == "function" then n.ts_name_site
297
+ contains(["var", "let", "const", "using"], n.ts_name_kind)
298
+ && n.parent is ts_declarator && n.parent.name.node == n
299
+ && n.parent.init is (ts_function, ts_arrow) then n.parent.init.node
300
+ else then null
301
+ }
302
+ in ts_identifier let ts_name_usage_references {
303
+ value binding: if ts_name_declared then ts_name_binding else null
304
+ value refs: filter(binding.references, reference => reference is ts_identifier)
305
+ value if binding == null || binding.node != node || any(refs, reference => reference.access == null) then null else refs
306
+ }
307
+ in ts_node {
308
+ let uses null
309
+ let value_uses null
310
+ let self_uses null
311
+ let writes null
312
+ let ambient null
313
+ let resolved null
314
+ let exported null
315
+ let restricted_binding false
316
+ }
317
+ in ts_identifier {
318
+ let uses if ts_name_usage_references == null then null
319
+ else count(filter(ts_name_usage_references, reference => reference.access != "write"))
320
+ let value_uses if ts_name_usage_references == null then null
321
+ else count(filter(ts_name_usage_references, reference => contains(["read", "read_write"], reference.access)))
322
+ let self_uses if ts_name_usage_references == null then null else {
323
+ value self_owner: ts_name_self_owner(node)
324
+ value if self_owner == null then 0 else count(filter(ts_name_usage_references, reference => reference.access != "write"
325
+ && self_owner.offset <= reference.offset && reference.end_offset <= self_owner.end_offset))
326
+ }
327
+ let writes if ts_name_usage_references == null then null
328
+ else count(filter(ts_name_usage_references, reference => contains(["write", "read_write"], reference.access)))
329
+ let ambient if ts_name_declared && ts_name_binding.node == node
330
+ then all(ts_name_binding.declarations, declaration => declaration.ts_name_ambient) else null
331
+ let exported if ts_name_declared && ts_name_binding.node == node
332
+ then any(ts_name_binding.declarations, declaration =>
333
+ declaration.ts_name_kind != "parameter" && declaration.ts_name_site.parent is ts_export)
334
+ else null
335
+ let restricted_binding contains(["NaN", "Infinity", "undefined", "eval", "arguments", "globalThis"], identifier_name)
336
+ && ts_name_declared && ts_name_binding != null
337
+ && (identifier_name != "undefined" || ts_name_binding.node.writes != 0
338
+ || any(ts_name_binding.declarations, declaration =>
339
+ declaration.parent not ts_declarator || declaration.parent.name.node != declaration
340
+ || declaration.parent.init != null))
341
+ let resolved when {
342
+ ts_name_declared || !ts_name_referenced then null
343
+ ts_name_binding != null then if ts_name_binding.node.ts_name_kind == "import" then "import" else "local"
344
+ contains(name_role_classes(node, "reference"), "value") && ts_arguments_owner(node) != null then "local"
345
+ contains(["read", "write", "read_write"], access) then "global"
346
+ else then null
347
+ }
348
+ }