@data-vegle/rbxts-go 0.0.0-stage → 1.0.0

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 +239 -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 +262 -0
  49. package/plugins/session.go +223 -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 +99 -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,431 @@
1
+ // Package plugins handles compilerOptions.plugins and the built-in setget option. rbxts-transformer-setget runs natively,
2
+ // with or without its package, and so does a plugin that a native plugin was registered for with pluginapi.Register.
3
+ // Any other plugin hands the whole build to the JS compiler. See docs/setget.md and docs/plugin-api.md.
4
+ package plugins
5
+
6
+ import (
7
+ "encoding/json"
8
+ "fmt"
9
+ "os"
10
+ "path/filepath"
11
+ "strings"
12
+
13
+ "github.com/microsoft/TypeScript/tsc/internal/collections"
14
+ "github.com/microsoft/TypeScript/tsc/internal/tsoptions"
15
+ "github.com/microsoft/TypeScript/tsc/internal/tspath"
16
+ "github.com/microsoft/TypeScript/tsc/internal/vfs/osvfs"
17
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/pluginapi"
18
+ )
19
+
20
+ // SetgetPlugin is the plugin that runs natively, and SetgetVersion is the only version the port matches.
21
+ const (
22
+ SetgetPlugin = "rbxts-transformer-setget"
23
+ SetgetVersion = "2.0.0"
24
+ // BuiltinBlock is the top-level tsconfig key for the built-in option.
25
+ BuiltinBlock = "rbxtsgo"
26
+ )
27
+
28
+ // Action says what a tsconfig's plugins call for.
29
+ type Action int
30
+
31
+ const (
32
+ // ActionNone builds as usual, with no reprint. JS does the same when no entry has a "transform".
33
+ ActionNone Action = iota
34
+ // ActionSetget runs the port.
35
+ ActionSetget
36
+ // ActionFallback hands the build to the JS compiler.
37
+ ActionFallback
38
+ // ActionError stops the build with Reason.
39
+ ActionError
40
+ // ActionPlugins runs the registered native plugins in Decision.Plugins, after setget when Decision.Setget is set.
41
+ ActionPlugins
42
+ )
43
+
44
+ // Options is the part of the setting that comes from the command line. Nil means not given.
45
+ type Options struct {
46
+ Setget *bool
47
+ Prefix *string
48
+ }
49
+
50
+ // Decision is the result of Decide.
51
+ type Decision struct {
52
+ Action Action
53
+ // Prefix is the plugin's customPrefix, or its default.
54
+ Prefix string
55
+ // Reason says why the build needs the JS compiler, naming the plugin, for ActionFallback. For ActionError, it's the
56
+ // error.
57
+ Reason string
58
+ // Setget says the setget port runs: always for ActionSetget, and for ActionPlugins when it is on as well.
59
+ Setget bool
60
+ // Plugins are the native plugins to run for ActionPlugins, in the order of their entries.
61
+ Plugins []PluginUse
62
+ }
63
+
64
+ // PluginUse is one entry of compilerOptions.plugins that a registered native plugin replaces.
65
+ type PluginUse struct {
66
+ Plugin pluginapi.Plugin
67
+ // Transform is the entry's "transform".
68
+ Transform string
69
+ values jsonObject
70
+ }
71
+
72
+ // Config is the entry decoded from JSON, as Plugin.New gets it. Each call makes a new one, so a plugin that changes
73
+ // its config changes nobody else's.
74
+ func (u PluginUse) Config() map[string]any {
75
+ config, _ := jsonValue(u.values).(map[string]any)
76
+ if config == nil {
77
+ config = map[string]any{}
78
+ }
79
+ return config
80
+ }
81
+
82
+ // jsonValue turns what the tsconfig parser gives into what encoding/json would: objects as map[string]any.
83
+ func jsonValue(value any) any {
84
+ switch value := value.(type) {
85
+ case *collections.OrderedMap[string, any]:
86
+ result := make(map[string]any, value.Size())
87
+ for key, entry := range value.Entries() {
88
+ result[key] = jsonValue(entry)
89
+ }
90
+ return result
91
+ case []any:
92
+ result := make([]any, len(value))
93
+ for i, entry := range value {
94
+ result[i] = jsonValue(entry)
95
+ }
96
+ return result
97
+ }
98
+ return value
99
+ }
100
+
101
+ // Reprints says the build goes through the plugin step: transform, print, and a new program from the printed text.
102
+ func (d Decision) Reprints() bool {
103
+ return d.Action == ActionSetget || d.Action == ActionPlugins
104
+ }
105
+
106
+ // Key is the part of an incremental build's key that the decision adds: a change in it means everything compiles
107
+ // again. For native plugins it holds their names and entries.
108
+ func (d Decision) Key() string {
109
+ key := fmt.Sprint(d.Action, d.Prefix)
110
+ if d.Action != ActionPlugins {
111
+ return key
112
+ }
113
+ key += fmt.Sprint(",setget=", d.Setget)
114
+ for _, use := range d.Plugins {
115
+ // encoding/json writes a map's keys in sorted order.
116
+ config, _ := json.Marshal(use.Config())
117
+ key += fmt.Sprintf(",%s=%s", use.Plugin.Name, config)
118
+ }
119
+ return key
120
+ }
121
+
122
+ type pluginConfig struct {
123
+ transform string
124
+ values interface{ Get(string) (any, bool) }
125
+ }
126
+
127
+ type jsonObject = interface{ Get(string) (any, bool) }
128
+
129
+ func readConfig(path string) (jsonObject, bool) {
130
+ text, err := os.ReadFile(path)
131
+ if err != nil {
132
+ return nil, false
133
+ }
134
+ fileName := tspath.RootedFilePathFromAbsolute(filepath.ToSlash(path))
135
+ raw, _ := tsoptions.ParseConfigFileTextToJson(fileName, osvfs.FS().CaseSensitivity().PathKey(fileName.AsPath()), string(text))
136
+ object, ok := raw.(jsonObject)
137
+ return object, ok
138
+ }
139
+
140
+ func exists(path string) bool {
141
+ info, err := os.Stat(path)
142
+ return err == nil && !info.IsDir()
143
+ }
144
+
145
+ // resolveExtends is the part of require.resolve(config.extends, { paths: [dirname] }) that a tsconfig can reach.
146
+ func resolveExtends(spec, directory string) (string, bool) {
147
+ try := func(base string) (string, bool) {
148
+ for _, candidate := range []string{base, base + ".json"} {
149
+ if exists(candidate) {
150
+ return candidate, true
151
+ }
152
+ }
153
+ return "", false
154
+ }
155
+ if filepath.IsAbs(spec) {
156
+ return try(spec)
157
+ }
158
+ if strings.HasPrefix(spec, "./") || strings.HasPrefix(spec, "../") {
159
+ return try(filepath.Join(directory, spec))
160
+ }
161
+ for current := directory; ; {
162
+ if found, ok := try(filepath.Join(current, "node_modules", spec)); ok {
163
+ return found, true
164
+ }
165
+ parent := filepath.Dir(current)
166
+ if parent == current {
167
+ return "", false
168
+ }
169
+ current = parent
170
+ }
171
+ }
172
+
173
+ // getPluginConfigs is getPluginConfigs: the entries with a string "transform", then those of what the tsconfig extends.
174
+ // So unlike every other option, a parent's plugins are added to the child's. active is compilerOptions.plugins as
175
+ // TypeScript merges it, where a child's list replaces its parent's. Upstream only looks at the entries if that isn't
176
+ // empty.
177
+ func getPluginConfigs(path string, seen map[string]bool) (configs []pluginConfig, active *bool, err error) {
178
+ if seen[path] {
179
+ return nil, nil, fmt.Errorf("tsconfig extends itself through %s", path)
180
+ }
181
+ seen[path] = true
182
+ config, ok := readConfig(path)
183
+ if !ok {
184
+ return nil, nil, nil
185
+ }
186
+ if compilerOptions, ok := config.Get("compilerOptions"); ok {
187
+ if object, ok := compilerOptions.(jsonObject); ok {
188
+ if plugins, ok := object.Get("plugins"); ok {
189
+ list, isList := plugins.([]any)
190
+ value := isList && len(list) > 0
191
+ active = &value
192
+ for _, entry := range list {
193
+ if values, ok := entry.(jsonObject); ok {
194
+ if transform, ok := values.Get("transform"); ok {
195
+ if name, ok := transform.(string); ok && name != "" {
196
+ configs = append(configs, pluginConfig{transform: name, values: values})
197
+ }
198
+ }
199
+ }
200
+ }
201
+ }
202
+ }
203
+ }
204
+ extends, ok := config.Get("extends")
205
+ if !ok || extends == nil {
206
+ return configs, active, nil
207
+ }
208
+ spec, ok := extends.(string)
209
+ if !ok {
210
+ // require.resolve throws on a list, if upstream gets this far. TS merges the list, later entries first.
211
+ list, _ := extends.([]any)
212
+ for i := len(list) - 1; i >= 0 && active == nil; i-- {
213
+ if name, ok := list[i].(string); ok {
214
+ if extended, ok := resolveExtends(name, filepath.Dir(path)); ok {
215
+ _, active, _ = getPluginConfigs(extended, seen)
216
+ }
217
+ }
218
+ }
219
+ return nil, active, fmt.Errorf(`"extends" in %s isn't a single path`, path)
220
+ }
221
+ if spec == "" {
222
+ return configs, active, nil
223
+ }
224
+ extended, ok := resolveExtends(spec, filepath.Dir(path))
225
+ if !ok {
226
+ return configs, active, fmt.Errorf(`can't resolve "extends": %q in %s`, spec, path)
227
+ }
228
+ parents, parentActive, err := getPluginConfigs(extended, seen)
229
+ if active == nil {
230
+ active = parentActive
231
+ }
232
+ return append(configs, parents...), active, err
233
+ }
234
+
235
+ // builtinSetting is the top-level "rbxtsgo" block. TS ignores top-level keys it doesn't know, as it does for "rbxts".
236
+ // The nearest tsconfig that sets a value wins, and extends is followed as for compilerOptions.
237
+ type builtinSetting struct {
238
+ setget *bool
239
+ prefix *string
240
+ err string
241
+ }
242
+
243
+ func readBuiltinSetting(path string, seen map[string]bool) builtinSetting {
244
+ var setting builtinSetting
245
+ if seen[path] {
246
+ return setting
247
+ }
248
+ seen[path] = true
249
+ config, ok := readConfig(path)
250
+ if !ok {
251
+ return setting
252
+ }
253
+ if block, ok := config.Get(BuiltinBlock); ok {
254
+ if object, ok := block.(jsonObject); ok {
255
+ if value, ok := object.Get("setget"); ok {
256
+ if on, ok := value.(bool); ok {
257
+ setting.setget = &on
258
+ } else {
259
+ setting.err = fmt.Sprintf(`"%s.setget" in %s must be true or false`, BuiltinBlock, path)
260
+ }
261
+ }
262
+ if value, ok := object.Get("setgetPrefix"); ok {
263
+ if prefix, ok := value.(string); ok {
264
+ setting.prefix = &prefix
265
+ } else {
266
+ setting.err = fmt.Sprintf(`"%s.setgetPrefix" in %s must be a string`, BuiltinBlock, path)
267
+ }
268
+ }
269
+ } else {
270
+ setting.err = fmt.Sprintf(`"%s" in %s must be an object`, BuiltinBlock, path)
271
+ }
272
+ }
273
+ extends, _ := config.Get("extends")
274
+ var parents []string
275
+ switch value := extends.(type) {
276
+ case string:
277
+ parents = []string{value}
278
+ case []any:
279
+ for i := len(value) - 1; i >= 0; i-- {
280
+ if name, ok := value[i].(string); ok {
281
+ parents = append(parents, name)
282
+ }
283
+ }
284
+ }
285
+ for _, spec := range parents {
286
+ if extended, ok := resolveExtends(spec, filepath.Dir(path)); ok {
287
+ parent := readBuiltinSetting(extended, seen)
288
+ if setting.setget == nil {
289
+ setting.setget = parent.setget
290
+ }
291
+ if setting.prefix == nil {
292
+ setting.prefix = parent.prefix
293
+ }
294
+ if setting.err == "" {
295
+ setting.err = parent.err
296
+ }
297
+ }
298
+ }
299
+ return setting
300
+ }
301
+
302
+ // Decide works out what a project's setget setting and plugins call for, from the tsconfig at tsConfigPath and what it
303
+ // extends, and the command line. Setget is on if the built-in option says so or a plugin entry names it. No package
304
+ // needs to be installed for either. JS would warn about a missing plugin and then reject every accessor.
305
+ //
306
+ // The build is native when every entry with a "transform" is setget or has a registered native plugin. One entry that
307
+ // has neither hands the whole build to the JS compiler, which then runs the JS version of every plugin.
308
+ func Decide(tsConfigPath string, options Options) Decision {
309
+ setting := readBuiltinSetting(tsConfigPath, map[string]bool{})
310
+ if setting.err != "" {
311
+ return Decision{Action: ActionError, Reason: setting.err}
312
+ }
313
+ builtin := setting.setget != nil && *setting.setget
314
+ if options.Setget != nil {
315
+ builtin = *options.Setget
316
+ }
317
+
318
+ configs, active, err := getPluginConfigs(tsConfigPath, map[string]bool{})
319
+ entriesActive := active != nil && *active
320
+ if err != nil && entriesActive {
321
+ return Decision{Action: ActionFallback, Reason: "rbxtsgo can't read the tsconfig's plugins: " + err.Error()}
322
+ }
323
+ if !entriesActive {
324
+ configs = nil
325
+ }
326
+ if !builtin && len(configs) == 0 {
327
+ return Decision{Action: ActionNone}
328
+ }
329
+
330
+ var others []string
331
+ var setget []pluginConfig
332
+ var native []PluginUse
333
+ // setgetAfter names the native plugin that a setget entry comes after, if any.
334
+ setgetAfter := ""
335
+ for _, config := range configs {
336
+ if config.transform == SetgetPlugin {
337
+ setget = append(setget, config)
338
+ if len(native) > 0 && setgetAfter == "" {
339
+ setgetAfter = native[0].Transform
340
+ }
341
+ } else if plugin, ok := pluginapi.Lookup(config.transform); ok {
342
+ native = append(native, PluginUse{Plugin: plugin, Transform: config.transform, values: config.values})
343
+ } else {
344
+ others = append(others, `"`+config.transform+`"`)
345
+ }
346
+ }
347
+ if len(others) > 0 {
348
+ noun := "plugin"
349
+ if len(others) > 1 {
350
+ noun = "plugins"
351
+ }
352
+ if builtin {
353
+ return Decision{Action: ActionError, Reason: fmt.Sprintf("The built-in setget option can't be used with the transformer %s %s, which only the JS compiler can run, because the JS compiler doesn't have the option.\nTo build this project, turn the option off, and add \"%s\" to compilerOptions.plugins with the package installed.", noun, strings.Join(others, ", "), SetgetPlugin)}
354
+ }
355
+ return Decision{Action: ActionFallback, Reason: fmt.Sprintf("rbxtsgo can't run the transformer %s %s", noun, strings.Join(others, ", "))}
356
+ }
357
+ fallback := func(why string) Decision {
358
+ return Decision{Action: ActionFallback, Reason: fmt.Sprintf(`rbxtsgo can't run the transformer plugin "%s" %s`, SetgetPlugin, why)}
359
+ }
360
+
361
+ prefix := defaultPrefix
362
+ if len(setget) > 1 && !builtin {
363
+ return fallback("more than once, and it's listed several times here")
364
+ }
365
+ if len(setget) > 0 && !builtin {
366
+ config := setget[0]
367
+ if value, ok := config.values.Get("type"); ok && value != nil && value != "program" {
368
+ return fallback(fmt.Sprintf(`with "type": %v`, value))
369
+ }
370
+ if value, ok := config.values.Get("import"); ok && value != nil && value != "default" {
371
+ return fallback(fmt.Sprintf(`with "import": %v`, value))
372
+ }
373
+ // With a package at another version, what the project asks for is that version, so only JS can give it.
374
+ if manifest, found := findPluginManifest(filepath.Dir(tsConfigPath)); found {
375
+ var pkg struct {
376
+ Version string `json:"version"`
377
+ }
378
+ if text, err := os.ReadFile(manifest); err == nil {
379
+ _ = json.Unmarshal(text, &pkg)
380
+ }
381
+ if pkg.Version != SetgetVersion {
382
+ return fallback(fmt.Sprintf("at version %s, because the port matches %s", pkg.Version, SetgetVersion))
383
+ }
384
+ }
385
+ }
386
+ if len(setget) > 0 {
387
+ if value, ok := setget[0].values.Get("customPrefix"); ok && value != nil {
388
+ text, isString := value.(string)
389
+ if !isString {
390
+ if builtin {
391
+ return Decision{Action: ActionError, Reason: fmt.Sprintf(`"customPrefix" of the plugin "%s" must be a string`, SetgetPlugin)}
392
+ }
393
+ return fallback(fmt.Sprintf(`with "customPrefix": %v`, value))
394
+ }
395
+ prefix = text
396
+ }
397
+ }
398
+ if setting.prefix != nil {
399
+ prefix = *setting.prefix
400
+ }
401
+ if options.Prefix != nil {
402
+ prefix = *options.Prefix
403
+ }
404
+ setgetOn := builtin || len(setget) > 0
405
+ if len(native) == 0 {
406
+ return Decision{Action: ActionSetget, Prefix: prefix, Setget: true}
407
+ }
408
+ if !setgetOn {
409
+ return Decision{Action: ActionPlugins, Plugins: native}
410
+ }
411
+ // Setget reads the file as written: it looks at parents and at source text. So it can only go first.
412
+ if setgetAfter != "" {
413
+ return Decision{Action: ActionError, Reason: fmt.Sprintf("\"%s\" is listed after the native plugin \"%s\" in compilerOptions.plugins, and rbxtsgo can only run setget before native plugins.\nTo build this project, move the \"%s\" entry to the front of the list.", SetgetPlugin, setgetAfter, SetgetPlugin)}
414
+ }
415
+ return Decision{Action: ActionPlugins, Prefix: prefix, Setget: true, Plugins: native}
416
+ }
417
+
418
+ // findPluginManifest looks where resolve.sync(transform, { basedir }) does.
419
+ func findPluginManifest(directory string) (string, bool) {
420
+ for current := directory; ; {
421
+ manifest := filepath.Join(current, "node_modules", SetgetPlugin, "package.json")
422
+ if exists(manifest) {
423
+ return manifest, true
424
+ }
425
+ parent := filepath.Dir(current)
426
+ if parent == current {
427
+ return "", false
428
+ }
429
+ current = parent
430
+ }
431
+ }
@@ -0,0 +1,84 @@
1
+ package plugins
2
+
3
+ import (
4
+ "errors"
5
+ "fmt"
6
+ "io"
7
+ "os"
8
+ "os/exec"
9
+ "path/filepath"
10
+ "slices"
11
+ "strings"
12
+ )
13
+
14
+ const jsCompilerPath = "node_modules/roblox-ts/out/CLI/cli.js"
15
+
16
+ // flagsOfOurs are the flags only rbxtsgo has that take a value, and boolFlagsOfOurs the ones that don't.
17
+ var (
18
+ flagsOfOurs = []string{"checkers", "cpuprofile", "setgetPrefix"}
19
+ boolFlagsOfOurs = []string{"setget", "no-setget", "fix-emit", "no-fix-emit"}
20
+ )
21
+
22
+ // passAlongArgs removes our own flags from the command line, because yargs in the JS compiler rejects unknown ones.
23
+ func passAlongArgs(args []string) []string {
24
+ result := make([]string, 0, len(args))
25
+ for i := 0; i < len(args); i++ {
26
+ arg := args[i]
27
+ name := strings.TrimLeft(arg, "-")
28
+ if name == arg {
29
+ result = append(result, arg)
30
+ continue
31
+ }
32
+ name, _, hasValue := strings.Cut(name, "=")
33
+ if slices.Contains(boolFlagsOfOurs, name) {
34
+ continue
35
+ }
36
+ ours := slices.Contains(flagsOfOurs, name)
37
+ if !ours {
38
+ result = append(result, arg)
39
+ } else if !hasValue {
40
+ i++
41
+ }
42
+ }
43
+ return result
44
+ }
45
+
46
+ // findJSCompiler looks in the tsconfig's folder and above, as node_modules lookups do.
47
+ func findJSCompiler(directory string) (string, bool) {
48
+ for current := directory; ; {
49
+ candidate := filepath.Join(current, filepath.FromSlash(jsCompilerPath))
50
+ if exists(candidate) {
51
+ return candidate, true
52
+ }
53
+ parent := filepath.Dir(current)
54
+ if parent == current {
55
+ return "", false
56
+ }
57
+ current = parent
58
+ }
59
+ }
60
+
61
+ // Delegate runs the whole build with the project's JS compiler, with the command line it was given, and returns the
62
+ // exit code. It only returns an error when the JS compiler can't start, with what to do about it.
63
+ func Delegate(reason, tsConfigPath string, args []string, stderr io.Writer) (int, error) {
64
+ directory := filepath.Dir(tsConfigPath)
65
+ compiler, found := findJSCompiler(directory)
66
+ if !found {
67
+ return 1, fmt.Errorf("%s, and the JS compiler isn't installed at %s.\nTo build this project, install it with `npm install --save-dev roblox-ts@3.0.0` in %s, or remove the plugin from compilerOptions.plugins in %s.", reason, filepath.Join(directory, filepath.FromSlash(jsCompilerPath)), directory, tsConfigPath)
68
+ }
69
+ node, err := exec.LookPath("node")
70
+ if err != nil {
71
+ return 1, fmt.Errorf("%s, and the JS compiler at %s needs Node.js, which isn't on PATH.\nTo build this project, install Node.js, or remove the plugin from compilerOptions.plugins in %s.", reason, compiler, tsConfigPath)
72
+ }
73
+ fmt.Fprintf(stderr, "%s. Handing the whole build to the JS compiler at %s.\n", reason, compiler)
74
+ command := exec.Command(node, append([]string{compiler}, passAlongArgs(args)...)...)
75
+ command.Stdin, command.Stdout, command.Stderr = os.Stdin, os.Stdout, os.Stderr
76
+ if err := command.Run(); err != nil {
77
+ var exit *exec.ExitError
78
+ if errors.As(err, &exit) {
79
+ return exit.ExitCode(), nil
80
+ }
81
+ return 1, fmt.Errorf("%s, and the JS compiler at %s didn't start: %w", reason, compiler, err)
82
+ }
83
+ return 0, nil
84
+ }
@@ -0,0 +1,60 @@
1
+ package plugins
2
+
3
+ import (
4
+ "errors"
5
+ "fmt"
6
+ "path/filepath"
7
+ )
8
+
9
+ // FixEmitSetting is "fixEmit" in the tsconfig's top-level "rbxtsgo" block: the output fixes that differ from JS 3.0.0,
10
+ // listed in docs/known-differences.md. The nearest tsconfig that sets it wins, and extends is followed, as for the
11
+ // block's other settings. An error means the value isn't a boolean, and on is false then.
12
+ func FixEmitSetting(tsConfigPath string) (on bool, err error) {
13
+ value, text := readFixEmit(tsConfigPath, map[string]bool{})
14
+ if text != "" {
15
+ return false, errors.New(text)
16
+ }
17
+ return value != nil && *value, nil
18
+ }
19
+
20
+ func readFixEmit(path string, seen map[string]bool) (*bool, string) {
21
+ if seen[path] {
22
+ return nil, ""
23
+ }
24
+ seen[path] = true
25
+ config, ok := readConfig(path)
26
+ if !ok {
27
+ return nil, ""
28
+ }
29
+ if block, ok := config.Get(BuiltinBlock); ok {
30
+ if object, ok := block.(jsonObject); ok {
31
+ if value, ok := object.Get("fixEmit"); ok {
32
+ if on, ok := value.(bool); ok {
33
+ return &on, ""
34
+ }
35
+ return nil, fmt.Sprintf(`"%s.fixEmit" in %s must be true or false`, BuiltinBlock, path)
36
+ }
37
+ }
38
+ }
39
+ extends, _ := config.Get("extends")
40
+ var parents []string
41
+ switch value := extends.(type) {
42
+ case string:
43
+ parents = []string{value}
44
+ case []any:
45
+ // As with compilerOptions, the last entry of an extends list wins.
46
+ for i := len(value) - 1; i >= 0; i-- {
47
+ if name, ok := value[i].(string); ok {
48
+ parents = append(parents, name)
49
+ }
50
+ }
51
+ }
52
+ for _, spec := range parents {
53
+ if extended, ok := resolveExtends(spec, filepath.Dir(path)); ok {
54
+ if value, text := readFixEmit(extended, seen); value != nil || text != "" {
55
+ return value, text
56
+ }
57
+ }
58
+ }
59
+ return nil, ""
60
+ }