@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,559 @@
1
+ // Package rojoresolver is a port of @roblox-ts/rojo-resolver 1.1.0, which maps file paths to Roblox instance
2
+ // paths using a Rojo project file. Output must match the JS version byte for byte, so the tests compare it with
3
+ // the real npm package.
4
+ //
5
+ // Where the JS throws, this returns an error. The error's text is not meant to match, since in rbxtsc those are
6
+ // uncaught exceptions rather than diagnostics.
7
+ package rojoresolver
8
+
9
+ import (
10
+ "errors"
11
+ "fmt"
12
+ "io/fs"
13
+ "os"
14
+ "path/filepath"
15
+ "regexp"
16
+ "runtime"
17
+ "slices"
18
+ "strings"
19
+
20
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
21
+ )
22
+
23
+ const (
24
+ luaExt = ".lua"
25
+ luauExt = ".luau"
26
+ jsonExt = ".json"
27
+ tomlExt = ".toml"
28
+
29
+ initName = "init"
30
+
31
+ serverSubext = ".server"
32
+ clientSubext = ".client"
33
+
34
+ rojoDefaultName = "default.project.json"
35
+ rojoOldName = "roblox-project.json"
36
+ )
37
+
38
+ var rojoFileRegex = regexp.MustCompile(`^.+\.project\.json$`)
39
+
40
+ func isModuleExt(ext string) bool { return ext == luauExt || ext == jsonExt || ext == tomlExt }
41
+ func isScriptExt(ext string) bool { return ext == luauExt }
42
+
43
+ // RbxType is the enum of the same name, with the same values.
44
+ type RbxType int
45
+
46
+ const (
47
+ RbxTypeModuleScript RbxType = iota
48
+ RbxTypeScript
49
+ RbxTypeLocalScript
50
+ RbxTypeUnknown
51
+ )
52
+
53
+ // FileRelation is the enum of the same name, with the same values.
54
+ type FileRelation int
55
+
56
+ const (
57
+ FileRelationOutToOut FileRelation = iota // absolute
58
+ FileRelationOutToIn // error
59
+ FileRelationInToOut // absolute
60
+ FileRelationInToIn // relative
61
+ )
62
+
63
+ // NetworkType is the enum of the same name, with the same values.
64
+ type NetworkType int
65
+
66
+ const (
67
+ NetworkTypeUnknown NetworkType = iota
68
+ NetworkTypeClient
69
+ NetworkTypeServer
70
+ )
71
+
72
+ // RbxPath is a Roblox tree path. A nil RbxPath stands for undefined.
73
+ type RbxPath = []string
74
+
75
+ // RbxPathParent marks a step up in a relative path, where JS has a Symbol. It can't be an instance name,
76
+ // because it has a NUL byte.
77
+ const RbxPathParent = "\x00Parent"
78
+
79
+ var (
80
+ defaultIsolatedContainers = []RbxPath{
81
+ {"StarterPack"},
82
+ {"StarterGui"},
83
+ {"StarterPlayer", "StarterPlayerScripts"},
84
+ {"StarterPlayer", "StarterCharacterScripts"},
85
+ {"StarterPlayer", "StarterCharacter"},
86
+ {"PluginDebugService"},
87
+ }
88
+ clientContainers = []RbxPath{{"StarterPack"}, {"StarterGui"}, {"StarterPlayer"}}
89
+ serverContainers = []RbxPath{{"ServerStorage"}, {"ServerScriptService"}}
90
+ )
91
+
92
+ // PartitionInfo is one $path that maps a folder.
93
+ type PartitionInfo struct {
94
+ RbxPath RbxPath
95
+ FsPath string
96
+ }
97
+
98
+ var path = nodepath.Native
99
+
100
+ func stripRojoExts(filePath string) string {
101
+ ext := path.Extname(filePath)
102
+ if isModuleExt(ext) {
103
+ filePath = filePath[:len(filePath)-len(ext)]
104
+ if isScriptExt(ext) {
105
+ subext := path.Extname(filePath)
106
+ if subext == serverSubext || subext == clientSubext {
107
+ filePath = filePath[:len(filePath)-len(subext)]
108
+ }
109
+ }
110
+ }
111
+ return filePath
112
+ }
113
+
114
+ func arrayStartsWith(a, b []string) bool {
115
+ for i := range min(len(a), len(b)) {
116
+ if a[i] != b[i] {
117
+ return false
118
+ }
119
+ }
120
+ return true
121
+ }
122
+
123
+ func isPathDescendantOf(filePath, dirPath string) bool {
124
+ if dirPath == filePath {
125
+ return true
126
+ }
127
+ if startsWithDotDot, ok := path.RelativeStartsWithDotDot(dirPath, filePath); ok {
128
+ return !startsWithDotDot
129
+ }
130
+ return !strings.HasPrefix(path.Relative(dirPath, filePath), "..")
131
+ }
132
+
133
+ func convertToLuau(filePath string) string {
134
+ ext := path.Extname(filePath)
135
+ if ext == luaExt {
136
+ return filePath[:len(filePath)-len(ext)] + luauExt
137
+ }
138
+ return filePath
139
+ }
140
+
141
+ func pathExists(p string) bool {
142
+ _, err := os.Stat(p)
143
+ return err == nil
144
+ }
145
+
146
+ // readDir returns directory entries in the order Node's readdirSync does. libuv sorts by byte value on Unix.
147
+ // On Windows it keeps the OS order, which on NTFS ignores case, so os.ReadDir's byte sort would differ.
148
+ func readDirEntries(dir string) ([]os.DirEntry, error) {
149
+ f, err := os.Open(dir)
150
+ if err != nil {
151
+ return nil, err
152
+ }
153
+ defer f.Close()
154
+ entries, err := f.ReadDir(-1)
155
+ if err != nil {
156
+ return nil, err
157
+ }
158
+ if runtime.GOOS != "windows" {
159
+ slices.SortFunc(entries, func(a, b os.DirEntry) int { return strings.Compare(a.Name(), b.Name()) })
160
+ }
161
+ return entries, nil
162
+ }
163
+
164
+ func readDir(dir string) ([]string, error) {
165
+ f, err := os.Open(dir)
166
+ if err != nil {
167
+ return nil, err
168
+ }
169
+ defer f.Close()
170
+ names, err := f.Readdirnames(-1)
171
+ if err != nil {
172
+ return nil, err
173
+ }
174
+ if runtime.GOOS != "windows" {
175
+ slices.Sort(names)
176
+ }
177
+ return names, nil
178
+ }
179
+
180
+ // FindRojoConfigFilePath is RojoResolver.findRojoConfigFilePath. The path is "" where JS has undefined.
181
+ func FindRojoConfigFilePath(projectPath string) (string, []string, error) {
182
+ var warnings []string
183
+ defaultPath := path.Join(projectPath, rojoDefaultName)
184
+ if pathExists(defaultPath) {
185
+ return defaultPath, warnings, nil
186
+ }
187
+ names, err := readDir(projectPath)
188
+ if err != nil {
189
+ return "", nil, err
190
+ }
191
+ var candidates []string
192
+ for _, fileName := range names {
193
+ if fileName != rojoDefaultName && (fileName == rojoOldName || rojoFileRegex.MatchString(fileName)) {
194
+ candidates = append(candidates, path.Join(projectPath, fileName))
195
+ }
196
+ }
197
+ if len(candidates) > 1 {
198
+ warnings = append(warnings, "Multiple *.project.json files found, using "+candidates[0])
199
+ }
200
+ if len(candidates) == 0 {
201
+ return "", warnings, nil
202
+ }
203
+ return candidates[0], warnings, nil
204
+ }
205
+
206
+ // Resolver is RojoResolver.
207
+ type Resolver struct {
208
+ warnings []string
209
+ rbxPath []string
210
+ partitions []PartitionInfo
211
+ filePathToRbxPath map[string]RbxPath
212
+ // IsGame is true once a tree has "$className": "DataModel".
213
+ IsGame bool
214
+ }
215
+
216
+ func newResolver() *Resolver {
217
+ return &Resolver{filePathToRbxPath: map[string]RbxPath{}}
218
+ }
219
+
220
+ // Warnings is getWarnings.
221
+ func (r *Resolver) Warnings() []string { return r.warnings }
222
+
223
+ // Partitions is getPartitions: last seen first, because the JS uses unshift.
224
+ func (r *Resolver) Partitions() []PartitionInfo { return r.partitions }
225
+
226
+ // FromPath is RojoResolver.fromPath.
227
+ func FromPath(rojoConfigFilePath string) (*Resolver, error) {
228
+ r := newResolver()
229
+ if err := r.parseConfig(path.Resolve(rojoConfigFilePath), true); err != nil {
230
+ return nil, err
231
+ }
232
+ return r, nil
233
+ }
234
+
235
+ // Synthetic is RojoResolver.synthetic: a resolver for ProjectType.Package, which forces all imports to be relative.
236
+ func Synthetic(basePath string) (*Resolver, error) {
237
+ r := newResolver()
238
+ tree := &object{keys: []string{"$path"}, values: map[string]any{"$path": basePath}}
239
+ if err := r.parseTree(basePath, "", tree, true); err != nil {
240
+ return nil, err
241
+ }
242
+ return r, nil
243
+ }
244
+
245
+ // FromTreeJSON is RojoResolver.fromTree, with the tree given as JSON text to keep its property order.
246
+ func FromTreeJSON(basePath, treeJSON string) (*Resolver, error) {
247
+ value, err := parseJSON(treeJSON)
248
+ if err != nil {
249
+ return nil, err
250
+ }
251
+ r := newResolver()
252
+ if err := r.parseTree(basePath, "", value, true); err != nil {
253
+ return nil, err
254
+ }
255
+ return r, nil
256
+ }
257
+
258
+ func (r *Resolver) parseConfig(rojoConfigFilePath string, doNotPush bool) error {
259
+ realPath, err := filepath.EvalSymlinks(rojoConfigFilePath)
260
+ if err != nil {
261
+ return err
262
+ }
263
+ if !pathExists(realPath) {
264
+ r.warnings = append(r.warnings, fmt.Sprintf(`RojoResolver: Path does not exist "%s"`, rojoConfigFilePath))
265
+ return nil
266
+ }
267
+ data, err := os.ReadFile(realPath)
268
+ if err != nil {
269
+ return err
270
+ }
271
+ configJSON, parseErr := parseJSON(string(data))
272
+ if parseErr != nil {
273
+ // The JS has try/finally with no catch: the finally block warns, then the SyntaxError propagates.
274
+ r.warnings = append(r.warnings, "RojoResolver: Invalid configuration! "+errorsText(validateRojoConfig(nil)))
275
+ return fmt.Errorf("%s: %w", realPath, parseErr)
276
+ }
277
+ if errs := validateRojoConfig(configJSON); errs != nil {
278
+ r.warnings = append(r.warnings, "RojoResolver: Invalid configuration! "+errorsText(errs))
279
+ return nil
280
+ }
281
+ config := configJSON.(*object)
282
+ return r.parseTree(path.Dirname(rojoConfigFilePath), config.values["name"].(string), config.values["tree"], doNotPush)
283
+ }
284
+
285
+ func (r *Resolver) parseTree(basePath, name string, treeValue any, doNotPush bool) error {
286
+ if !doNotPush {
287
+ r.rbxPath = append(r.rbxPath, name)
288
+ }
289
+ if tree, ok := treeValue.(*object); ok {
290
+ if value, exists := tree.get("$path"); exists {
291
+ itemPath, err := pathValue(value)
292
+ if err != nil {
293
+ return err
294
+ }
295
+ if err := r.parsePath(path.Resolve(basePath, itemPath)); err != nil {
296
+ return err
297
+ }
298
+ }
299
+ if className, _ := tree.values["$className"].(string); className == "DataModel" {
300
+ r.IsGame = true
301
+ }
302
+ for _, childName := range tree.keys {
303
+ if !strings.HasPrefix(childName, "$") {
304
+ if err := r.parseTree(basePath, childName, tree.values[childName], false); err != nil {
305
+ return err
306
+ }
307
+ }
308
+ }
309
+ }
310
+ if !doNotPush {
311
+ r.rbxPath = r.rbxPath[:len(r.rbxPath)-1]
312
+ }
313
+ return nil
314
+ }
315
+
316
+ func pathValue(value any) (string, error) {
317
+ switch v := value.(type) {
318
+ case string:
319
+ return v, nil
320
+ case *object:
321
+ if optional, ok := v.values["optional"].(string); ok {
322
+ return optional, nil
323
+ }
324
+ }
325
+ // path.resolve throws here, on a $path object with no "optional".
326
+ return "", errors.New(`$path must be a string or have an "optional" string`)
327
+ }
328
+
329
+ func (r *Resolver) parsePath(itemPath string) error {
330
+ itemPath = convertToLuau(itemPath)
331
+ realPath := itemPath
332
+ exists := pathExists(itemPath)
333
+ if exists {
334
+ resolved, err := filepath.EvalSymlinks(itemPath)
335
+ if err != nil {
336
+ return err
337
+ }
338
+ realPath = resolved
339
+ }
340
+ ext := path.Extname(itemPath)
341
+ if isModuleExt(ext) {
342
+ r.filePathToRbxPath[itemPath] = append(RbxPath{}, r.rbxPath...)
343
+ return nil
344
+ }
345
+ isDirectory := false
346
+ if pathExists(realPath) {
347
+ info, err := os.Stat(realPath)
348
+ if err != nil {
349
+ return err
350
+ }
351
+ isDirectory = info.IsDir()
352
+ }
353
+ if isDirectory {
354
+ names, err := readDir(realPath)
355
+ if err != nil {
356
+ return err
357
+ }
358
+ if slices.Contains(names, rojoDefaultName) {
359
+ return r.parseConfig(path.Join(itemPath, rojoDefaultName), true)
360
+ }
361
+ }
362
+ r.partitions = slices.Insert(r.partitions, 0, PartitionInfo{FsPath: itemPath, RbxPath: append(RbxPath{}, r.rbxPath...)})
363
+ if isDirectory {
364
+ return r.searchDirectory(itemPath, "")
365
+ }
366
+ return nil
367
+ }
368
+
369
+ func (r *Resolver) searchDirectory(directory, item string) error {
370
+ realPath, err := filepath.EvalSymlinks(directory)
371
+ if err != nil {
372
+ return err
373
+ }
374
+ return r.searchRealDirectory(directory, realPath, item)
375
+ }
376
+
377
+ // searchRealDirectory is the walk of searchDirectory. The JS calls realpathSync and statSync on every child, and doing
378
+ // that here took most of a rebuild on Windows (see 0006). The entry's own type gives the same answer for anything that
379
+ // isn't a link, so only links pay for it.
380
+ func (r *Resolver) searchRealDirectory(directory, realPath, item string) error {
381
+ children, err := readDirEntries(realPath)
382
+ if err != nil {
383
+ return err
384
+ }
385
+
386
+ if slices.ContainsFunc(children, func(child os.DirEntry) bool { return child.Name() == rojoDefaultName }) {
387
+ return r.parseConfig(path.Join(directory, rojoDefaultName), false)
388
+ }
389
+
390
+ if item != "" {
391
+ r.rbxPath = append(r.rbxPath, item)
392
+ }
393
+
394
+ kinds := make([]childKind, len(children))
395
+ for i, child := range children {
396
+ kind, err := kindOfChild(path.Join(directory, child.Name()), path.Join(realPath, child.Name()), child)
397
+ if err != nil {
398
+ return err
399
+ }
400
+ kinds[i] = kind
401
+ }
402
+
403
+ // *.project.json
404
+ for i, child := range children {
405
+ if kinds[i].regular && child.Name() != rojoDefaultName && rojoFileRegex.MatchString(child.Name()) {
406
+ if err := r.parseConfig(path.Join(directory, child.Name()), false); err != nil {
407
+ return err
408
+ }
409
+ }
410
+ }
411
+
412
+ // folders
413
+ for i, child := range children {
414
+ if kinds[i].directory {
415
+ if err := r.searchRealDirectory(path.Join(directory, child.Name()), kinds[i].realPath, child.Name()); err != nil {
416
+ return err
417
+ }
418
+ }
419
+ }
420
+
421
+ if item != "" {
422
+ r.rbxPath = r.rbxPath[:len(r.rbxPath)-1]
423
+ }
424
+ return nil
425
+ }
426
+
427
+ type childKind struct {
428
+ regular, directory bool
429
+ realPath string
430
+ }
431
+
432
+ func kindOfChild(childPath, realChild string, child os.DirEntry) (childKind, error) {
433
+ if entryType := child.Type(); entryType&(fs.ModeSymlink|fs.ModeIrregular) == 0 {
434
+ return childKind{regular: entryType.IsRegular(), directory: entryType.IsDir(), realPath: realChild}, nil
435
+ }
436
+ resolved, err := filepath.EvalSymlinks(childPath)
437
+ if err != nil {
438
+ return childKind{}, err
439
+ }
440
+ info, err := os.Stat(resolved)
441
+ if err != nil {
442
+ return childKind{}, err
443
+ }
444
+ return childKind{regular: info.Mode().IsRegular(), directory: info.IsDir(), realPath: resolved}, nil
445
+ }
446
+
447
+ // GetRbxPathFromFilePath returns nil where the JS returns undefined. A partition root gives a non-nil path.
448
+ func (r *Resolver) GetRbxPathFromFilePath(filePath string) RbxPath {
449
+ filePath = path.Resolve(filePath)
450
+ filePath = convertToLuau(filePath)
451
+
452
+ if rbxPath, ok := r.filePathToRbxPath[filePath]; ok {
453
+ return rbxPath
454
+ }
455
+
456
+ ext := path.Extname(filePath)
457
+ for _, partition := range r.partitions {
458
+ if isPathDescendantOf(filePath, partition.FsPath) {
459
+ stripped := stripRojoExts(filePath)
460
+ relativePath := path.Relative(partition.FsPath, stripped)
461
+ relativeParts := []string{}
462
+ if relativePath != "" {
463
+ relativeParts = strings.Split(relativePath, path.Sep)
464
+ }
465
+ if isScriptExt(ext) && len(relativeParts) > 0 && relativeParts[len(relativeParts)-1] == initName {
466
+ relativeParts = relativeParts[:len(relativeParts)-1]
467
+ }
468
+ return append(append(RbxPath{}, partition.RbxPath...), relativeParts...)
469
+ }
470
+ }
471
+ return nil
472
+ }
473
+
474
+ // GetRbxTypeFromFilePath is getRbxTypeFromFilePath.
475
+ func (r *Resolver) GetRbxTypeFromFilePath(filePath string) RbxType {
476
+ filePath = convertToLuau(filePath)
477
+ ext := path.Extname(filePath)
478
+ subext := path.Extname(path.Basename(filePath, ext))
479
+ if !isScriptExt(ext) {
480
+ // non-script exts cannot use .server, .client, etc.
481
+ return RbxTypeModuleScript
482
+ }
483
+ switch subext {
484
+ case "":
485
+ return RbxTypeModuleScript
486
+ case serverSubext:
487
+ return RbxTypeScript
488
+ case clientSubext:
489
+ return RbxTypeLocalScript
490
+ }
491
+ return RbxTypeUnknown
492
+ }
493
+
494
+ // getContainer returns the index of the matching container, or -1. Callers in the JS compare containers by
495
+ // identity, and an index in the same list says the same thing.
496
+ func (r *Resolver) getContainer(from []RbxPath, rbxPath RbxPath) int {
497
+ if r.IsGame && rbxPath != nil {
498
+ for i, container := range from {
499
+ if arrayStartsWith(rbxPath, container) {
500
+ return i
501
+ }
502
+ }
503
+ }
504
+ return -1
505
+ }
506
+
507
+ // GetFileRelation is getFileRelation.
508
+ func (r *Resolver) GetFileRelation(fileRbxPath, moduleRbxPath RbxPath) FileRelation {
509
+ fileContainer := r.getContainer(defaultIsolatedContainers, fileRbxPath)
510
+ moduleContainer := r.getContainer(defaultIsolatedContainers, moduleRbxPath)
511
+ switch {
512
+ case fileContainer != -1 && moduleContainer != -1:
513
+ if fileContainer == moduleContainer {
514
+ return FileRelationInToIn
515
+ }
516
+ return FileRelationOutToIn
517
+ case fileContainer != -1:
518
+ return FileRelationInToOut
519
+ case moduleContainer != -1:
520
+ return FileRelationOutToIn
521
+ }
522
+ return FileRelationOutToOut
523
+ }
524
+
525
+ // IsIsolated is isIsolated.
526
+ func (r *Resolver) IsIsolated(rbxPath RbxPath) bool {
527
+ return r.getContainer(defaultIsolatedContainers, rbxPath) != -1
528
+ }
529
+
530
+ // GetNetworkType is getNetworkType.
531
+ func (r *Resolver) GetNetworkType(rbxPath RbxPath) NetworkType {
532
+ if r.getContainer(serverContainers, rbxPath) != -1 {
533
+ return NetworkTypeServer
534
+ }
535
+ if r.getContainer(clientContainers, rbxPath) != -1 {
536
+ return NetworkTypeClient
537
+ }
538
+ return NetworkTypeUnknown
539
+ }
540
+
541
+ // Relative is RojoResolver.relative. Steps up are RbxPathParent.
542
+ func Relative(rbxFrom, rbxTo RbxPath) []string {
543
+ maxLength := max(len(rbxFrom), len(rbxTo))
544
+ diffIndex := maxLength
545
+ for i := range maxLength {
546
+ if i >= len(rbxFrom) || i >= len(rbxTo) || rbxFrom[i] != rbxTo[i] {
547
+ diffIndex = i
548
+ break
549
+ }
550
+ }
551
+ result := []string{}
552
+ for i := diffIndex; i < len(rbxFrom); i++ {
553
+ result = append(result, RbxPathParent)
554
+ }
555
+ if diffIndex < len(rbxTo) {
556
+ result = append(result, rbxTo[diffIndex:]...)
557
+ }
558
+ return result
559
+ }
@@ -0,0 +1,83 @@
1
+ package shared
2
+
3
+ // From src/Shared/constants.ts.
4
+ const (
5
+ // CompilerVersion is the roblox-ts version this port matches.
6
+ CompilerVersion = "3.0.0"
7
+
8
+ NodeModules = "node_modules"
9
+ RbxtsScope = "@rbxts"
10
+
11
+ TSExt = ".ts"
12
+ TSXExt = ".tsx"
13
+ DExt = ".d"
14
+ DTSExt = DExt + TSExt
15
+
16
+ IndexName = "index"
17
+ InitName = "init"
18
+
19
+ ServerSubext = ".server"
20
+ ClientSubext = ".client"
21
+ ModuleSubext = ""
22
+
23
+ ParentField = "Parent"
24
+ )
25
+
26
+ // FilenameWarnings maps a file name that won't work to the name to suggest.
27
+ var FilenameWarnings = func() map[string]string {
28
+ warnings := map[string]string{}
29
+ for _, scriptType := range []string{ServerSubext, ClientSubext, ModuleSubext} {
30
+ for _, fileType := range []string{TSExt, TSXExt, DTSExt} {
31
+ warnings[InitName+scriptType+fileType] = IndexName + scriptType + fileType
32
+ }
33
+ }
34
+ return warnings
35
+ }()
36
+
37
+ // ProjectType is the enum of the same name. "" stands for undefined in ProjectOptions.Type.
38
+ type ProjectType string
39
+
40
+ const (
41
+ ProjectTypeGame ProjectType = "game"
42
+ ProjectTypeModel ProjectType = "model"
43
+ ProjectTypePackage ProjectType = "package"
44
+ )
45
+
46
+ // ProjectOptions is the interface of the same name in src/Shared/types.ts.
47
+ type ProjectOptions struct {
48
+ IncludePath string
49
+ // Rojo is "" where JS has undefined. Upstream treats those the same way.
50
+ Rojo string
51
+ // Type is "" where JS has undefined.
52
+ Type ProjectType
53
+ LogTruthyChanges bool
54
+ NoInclude bool
55
+ UsePolling bool
56
+ Verbose bool
57
+ Watch bool
58
+ WriteOnlyChanged bool
59
+ WriteTransformedFiles bool
60
+ OptimizedLoops bool
61
+ AllowCommentDirectives bool
62
+ Luau bool
63
+ // FixEmit is rbxtsgo's own. It turns on the output fixes that differ from JS 3.0.0 for code that compiles there. See
64
+ // docs/known-differences.md. BuildKey hashes these options, so changing it forces a rebuild.
65
+ FixEmit bool
66
+ }
67
+
68
+ // DefaultProjectOptions is DEFAULT_PROJECT_OPTIONS.
69
+ func DefaultProjectOptions() ProjectOptions {
70
+ return ProjectOptions{OptimizedLoops: true, Luau: true}
71
+ }
72
+
73
+ // ProjectData is the interface of the same name in src/Shared/types.ts. transformerWatcher is left out, because
74
+ // transformer plugins are a later phase.
75
+ type ProjectData struct {
76
+ IsPackage bool
77
+ NodeModulesPath string
78
+ ProjectOptions ProjectOptions
79
+ ProjectPath string
80
+ // RojoConfigPath is "" where JS has undefined.
81
+ RojoConfigPath string
82
+ TsConfigPath string
83
+ }