@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
package/cli/cli.go
ADDED
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
// Package cli is the port of src/CLI from roblox-ts 3.0.0: cli.ts and commands/build.ts. It builds a roblox-ts
|
|
2
|
+
// project to Luau.
|
|
3
|
+
//
|
|
4
|
+
// It is a package, and not the main package itself, so that a compiler with native plugins can be built: a main
|
|
5
|
+
// package that imports the plugins, which register themselves, and calls Main. See docs/plugin-api.md.
|
|
6
|
+
package cli
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
"context"
|
|
10
|
+
"errors"
|
|
11
|
+
"flag"
|
|
12
|
+
"fmt"
|
|
13
|
+
"os"
|
|
14
|
+
"os/signal"
|
|
15
|
+
"path/filepath"
|
|
16
|
+
"runtime/debug"
|
|
17
|
+
"runtime/pprof"
|
|
18
|
+
"slices"
|
|
19
|
+
"strings"
|
|
20
|
+
"syscall"
|
|
21
|
+
"time"
|
|
22
|
+
|
|
23
|
+
"github.com/microsoft/TypeScript/tsc/internal/tsoptions"
|
|
24
|
+
"github.com/microsoft/TypeScript/tsc/internal/tspath"
|
|
25
|
+
"github.com/microsoft/TypeScript/tsc/internal/vfs/osvfs"
|
|
26
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/compile"
|
|
27
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/plugins"
|
|
28
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project"
|
|
29
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
|
|
30
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
31
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/tsbridge"
|
|
32
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/watch"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
var path = nodepath.Native
|
|
36
|
+
|
|
37
|
+
// tristate tells a flag that was given from one that wasn't, because the tsconfig's "rbxts" section and
|
|
38
|
+
// DEFAULT_PROJECT_OPTIONS come in underneath the command line.
|
|
39
|
+
type tristate struct{ value, set bool }
|
|
40
|
+
|
|
41
|
+
func (t *tristate) String() string { return fmt.Sprint(t.value) }
|
|
42
|
+
func (t *tristate) IsBoolFlag() bool { return true }
|
|
43
|
+
func (t *tristate) Set(s string) error {
|
|
44
|
+
t.set = true
|
|
45
|
+
t.value = s == "true" || s == "1" || s == ""
|
|
46
|
+
return nil
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Main runs the command line of the process and exits with its exit code.
|
|
50
|
+
func Main() {
|
|
51
|
+
os.Exit(Run(os.Args[1:]))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Run runs a command line, without the program name, and returns the exit code. It keeps the command line's settings
|
|
55
|
+
// in package variables, so one process can't run two at the same time.
|
|
56
|
+
func Run(args []string) int {
|
|
57
|
+
exitCode = 0
|
|
58
|
+
pluginOptions = plugins.Options{}
|
|
59
|
+
commandLine = args
|
|
60
|
+
flags := flag.NewFlagSet("rbxtsgo", flag.ContinueOnError)
|
|
61
|
+
projectPath := flags.String("project", ".", "project path")
|
|
62
|
+
flags.StringVar(projectPath, "p", ".", "project path")
|
|
63
|
+
var watch, usePolling, verbose, noInclude, logTruthyChanges, writeOnlyChanged, writeTransformedFiles, optimizedLoops, allowCommentDirectives, luauFlag tristate
|
|
64
|
+
flags.Var(&watch, "watch", "enable watch mode")
|
|
65
|
+
flags.Var(&watch, "w", "enable watch mode")
|
|
66
|
+
flags.Var(&usePolling, "usePolling", "use polling for watch mode")
|
|
67
|
+
flags.Var(&verbose, "verbose", "enable verbose logs")
|
|
68
|
+
flags.Var(&noInclude, "noInclude", "do not copy include files")
|
|
69
|
+
flags.Var(&logTruthyChanges, "logTruthyChanges", "logs changes to truthiness evaluation from Lua truthiness rules")
|
|
70
|
+
flags.Var(&writeOnlyChanged, "writeOnlyChanged", "")
|
|
71
|
+
flags.Var(&writeTransformedFiles, "writeTransformedFiles", "writes resulting TypeScript ASTs after transformers to out directory")
|
|
72
|
+
flags.Var(&optimizedLoops, "optimizedLoops", "")
|
|
73
|
+
flags.Var(&allowCommentDirectives, "allowCommentDirectives", "")
|
|
74
|
+
flags.Var(&luauFlag, "luau", "emit files with .luau extension")
|
|
75
|
+
projectType := flags.String("type", "", "override project type: game, model or package")
|
|
76
|
+
includePath := flags.String("includePath", "", "folder to copy runtime files to")
|
|
77
|
+
flags.StringVar(includePath, "i", "", "folder to copy runtime files to")
|
|
78
|
+
rojo := flags.String("rojo", "", "manually select Rojo project file")
|
|
79
|
+
checkers := flags.Int("checkers", 0, "number of type checkers and transform workers (default 4)")
|
|
80
|
+
cpuProfile := flags.String("cpuprofile", "", "write a CPU profile to this file")
|
|
81
|
+
memProfile := flags.String("memprofile", "", "write a heap profile to this file when the build ends")
|
|
82
|
+
var fixEmit tristate
|
|
83
|
+
flags.Var(&fixEmit, "fix-emit", "turn on fixes for output that is wrong in roblox-ts 3.0.0, which makes the output differ from it where it was wrong. Also tsconfig \"rbxtsgo\": { \"fixEmit\": true }")
|
|
84
|
+
var setget tristate
|
|
85
|
+
flags.Var(&setget, "setget", "turn getters and setters into methods, as rbxts-transformer-setget does")
|
|
86
|
+
setgetPrefix := flags.String("setgetPrefix", "", "prefix of the methods setget makes (default __)")
|
|
87
|
+
if err := flags.Parse(rewriteArgs(args)); err != nil {
|
|
88
|
+
if errors.Is(err, flag.ErrHelp) {
|
|
89
|
+
return 0
|
|
90
|
+
}
|
|
91
|
+
return 1
|
|
92
|
+
}
|
|
93
|
+
if setget.set {
|
|
94
|
+
pluginOptions.Setget = &setget.value
|
|
95
|
+
}
|
|
96
|
+
flags.Visit(func(f *flag.Flag) {
|
|
97
|
+
if f.Name == "setgetPrefix" {
|
|
98
|
+
pluginOptions.Prefix = setgetPrefix
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
if flags.NArg() > 0 && !(flags.NArg() == 1 && flags.Arg(0) == "build") {
|
|
102
|
+
fmt.Fprintln(os.Stderr, "unknown arguments:", strings.Join(flags.Args(), " "))
|
|
103
|
+
return 1
|
|
104
|
+
}
|
|
105
|
+
if *cpuProfile != "" {
|
|
106
|
+
file, err := os.Create(*cpuProfile)
|
|
107
|
+
if err == nil {
|
|
108
|
+
defer file.Close()
|
|
109
|
+
if pprof.StartCPUProfile(file) == nil {
|
|
110
|
+
defer pprof.StopCPUProfile()
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if *memProfile != "" {
|
|
116
|
+
defer func() {
|
|
117
|
+
if file, err := os.Create(*memProfile); err == nil {
|
|
118
|
+
defer file.Close()
|
|
119
|
+
_ = pprof.WriteHeapProfile(file)
|
|
120
|
+
}
|
|
121
|
+
}()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
err := build(*projectPath, func(options *shared.ProjectOptions) {
|
|
125
|
+
setBool := func(flag tristate, target *bool) {
|
|
126
|
+
if flag.set {
|
|
127
|
+
*target = flag.value
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
setBool(watch, &options.Watch)
|
|
131
|
+
setBool(usePolling, &options.UsePolling)
|
|
132
|
+
setBool(verbose, &options.Verbose)
|
|
133
|
+
setBool(noInclude, &options.NoInclude)
|
|
134
|
+
setBool(logTruthyChanges, &options.LogTruthyChanges)
|
|
135
|
+
setBool(writeOnlyChanged, &options.WriteOnlyChanged)
|
|
136
|
+
setBool(writeTransformedFiles, &options.WriteTransformedFiles)
|
|
137
|
+
setBool(optimizedLoops, &options.OptimizedLoops)
|
|
138
|
+
setBool(allowCommentDirectives, &options.AllowCommentDirectives)
|
|
139
|
+
setBool(luauFlag, &options.Luau)
|
|
140
|
+
setBool(fixEmit, &options.FixEmit)
|
|
141
|
+
if *projectType != "" {
|
|
142
|
+
options.Type = shared.ProjectType(*projectType)
|
|
143
|
+
}
|
|
144
|
+
if *includePath != "" {
|
|
145
|
+
options.IncludePath = *includePath
|
|
146
|
+
}
|
|
147
|
+
if *rojo != "" {
|
|
148
|
+
options.Rojo = *rojo
|
|
149
|
+
}
|
|
150
|
+
}, *checkers)
|
|
151
|
+
if err != nil {
|
|
152
|
+
var diagnosticErr *shared.DiagnosticError
|
|
153
|
+
if errors.As(err, &diagnosticErr) {
|
|
154
|
+
printDiagnostics(diagnosticErr.Diagnostics)
|
|
155
|
+
} else {
|
|
156
|
+
fmt.Fprintln(os.Stderr, err)
|
|
157
|
+
}
|
|
158
|
+
return 1
|
|
159
|
+
}
|
|
160
|
+
if exitCode != 0 {
|
|
161
|
+
return exitCode
|
|
162
|
+
}
|
|
163
|
+
return 0
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
var exitCode int
|
|
167
|
+
|
|
168
|
+
// commandLine is what the JS compiler gets when a plugin hands it the build.
|
|
169
|
+
var commandLine []string
|
|
170
|
+
|
|
171
|
+
// pluginOptions is the setget setting from the command line.
|
|
172
|
+
var pluginOptions plugins.Options
|
|
173
|
+
|
|
174
|
+
// rewriteArgs makes yargs' spellings work with the flag package: --no-x means --x=false.
|
|
175
|
+
func rewriteArgs(args []string) []string {
|
|
176
|
+
result := make([]string, 0, len(args))
|
|
177
|
+
for _, arg := range args {
|
|
178
|
+
if rest, ok := strings.CutPrefix(arg, "--no-"); ok {
|
|
179
|
+
result = append(result, "--"+rest+"=false")
|
|
180
|
+
continue
|
|
181
|
+
}
|
|
182
|
+
result = append(result, arg)
|
|
183
|
+
}
|
|
184
|
+
return result
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// findTsConfigPath is findTsConfigPath.
|
|
188
|
+
func findTsConfigPath(projectPath string) (string, error) {
|
|
189
|
+
tsConfigPath := path.Resolve(projectPath)
|
|
190
|
+
if info, err := os.Stat(tsConfigPath); err != nil || info.IsDir() {
|
|
191
|
+
// ts.findConfigFile walks up the ancestors.
|
|
192
|
+
found := ""
|
|
193
|
+
for directory := tsConfigPath; ; {
|
|
194
|
+
candidate := path.Join(directory, "tsconfig.json")
|
|
195
|
+
if _, err := os.Stat(candidate); err == nil {
|
|
196
|
+
found = candidate
|
|
197
|
+
break
|
|
198
|
+
}
|
|
199
|
+
parent := path.Dirname(directory)
|
|
200
|
+
if parent == directory {
|
|
201
|
+
break
|
|
202
|
+
}
|
|
203
|
+
directory = parent
|
|
204
|
+
}
|
|
205
|
+
if found == "" {
|
|
206
|
+
return "", errors.New("Unable to find tsconfig.json!")
|
|
207
|
+
}
|
|
208
|
+
tsConfigPath = found
|
|
209
|
+
}
|
|
210
|
+
return path.Resolve(tsConfigPath), nil
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// applyTsConfigOptions is getTsConfigProjectOptions: the "rbxts" section of tsconfig.json.
|
|
214
|
+
func applyTsConfigOptions(tsConfigPath string, options *shared.ProjectOptions) {
|
|
215
|
+
// The "rbxtsgo" block is ours, and is read with its other settings. build and Load report a bad value.
|
|
216
|
+
options.FixEmit, _ = plugins.FixEmitSetting(tsConfigPath)
|
|
217
|
+
text, err := os.ReadFile(tsConfigPath)
|
|
218
|
+
if err != nil {
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
fileName := tspath.RootedFilePathFromAbsolute(filepath.ToSlash(tsConfigPath))
|
|
222
|
+
raw, _ := tsoptions.ParseConfigFileTextToJson(fileName, osvfs.FS().CaseSensitivity().PathKey(fileName.AsPath()), string(text))
|
|
223
|
+
section, ok := raw.(interface{ Get(string) (any, bool) })
|
|
224
|
+
if !ok {
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
rbxts, ok := section.Get("rbxts")
|
|
228
|
+
if !ok {
|
|
229
|
+
return
|
|
230
|
+
}
|
|
231
|
+
values, ok := rbxts.(interface{ Get(string) (any, bool) })
|
|
232
|
+
if !ok {
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
setBool := func(name string, target *bool) {
|
|
236
|
+
if value, ok := values.Get(name); ok {
|
|
237
|
+
if b, ok := value.(bool); ok {
|
|
238
|
+
*target = b
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
setString := func(name string, target *string) {
|
|
243
|
+
if value, ok := values.Get(name); ok {
|
|
244
|
+
if s, ok := value.(string); ok {
|
|
245
|
+
*target = s
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
setBool("logTruthyChanges", &options.LogTruthyChanges)
|
|
250
|
+
setBool("noInclude", &options.NoInclude)
|
|
251
|
+
setBool("usePolling", &options.UsePolling)
|
|
252
|
+
setBool("verbose", &options.Verbose)
|
|
253
|
+
setBool("watch", &options.Watch)
|
|
254
|
+
setBool("writeOnlyChanged", &options.WriteOnlyChanged)
|
|
255
|
+
setBool("writeTransformedFiles", &options.WriteTransformedFiles)
|
|
256
|
+
setBool("optimizedLoops", &options.OptimizedLoops)
|
|
257
|
+
setBool("allowCommentDirectives", &options.AllowCommentDirectives)
|
|
258
|
+
setBool("luau", &options.Luau)
|
|
259
|
+
setString("includePath", &options.IncludePath)
|
|
260
|
+
setString("rojo", &options.Rojo)
|
|
261
|
+
var projectType string
|
|
262
|
+
setString("type", &projectType)
|
|
263
|
+
if projectType != "" {
|
|
264
|
+
options.Type = shared.ProjectType(projectType)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// build is the handler of the build command.
|
|
269
|
+
func build(projectPath string, applyFlags func(*shared.ProjectOptions), checkers int) error {
|
|
270
|
+
tsConfigPath, err := findTsConfigPath(projectPath)
|
|
271
|
+
if err != nil {
|
|
272
|
+
return err
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
options := shared.DefaultProjectOptions()
|
|
276
|
+
applyTsConfigOptions(tsConfigPath, &options)
|
|
277
|
+
applyFlags(&options)
|
|
278
|
+
shared.LogService.Verbose = options.Verbose
|
|
279
|
+
tuneGarbageCollector(options.Watch)
|
|
280
|
+
decision := plugins.Decide(tsConfigPath, pluginOptions)
|
|
281
|
+
if decision.Action == plugins.ActionError {
|
|
282
|
+
return errors.New(decision.Reason)
|
|
283
|
+
}
|
|
284
|
+
if _, err := plugins.FixEmitSetting(tsConfigPath); err != nil {
|
|
285
|
+
return err
|
|
286
|
+
}
|
|
287
|
+
if decision.Action == plugins.ActionFallback {
|
|
288
|
+
// JS 3.0.0 doesn't have these fixes, so handing it the build would silently drop what was asked for.
|
|
289
|
+
if options.FixEmit {
|
|
290
|
+
return errors.New(decision.Reason + ", and the JS compiler doesn't have --fix-emit.\nTo build this project, turn fixEmit off, or remove the plugin from compilerOptions.plugins.")
|
|
291
|
+
}
|
|
292
|
+
code, err := plugins.Delegate(decision.Reason, tsConfigPath, commandLine, os.Stderr)
|
|
293
|
+
exitCode = code
|
|
294
|
+
return err
|
|
295
|
+
}
|
|
296
|
+
if options.Watch {
|
|
297
|
+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
298
|
+
defer stop()
|
|
299
|
+
return watch.Run(ctx, watch.Config{
|
|
300
|
+
Load: func() (*shared.ProjectData, error) {
|
|
301
|
+
// The "rbxts" section can change between loads, so this starts from the defaults again.
|
|
302
|
+
options := shared.DefaultProjectOptions()
|
|
303
|
+
applyTsConfigOptions(tsConfigPath, &options)
|
|
304
|
+
applyFlags(&options)
|
|
305
|
+
if _, err := plugins.FixEmitSetting(tsConfigPath); err != nil {
|
|
306
|
+
return nil, err
|
|
307
|
+
}
|
|
308
|
+
return project.CreateProjectData(tsConfigPath, options)
|
|
309
|
+
},
|
|
310
|
+
Checkers: checkers,
|
|
311
|
+
UsePolling: options.UsePolling,
|
|
312
|
+
FullRebuilds: os.Getenv("RBXTSGO_WATCH_FULL") == "1",
|
|
313
|
+
PluginOptions: pluginOptions,
|
|
314
|
+
})
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
data, err := project.CreateProjectData(tsConfigPath, options)
|
|
318
|
+
if err != nil {
|
|
319
|
+
return err
|
|
320
|
+
}
|
|
321
|
+
parsed, err := project.GetParsedCommandLine(data)
|
|
322
|
+
if err != nil {
|
|
323
|
+
return err
|
|
324
|
+
}
|
|
325
|
+
var times compile.Times
|
|
326
|
+
started := time.Now()
|
|
327
|
+
phase := started
|
|
328
|
+
lap := func(target *time.Duration) {
|
|
329
|
+
*target = time.Since(phase)
|
|
330
|
+
phase = time.Now()
|
|
331
|
+
}
|
|
332
|
+
p := tsbridge.NewProject(parsed, tsbridge.ProjectOptions{Checkers: checkers})
|
|
333
|
+
translator := project.CreatePathTranslator(p, data)
|
|
334
|
+
lap(×.Program)
|
|
335
|
+
// Start type-checking now, in the background, while this goroutine cleans the output folder and copies files.
|
|
336
|
+
// Only when the build is sure to check this program in full: a plugin makes another program, and an incremental
|
|
337
|
+
// build checks the files that changed, or none.
|
|
338
|
+
// Native plugins need no more than that: their pass uses a checker of this program, and nothing else ever does.
|
|
339
|
+
if decision.Action == plugins.ActionNone && translator.BuildInfoOutputPath == "" {
|
|
340
|
+
p.Precheck(context.Background(), p.SourceFilesToCompile())
|
|
341
|
+
}
|
|
342
|
+
defer func() { shared.LogService.WriteLineIfVerbose(formatBuildTimes(×, time.Since(started))) }()
|
|
343
|
+
if err := project.Cleanup(translator); err != nil {
|
|
344
|
+
return err
|
|
345
|
+
}
|
|
346
|
+
if err := project.CopyInclude(data); err != nil {
|
|
347
|
+
return err
|
|
348
|
+
}
|
|
349
|
+
// upstream makes a Set of these, which keeps first-seen order
|
|
350
|
+
var rootDirs []string
|
|
351
|
+
for _, rootDir := range project.GetRootDirs(p.Options()) {
|
|
352
|
+
if !slices.Contains(rootDirs, rootDir) {
|
|
353
|
+
rootDirs = append(rootDirs, rootDir)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if err := project.CopyFiles(data, translator, rootDirs); err != nil {
|
|
357
|
+
return err
|
|
358
|
+
}
|
|
359
|
+
lap(×.Copy)
|
|
360
|
+
// emitBuildInfo and createProgramFactory's hash. With nothing changed, the rest of compileFiles has no work to do.
|
|
361
|
+
var inc *compile.Incremental
|
|
362
|
+
if translator.BuildInfoOutputPath != "" {
|
|
363
|
+
inc = compile.LoadIncremental(data, translator, compile.BuildKey(data, p, decision.Key()))
|
|
364
|
+
}
|
|
365
|
+
unchanged := inc.Unchanged(p)
|
|
366
|
+
// A native plugin may read more than the program, and may write files of its own in Finish. So with one, the
|
|
367
|
+
// plugins run on every build, and only the compile after them is incremental: it goes by the printed text.
|
|
368
|
+
if decision.Action == plugins.ActionPlugins {
|
|
369
|
+
unchanged = false
|
|
370
|
+
}
|
|
371
|
+
lap(×.State)
|
|
372
|
+
if unchanged {
|
|
373
|
+
fmt.Printf("No changes, finished in %s%s", shared.FormatElapsed(time.Since(started)), shared.EOL)
|
|
374
|
+
return nil
|
|
375
|
+
}
|
|
376
|
+
prepared, failure, err := project.PrepareCompile(p, data, translator, p.Program().CaseSensitivity())
|
|
377
|
+
if err != nil {
|
|
378
|
+
return err
|
|
379
|
+
}
|
|
380
|
+
if failure != nil {
|
|
381
|
+
printDiagnostics(failure)
|
|
382
|
+
if shared.HasErrors(failure) {
|
|
383
|
+
exitCode = 1
|
|
384
|
+
}
|
|
385
|
+
return nil
|
|
386
|
+
}
|
|
387
|
+
lap(×.Prepare)
|
|
388
|
+
applied, err := plugins.Apply(context.Background(), p, decision, p.SourceFilesToCompile(), plugins.NewBuild(data, translator, prepared.RojoResolver))
|
|
389
|
+
lap(×.Plugin)
|
|
390
|
+
if err != nil {
|
|
391
|
+
return err
|
|
392
|
+
}
|
|
393
|
+
if applied.Exit != nil {
|
|
394
|
+
fmt.Println(strings.Join(applied.Exit, "\n"))
|
|
395
|
+
exitCode = 1
|
|
396
|
+
return nil
|
|
397
|
+
}
|
|
398
|
+
if err := applied.WriteTransformedFiles(data, translator); err != nil {
|
|
399
|
+
return err
|
|
400
|
+
}
|
|
401
|
+
result, err := compile.CompileIncremental(context.Background(), &compile.Context{
|
|
402
|
+
Project: applied.Project,
|
|
403
|
+
Data: data,
|
|
404
|
+
Translator: translator,
|
|
405
|
+
Prepared: prepared,
|
|
406
|
+
Replay: shared.NewContextReplay(),
|
|
407
|
+
Times: ×,
|
|
408
|
+
}, inc, p, applied.Files)
|
|
409
|
+
if err != nil {
|
|
410
|
+
return err
|
|
411
|
+
}
|
|
412
|
+
diagnostics := applied.MapDiagnostics(result.Diagnostics)
|
|
413
|
+
printDiagnostics(diagnostics)
|
|
414
|
+
if shared.HasErrors(diagnostics) {
|
|
415
|
+
exitCode = 1
|
|
416
|
+
return nil
|
|
417
|
+
}
|
|
418
|
+
fmt.Printf("Built in %s%s", shared.FormatElapsed(time.Since(started)), shared.EOL)
|
|
419
|
+
return nil
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// formatBuildTimes is the build's phase timings, for --verbose.
|
|
423
|
+
func formatBuildTimes(t *compile.Times, total time.Duration) string {
|
|
424
|
+
ms := func(d time.Duration) string { return fmt.Sprintf("%.1fms", float64(d.Microseconds())/1000) }
|
|
425
|
+
return fmt.Sprintf("[timing] build total %s: program %s, copy %s, state %s, prepare %s, plugin %s, check %s, lower %s, write %s, declarations %s; checked %d, lowered %d",
|
|
426
|
+
ms(total), ms(t.Program), ms(t.Copy), ms(t.State), ms(t.Prepare), ms(t.Plugin), ms(t.Check), ms(t.Lower), ms(t.Write), ms(t.Declarations), t.FilesChecked, t.FilesLowered)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// printDiagnostics is ts.createDiagnosticReporter(ts.sys, true): pretty output on stdout.
|
|
430
|
+
func printDiagnostics(list []*shared.Diagnostic) {
|
|
431
|
+
if len(list) == 0 {
|
|
432
|
+
return
|
|
433
|
+
}
|
|
434
|
+
cwd := shared.ProcessCwd()
|
|
435
|
+
caseSensitivity := osvfs.FS().CaseSensitivity()
|
|
436
|
+
for _, d := range list {
|
|
437
|
+
fmt.Print(shared.FormatDiagnostics([]*shared.Diagnostic{d}, cwd, caseSensitivity, "\n"))
|
|
438
|
+
fmt.Print("\n")
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// buildGCPercent is GOGC for a one-shot build. Nearly everything a build allocates stays alive until the process
|
|
443
|
+
// exits (syntax trees, and the types of four checkers), so a collection finds little to free and mostly costs time.
|
|
444
|
+
// Collecting a little less often was measured to shorten a build of a large project by about 5% for about 1% more
|
|
445
|
+
// memory. Watch mode keeps Go's default, because it lives long and its heap should stay tight. Setting GOGC wins over
|
|
446
|
+
// this.
|
|
447
|
+
const buildGCPercent = 150
|
|
448
|
+
|
|
449
|
+
func tuneGarbageCollector(watch bool) {
|
|
450
|
+
if watch || os.Getenv("GOGC") != "" {
|
|
451
|
+
return
|
|
452
|
+
}
|
|
453
|
+
debug.SetGCPercent(buildGCPercent)
|
|
454
|
+
}
|