@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.
- package/LICENSE +21 -0
- package/README.md +181 -2
- package/README.rbxtsgo.md +169 -0
- package/THIRD_PARTY_NOTICES.md +745 -0
- package/cli/cli.go +454 -0
- package/compile/compile.go +388 -0
- package/compile/declarations.go +221 -0
- package/compile/incremental.go +349 -0
- package/compile/times.go +9 -0
- package/docs/plugin-api.md +239 -0
- package/go.mod +20 -0
- package/go.sum +24 -0
- package/internal/difftest/difftest.go +530 -0
- package/internal/plugintest/plugintest.go +181 -0
- package/luau/create.go +351 -0
- package/luau/doc.go +71 -0
- package/luau/globals.go +178 -0
- package/luau/jsnum.go +188 -0
- package/luau/kinds.go +128 -0
- package/luau/list.go +227 -0
- package/luau/nodes.go +479 -0
- package/luau/operators.go +46 -0
- package/luau/render.go +134 -0
- package/luau/render_expressions.go +264 -0
- package/luau/render_fields.go +81 -0
- package/luau/render_indexable.go +81 -0
- package/luau/render_state.go +134 -0
- package/luau/render_statements.go +235 -0
- package/luau/render_util.go +296 -0
- package/luau/solve_temp_ids.go +212 -0
- package/luau/typeguards.go +175 -0
- package/luau/util.go +184 -0
- package/luau/visit.go +138 -0
- package/npm/index.d.ts +3 -0
- package/npm/rbxts-go.sh +187 -0
- package/package.json +45 -5
- package/patches/typescript/0001-checker-exports.patch +40 -0
- package/patches/typescript/0002-iterable-arity.patch +15 -0
- package/patches/typescript/0003-context-sensitive-this.patch +15 -0
- package/patches/typescript/0004-printer-keep-parse-tree-parentheses.patch +26 -0
- package/pluginapi/host.go +31 -0
- package/pluginapi/pluginapi.go +153 -0
- package/plugins/LICENSE-rbxts-transformer-setget +18 -0
- package/plugins/apply.go +342 -0
- package/plugins/config.go +431 -0
- package/plugins/fallback.go +84 -0
- package/plugins/fixemit.go +60 -0
- package/plugins/native.go +262 -0
- package/plugins/session.go +223 -0
- package/plugins/setget.go +478 -0
- package/project/copy.go +333 -0
- package/project/include/Promise.lua +2068 -0
- package/project/include/RuntimeLib.lua +260 -0
- package/project/nodepath/nodepath.go +917 -0
- package/project/pathtranslator/pathtranslator.go +214 -0
- package/project/project.go +498 -0
- package/project/rojoresolver/json.go +248 -0
- package/project/rojoresolver/rojoresolver.go +559 -0
- package/project/shared/constants.go +83 -0
- package/project/shared/diagnostic.go +238 -0
- package/project/shared/diagnostics.go +388 -0
- package/project/shared/elapsed.go +14 -0
- package/project/shared/eol_other.go +6 -0
- package/project/shared/eol_windows.go +4 -0
- package/project/shared/kleur.go +40 -0
- package/project/shared/util.go +183 -0
- package/scripts/build-with-plugins.sh +130 -0
- package/scripts/setup-third-party.sh +75 -0
- package/transformer/add_index_diagnostics.go +21 -0
- package/transformer/add_one_if_array_type.go +15 -0
- package/transformer/array_binding_pattern_contains_hoists.go +25 -0
- package/transformer/assert_never.go +24 -0
- package/transformer/assignment.go +64 -0
- package/transformer/call_macros.go +70 -0
- package/transformer/check_variable_hoist.go +125 -0
- package/transformer/clean_module_name.go +28 -0
- package/transformer/constructor_macros.go +122 -0
- package/transformer/convert_to_indexable_expression.go +12 -0
- package/transformer/create_binary_from_operator.go +86 -0
- package/transformer/create_continue_statements.go +68 -0
- package/transformer/create_get_service.go +15 -0
- package/transformer/create_hoist_declaration.go +27 -0
- package/transformer/create_import_expression.go +220 -0
- package/transformer/create_string_from_literal.go +195 -0
- package/transformer/create_truthiness_checks.go +63 -0
- package/transformer/create_type_check.go +9 -0
- package/transformer/ensure_transform_order.go +53 -0
- package/transformer/expression_chain.go +27 -0
- package/transformer/expression_might_mutate.go +60 -0
- package/transformer/find_constructor.go +15 -0
- package/transformer/fixup_whitespace_and_decode_entities.go +377 -0
- package/transformer/get_accessor_for_binding_type.go +160 -0
- package/transformer/get_add_iterable_to_array_builder.go +363 -0
- package/transformer/get_assignable_value.go +15 -0
- package/transformer/get_constant_value_literal.go +20 -0
- package/transformer/get_declared_variables.go +34 -0
- package/transformer/get_extends_node.go +23 -0
- package/transformer/get_flags.go +21 -0
- package/transformer/get_kind_name.go +18 -0
- package/transformer/get_original_symbol_of_node.go +16 -0
- package/transformer/get_source_file_from_module_specifier.go +68 -0
- package/transformer/get_statements.go +12 -0
- package/transformer/has_multiple_definitions.go +18 -0
- package/transformer/hoist_functions_past_final_statement.go +43 -0
- package/transformer/identifier_macros.go +17 -0
- package/transformer/is_blocked_by_try_statement.go +19 -0
- package/transformer/is_method.go +104 -0
- package/transformer/is_symbol_mutable.go +26 -0
- package/transformer/is_symbol_of_value.go +11 -0
- package/transformer/is_used_as_statement.go +25 -0
- package/transformer/is_valid_method_index_without_call.go +36 -0
- package/transformer/macro_manager.go +92 -0
- package/transformer/object_accessor.go +32 -0
- package/transformer/offset.go +58 -0
- package/transformer/pointer.go +53 -0
- package/transformer/property_call_macros.go +1035 -0
- package/transformer/state.go +606 -0
- package/transformer/transform_array_assignment_pattern.go +65 -0
- package/transformer/transform_array_binding_pattern.go +49 -0
- package/transformer/transform_array_literal_expression.go +97 -0
- package/transformer/transform_await_expression.go +12 -0
- package/transformer/transform_binary_expression.go +221 -0
- package/transformer/transform_binding_name.go +25 -0
- package/transformer/transform_block.go +14 -0
- package/transformer/transform_boolean_literal.go +13 -0
- package/transformer/transform_break_statement.go +26 -0
- package/transformer/transform_call_expression.go +266 -0
- package/transformer/transform_class_constructor.go +125 -0
- package/transformer/transform_class_declaration.go +12 -0
- package/transformer/transform_class_expression.go +14 -0
- package/transformer/transform_class_like_declaration.go +355 -0
- package/transformer/transform_conditional_expression.go +70 -0
- package/transformer/transform_continue_statement.go +30 -0
- package/transformer/transform_decorators.go +253 -0
- package/transformer/transform_delete_expression.go +17 -0
- package/transformer/transform_do_statement.go +39 -0
- package/transformer/transform_element_access_expression.go +114 -0
- package/transformer/transform_entity_name.go +21 -0
- package/transformer/transform_enum_declaration.go +135 -0
- package/transformer/transform_export_assignment.go +57 -0
- package/transformer/transform_export_declaration.go +150 -0
- package/transformer/transform_expression.go +120 -0
- package/transformer/transform_expression_statement.go +71 -0
- package/transformer/transform_for_of_statement.go +461 -0
- package/transformer/transform_for_statement.go +599 -0
- package/transformer/transform_function_declaration.go +84 -0
- package/transformer/transform_function_expression.go +48 -0
- package/transformer/transform_identifier.go +194 -0
- package/transformer/transform_if_statement.go +44 -0
- package/transformer/transform_import_declaration.go +146 -0
- package/transformer/transform_import_equals_declaration.go +46 -0
- package/transformer/transform_import_expression.go +36 -0
- package/transformer/transform_initializer.go +22 -0
- package/transformer/transform_interpolated_string_part.go +12 -0
- package/transformer/transform_jsx.go +41 -0
- package/transformer/transform_jsx_attributes.go +110 -0
- package/transformer/transform_jsx_children.go +50 -0
- package/transformer/transform_jsx_element.go +14 -0
- package/transformer/transform_jsx_expression.go +20 -0
- package/transformer/transform_jsx_fragment.go +42 -0
- package/transformer/transform_jsx_self_closing_element.go +13 -0
- package/transformer/transform_jsx_tag_name.go +53 -0
- package/transformer/transform_logical.go +160 -0
- package/transformer/transform_logical_or_coalescing_assignment_expression.go +103 -0
- package/transformer/transform_method_declaration.go +102 -0
- package/transformer/transform_module_declaration.go +143 -0
- package/transformer/transform_new_expression.go +27 -0
- package/transformer/transform_no_substitution_template_literal.go +16 -0
- package/transformer/transform_numeric_literal.go +15 -0
- package/transformer/transform_object_assignment_pattern.go +86 -0
- package/transformer/transform_object_binding_pattern.go +49 -0
- package/transformer/transform_object_literal_expression.go +107 -0
- package/transformer/transform_omitted_expression.go +9 -0
- package/transformer/transform_optional_chain.go +312 -0
- package/transformer/transform_parameters.go +124 -0
- package/transformer/transform_parenthesized_expression.go +17 -0
- package/transformer/transform_property_access_expression.go +37 -0
- package/transformer/transform_property_declaration.go +33 -0
- package/transformer/transform_property_name.go +20 -0
- package/transformer/transform_return_statement.go +83 -0
- package/transformer/transform_source_file.go +230 -0
- package/transformer/transform_spread_element.go +35 -0
- package/transformer/transform_statement.go +84 -0
- package/transformer/transform_statement_list.go +90 -0
- package/transformer/transform_string_literal.go +22 -0
- package/transformer/transform_super_keyword.go +9 -0
- package/transformer/transform_switch_statement.go +145 -0
- package/transformer/transform_tagged_template_expression.go +34 -0
- package/transformer/transform_template_expression.go +35 -0
- package/transformer/transform_this_expression.go +33 -0
- package/transformer/transform_throw_statement.go +18 -0
- package/transformer/transform_try_statement.go +192 -0
- package/transformer/transform_type_expression.go +12 -0
- package/transformer/transform_unary_expression.go +79 -0
- package/transformer/transform_variable_statement.go +178 -0
- package/transformer/transform_void_expression.go +13 -0
- package/transformer/transform_while_statement.go +34 -0
- package/transformer/transform_writable.go +69 -0
- package/transformer/transform_yield_expression.go +56 -0
- package/transformer/type_guards.go +30 -0
- package/transformer/types.go +381 -0
- package/transformer/validate_identifier.go +17 -0
- package/transformer/validate_method_assignment.go +106 -0
- package/transformer/validate_not_any.go +37 -0
- package/transformer/wrap_expression_statement.go +20 -0
- package/transformer/wrap_return_if_lua_tuple.go +63 -0
- package/transformer/wrap_statements_as_generator.go +22 -0
- package/tsbridge/affected.go +141 -0
- package/tsbridge/config.go +71 -0
- package/tsbridge/diagnostics.go +128 -0
- package/tsbridge/doc.go +16 -0
- package/tsbridge/macros.go +207 -0
- package/tsbridge/macrospec.go +30 -0
- package/tsbridge/pool.go +142 -0
- package/tsbridge/project.go +137 -0
- package/tsbridge/reprint.go +99 -0
- package/tsbridge/update.go +128 -0
- package/watch/poll.go +126 -0
- package/watch/session.go +651 -0
- package/watch/watch.go +180 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
// Package compile is the port of src/Project/functions/compileFiles.ts after the checks that project.PrepareCompile
|
|
2
|
+
// does: pre-emit diagnostics, transforming in parallel, and writing the results.
|
|
3
|
+
//
|
|
4
|
+
// Upstream handles files one at a time. It adds a file's pre-emit diagnostics, stops transforming as soon as any error
|
|
5
|
+
// exists, and otherwise transforms the file. So file i is transformed only if files 0..i have no pre-emit errors and
|
|
6
|
+
// files 0..i-1 have no transform errors. This port transforms in parallel with one worker per checker, keeps each
|
|
7
|
+
// file's results apart, and merges them in file order under that same rule. Which diagnostics get reported then doesn't
|
|
8
|
+
// depend on scheduling. See the notes on Run for the two pieces of per-compile state this affects.
|
|
9
|
+
//
|
|
10
|
+
// Declaration emit is in declarations.go, and emitBuildInfo is in incremental.go.
|
|
11
|
+
package compile
|
|
12
|
+
|
|
13
|
+
import (
|
|
14
|
+
"context"
|
|
15
|
+
"fmt"
|
|
16
|
+
"os"
|
|
17
|
+
"runtime"
|
|
18
|
+
"sync"
|
|
19
|
+
"sync/atomic"
|
|
20
|
+
"time"
|
|
21
|
+
"unicode/utf8"
|
|
22
|
+
|
|
23
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
24
|
+
"github.com/microsoft/TypeScript/tsc/internal/diagnostics"
|
|
25
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
26
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project"
|
|
27
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
|
|
28
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/pathtranslator"
|
|
29
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
30
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/transformer"
|
|
31
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/tsbridge"
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
// Mode says how much state one Run shares between files.
|
|
35
|
+
type Mode int
|
|
36
|
+
|
|
37
|
+
const (
|
|
38
|
+
// ModeWhole is one compileFiles call with every file. An error stops later files from being transformed.
|
|
39
|
+
ModeWhole Mode = iota
|
|
40
|
+
// ModeIsolated is one compileFiles call per file on one program, as upstream's src/CLI/test.ts does. Each file gets
|
|
41
|
+
// a new MultiTransformState, and an error in one file doesn't affect the others.
|
|
42
|
+
ModeIsolated
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
// FileResult is what one file produced.
|
|
46
|
+
type FileResult struct {
|
|
47
|
+
File *ast.SourceFile
|
|
48
|
+
// Source is the rendered Luau, with lone surrogates written the way JS writes them. Check HasSource.
|
|
49
|
+
Source string
|
|
50
|
+
HasSource bool
|
|
51
|
+
// Diagnostics holds this file's pre-emit diagnostics followed by the ones from transforming it, as far as upstream's
|
|
52
|
+
// stopping rule lets them through.
|
|
53
|
+
Diagnostics []*shared.Diagnostic
|
|
54
|
+
// Panic holds an internal compiler error from transforming this file: an upstream assert or a port bug.
|
|
55
|
+
Panic string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Context is what a compile needs that doesn't change between Run calls.
|
|
59
|
+
type Context struct {
|
|
60
|
+
Project *tsbridge.Project
|
|
61
|
+
Data *shared.ProjectData
|
|
62
|
+
Translator *pathtranslator.Translator
|
|
63
|
+
Prepared *project.CompileContext
|
|
64
|
+
// Replay holds the accumulated text of context diagnostics. It must outlive one Run, as in upstream.
|
|
65
|
+
Replay *shared.ContextReplay
|
|
66
|
+
// Times receives where the time goes, when it's set.
|
|
67
|
+
Times *Times
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type transformed struct {
|
|
71
|
+
source string
|
|
72
|
+
hasSource bool
|
|
73
|
+
diagnostics []*shared.Diagnostic
|
|
74
|
+
panicText string
|
|
75
|
+
ran bool
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Run transforms files and returns a result per file, in input order.
|
|
79
|
+
//
|
|
80
|
+
// Two pieces of state are per worker here and global upstream. The once-per-symbol diagnostic caches can't lose or
|
|
81
|
+
// repeat anything that gets reported: reporting one means an error, the merge drops everything after the first file
|
|
82
|
+
// with an error, and no earlier file in any worker can have set the cache. And context diagnostics accumulate text in
|
|
83
|
+
// the order they're created, so the merge rebuilds that text in file order with Context.Replay.
|
|
84
|
+
func Run(ctx context.Context, c *Context, files []*ast.SourceFile, mode Mode) ([]*FileResult, error) {
|
|
85
|
+
p := c.Project
|
|
86
|
+
options := p.Options()
|
|
87
|
+
|
|
88
|
+
times := c.Times
|
|
89
|
+
if times == nil {
|
|
90
|
+
times = &Times{}
|
|
91
|
+
}
|
|
92
|
+
phase := time.Now()
|
|
93
|
+
preEmit := make([][]*shared.Diagnostic, len(files))
|
|
94
|
+
for i, list := range p.PreEmitDiagnosticsForFiles(ctx, files) {
|
|
95
|
+
preEmit[i] = append(shared.FromTSList(list), project.FileUsesCommentDirectives(c.Data, files[i])...)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
times.Check = time.Since(phase)
|
|
99
|
+
times.FilesChecked = len(files)
|
|
100
|
+
phase = time.Now()
|
|
101
|
+
|
|
102
|
+
// In whole mode, no file at or after the first pre-emit error gets transformed.
|
|
103
|
+
limit := len(files)
|
|
104
|
+
if mode == ModeWhole {
|
|
105
|
+
for i, list := range preEmit {
|
|
106
|
+
if shared.HasErrors(list) {
|
|
107
|
+
limit = i
|
|
108
|
+
break
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
index := make(map[*ast.SourceFile]int, len(files))
|
|
113
|
+
for i, file := range files {
|
|
114
|
+
index[file] = i
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
results := make([]transformed, len(files))
|
|
118
|
+
stateOptions := transformer.StateOptions{
|
|
119
|
+
Program: p.Program(),
|
|
120
|
+
Data: c.Data,
|
|
121
|
+
PathTranslator: c.Translator,
|
|
122
|
+
Compile: c.Prepared,
|
|
123
|
+
Options: options,
|
|
124
|
+
}
|
|
125
|
+
var macroErr error
|
|
126
|
+
workers := p.NewWorkers(ctx, files, tsbridge.DefaultMacroSpec())
|
|
127
|
+
macroErrs := make([]error, len(workers))
|
|
128
|
+
workerIndex := make(map[*tsbridge.Worker]int, len(workers))
|
|
129
|
+
for i, w := range workers {
|
|
130
|
+
workerIndex[w] = i
|
|
131
|
+
}
|
|
132
|
+
p.RunWorkers(workers, func(w *tsbridge.Worker) {
|
|
133
|
+
macros, err := w.Macros()
|
|
134
|
+
if err != nil {
|
|
135
|
+
macroErrs[workerIndex[w]] = err
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
services := &transformer.TransformServices{MacroManager: macros}
|
|
139
|
+
multi := transformer.NewMultiTransformState()
|
|
140
|
+
for _, file := range w.Files() {
|
|
141
|
+
i := index[file]
|
|
142
|
+
if i >= limit {
|
|
143
|
+
continue
|
|
144
|
+
}
|
|
145
|
+
if mode == ModeIsolated {
|
|
146
|
+
multi = transformer.NewMultiTransformState()
|
|
147
|
+
if shared.HasErrors(preEmit[i]) {
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
result := &results[i]
|
|
152
|
+
transformOne(ctx, w, stateOptions, services, multi, file, result)
|
|
153
|
+
// Nothing after a failed file can be reported, so don't spend time on it.
|
|
154
|
+
if mode == ModeWhole && (result.panicText != "" || shared.HasErrors(result.diagnostics)) {
|
|
155
|
+
break
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
for _, err := range macroErrs {
|
|
160
|
+
if err != nil {
|
|
161
|
+
macroErr = err
|
|
162
|
+
break
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if macroErr != nil {
|
|
166
|
+
return nil, shared.NewProjectError(macroErr.Error())
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
times.Lower = time.Since(phase)
|
|
170
|
+
for i := range results {
|
|
171
|
+
if results[i].ran {
|
|
172
|
+
times.FilesLowered++
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Merge in file order.
|
|
177
|
+
output := make([]*FileResult, len(files))
|
|
178
|
+
stopped := false
|
|
179
|
+
for i, file := range files {
|
|
180
|
+
result := &FileResult{File: file, Diagnostics: preEmit[i]}
|
|
181
|
+
output[i] = result
|
|
182
|
+
if mode == ModeWhole {
|
|
183
|
+
if stopped {
|
|
184
|
+
continue
|
|
185
|
+
}
|
|
186
|
+
if shared.HasErrors(preEmit[i]) {
|
|
187
|
+
stopped = true
|
|
188
|
+
continue
|
|
189
|
+
}
|
|
190
|
+
} else if shared.HasErrors(preEmit[i]) {
|
|
191
|
+
c.Replay.Apply(result.Diagnostics)
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
t := &results[i]
|
|
195
|
+
result.Panic = t.panicText
|
|
196
|
+
result.Diagnostics = append(result.Diagnostics, t.diagnostics...)
|
|
197
|
+
c.Replay.Apply(result.Diagnostics)
|
|
198
|
+
if t.panicText != "" || shared.HasErrors(t.diagnostics) {
|
|
199
|
+
stopped = true
|
|
200
|
+
continue
|
|
201
|
+
}
|
|
202
|
+
result.Source = t.source
|
|
203
|
+
result.HasSource = t.hasSource
|
|
204
|
+
}
|
|
205
|
+
// compileFiles writes nothing when any error was reported, including files that compiled before it.
|
|
206
|
+
if mode == ModeWhole && stopped {
|
|
207
|
+
for _, result := range output {
|
|
208
|
+
result.Source = ""
|
|
209
|
+
result.HasSource = false
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return output, nil
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
func transformOne(ctx context.Context, w *tsbridge.Worker, options transformer.StateOptions, services *transformer.TransformServices, multi *transformer.MultiTransformState, file *ast.SourceFile, result *transformed) {
|
|
216
|
+
result.ran = true
|
|
217
|
+
service := &shared.DiagnosticService{}
|
|
218
|
+
defer func() {
|
|
219
|
+
result.diagnostics = service.Flush()
|
|
220
|
+
if r := recover(); r != nil {
|
|
221
|
+
result.panicText = panicText(r)
|
|
222
|
+
}
|
|
223
|
+
}()
|
|
224
|
+
state := transformer.NewTransformState(ctx, w, options, services, multi, service, file)
|
|
225
|
+
statements := transformer.TransformSourceFile(state, file)
|
|
226
|
+
if service.HasErrors() {
|
|
227
|
+
return
|
|
228
|
+
}
|
|
229
|
+
result.source = WriteableText(luau.RenderAST(statements))
|
|
230
|
+
result.hasSource = true
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
func panicText(r any) string {
|
|
234
|
+
switch v := r.(type) {
|
|
235
|
+
case string:
|
|
236
|
+
return v
|
|
237
|
+
case error:
|
|
238
|
+
return v.Error()
|
|
239
|
+
}
|
|
240
|
+
return fmt.Sprint(r)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// WriteableText turns WTF-8 into what JS writes. A Go string can hold a lone surrogate from an escape such as "\uD83D",
|
|
244
|
+
// as the bytes ED A0 BD. JS holds UTF-16 units: two adjacent surrogates that form a pair are one character, and when
|
|
245
|
+
// fs.outputFileSync encodes a lone one, it writes U+FFFD. Everything else is left as it is.
|
|
246
|
+
func WriteableText(text string) string {
|
|
247
|
+
isSurrogate := func(i int) bool {
|
|
248
|
+
return i+2 < len(text) && text[i] == 0xED && text[i+1] >= 0xA0 && text[i+1] <= 0xBF
|
|
249
|
+
}
|
|
250
|
+
found := false
|
|
251
|
+
for i := range len(text) {
|
|
252
|
+
if isSurrogate(i) {
|
|
253
|
+
found = true
|
|
254
|
+
break
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if !found {
|
|
258
|
+
return text
|
|
259
|
+
}
|
|
260
|
+
unit := func(i int) rune {
|
|
261
|
+
return 0xD000 | rune(text[i+1]&0x3F)<<6 | rune(text[i+2]&0x3F)
|
|
262
|
+
}
|
|
263
|
+
out := make([]byte, 0, len(text))
|
|
264
|
+
for i := 0; i < len(text); {
|
|
265
|
+
if !isSurrogate(i) {
|
|
266
|
+
out = append(out, text[i])
|
|
267
|
+
i++
|
|
268
|
+
continue
|
|
269
|
+
}
|
|
270
|
+
high := unit(i)
|
|
271
|
+
if high < 0xDC00 && isSurrogate(i+3) {
|
|
272
|
+
if low := unit(i + 3); low >= 0xDC00 {
|
|
273
|
+
out = utf8.AppendRune(out, 0x10000+(high-0xD800)<<10+(low-0xDC00))
|
|
274
|
+
i += 6
|
|
275
|
+
continue
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
out = append(out, 0xEF, 0xBF, 0xBD)
|
|
279
|
+
i += 3
|
|
280
|
+
}
|
|
281
|
+
return string(out)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// EmitResult is ts.EmitResult, as compileFiles returns it.
|
|
285
|
+
type EmitResult struct {
|
|
286
|
+
EmittedFiles []string
|
|
287
|
+
EmitSkipped bool
|
|
288
|
+
Diagnostics []*shared.Diagnostic
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// CompileFiles is the rest of compileFiles, for one whole compile. It writes the output files in file order.
|
|
292
|
+
func CompileFiles(ctx context.Context, c *Context, files []*ast.SourceFile) (*EmitResult, error) {
|
|
293
|
+
shared.LogService.WriteLineIfVerbose("compiling as " + string(c.Prepared.ProjectType) + "..")
|
|
294
|
+
results, err := Run(ctx, c, files, ModeWhole)
|
|
295
|
+
if err != nil {
|
|
296
|
+
return nil, err
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
var all []*shared.Diagnostic
|
|
300
|
+
for _, result := range results {
|
|
301
|
+
if result.Panic != "" {
|
|
302
|
+
return nil, fmt.Errorf("internal error while compiling %s: %s", result.File.FileName().AsString(), result.Panic)
|
|
303
|
+
}
|
|
304
|
+
all = append(all, result.Diagnostics...)
|
|
305
|
+
}
|
|
306
|
+
if shared.HasErrors(all) {
|
|
307
|
+
return &EmitResult{EmitSkipped: true, Diagnostics: all}, nil
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
phase := time.Now()
|
|
311
|
+
emitted, err := WriteResults(c, results)
|
|
312
|
+
if c.Times != nil {
|
|
313
|
+
c.Times.Write = time.Since(phase)
|
|
314
|
+
}
|
|
315
|
+
if err == nil && c.Project.Options().Declaration.IsTrue() {
|
|
316
|
+
phase = time.Now()
|
|
317
|
+
var declarations []string
|
|
318
|
+
declarations, err = EmitDeclarations(ctx, c, files)
|
|
319
|
+
if c.Times != nil {
|
|
320
|
+
c.Times.Declarations = time.Since(phase)
|
|
321
|
+
}
|
|
322
|
+
// Upstream's emittedFiles has only the Luau, because ts.sys.writeFile writes the declarations. Watch's report
|
|
323
|
+
// of what it wrote wants both, and nothing else reads them.
|
|
324
|
+
emitted = append(emitted, declarations...)
|
|
325
|
+
}
|
|
326
|
+
if err != nil {
|
|
327
|
+
return nil, err
|
|
328
|
+
}
|
|
329
|
+
return &EmitResult{EmittedFiles: emitted, Diagnostics: all}, nil
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// WriteResults writes each rendered file to its output path, as the emit loop of compileFiles does. The files are
|
|
333
|
+
// independent, so several goroutines write them at once. The returned paths keep the order of the results, and the
|
|
334
|
+
// error returned is the first one in that order, with the paths before it, as a sequential loop would give.
|
|
335
|
+
func WriteResults(c *Context, results []*FileResult) ([]string, error) {
|
|
336
|
+
writeOne := func(result *FileResult) (string, error) {
|
|
337
|
+
outPath := c.Translator.GetOutputPath(nodepath.Native.Normalize(result.File.FileName().AsString()))
|
|
338
|
+
if c.Data.ProjectOptions.WriteOnlyChanged {
|
|
339
|
+
if current, err := os.ReadFile(outPath); err == nil && string(current) == result.Source {
|
|
340
|
+
return "", nil
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if err := os.MkdirAll(nodepath.Native.Dirname(outPath), 0o777); err != nil {
|
|
344
|
+
return "", err
|
|
345
|
+
}
|
|
346
|
+
if err := os.WriteFile(outPath, []byte(result.Source), 0o666); err != nil {
|
|
347
|
+
return "", err
|
|
348
|
+
}
|
|
349
|
+
return outPath, nil
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
paths := make([]string, len(results))
|
|
353
|
+
errs := make([]error, len(results))
|
|
354
|
+
writers := min(runtime.GOMAXPROCS(0), 8, len(results))
|
|
355
|
+
var next atomic.Int64
|
|
356
|
+
var wg sync.WaitGroup
|
|
357
|
+
for range writers {
|
|
358
|
+
wg.Add(1)
|
|
359
|
+
go func() {
|
|
360
|
+
defer wg.Done()
|
|
361
|
+
for {
|
|
362
|
+
i := int(next.Add(1)) - 1
|
|
363
|
+
if i >= len(results) {
|
|
364
|
+
return
|
|
365
|
+
}
|
|
366
|
+
if results[i].HasSource {
|
|
367
|
+
paths[i], errs[i] = writeOne(results[i])
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}()
|
|
371
|
+
}
|
|
372
|
+
wg.Wait()
|
|
373
|
+
|
|
374
|
+
var emitted []string
|
|
375
|
+
for i := range results {
|
|
376
|
+
if errs[i] != nil {
|
|
377
|
+
return emitted, errs[i]
|
|
378
|
+
}
|
|
379
|
+
if paths[i] != "" {
|
|
380
|
+
emitted = append(emitted, paths[i])
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return emitted, nil
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
func failure(message string) *EmitResult {
|
|
387
|
+
return &EmitResult{EmitSkipped: true, Diagnostics: []*shared.Diagnostic{shared.TextDiagnostic(message, diagnostics.CategoryError)}}
|
|
388
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
package compile
|
|
2
|
+
|
|
3
|
+
// Declaration emit: the proxyProgram.emit call in compileFiles.ts, and the two afterDeclarations transformers from
|
|
4
|
+
// src/Project/transformers/builtin. TS 7 has no hook for custom transformers, so those run on the printed text: parse it,
|
|
5
|
+
// find the module specifiers, and replace them. The parser finds exactly the nodes the upstream visitor finds, and text
|
|
6
|
+
// outside them can't change.
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
"context"
|
|
10
|
+
"os"
|
|
11
|
+
"regexp"
|
|
12
|
+
"slices"
|
|
13
|
+
"sort"
|
|
14
|
+
"strings"
|
|
15
|
+
|
|
16
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
17
|
+
"github.com/microsoft/TypeScript/tsc/internal/compiler"
|
|
18
|
+
"github.com/microsoft/TypeScript/tsc/internal/core"
|
|
19
|
+
"github.com/microsoft/TypeScript/tsc/internal/module"
|
|
20
|
+
"github.com/microsoft/TypeScript/tsc/internal/parser"
|
|
21
|
+
"github.com/microsoft/TypeScript/tsc/internal/scanner"
|
|
22
|
+
"github.com/microsoft/TypeScript/tsc/internal/tspath"
|
|
23
|
+
"github.com/microsoft/TypeScript/tsc/internal/vfs"
|
|
24
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
// EmitDeclarations writes the declaration files of files and returns the paths it wrote. Like upstream it ignores the
|
|
28
|
+
// emit's own diagnostics: pre-emit diagnostics have reported the same problems by the time this runs.
|
|
29
|
+
func EmitDeclarations(ctx context.Context, c *Context, files []*ast.SourceFile) ([]string, error) {
|
|
30
|
+
if len(files) == 0 {
|
|
31
|
+
return nil, nil
|
|
32
|
+
}
|
|
33
|
+
program := c.Project.Program()
|
|
34
|
+
options := c.Project.Options()
|
|
35
|
+
rewriter := newDeclarationRewriter(program, options)
|
|
36
|
+
type output struct {
|
|
37
|
+
path, text string
|
|
38
|
+
}
|
|
39
|
+
outputs := make([][]output, len(files))
|
|
40
|
+
index := make(map[*ast.SourceFile]int, len(files))
|
|
41
|
+
for i, file := range files {
|
|
42
|
+
index[file] = i
|
|
43
|
+
}
|
|
44
|
+
program.Emit(ctx, compiler.EmitOptions{
|
|
45
|
+
TargetSourceFiles: files,
|
|
46
|
+
EmitOnly: compiler.EmitOnlyDts,
|
|
47
|
+
WriteFile: func(fileName tspath.RootedFilePath, text string, data *compiler.WriteFileData) error {
|
|
48
|
+
name := fileName.AsString()
|
|
49
|
+
if data != nil && data.SourceFile != nil {
|
|
50
|
+
if i, ok := index[data.SourceFile]; ok {
|
|
51
|
+
if options.RootDir == "" && strings.HasSuffix(name, ".d.ts") {
|
|
52
|
+
// See 0004: TS 5 and TS 7 pick a different default rootDir, and the translator has TS 5's.
|
|
53
|
+
name = c.Translator.GetOutputDeclarationPath(nodepath.Native.Normalize(data.SourceFile.FileName().AsString()))
|
|
54
|
+
}
|
|
55
|
+
if strings.HasSuffix(name, ".d.ts") {
|
|
56
|
+
text = rewriter.rewrite(text, data.SourceFile)
|
|
57
|
+
}
|
|
58
|
+
outputs[i] = append(outputs[i], output{name, text})
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return nil
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
var written []string
|
|
65
|
+
for _, list := range outputs {
|
|
66
|
+
for _, item := range list {
|
|
67
|
+
target := nodepath.Native.Normalize(item.path)
|
|
68
|
+
if c.Data.ProjectOptions.WriteOnlyChanged {
|
|
69
|
+
if current, err := os.ReadFile(target); err == nil && string(current) == item.text {
|
|
70
|
+
continue
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if err := os.MkdirAll(nodepath.Native.Dirname(target), 0o777); err != nil {
|
|
74
|
+
return written, err
|
|
75
|
+
}
|
|
76
|
+
if err := os.WriteFile(target, []byte(item.text), 0o666); err != nil {
|
|
77
|
+
return written, err
|
|
78
|
+
}
|
|
79
|
+
written = append(written, target)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return written, nil
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
type declarationRewriter struct {
|
|
86
|
+
program *compiler.Program
|
|
87
|
+
options *core.CompilerOptions
|
|
88
|
+
resolver *module.DefaultResolver
|
|
89
|
+
active bool
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
func newDeclarationRewriter(program *compiler.Program, options *core.CompilerOptions) *declarationRewriter {
|
|
93
|
+
return &declarationRewriter{
|
|
94
|
+
program: program,
|
|
95
|
+
options: options,
|
|
96
|
+
resolver: module.NewResolver(module.ResolverOptions{
|
|
97
|
+
Host: &resolutionHost{fs: program.Host().FS(), currentDirectory: program.GetCurrentDirectory()},
|
|
98
|
+
CompilerOptions: options,
|
|
99
|
+
}),
|
|
100
|
+
// `if (!compilerOptions.baseUrl && !compilerOptions.paths) return sourceFile;`. Normalization turns baseUrl into paths.
|
|
101
|
+
active: options.Paths != nil,
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
type resolutionHost struct {
|
|
106
|
+
fs vfs.FS
|
|
107
|
+
currentDirectory tspath.RootedDirectoryPath
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
func (h *resolutionHost) FS() vfs.FS { return h.fs }
|
|
111
|
+
func (h *resolutionHost) GetCurrentDirectory() tspath.RootedDirectoryPath { return h.currentDirectory }
|
|
112
|
+
|
|
113
|
+
// typeReference finds the reference directives transformTypeReferenceDirectives changes. They only ever sit in the
|
|
114
|
+
// comments at the top of a file, which is where the printer puts them.
|
|
115
|
+
var typeReference = regexp.MustCompile(`(?m)^(/// <reference types=")types("[^\n]*)$`)
|
|
116
|
+
|
|
117
|
+
type textEdit struct {
|
|
118
|
+
start, end int
|
|
119
|
+
text string
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
func (r *declarationRewriter) rewrite(text string, source *ast.SourceFile) string {
|
|
123
|
+
var edits []textEdit
|
|
124
|
+
if r.active {
|
|
125
|
+
parsed := parser.ParseSourceFile(ast.SourceFileParseOptions{
|
|
126
|
+
FileName: source.FileName(),
|
|
127
|
+
PathKey: source.PathKey() + ".d.ts",
|
|
128
|
+
}, text, core.ScriptKindTS)
|
|
129
|
+
var visit func(node *ast.Node) bool
|
|
130
|
+
replace := func(literal *ast.Node) {
|
|
131
|
+
if literal == nil || !ast.IsStringLiteralLike(literal) {
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
if updated, ok := r.update(literal.Text(), source); ok {
|
|
135
|
+
edits = append(edits, textEdit{scanner.GetTokenPosOfNode(literal, parsed, false), literal.End(), `"` + updated + `"`})
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
visit = func(node *ast.Node) bool {
|
|
139
|
+
switch {
|
|
140
|
+
case ast.IsExternalModuleReference(node):
|
|
141
|
+
replace(node.Expression())
|
|
142
|
+
return false
|
|
143
|
+
case ast.IsImportDeclaration(node):
|
|
144
|
+
replace(node.ModuleSpecifier())
|
|
145
|
+
return false
|
|
146
|
+
case ast.IsExportDeclaration(node):
|
|
147
|
+
if node.ModuleSpecifier() != nil {
|
|
148
|
+
replace(node.ModuleSpecifier())
|
|
149
|
+
return false
|
|
150
|
+
}
|
|
151
|
+
case ast.IsImportTypeNode(node):
|
|
152
|
+
if argument := node.AsImportTypeNode().Argument; argument != nil && ast.IsLiteralTypeNode(argument) {
|
|
153
|
+
// `return !text ? node : update(...)`. An empty specifier stays.
|
|
154
|
+
literal := argument.AsLiteralTypeNode().Literal
|
|
155
|
+
if ast.IsStringLiteralLike(literal) && literal.Text() != "" {
|
|
156
|
+
replace(literal)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
node.ForEachChild(visit)
|
|
161
|
+
return false
|
|
162
|
+
}
|
|
163
|
+
parsed.AsNode().ForEachChild(visit)
|
|
164
|
+
}
|
|
165
|
+
if len(edits) > 0 {
|
|
166
|
+
sort.Slice(edits, func(i, j int) bool { return edits[i].start < edits[j].start })
|
|
167
|
+
var b strings.Builder
|
|
168
|
+
position := 0
|
|
169
|
+
for _, edit := range edits {
|
|
170
|
+
b.WriteString(text[position:edit.start])
|
|
171
|
+
b.WriteString(edit.text)
|
|
172
|
+
position = edit.end
|
|
173
|
+
}
|
|
174
|
+
b.WriteString(text[position:])
|
|
175
|
+
text = b.String()
|
|
176
|
+
}
|
|
177
|
+
return typeReference.ReplaceAllString(text, "${1}@rbxts/types${2}")
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// update is update() in transformPaths.ts. Upstream also takes a failed lookup that is a URL, which no file system
|
|
181
|
+
// lookup here produces.
|
|
182
|
+
func (r *declarationRewriter) update(moduleName string, source *ast.SourceFile) (string, bool) {
|
|
183
|
+
resolved, _, _ := r.resolver.ResolveModuleName(moduleName, source.FileName(), core.ResolutionModeNone, nil)
|
|
184
|
+
if resolved == nil || !resolved.IsResolved() || resolved.IsExternalLibraryImport {
|
|
185
|
+
return "", false
|
|
186
|
+
}
|
|
187
|
+
path := nodepath.Native
|
|
188
|
+
fileDir := tspath.NormalizePath(path.Dirname(source.FileName().AsString()))
|
|
189
|
+
resolvedFileName := resolved.ResolvedFileName.AsString()
|
|
190
|
+
modulePath := path.Dirname(resolvedFileName)
|
|
191
|
+
p := tspath.NormalizeSlashes(tspath.NormalizePath(path.Join(path.Relative(fileDir, modulePath), path.Basename(resolvedFileName, ""))))
|
|
192
|
+
if resolved.Extension != "" && slices.Contains(r.implicitExtensions(), resolved.Extension) {
|
|
193
|
+
p = p[:len(p)-len(resolved.Extension)]
|
|
194
|
+
}
|
|
195
|
+
if p == "" {
|
|
196
|
+
return "", false
|
|
197
|
+
}
|
|
198
|
+
if p[0] != '.' {
|
|
199
|
+
p = "./" + p
|
|
200
|
+
}
|
|
201
|
+
return p, true
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// implicitExtensions is getImplicitExtensions.
|
|
205
|
+
func (r *declarationRewriter) implicitExtensions() []string {
|
|
206
|
+
result := []string{".ts", ".d.ts"}
|
|
207
|
+
if r.options.AllowJs.IsTrue() {
|
|
208
|
+
result = append(result, ".js")
|
|
209
|
+
}
|
|
210
|
+
allowJsx := r.options.Jsx != core.JsxEmitNone
|
|
211
|
+
if allowJsx {
|
|
212
|
+
result = append(result, ".tsx")
|
|
213
|
+
}
|
|
214
|
+
if r.options.AllowJs.IsTrue() && allowJsx {
|
|
215
|
+
result = append(result, ".jsx")
|
|
216
|
+
}
|
|
217
|
+
if r.options.ResolveJsonModule.IsTrue() {
|
|
218
|
+
result = append(result, ".json")
|
|
219
|
+
}
|
|
220
|
+
return result
|
|
221
|
+
}
|