@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,342 @@
1
+ package plugins
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "os"
7
+ "runtime"
8
+ "sort"
9
+ "sync"
10
+
11
+ "github.com/microsoft/TypeScript/tsc/internal/ast"
12
+ "github.com/microsoft/TypeScript/tsc/internal/checker"
13
+ "github.com/microsoft/TypeScript/tsc/internal/core"
14
+ "github.com/microsoft/TypeScript/tsc/internal/printer"
15
+ "github.com/microsoft/TypeScript/tsc/internal/scanner"
16
+ "github.com/microsoft/TypeScript/tsc/internal/sourcemap"
17
+ "github.com/microsoft/TypeScript/tsc/internal/tspath"
18
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
19
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/project/pathtranslator"
20
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
21
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/tsbridge"
22
+ )
23
+
24
+ // Applied is what's left to compile after the plugin step of compileFiles.
25
+ type Applied struct {
26
+ // Project and Files are what to compile: the reprinted program, or the input when no plugin ran.
27
+ Project *tsbridge.Project
28
+ Files []*ast.SourceFile
29
+ // Exit is set when the plugin ended the process, with the lines it wrote to stdout. Nothing else happens then.
30
+ Exit []string
31
+ // Printed holds each file's reprinted text, by file name.
32
+ Printed map[string]string
33
+
34
+ maps map[*ast.SourceFile]*reprintMap
35
+ // warnings are what native plugins reported with AddDiagnostic when none of it was an error. They are at places in
36
+ // the source as written. MapDiagnostics puts them in front of the compile's.
37
+ warnings []*shared.Diagnostic
38
+ }
39
+
40
+ // Stats says how much the plugin step asked of the first program.
41
+ type Stats struct {
42
+ // Candidates is how many files have a property access that might be an accessor. Only these need a checker.
43
+ Candidates int
44
+ // Filtered is false when a computed accessor name or a mapped type's `as` clause turned the name filter off.
45
+ Filtered bool
46
+ Names int
47
+ }
48
+
49
+ // Apply is the "running transformers" part of compileFiles. Like JS, it transforms using the first program, prints
50
+ // every file, and makes a new program from the printed text, so that everything after this sees printed source.
51
+ //
52
+ // The first program is never fully checked. See docs/setget.md.
53
+ //
54
+ // build is what native plugins get to read about the build. Setget alone doesn't need it, so it can be nil then.
55
+ func Apply(ctx context.Context, p *tsbridge.Project, decision Decision, files []*ast.SourceFile, build *Build) (*Applied, error) {
56
+ result, _, err := ApplyWithStats(ctx, p, decision, files, build)
57
+ return result, err
58
+ }
59
+
60
+ // ApplyWithStats is Apply, and also says what it asked of the first program.
61
+ func ApplyWithStats(ctx context.Context, p *tsbridge.Project, decision Decision, files []*ast.SourceFile, build *Build) (*Applied, Stats, error) {
62
+ applied := &Applied{Project: p, Files: files}
63
+ var stats Stats
64
+ if !decision.Reprints() {
65
+ return applied, stats, nil
66
+ }
67
+ if decision.Action == ActionPlugins {
68
+ run := &nativeRun{}
69
+ result, err := run.transform(ctx, p, decision, files, build, false, func() *accessorNames { return collectAccessorNames(p) })
70
+ if err != nil {
71
+ return nil, stats, err
72
+ }
73
+ if result.exit != nil {
74
+ applied.Exit = result.exit
75
+ return applied, stats, nil
76
+ }
77
+ applied, err = reprinted(p, files, result.outcomes)
78
+ if err != nil {
79
+ return nil, stats, err
80
+ }
81
+ applied.warnings = result.warnings
82
+ return applied, stats, nil
83
+ }
84
+
85
+ names := collectAccessorNames(p)
86
+ stats.Filtered = !names.all
87
+ stats.Names = len(names.names)
88
+
89
+ outcomes := make([]fileOutcome, len(files))
90
+ index := make(map[*ast.SourceFile]int, len(files))
91
+ var needChecker, noChecker []*ast.SourceFile
92
+ uses := make([]bool, len(files))
93
+ if !names.empty() {
94
+ forEachParallel(files, func(i int, file *ast.SourceFile) { uses[i] = fileUsesName(file, names) })
95
+ }
96
+ for i, file := range files {
97
+ index[file] = i
98
+ if uses[i] {
99
+ needChecker = append(needChecker, file)
100
+ } else {
101
+ noChecker = append(noChecker, file)
102
+ }
103
+ }
104
+ stats.Candidates = len(needChecker)
105
+
106
+ // Each worker goes through its files in order and stops at a plugin exit. So the first exit in file order always
107
+ // gets found, whatever the scheduling: every file before it runs to completion.
108
+ if len(needChecker) > 0 {
109
+ workers := p.NewWorkers(ctx, needChecker, tsbridge.DefaultMacroSpec())
110
+ p.RunWorkers(workers, func(w *tsbridge.Worker) {
111
+ for _, file := range w.Files() {
112
+ outcome := &outcomes[index[file]]
113
+ transformFile(file, w.Checker(), decision.Prefix, names, outcome)
114
+ if outcome.exit != nil {
115
+ return
116
+ }
117
+ }
118
+ })
119
+ }
120
+ forEachParallel(noChecker, func(_ int, file *ast.SourceFile) {
121
+ transformFile(file, nil, decision.Prefix, names, &outcomes[index[file]])
122
+ })
123
+
124
+ for i := range outcomes {
125
+ if outcomes[i].exit != nil {
126
+ applied.Exit = outcomes[i].exit
127
+ return applied, stats, nil
128
+ }
129
+ if outcomes[i].panicText != "" {
130
+ return nil, stats, fmt.Errorf("internal error while transforming %s: %s", files[i].FileName().AsString(), outcomes[i].panicText)
131
+ }
132
+ }
133
+
134
+ applied, err := reprinted(p, files, outcomes)
135
+ return applied, stats, err
136
+ }
137
+
138
+ // reprinted makes the program to compile from each file's printed text, and keeps what maps its diagnostics back to
139
+ // the source. It is the same for setget and for native plugins.
140
+ func reprinted(p *tsbridge.Project, files []*ast.SourceFile, outcomes []fileOutcome) (*Applied, error) {
141
+ applied := &Applied{}
142
+ printed := make(map[tspath.PathKey]string, len(files))
143
+ applied.Printed = make(map[string]string, len(files))
144
+ for i, file := range files {
145
+ printed[file.PathKey()] = outcomes[i].text
146
+ applied.Printed[file.FileName().AsString()] = outcomes[i].text
147
+ }
148
+ next := p.WithPrintedFiles(printed)
149
+ applied.Project = next
150
+ applied.maps = make(map[*ast.SourceFile]*reprintMap, len(files))
151
+ applied.Files = make([]*ast.SourceFile, len(files))
152
+ byPath := next.Program().FilesByPath()
153
+ for i, file := range files {
154
+ printedFile := byPath[file.PathKey()]
155
+ if printedFile == nil {
156
+ return nil, fmt.Errorf("internal error: %s is missing from the reprinted program", file.FileName().AsString())
157
+ }
158
+ applied.Files[i] = printedFile
159
+ applied.maps[printedFile] = &reprintMap{original: file, generator: outcomes[i].generator}
160
+ }
161
+ return applied, nil
162
+ }
163
+
164
+ type fileOutcome struct {
165
+ text string
166
+ generator *sourcemap.Generator
167
+ exit []string
168
+ panicText string
169
+ }
170
+
171
+ func transformFile(file *ast.SourceFile, c *checker.Checker, prefix string, names *accessorNames, outcome *fileOutcome) {
172
+ defer func() {
173
+ switch r := recover().(type) {
174
+ case nil:
175
+ case *pluginExit:
176
+ outcome.exit = r.lines
177
+ default:
178
+ outcome.panicText = fmt.Sprint(r)
179
+ }
180
+ }()
181
+ context := newTransformContext(file, c, prefix, names)
182
+ printFile(file, context.transformFile(), context.emit, outcome)
183
+ }
184
+
185
+ // printFile prints a transformed file back to TypeScript, with the source map that MapDiagnostics reads.
186
+ func printFile(file *ast.SourceFile, transformed *ast.SourceFile, emit *printer.EmitContext, outcome *fileOutcome) {
187
+ // ts.createPrinter() has no options. That means no target, which is why numeric separators don't survive: 1_000
188
+ // prints as 1000.
189
+ options := printer.PrinterOptions{NewLine: core.NewLineKindLF, KeepParseTreeParentheses: true}
190
+ writer := printer.NewTextWriter("\n", 0)
191
+ generator := sourcemap.NewGenerator("", "", file.FileName().Directory(), tspath.CaseSensitive)
192
+ printer.NewPrinter(options, printer.PrintHandlers{}, emit).Write(transformed.AsNode(), transformed, writer, generator)
193
+ outcome.text = writer.String()
194
+ outcome.generator = generator
195
+ }
196
+
197
+ // collectAccessorNames walks every file in the program, including declaration files.
198
+ func collectAccessorNames(p *tsbridge.Project) *accessorNames {
199
+ files := p.Program().SourceFiles()
200
+ facts := make([]fileAccessorFacts, len(files))
201
+ forEachParallel(files, func(i int, file *ast.SourceFile) { facts[i] = scanAccessors(file) })
202
+ result := &accessorNames{names: map[string]bool{}}
203
+ mapped := false
204
+ computed := false
205
+ for _, fact := range facts {
206
+ for _, name := range fact.names {
207
+ result.names[name] = true
208
+ }
209
+ mapped = mapped || fact.mappedTypeName
210
+ computed = computed || fact.computed
211
+ }
212
+ result.all = computed || (mapped && len(result.names) > 0)
213
+ return result
214
+ }
215
+
216
+ func forEachParallel(files []*ast.SourceFile, fn func(i int, file *ast.SourceFile)) {
217
+ var wg sync.WaitGroup
218
+ gate := make(chan struct{}, runtime.GOMAXPROCS(0))
219
+ for i, file := range files {
220
+ wg.Add(1)
221
+ gate <- struct{}{}
222
+ go func() {
223
+ defer wg.Done()
224
+ defer func() { <-gate }()
225
+ fn(i, file)
226
+ }()
227
+ }
228
+ wg.Wait()
229
+ }
230
+
231
+ // WriteTransformedFiles is the writeTransformedFiles option. JS writes these right after printing, in file order.
232
+ func (a *Applied) WriteTransformedFiles(data *shared.ProjectData, translator *pathtranslator.Translator) error {
233
+ if !data.ProjectOptions.WriteTransformedFiles || a.Printed == nil {
234
+ return nil
235
+ }
236
+ for _, file := range a.Files {
237
+ outPath := translator.GetOutputTransformedPath(nodepath.Native.Normalize(file.FileName().AsString()))
238
+ if err := os.MkdirAll(nodepath.Native.Dirname(outPath), 0o777); err != nil {
239
+ return err
240
+ }
241
+ if err := os.WriteFile(outPath, []byte(a.Printed[file.FileName().AsString()]), 0o666); err != nil {
242
+ return err
243
+ }
244
+ }
245
+ return nil
246
+ }
247
+
248
+ // reprintMap maps a position in a printed file back to the source file it came from.
249
+ type reprintMap struct {
250
+ original *ast.SourceFile
251
+ generator *sourcemap.Generator
252
+ once sync.Once
253
+ mappings []sourcemap.Mapping
254
+ }
255
+
256
+ func (m *reprintMap) load() {
257
+ m.once.Do(func() {
258
+ decoder := sourcemap.DecodeMappings(m.generator.RawSourceMap().Mappings)
259
+ for mapping := range decoder.Values() {
260
+ if mapping.SourceIndex != sourcemap.MissingSource {
261
+ m.mappings = append(m.mappings, *mapping)
262
+ }
263
+ }
264
+ sort.SliceStable(m.mappings, func(i, j int) bool {
265
+ a, b := &m.mappings[i], &m.mappings[j]
266
+ return a.GeneratedLine < b.GeneratedLine || a.GeneratedLine == b.GeneratedLine && a.GeneratedCharacter < b.GeneratedCharacter
267
+ })
268
+ })
269
+ }
270
+
271
+ // position finds the original position for a printed one: the last mapping at or before it. Every node with a place in
272
+ // the source writes a mapping at its first token, so a diagnostic's start normally matches one exactly.
273
+ func (m *reprintMap) position(printedFile *ast.SourceFile, pos int) (int, bool) {
274
+ m.load()
275
+ pos = min(pos, len(printedFile.Text()))
276
+ line, character := scanner.GetECMALineAndUTF16CharacterOfPosition(printedFile, pos)
277
+ i := sort.Search(len(m.mappings), func(i int) bool {
278
+ a := &m.mappings[i]
279
+ return a.GeneratedLine > line || a.GeneratedLine == line && a.GeneratedCharacter > character
280
+ })
281
+ if i == 0 {
282
+ return 0, false
283
+ }
284
+ found := &m.mappings[i-1]
285
+ return scanner.GetECMAPositionOfLineAndUTF16Character(m.original, found.SourceLine, found.SourceCharacter), true
286
+ }
287
+
288
+ // locate maps a printed range to the original file. It keeps the printed length when the end doesn't map to a later
289
+ // place on the same line.
290
+ func (m *reprintMap) locate(printedFile *ast.SourceFile, loc core.TextRange) (core.TextRange, bool) {
291
+ start, ok := m.position(printedFile, loc.Pos())
292
+ if !ok {
293
+ return loc, false
294
+ }
295
+ end := start + loc.Len()
296
+ if mapped, ok := m.position(printedFile, loc.End()); ok && mapped > start {
297
+ end = mapped
298
+ }
299
+ lineEnd := scanner.GetECMAEndLinePosition(m.original, scanner.GetECMALineOfPosition(m.original, start))
300
+ if end > lineEnd && lineEnd > start {
301
+ end = lineEnd
302
+ }
303
+ return core.NewTextRange(start, min(end, len(m.original.Text()))), true
304
+ }
305
+
306
+ // MapDiagnostics points diagnostics at the source the user wrote, not at the printed text. This is where rbxtsgo differs
307
+ // from JS on purpose. See docs/known-differences.md. A compound assignment prints its object twice, so an error in it
308
+ // is reported twice in the printed text. Those are the same place in the source, and only one stays.
309
+ //
310
+ // The warnings of native plugins come first in the result. They are at source positions already.
311
+ func (a *Applied) MapDiagnostics(list []*shared.Diagnostic) []*shared.Diagnostic {
312
+ if a.maps == nil {
313
+ return list
314
+ }
315
+ result := make([]*shared.Diagnostic, 0, len(a.warnings)+len(list))
316
+ result = append(result, a.warnings...)
317
+ type place struct {
318
+ file *ast.SourceFile
319
+ start int
320
+ code int32
321
+ message string
322
+ }
323
+ seen := make(map[place]bool, len(list))
324
+ relocate := func(file *ast.SourceFile, loc core.TextRange) (*ast.SourceFile, core.TextRange, bool) {
325
+ m := a.maps[file]
326
+ if m == nil {
327
+ return nil, core.TextRange{}, false
328
+ }
329
+ mapped, ok := m.locate(file, loc)
330
+ return m.original, mapped, ok
331
+ }
332
+ for _, d := range list {
333
+ moved := d.Relocated(relocate)
334
+ key := place{moved.File(), moved.Start(), moved.Code(), moved.Message()}
335
+ if moved.File() != nil && seen[key] {
336
+ continue
337
+ }
338
+ seen[key] = true
339
+ result = append(result, moved)
340
+ }
341
+ return result
342
+ }