@data-vegle/rbxts-go 0.0.0-stage → 1.0.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 (220) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +181 -2
  3. package/README.rbxtsgo.md +169 -0
  4. package/THIRD_PARTY_NOTICES.md +745 -0
  5. package/cli/cli.go +454 -0
  6. package/compile/compile.go +388 -0
  7. package/compile/declarations.go +221 -0
  8. package/compile/incremental.go +349 -0
  9. package/compile/times.go +9 -0
  10. package/docs/plugin-api.md +243 -0
  11. package/go.mod +20 -0
  12. package/go.sum +24 -0
  13. package/internal/difftest/difftest.go +530 -0
  14. package/internal/plugintest/plugintest.go +181 -0
  15. package/luau/create.go +351 -0
  16. package/luau/doc.go +71 -0
  17. package/luau/globals.go +178 -0
  18. package/luau/jsnum.go +188 -0
  19. package/luau/kinds.go +128 -0
  20. package/luau/list.go +227 -0
  21. package/luau/nodes.go +479 -0
  22. package/luau/operators.go +46 -0
  23. package/luau/render.go +134 -0
  24. package/luau/render_expressions.go +264 -0
  25. package/luau/render_fields.go +81 -0
  26. package/luau/render_indexable.go +81 -0
  27. package/luau/render_state.go +134 -0
  28. package/luau/render_statements.go +235 -0
  29. package/luau/render_util.go +296 -0
  30. package/luau/solve_temp_ids.go +212 -0
  31. package/luau/typeguards.go +175 -0
  32. package/luau/util.go +184 -0
  33. package/luau/visit.go +138 -0
  34. package/npm/index.d.ts +3 -0
  35. package/npm/rbxts-go.sh +187 -0
  36. package/package.json +45 -5
  37. package/patches/typescript/0001-checker-exports.patch +40 -0
  38. package/patches/typescript/0002-iterable-arity.patch +15 -0
  39. package/patches/typescript/0003-context-sensitive-this.patch +15 -0
  40. package/patches/typescript/0004-printer-keep-parse-tree-parentheses.patch +26 -0
  41. package/pluginapi/host.go +31 -0
  42. package/pluginapi/pluginapi.go +153 -0
  43. package/plugins/LICENSE-rbxts-transformer-setget +18 -0
  44. package/plugins/apply.go +342 -0
  45. package/plugins/config.go +431 -0
  46. package/plugins/fallback.go +84 -0
  47. package/plugins/fixemit.go +60 -0
  48. package/plugins/native.go +271 -0
  49. package/plugins/session.go +236 -0
  50. package/plugins/setget.go +478 -0
  51. package/project/copy.go +333 -0
  52. package/project/include/Promise.lua +2068 -0
  53. package/project/include/RuntimeLib.lua +260 -0
  54. package/project/nodepath/nodepath.go +917 -0
  55. package/project/pathtranslator/pathtranslator.go +214 -0
  56. package/project/project.go +498 -0
  57. package/project/rojoresolver/json.go +248 -0
  58. package/project/rojoresolver/rojoresolver.go +559 -0
  59. package/project/shared/constants.go +83 -0
  60. package/project/shared/diagnostic.go +238 -0
  61. package/project/shared/diagnostics.go +388 -0
  62. package/project/shared/elapsed.go +14 -0
  63. package/project/shared/eol_other.go +6 -0
  64. package/project/shared/eol_windows.go +4 -0
  65. package/project/shared/kleur.go +40 -0
  66. package/project/shared/util.go +183 -0
  67. package/scripts/build-with-plugins.sh +130 -0
  68. package/scripts/setup-third-party.sh +75 -0
  69. package/transformer/add_index_diagnostics.go +21 -0
  70. package/transformer/add_one_if_array_type.go +15 -0
  71. package/transformer/array_binding_pattern_contains_hoists.go +25 -0
  72. package/transformer/assert_never.go +24 -0
  73. package/transformer/assignment.go +64 -0
  74. package/transformer/call_macros.go +70 -0
  75. package/transformer/check_variable_hoist.go +125 -0
  76. package/transformer/clean_module_name.go +28 -0
  77. package/transformer/constructor_macros.go +122 -0
  78. package/transformer/convert_to_indexable_expression.go +12 -0
  79. package/transformer/create_binary_from_operator.go +86 -0
  80. package/transformer/create_continue_statements.go +68 -0
  81. package/transformer/create_get_service.go +15 -0
  82. package/transformer/create_hoist_declaration.go +27 -0
  83. package/transformer/create_import_expression.go +220 -0
  84. package/transformer/create_string_from_literal.go +195 -0
  85. package/transformer/create_truthiness_checks.go +63 -0
  86. package/transformer/create_type_check.go +9 -0
  87. package/transformer/ensure_transform_order.go +53 -0
  88. package/transformer/expression_chain.go +27 -0
  89. package/transformer/expression_might_mutate.go +60 -0
  90. package/transformer/find_constructor.go +15 -0
  91. package/transformer/fixup_whitespace_and_decode_entities.go +377 -0
  92. package/transformer/get_accessor_for_binding_type.go +160 -0
  93. package/transformer/get_add_iterable_to_array_builder.go +363 -0
  94. package/transformer/get_assignable_value.go +15 -0
  95. package/transformer/get_constant_value_literal.go +20 -0
  96. package/transformer/get_declared_variables.go +34 -0
  97. package/transformer/get_extends_node.go +23 -0
  98. package/transformer/get_flags.go +21 -0
  99. package/transformer/get_kind_name.go +18 -0
  100. package/transformer/get_original_symbol_of_node.go +16 -0
  101. package/transformer/get_source_file_from_module_specifier.go +68 -0
  102. package/transformer/get_statements.go +12 -0
  103. package/transformer/has_multiple_definitions.go +18 -0
  104. package/transformer/hoist_functions_past_final_statement.go +43 -0
  105. package/transformer/identifier_macros.go +17 -0
  106. package/transformer/is_blocked_by_try_statement.go +19 -0
  107. package/transformer/is_method.go +104 -0
  108. package/transformer/is_symbol_mutable.go +26 -0
  109. package/transformer/is_symbol_of_value.go +11 -0
  110. package/transformer/is_used_as_statement.go +25 -0
  111. package/transformer/is_valid_method_index_without_call.go +36 -0
  112. package/transformer/macro_manager.go +92 -0
  113. package/transformer/object_accessor.go +32 -0
  114. package/transformer/offset.go +58 -0
  115. package/transformer/pointer.go +53 -0
  116. package/transformer/property_call_macros.go +1035 -0
  117. package/transformer/state.go +606 -0
  118. package/transformer/transform_array_assignment_pattern.go +65 -0
  119. package/transformer/transform_array_binding_pattern.go +49 -0
  120. package/transformer/transform_array_literal_expression.go +97 -0
  121. package/transformer/transform_await_expression.go +12 -0
  122. package/transformer/transform_binary_expression.go +221 -0
  123. package/transformer/transform_binding_name.go +25 -0
  124. package/transformer/transform_block.go +14 -0
  125. package/transformer/transform_boolean_literal.go +13 -0
  126. package/transformer/transform_break_statement.go +26 -0
  127. package/transformer/transform_call_expression.go +266 -0
  128. package/transformer/transform_class_constructor.go +125 -0
  129. package/transformer/transform_class_declaration.go +12 -0
  130. package/transformer/transform_class_expression.go +14 -0
  131. package/transformer/transform_class_like_declaration.go +355 -0
  132. package/transformer/transform_conditional_expression.go +70 -0
  133. package/transformer/transform_continue_statement.go +30 -0
  134. package/transformer/transform_decorators.go +253 -0
  135. package/transformer/transform_delete_expression.go +17 -0
  136. package/transformer/transform_do_statement.go +39 -0
  137. package/transformer/transform_element_access_expression.go +114 -0
  138. package/transformer/transform_entity_name.go +21 -0
  139. package/transformer/transform_enum_declaration.go +135 -0
  140. package/transformer/transform_export_assignment.go +57 -0
  141. package/transformer/transform_export_declaration.go +150 -0
  142. package/transformer/transform_expression.go +120 -0
  143. package/transformer/transform_expression_statement.go +71 -0
  144. package/transformer/transform_for_of_statement.go +461 -0
  145. package/transformer/transform_for_statement.go +599 -0
  146. package/transformer/transform_function_declaration.go +84 -0
  147. package/transformer/transform_function_expression.go +48 -0
  148. package/transformer/transform_identifier.go +194 -0
  149. package/transformer/transform_if_statement.go +44 -0
  150. package/transformer/transform_import_declaration.go +146 -0
  151. package/transformer/transform_import_equals_declaration.go +46 -0
  152. package/transformer/transform_import_expression.go +36 -0
  153. package/transformer/transform_initializer.go +22 -0
  154. package/transformer/transform_interpolated_string_part.go +12 -0
  155. package/transformer/transform_jsx.go +41 -0
  156. package/transformer/transform_jsx_attributes.go +110 -0
  157. package/transformer/transform_jsx_children.go +50 -0
  158. package/transformer/transform_jsx_element.go +14 -0
  159. package/transformer/transform_jsx_expression.go +20 -0
  160. package/transformer/transform_jsx_fragment.go +42 -0
  161. package/transformer/transform_jsx_self_closing_element.go +13 -0
  162. package/transformer/transform_jsx_tag_name.go +53 -0
  163. package/transformer/transform_logical.go +160 -0
  164. package/transformer/transform_logical_or_coalescing_assignment_expression.go +103 -0
  165. package/transformer/transform_method_declaration.go +102 -0
  166. package/transformer/transform_module_declaration.go +143 -0
  167. package/transformer/transform_new_expression.go +27 -0
  168. package/transformer/transform_no_substitution_template_literal.go +16 -0
  169. package/transformer/transform_numeric_literal.go +15 -0
  170. package/transformer/transform_object_assignment_pattern.go +86 -0
  171. package/transformer/transform_object_binding_pattern.go +49 -0
  172. package/transformer/transform_object_literal_expression.go +107 -0
  173. package/transformer/transform_omitted_expression.go +9 -0
  174. package/transformer/transform_optional_chain.go +312 -0
  175. package/transformer/transform_parameters.go +124 -0
  176. package/transformer/transform_parenthesized_expression.go +17 -0
  177. package/transformer/transform_property_access_expression.go +37 -0
  178. package/transformer/transform_property_declaration.go +33 -0
  179. package/transformer/transform_property_name.go +20 -0
  180. package/transformer/transform_return_statement.go +83 -0
  181. package/transformer/transform_source_file.go +230 -0
  182. package/transformer/transform_spread_element.go +35 -0
  183. package/transformer/transform_statement.go +84 -0
  184. package/transformer/transform_statement_list.go +90 -0
  185. package/transformer/transform_string_literal.go +22 -0
  186. package/transformer/transform_super_keyword.go +9 -0
  187. package/transformer/transform_switch_statement.go +145 -0
  188. package/transformer/transform_tagged_template_expression.go +34 -0
  189. package/transformer/transform_template_expression.go +35 -0
  190. package/transformer/transform_this_expression.go +33 -0
  191. package/transformer/transform_throw_statement.go +18 -0
  192. package/transformer/transform_try_statement.go +192 -0
  193. package/transformer/transform_type_expression.go +12 -0
  194. package/transformer/transform_unary_expression.go +79 -0
  195. package/transformer/transform_variable_statement.go +178 -0
  196. package/transformer/transform_void_expression.go +13 -0
  197. package/transformer/transform_while_statement.go +34 -0
  198. package/transformer/transform_writable.go +69 -0
  199. package/transformer/transform_yield_expression.go +56 -0
  200. package/transformer/type_guards.go +30 -0
  201. package/transformer/types.go +381 -0
  202. package/transformer/validate_identifier.go +17 -0
  203. package/transformer/validate_method_assignment.go +106 -0
  204. package/transformer/validate_not_any.go +37 -0
  205. package/transformer/wrap_expression_statement.go +20 -0
  206. package/transformer/wrap_return_if_lua_tuple.go +63 -0
  207. package/transformer/wrap_statements_as_generator.go +22 -0
  208. package/tsbridge/affected.go +141 -0
  209. package/tsbridge/config.go +71 -0
  210. package/tsbridge/diagnostics.go +128 -0
  211. package/tsbridge/doc.go +16 -0
  212. package/tsbridge/macros.go +207 -0
  213. package/tsbridge/macrospec.go +30 -0
  214. package/tsbridge/pool.go +142 -0
  215. package/tsbridge/project.go +137 -0
  216. package/tsbridge/reprint.go +120 -0
  217. package/tsbridge/update.go +128 -0
  218. package/watch/poll.go +126 -0
  219. package/watch/session.go +651 -0
  220. package/watch/watch.go +180 -0
@@ -0,0 +1,271 @@
1
+ package plugins
2
+
3
+ // Native plugins: the transformers registered with pluginapi.Register. docs/plugin-api.md is the contract this file
4
+ // keeps. What it adds to the setget step is only how a file's tree is made. Printing it, making the program from the
5
+ // printed text and mapping diagnostics back are the same code.
6
+
7
+ import (
8
+ "context"
9
+ "fmt"
10
+ "runtime/debug"
11
+
12
+ "github.com/microsoft/TypeScript/tsc/internal/ast"
13
+ "github.com/microsoft/TypeScript/tsc/internal/diagnostics"
14
+ "github.com/microsoft/TypeScript/tsc/internal/printer"
15
+ "github.com/microsoft/TypeScript/tsc/internal/scanner"
16
+ "github.com/microsoft/TypeScript/tsc/internal/tspath"
17
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/pluginapi"
18
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/project/pathtranslator"
19
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/project/rojoresolver"
20
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
21
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/tsbridge"
22
+ )
23
+
24
+ // Build is what native plugins may read about the build besides the program. It becomes the pluginapi.Host.
25
+ type Build struct {
26
+ Data *shared.ProjectData
27
+ Translator *pathtranslator.Translator
28
+ // RojoResolver is nil for a project with no Rojo file.
29
+ RojoResolver *rojoresolver.Resolver
30
+ }
31
+
32
+ // NewBuild makes a Build from what a compile has at hand. resolver is the one project.PrepareCompile made, which is a
33
+ // made-up one when the project has no Rojo file. Plugins get nil then.
34
+ func NewBuild(data *shared.ProjectData, translator *pathtranslator.Translator, resolver *rojoresolver.Resolver) *Build {
35
+ if data.RojoConfigPath == "" {
36
+ resolver = nil
37
+ }
38
+ return &Build{Data: data, Translator: translator, RojoResolver: resolver}
39
+ }
40
+
41
+ // nativeRun holds the transformers of a decision's plugins. A build uses one once. A Session keeps one, so that the
42
+ // transformers live from one program to the next.
43
+ type nativeRun struct {
44
+ // transformers is nil until New has run for every plugin, and again after anything went wrong in one of them.
45
+ transformers []pluginapi.Transformer
46
+ // previous is the program the transformers last saw, by path, for Updater's changed files.
47
+ previous map[tspath.PathKey]*ast.SourceFile
48
+ }
49
+
50
+ type nativeResult struct {
51
+ // outcomes has each file's printed text, in the order of the files.
52
+ outcomes []fileOutcome
53
+ // exit is set when a plugin called Exit. Nothing else is set then.
54
+ exit []string
55
+ warnings []*shared.Diagnostic
56
+ }
57
+
58
+ // call runs one call into a plugin. A panic comes back as an error that names the plugin, and Exit's as its lines.
59
+ func call(plugin, doing string, fn func()) (exit []string, err error) {
60
+ defer func() {
61
+ switch r := recover().(type) {
62
+ case nil:
63
+ case *pluginapi.ExitRequest:
64
+ exit = append([]string{}, r.Lines...)
65
+ case *pluginExit:
66
+ exit = append([]string{}, r.lines...)
67
+ default:
68
+ err = fmt.Errorf("internal error in the plugin %q %s: %v", plugin, doing, r)
69
+ if shared.LogService.Verbose {
70
+ err = fmt.Errorf("%w\n%s", err, debug.Stack())
71
+ }
72
+ }
73
+ }()
74
+ fn()
75
+ return nil, nil
76
+ }
77
+
78
+ // changedFiles are the files of the program whose text isn't what previous has for the same path.
79
+ func changedFiles(previous map[tspath.PathKey]*ast.SourceFile, program []*ast.SourceFile) []*ast.SourceFile {
80
+ var changed []*ast.SourceFile
81
+ for _, file := range program {
82
+ if old := previous[file.PathKey()]; old != file && (old == nil || old.Text() != file.Text()) {
83
+ changed = append(changed, file)
84
+ }
85
+ }
86
+ return changed
87
+ }
88
+
89
+ // transform runs the plugin pass over one program: New or Update, then every file through setget (if it is on) and
90
+ // the plugins in order, then the printing, then Finish. It all happens on the calling goroutine with one checker,
91
+ // apart from the printing, which calls no plugin. names gives setget's accessor names, and is only called for setget.
92
+ // reused holds, by path, the outcome of the files that are not transformed again: they keep that outcome. It is nil when
93
+ // every file goes through.
94
+ //
95
+ // An error of AddDiagnostic comes back as a *shared.DiagnosticError.
96
+ func (r *nativeRun) transform(ctx context.Context, p *tsbridge.Project, decision Decision, files []*ast.SourceFile, build *Build, watch bool, reused map[tspath.PathKey]*fileOutcome, names func() *accessorNames) (result *nativeResult, err error) {
97
+ // After a panic, an Exit or an error, nothing says what state a transformer is in. The next program starts with New.
98
+ kept := false
99
+ defer func() {
100
+ if !kept {
101
+ r.transformers, r.previous = nil, nil
102
+ }
103
+ }()
104
+
105
+ host := &pluginapi.Host{
106
+ Program: p.Program(),
107
+ Options: p.Options(),
108
+ Files: files,
109
+ Checker: p.PluginChecker(ctx),
110
+ Watch: watch,
111
+ }
112
+ if build != nil {
113
+ host.Data, host.PathTranslator, host.RojoResolver = build.Data, build.Translator, build.RojoResolver
114
+ }
115
+
116
+ create := func(use PluginUse) (pluginapi.Transformer, []string, error) {
117
+ var transformer pluginapi.Transformer
118
+ var newErr error
119
+ exit, err := call(use.Plugin.Name, "in New", func() { transformer, newErr = use.Plugin.New(host, use.Config()) })
120
+ switch {
121
+ case exit != nil || err != nil:
122
+ return nil, exit, err
123
+ case newErr != nil:
124
+ return nil, nil, newErr
125
+ case transformer == nil:
126
+ return nil, nil, fmt.Errorf("internal error in the plugin %q: New returned no transformer", use.Plugin.Name)
127
+ }
128
+ return transformer, nil, nil
129
+ }
130
+ first := r.transformers == nil
131
+ if first {
132
+ r.transformers = make([]pluginapi.Transformer, len(decision.Plugins))
133
+ }
134
+ var changed []*ast.SourceFile
135
+ if !first {
136
+ changed = changedFiles(r.previous, p.Program().SourceFiles())
137
+ }
138
+ for i, use := range decision.Plugins {
139
+ updater, canUpdate := r.transformers[i].(pluginapi.Updater)
140
+ if first || !canUpdate {
141
+ transformer, exit, err := create(use)
142
+ if exit != nil {
143
+ return &nativeResult{exit: exit}, nil
144
+ }
145
+ if err != nil {
146
+ return nil, err
147
+ }
148
+ r.transformers[i] = transformer
149
+ continue
150
+ }
151
+ var updateErr error
152
+ exit, err := call(use.Plugin.Name, "in Update", func() { updateErr = updater.Update(host, changed) })
153
+ if exit != nil {
154
+ return &nativeResult{exit: exit}, nil
155
+ }
156
+ if err != nil {
157
+ return nil, err
158
+ }
159
+ if updateErr != nil {
160
+ return nil, updateErr
161
+ }
162
+ }
163
+ r.previous = p.Program().FilesByPath()
164
+
165
+ var setgetNames *accessorNames
166
+ if decision.Setget {
167
+ setgetNames = names()
168
+ }
169
+ transformed := make([]*ast.SourceFile, len(files))
170
+ emits := make([]*printer.EmitContext, len(files))
171
+ var reported []*shared.Diagnostic
172
+ for i, file := range files {
173
+ if reused[file.PathKey()] != nil {
174
+ continue
175
+ }
176
+ emit := printer.NewEmitContext()
177
+ node := file.AsNode()
178
+ doing := "while transforming " + file.FileName().AsString()
179
+ if decision.Setget {
180
+ exit, err := call(SetgetPlugin, doing, func() {
181
+ node = newTransformContextIn(emit, file, host.Checker, decision.Prefix, setgetNames).transformFile().AsNode()
182
+ })
183
+ if exit != nil {
184
+ return &nativeResult{exit: exit}, nil
185
+ }
186
+ if err != nil {
187
+ return nil, err
188
+ }
189
+ }
190
+ fileContext := pluginapi.NewFileContext(host, emit, func(category diagnostics.Category, at *ast.Node, message string) {
191
+ reported = append(reported, pluginDiagnostic(emit, file, category, at, message))
192
+ })
193
+ for j, transformer := range r.transformers {
194
+ name := decision.Plugins[j].Plugin.Name
195
+ var out *ast.Node
196
+ exit, err := call(name, doing, func() { out = transformer.TransformFile(fileContext, file, node) })
197
+ if exit != nil {
198
+ return &nativeResult{exit: exit}, nil
199
+ }
200
+ if err != nil {
201
+ return nil, err
202
+ }
203
+ if out == nil || out.Kind != ast.KindSourceFile {
204
+ return nil, fmt.Errorf("internal error in the plugin %q %s: TransformFile didn't return a SourceFile node", name, doing)
205
+ }
206
+ node = out
207
+ }
208
+ transformed[i], emits[i] = node.AsSourceFile(), emit
209
+ }
210
+ if shared.HasErrors(reported) {
211
+ // The plugins did nothing wrong, so they go on to the next program as they are.
212
+ kept = true
213
+ return nil, &shared.DiagnosticError{Diagnostics: reported}
214
+ }
215
+
216
+ // Printing comes before Finish, so that a tree that can't be printed stops the build before a plugin writes its
217
+ // files. No plugin runs during it, and each file has its own emit context, so the files print side by side.
218
+ outcomes := make([]fileOutcome, len(files))
219
+ forEachParallel(files, func(i int, file *ast.SourceFile) {
220
+ defer func() {
221
+ if r := recover(); r != nil {
222
+ outcomes[i].panicText = fmt.Sprint(r)
223
+ }
224
+ }()
225
+ if old := reused[file.PathKey()]; old != nil {
226
+ outcomes[i] = *old
227
+ return
228
+ }
229
+ printFile(file, transformed[i], emits[i], &outcomes[i])
230
+ })
231
+ for i := range outcomes {
232
+ if outcomes[i].panicText != "" {
233
+ return nil, fmt.Errorf("internal error while printing %s after the plugins: %s", files[i].FileName().AsString(), outcomes[i].panicText)
234
+ }
235
+ }
236
+
237
+ for i, transformer := range r.transformers {
238
+ var finishErr error
239
+ exit, err := call(decision.Plugins[i].Plugin.Name, "in Finish", func() { finishErr = transformer.Finish() })
240
+ if exit != nil {
241
+ return &nativeResult{exit: exit}, nil
242
+ }
243
+ if err != nil {
244
+ return nil, err
245
+ }
246
+ if finishErr != nil {
247
+ return nil, finishErr
248
+ }
249
+ }
250
+ kept = true
251
+ return &nativeResult{outcomes: outcomes, warnings: reported}, nil
252
+ }
253
+
254
+ // pluginDiagnostic is what AddDiagnostic makes: a diagnostic at the node of the source that at was made from. A node
255
+ // with no place in any source, such as one a plugin made from nothing, puts it at the start of the file.
256
+ func pluginDiagnostic(emit *printer.EmitContext, file *ast.SourceFile, category diagnostics.Category, at *ast.Node, message string) *shared.Diagnostic {
257
+ target, start, length := file, 0, 0
258
+ if at != nil {
259
+ original := emit.MostOriginal(at)
260
+ if ast.IsParseTreeNode(original) {
261
+ if owner := ast.GetSourceFileOfNode(original); owner != nil {
262
+ target = owner
263
+ }
264
+ }
265
+ if original.Pos() >= 0 && original.End() >= original.Pos() && original.End() <= len(target.Text()) {
266
+ start = scanner.GetTokenPosOfNode(original, target, false)
267
+ length = original.End() - start
268
+ }
269
+ }
270
+ return shared.LocatedDiagnostic(message, category, target, start, length)
271
+ }
@@ -0,0 +1,236 @@
1
+ package plugins
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "maps"
7
+
8
+ "github.com/microsoft/TypeScript/tsc/internal/ast"
9
+ "github.com/microsoft/TypeScript/tsc/internal/tspath"
10
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/tsbridge"
11
+ )
12
+
13
+ // Session is Apply for a process that compiles many times, as watch mode does. The result must equal what Apply gives on
14
+ // the same sources, and watch's edit sequence test checks that against a clean build. It holds because of what each step
15
+ // reads:
16
+ //
17
+ // - The accessor names come from one scan per file, so a file's scan is good until that file changes.
18
+ // - A file's printed text depends on the file, the names, the prefix and what the checker answers. The checker only
19
+ // sees through imports and the global scope, so tsbridge.AffectedFiles on the source program gives the files whose
20
+ // text can differ. A change in the names redoes everything, as a filter that changed can't be reasoned about.
21
+ // - The printed program depends only on the printed text and the other files. A file whose text came out the same keeps
22
+ // its parsed and bound object, so what comes after can see that nothing changed for it.
23
+ //
24
+ // Native plugins don't get this treatment. With them, every cycle runs every plugin over every file, on the same
25
+ // transformers (see "Watch mode" in docs/plugin-api.md), because nothing says what a plugin's result for one file
26
+ // depends on. What is kept is the last step: a file whose printed text came out the same keeps its parsed and bound
27
+ // object, so the compile after this still only does the files that changed.
28
+ //
29
+ // A Session belongs to one decision: make a new one after the tsconfig changes.
30
+ type Session struct {
31
+ facts map[*ast.SourceFile]fileAccessorFacts
32
+ names *accessorNames
33
+ outcomes map[tspath.PathKey]*fileOutcome
34
+ lastSource *tsbridge.Project
35
+ printed *tsbridge.Project
36
+ texts map[tspath.PathKey]string
37
+ // native holds the transformers of the native plugins between programs.
38
+ native *nativeRun
39
+ // LastRedone is how many files the last Apply transformed again, for --verbose.
40
+ LastRedone int
41
+ }
42
+
43
+ // NewSession makes a Session with nothing to reuse.
44
+ func NewSession() *Session { return &Session{} }
45
+
46
+ // Apply is Apply. When reuse is false it does what a first run does.
47
+ func (s *Session) Apply(ctx context.Context, p *tsbridge.Project, decision Decision, files []*ast.SourceFile, build *Build, reuse bool) (*Applied, error) {
48
+ applied := &Applied{Project: p, Files: files}
49
+ if !decision.Reprints() {
50
+ return applied, nil
51
+ }
52
+ if decision.Action == ActionPlugins {
53
+ return s.applyNative(ctx, p, decision, files, build, reuse)
54
+ }
55
+
56
+ names := s.collectNames(p)
57
+ change := tsbridge.AffectedFiles(s.lastSource, p)
58
+ if !reuse || s.names == nil || !names.same(s.names) {
59
+ change = &tsbridge.Change{Full: true, Reason: "accessor names changed"}
60
+ }
61
+ var redo []*ast.SourceFile
62
+ for _, file := range files {
63
+ if change.Reaches(file) || s.outcomes[file.PathKey()] == nil {
64
+ redo = append(redo, file)
65
+ }
66
+ }
67
+ s.LastRedone = len(redo)
68
+
69
+ outcomes := make([]fileOutcome, len(redo))
70
+ index := make(map[*ast.SourceFile]int, len(redo))
71
+ uses := make([]bool, len(redo))
72
+ if !names.empty() {
73
+ forEachParallel(redo, func(i int, file *ast.SourceFile) { uses[i] = fileUsesName(file, names) })
74
+ }
75
+ var needChecker, noChecker []*ast.SourceFile
76
+ for i, file := range redo {
77
+ index[file] = i
78
+ if uses[i] {
79
+ needChecker = append(needChecker, file)
80
+ } else {
81
+ noChecker = append(noChecker, file)
82
+ }
83
+ }
84
+ if len(needChecker) > 0 {
85
+ workers := p.NewWorkers(ctx, needChecker, tsbridge.DefaultMacroSpec())
86
+ p.RunWorkers(workers, func(w *tsbridge.Worker) {
87
+ for _, file := range w.Files() {
88
+ outcome := &outcomes[index[file]]
89
+ transformFile(file, w.Checker(), decision.Prefix, names, outcome)
90
+ if outcome.exit != nil {
91
+ return
92
+ }
93
+ }
94
+ })
95
+ }
96
+ forEachParallel(noChecker, func(_ int, file *ast.SourceFile) {
97
+ transformFile(file, nil, decision.Prefix, names, &outcomes[index[file]])
98
+ })
99
+ // Files with no earlier outcome are in file order here, and the rest had no exit last time, so this finds the same
100
+ // first exit a whole run would.
101
+ for i := range outcomes {
102
+ if outcomes[i].exit != nil {
103
+ applied.Exit = outcomes[i].exit
104
+ return applied, nil
105
+ }
106
+ if outcomes[i].panicText != "" {
107
+ return nil, fmt.Errorf("internal error while transforming %s: %s", redo[i].FileName().AsString(), outcomes[i].panicText)
108
+ }
109
+ }
110
+
111
+ // Nothing above changed this state, so an exit leaves it as it was. The next run compares with that.
112
+ nextOutcomes := make(map[tspath.PathKey]*fileOutcome, len(files))
113
+ for _, file := range files {
114
+ nextOutcomes[file.PathKey()] = s.outcomes[file.PathKey()]
115
+ }
116
+ for i, file := range redo {
117
+ nextOutcomes[file.PathKey()] = &outcomes[i]
118
+ }
119
+ s.outcomes = nextOutcomes
120
+ s.names = names
121
+ s.lastSource = p
122
+ return s.reprinted(p, files, change)
123
+ }
124
+
125
+ // applyNative is Apply with native plugins: the whole plugin pass again, on the transformers of the last program.
126
+ func (s *Session) applyNative(ctx context.Context, p *tsbridge.Project, decision Decision, files []*ast.SourceFile, build *Build, reuse bool) (*Applied, error) {
127
+ change := tsbridge.AffectedFiles(s.lastSource, p)
128
+ if !reuse || s.native == nil {
129
+ s.native = &nativeRun{}
130
+ change = &tsbridge.Change{Full: true, Reason: "first run of the plugins"}
131
+ }
132
+ // As in JS watch mode, the plugins only get the files a change can reach: the changed files and everything that
133
+ // imports them, directly or not. The others keep the text they were printed with last time. Plugins gather what
134
+ // they need from other files by reading the program, not from the calls to TransformFile. Setget's accessor names
135
+ // can change what any file prints, so with setget every file goes through again.
136
+ var kept map[tspath.PathKey]*fileOutcome
137
+ if !change.Full && !decision.Setget && s.outcomes != nil {
138
+ kept = make(map[tspath.PathKey]*fileOutcome, len(files))
139
+ for _, file := range files {
140
+ if outcome := s.outcomes[file.PathKey()]; outcome != nil && !change.Reaches(file) {
141
+ kept[file.PathKey()] = outcome
142
+ }
143
+ }
144
+ }
145
+ s.LastRedone = len(files) - len(kept)
146
+ result, err := s.native.transform(ctx, p, decision, files, build, true, kept, func() *accessorNames { return s.collectNames(p) })
147
+ if err != nil {
148
+ return nil, err
149
+ }
150
+ if result.exit != nil {
151
+ return &Applied{Project: p, Files: files, Exit: result.exit}, nil
152
+ }
153
+ // As in Apply, nothing above changed what the next run compares with.
154
+ outcomes := make(map[tspath.PathKey]*fileOutcome, len(files))
155
+ for i, file := range files {
156
+ outcomes[file.PathKey()] = &result.outcomes[i]
157
+ }
158
+ s.outcomes = outcomes
159
+ s.lastSource = p
160
+ applied, err := s.reprinted(p, files, change)
161
+ if err != nil {
162
+ return nil, err
163
+ }
164
+ applied.warnings = result.warnings
165
+ return applied, nil
166
+ }
167
+
168
+ // reprinted makes the program to compile from s.outcomes, reusing the last one where the printed text is the same.
169
+ func (s *Session) reprinted(p *tsbridge.Project, files []*ast.SourceFile, change *tsbridge.Change) (*Applied, error) {
170
+ applied := &Applied{}
171
+ changed := map[tspath.PathKey]string{}
172
+ texts := make(map[tspath.PathKey]string, len(files))
173
+ for _, file := range files {
174
+ key := file.PathKey()
175
+ text := s.outcomes[key].text
176
+ texts[key] = text
177
+ if old, ok := s.texts[key]; !ok || old != text {
178
+ changed[key] = text
179
+ }
180
+ }
181
+ s.texts = texts
182
+ switch {
183
+ case s.printed == nil:
184
+ s.printed = p.WithPrintedFiles(changed)
185
+ default:
186
+ s.printed = s.printed.UpdatePrinted(p, changed, texts, change.Full)
187
+ }
188
+ next := s.printed
189
+
190
+ applied.Project = next
191
+ applied.Printed = make(map[string]string, len(files))
192
+ applied.maps = make(map[*ast.SourceFile]*reprintMap, len(files))
193
+ applied.Files = make([]*ast.SourceFile, len(files))
194
+ byPath := next.Program().FilesByPath()
195
+ for i, file := range files {
196
+ applied.Printed[file.FileName().AsString()] = texts[file.PathKey()]
197
+ printedFile := byPath[file.PathKey()]
198
+ if printedFile == nil {
199
+ return nil, fmt.Errorf("internal error: %s is missing from the reprinted program", file.FileName().AsString())
200
+ }
201
+ applied.Files[i] = printedFile
202
+ applied.maps[printedFile] = &reprintMap{original: file, generator: s.outcomes[file.PathKey()].generator}
203
+ }
204
+ return applied, nil
205
+ }
206
+
207
+ // collectNames is collectAccessorNames, scanning only files it hasn't seen.
208
+ func (s *Session) collectNames(p *tsbridge.Project) *accessorNames {
209
+ files := p.Program().SourceFiles()
210
+ facts := make([]fileAccessorFacts, len(files))
211
+ forEachParallel(files, func(i int, file *ast.SourceFile) {
212
+ if known, ok := s.facts[file]; ok {
213
+ facts[i] = known
214
+ } else {
215
+ facts[i] = scanAccessors(file)
216
+ }
217
+ })
218
+ nextFacts := make(map[*ast.SourceFile]fileAccessorFacts, len(files))
219
+ result := &accessorNames{names: map[string]bool{}}
220
+ mapped, computed := false, false
221
+ for i, fact := range facts {
222
+ nextFacts[files[i]] = fact
223
+ for _, name := range fact.names {
224
+ result.names[name] = true
225
+ }
226
+ mapped = mapped || fact.mappedTypeName
227
+ computed = computed || fact.computed
228
+ }
229
+ s.facts = nextFacts
230
+ result.all = computed || (mapped && len(result.names) > 0)
231
+ return result
232
+ }
233
+
234
+ func (n *accessorNames) same(other *accessorNames) bool {
235
+ return n.all == other.all && maps.Equal(n.names, other.names)
236
+ }