@data-vegle/rbxts-go 0.0.0-stage → 1.0.1
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 +243 -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 +271 -0
- package/plugins/session.go +236 -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 +120 -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,214 @@
|
|
|
1
|
+
// Package pathtranslator is a port of @roblox-ts/path-translator 1.1.0, which maps between source and output
|
|
2
|
+
// paths. Quirks are kept on purpose, including what PathInfo does to a path with no directory part. Output
|
|
3
|
+
// must match the JS version byte for byte, so the tests compare it with the real npm package.
|
|
4
|
+
package pathtranslator
|
|
5
|
+
|
|
6
|
+
import (
|
|
7
|
+
"strings"
|
|
8
|
+
|
|
9
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
const (
|
|
13
|
+
tsExt = ".ts"
|
|
14
|
+
tsxExt = ".tsx"
|
|
15
|
+
dExt = ".d"
|
|
16
|
+
dtsExt = dExt + tsExt
|
|
17
|
+
transformedExt = ".transformed"
|
|
18
|
+
luaExt = ".lua"
|
|
19
|
+
luauExt = ".luau"
|
|
20
|
+
indexName = "index"
|
|
21
|
+
initName = "init"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
type pathInfo struct {
|
|
25
|
+
path *nodepath.Path
|
|
26
|
+
dirName string
|
|
27
|
+
fileName string
|
|
28
|
+
exts []string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
func newPathInfo(path *nodepath.Path, filePath string) *pathInfo {
|
|
32
|
+
dirName := path.Dirname(filePath)
|
|
33
|
+
// The slice start can run past the end. JS's slice gives "" there.
|
|
34
|
+
rest := ""
|
|
35
|
+
if start := len(dirName) + len(path.Sep); start < len(filePath) {
|
|
36
|
+
rest = filePath[start:]
|
|
37
|
+
}
|
|
38
|
+
parts := strings.Split(rest, ".")
|
|
39
|
+
exts := make([]string, 0, len(parts)-1)
|
|
40
|
+
for _, part := range parts[1:] {
|
|
41
|
+
exts = append(exts, "."+part)
|
|
42
|
+
}
|
|
43
|
+
return &pathInfo{path: path, dirName: dirName, fileName: parts[0], exts: exts}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// peek returns "" where JS returns undefined. No extension is ever "", since each starts with a dot.
|
|
47
|
+
func (p *pathInfo) peek(depth int) string {
|
|
48
|
+
i := len(p.exts) - (depth + 1)
|
|
49
|
+
if i < 0 {
|
|
50
|
+
return ""
|
|
51
|
+
}
|
|
52
|
+
return p.exts[i]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func (p *pathInfo) pop() string {
|
|
56
|
+
if len(p.exts) == 0 {
|
|
57
|
+
return ""
|
|
58
|
+
}
|
|
59
|
+
last := p.exts[len(p.exts)-1]
|
|
60
|
+
p.exts = p.exts[:len(p.exts)-1]
|
|
61
|
+
return last
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
func (p *pathInfo) push(ext string) { p.exts = append(p.exts, ext) }
|
|
65
|
+
|
|
66
|
+
func (p *pathInfo) join() string {
|
|
67
|
+
return p.path.Join(p.dirName, p.fileName+strings.Join(p.exts, ""))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Translator is PathTranslator.
|
|
71
|
+
type Translator struct {
|
|
72
|
+
RootDir string
|
|
73
|
+
OutDir string
|
|
74
|
+
// BuildInfoOutputPath is "" where JS has undefined.
|
|
75
|
+
BuildInfoOutputPath string
|
|
76
|
+
Declaration bool
|
|
77
|
+
UseLuauExtension bool
|
|
78
|
+
|
|
79
|
+
path *nodepath.Path
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// New makes a Translator using this platform's path rules.
|
|
83
|
+
func New(rootDir, outDir, buildInfoOutputPath string, declaration, useLuauExtension bool) *Translator {
|
|
84
|
+
return NewWithPath(nodepath.Native, rootDir, outDir, buildInfoOutputPath, declaration, useLuauExtension)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// NewWithPath makes a Translator with the given path rules. Tests use it to cover both platforms on one machine.
|
|
88
|
+
func NewWithPath(path *nodepath.Path, rootDir, outDir, buildInfoOutputPath string, declaration, useLuauExtension bool) *Translator {
|
|
89
|
+
return &Translator{
|
|
90
|
+
RootDir: rootDir,
|
|
91
|
+
OutDir: outDir,
|
|
92
|
+
BuildInfoOutputPath: buildInfoOutputPath,
|
|
93
|
+
Declaration: declaration,
|
|
94
|
+
UseLuauExtension: useLuauExtension,
|
|
95
|
+
path: path,
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
func (t *Translator) luauExtension() string {
|
|
100
|
+
if t.UseLuauExtension {
|
|
101
|
+
return luauExt
|
|
102
|
+
}
|
|
103
|
+
return luaExt
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
func (t *Translator) toOut(info *pathInfo) string {
|
|
107
|
+
return t.path.Join(t.OutDir, t.path.Relative(t.RootDir, info.join()))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
func (t *Translator) toRoot(info *pathInfo) string {
|
|
111
|
+
return t.path.Join(t.RootDir, t.path.Relative(t.OutDir, info.join()))
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// GetOutputPath maps an input path to an output path. .ts(x) but not .d.ts(x) becomes .lua(u),
|
|
115
|
+
// index becomes init, and src/* becomes out/*.
|
|
116
|
+
func (t *Translator) GetOutputPath(filePath string) string {
|
|
117
|
+
info := newPathInfo(t.path, filePath)
|
|
118
|
+
if (info.peek(0) == tsExt || info.peek(0) == tsxExt) && info.peek(1) != dExt {
|
|
119
|
+
info.pop()
|
|
120
|
+
if info.fileName == indexName {
|
|
121
|
+
info.fileName = initName
|
|
122
|
+
}
|
|
123
|
+
info.push(t.luauExtension())
|
|
124
|
+
}
|
|
125
|
+
return t.toOut(info)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// GetOutputDeclarationPath maps an input path to an output .d.ts path.
|
|
129
|
+
func (t *Translator) GetOutputDeclarationPath(filePath string) string {
|
|
130
|
+
info := newPathInfo(t.path, filePath)
|
|
131
|
+
if (info.peek(0) == tsExt || info.peek(0) == tsxExt) && info.peek(1) != dExt {
|
|
132
|
+
info.pop()
|
|
133
|
+
info.push(dtsExt)
|
|
134
|
+
}
|
|
135
|
+
return t.toOut(info)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// insert is Array.prototype.splice(index, 0, value), which clamps a negative index to 0.
|
|
139
|
+
func (p *pathInfo) insert(index int, value string) {
|
|
140
|
+
if index < 0 {
|
|
141
|
+
index = 0
|
|
142
|
+
}
|
|
143
|
+
p.exts = append(p.exts[:index:index], append([]string{value}, p.exts[index:]...)...)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// GetOutputTransformedPath maps an input path to an output .transformed.ts(x) path.
|
|
147
|
+
func (t *Translator) GetOutputTransformedPath(filePath string) string {
|
|
148
|
+
info := newPathInfo(t.path, filePath)
|
|
149
|
+
if info.peek(1) == dExt {
|
|
150
|
+
info.insert(len(info.exts)-2, transformedExt)
|
|
151
|
+
} else {
|
|
152
|
+
info.insert(len(info.exts)-1, transformedExt)
|
|
153
|
+
}
|
|
154
|
+
return t.toOut(info)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// GetInputPaths maps an output path to the input paths it could have come from.
|
|
158
|
+
func (t *Translator) GetInputPaths(filePath string) []string {
|
|
159
|
+
var possible []string
|
|
160
|
+
info := newPathInfo(t.path, filePath)
|
|
161
|
+
tryWith := func(ext string) {
|
|
162
|
+
info.push(ext)
|
|
163
|
+
possible = append(possible, t.toRoot(info))
|
|
164
|
+
info.pop()
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// index.*.lua(u) cannot come from a .ts file
|
|
168
|
+
if info.peek(0) == t.luauExtension() && info.fileName != indexName {
|
|
169
|
+
info.pop()
|
|
170
|
+
tryWith(tsExt)
|
|
171
|
+
tryWith(tsxExt)
|
|
172
|
+
if info.fileName == initName {
|
|
173
|
+
original := info.fileName
|
|
174
|
+
info.fileName = indexName
|
|
175
|
+
tryWith(tsExt)
|
|
176
|
+
tryWith(tsxExt)
|
|
177
|
+
info.fileName = original
|
|
178
|
+
}
|
|
179
|
+
info.push(t.luauExtension())
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if t.Declaration {
|
|
183
|
+
if (info.peek(0) == tsExt || info.peek(0) == tsxExt) && info.peek(1) == dExt {
|
|
184
|
+
ext := info.pop()
|
|
185
|
+
info.pop()
|
|
186
|
+
tryWith(tsExt)
|
|
187
|
+
tryWith(tsxExt)
|
|
188
|
+
info.push(dExt)
|
|
189
|
+
info.push(ext)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return append(possible, t.toRoot(info))
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// GetImportPath maps a source path to an import path, which is passed to the Rojo resolver and used virtually,
|
|
197
|
+
// so it might not exist. Inside node_modules the compiled file is assumed to be a sibling of filePath.
|
|
198
|
+
func (t *Translator) GetImportPath(filePath string, isNodeModule bool) string {
|
|
199
|
+
info := newPathInfo(t.path, filePath)
|
|
200
|
+
if info.peek(0) == tsExt || info.peek(0) == tsxExt {
|
|
201
|
+
info.pop()
|
|
202
|
+
if info.peek(0) == dExt {
|
|
203
|
+
info.pop()
|
|
204
|
+
}
|
|
205
|
+
if info.fileName == indexName {
|
|
206
|
+
info.fileName = initName
|
|
207
|
+
}
|
|
208
|
+
info.push(t.luauExtension())
|
|
209
|
+
}
|
|
210
|
+
if isNodeModule {
|
|
211
|
+
return info.join()
|
|
212
|
+
}
|
|
213
|
+
return t.toOut(info)
|
|
214
|
+
}
|
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
// Package project is the port of src/Project from roblox-ts 3.0.0: options, tsconfig validation, file-name checks,
|
|
2
|
+
// and the file copying and cleanup around compilation. Compiling itself comes after the transformer port.
|
|
3
|
+
//
|
|
4
|
+
// Not ported, on purpose: createProgramFactory and getChangedFilePaths (no incremental builds yet, see 0003 item 6),
|
|
5
|
+
// setupProjectWatchProgram (watch mode is a later phase), createTransformerList and friends (plugins are a later
|
|
6
|
+
// phase), and VirtualProject (the playground).
|
|
7
|
+
package project
|
|
8
|
+
|
|
9
|
+
import (
|
|
10
|
+
"encoding/json"
|
|
11
|
+
"os"
|
|
12
|
+
"regexp"
|
|
13
|
+
"slices"
|
|
14
|
+
"strings"
|
|
15
|
+
|
|
16
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
17
|
+
"github.com/microsoft/TypeScript/tsc/internal/core"
|
|
18
|
+
"github.com/microsoft/TypeScript/tsc/internal/tspath"
|
|
19
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
|
|
20
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/pathtranslator"
|
|
21
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/rojoresolver"
|
|
22
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
23
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/tsbridge"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
var path = nodepath.Native
|
|
27
|
+
|
|
28
|
+
var packageRegex = regexp.MustCompile(`^@[a-z0-9-]*/`)
|
|
29
|
+
|
|
30
|
+
func exists(p string) bool {
|
|
31
|
+
_, err := os.Stat(p)
|
|
32
|
+
return err == nil
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// findPackageJSON is ts.findPackageJson: the nearest package.json in this directory or an ancestor.
|
|
36
|
+
func findPackageJSON(directory string) string {
|
|
37
|
+
for {
|
|
38
|
+
candidate := path.Join(directory, "package.json")
|
|
39
|
+
if exists(candidate) {
|
|
40
|
+
return candidate
|
|
41
|
+
}
|
|
42
|
+
parent := path.Dirname(directory)
|
|
43
|
+
if parent == directory {
|
|
44
|
+
return ""
|
|
45
|
+
}
|
|
46
|
+
directory = parent
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// CreateProjectData is createProjectData. Rojo's warnings go to the log, as upstream.
|
|
51
|
+
func CreateProjectData(tsConfigPath string, options shared.ProjectOptions) (*shared.ProjectData, error) {
|
|
52
|
+
projectPath := path.Dirname(tsConfigPath)
|
|
53
|
+
|
|
54
|
+
pkgJSONPath := findPackageJSON(projectPath)
|
|
55
|
+
if pkgJSONPath == "" {
|
|
56
|
+
return nil, shared.NewProjectError("Unable to find package.json")
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
isPackage := false
|
|
60
|
+
if data, err := os.ReadFile(pkgJSONPath); err == nil {
|
|
61
|
+
var pkgJSON struct {
|
|
62
|
+
Name any `json:"name"`
|
|
63
|
+
}
|
|
64
|
+
if json.Unmarshal(data, &pkgJSON) == nil {
|
|
65
|
+
if name, ok := pkgJSON.Name.(string); ok {
|
|
66
|
+
isPackage = packageRegex.MatchString(name)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// intentionally uses the empty string case too
|
|
72
|
+
if options.IncludePath == "" {
|
|
73
|
+
options.IncludePath = path.Resolve(path.Join(projectPath, "include"))
|
|
74
|
+
} else {
|
|
75
|
+
options.IncludePath = path.Resolve(options.IncludePath)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
nodeModulesPath := path.Join(path.Dirname(pkgJSONPath), shared.NodeModules)
|
|
79
|
+
|
|
80
|
+
rojoConfigPath := ""
|
|
81
|
+
if options.Rojo != "" {
|
|
82
|
+
rojoConfigPath = path.Resolve(options.Rojo)
|
|
83
|
+
} else {
|
|
84
|
+
found, warnings, err := rojoresolver.FindRojoConfigFilePath(projectPath)
|
|
85
|
+
if err != nil {
|
|
86
|
+
return nil, err
|
|
87
|
+
}
|
|
88
|
+
rojoConfigPath = found
|
|
89
|
+
for _, warning := range warnings {
|
|
90
|
+
shared.LogService.Warn(warning)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return &shared.ProjectData{
|
|
95
|
+
TsConfigPath: tsConfigPath,
|
|
96
|
+
IsPackage: isPackage,
|
|
97
|
+
NodeModulesPath: nodeModulesPath,
|
|
98
|
+
ProjectOptions: options,
|
|
99
|
+
ProjectPath: projectPath,
|
|
100
|
+
RojoConfigPath: rojoConfigPath,
|
|
101
|
+
}, nil
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
func validateTypeRoots(nodeModulesPath string, typeRoots []string) bool {
|
|
105
|
+
typesPath := path.Resolve(nodeModulesPath)
|
|
106
|
+
return slices.ContainsFunc(typeRoots, func(typeRoot string) bool {
|
|
107
|
+
return path.Resolve(typeRoot) == typesPath
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// removedOption reads an option from the raw tsconfig, for options TS 7 no longer parses.
|
|
112
|
+
func rawCompilerOption(parsed *tsbridge.ParsedConfig, name string) (any, bool) {
|
|
113
|
+
raw, ok := parsed.Config.Raw.(interface {
|
|
114
|
+
Get(string) (any, bool)
|
|
115
|
+
})
|
|
116
|
+
if !ok {
|
|
117
|
+
return nil, false
|
|
118
|
+
}
|
|
119
|
+
compilerOptions, ok := raw.Get("compilerOptions")
|
|
120
|
+
if !ok {
|
|
121
|
+
return nil, false
|
|
122
|
+
}
|
|
123
|
+
options, ok := compilerOptions.(interface {
|
|
124
|
+
Get(string) (any, bool)
|
|
125
|
+
})
|
|
126
|
+
if !ok {
|
|
127
|
+
return nil, false
|
|
128
|
+
}
|
|
129
|
+
return options.Get(name)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ValidateCompilerOptions is validateCompilerOptions. It must run on the options as written, before normalization,
|
|
133
|
+
// because it requires "moduleResolution": "Node". The check on "target" is commented out upstream too.
|
|
134
|
+
func ValidateCompilerOptions(parsed *tsbridge.ParsedConfig, projectPath string) error {
|
|
135
|
+
y := shared.Color.Yellow
|
|
136
|
+
opts := parsed.Config.CompilerOptions()
|
|
137
|
+
var errs []string
|
|
138
|
+
|
|
139
|
+
// required compiler options
|
|
140
|
+
if !opts.NoLib.IsTrue() {
|
|
141
|
+
errs = append(errs, y(`"noLib"`)+" must be "+y("true"))
|
|
142
|
+
}
|
|
143
|
+
if !opts.Strict.IsTrue() {
|
|
144
|
+
errs = append(errs, y(`"strict"`)+" must be "+y("true"))
|
|
145
|
+
}
|
|
146
|
+
// TS 7 removed "moduleResolution": "node", so a config written for it has this pair instead. See 0007.
|
|
147
|
+
ts7Style := opts.Module == core.ModuleKindPreserve && opts.ModuleResolution == core.ModuleResolutionKindBundler
|
|
148
|
+
if opts.Module != core.ModuleKindCommonJS && !ts7Style {
|
|
149
|
+
errs = append(errs, y(`"module"`)+" must be "+y("commonjs"))
|
|
150
|
+
}
|
|
151
|
+
if opts.ModuleDetection != core.ModuleDetectionKindForce {
|
|
152
|
+
errs = append(errs, y(`"moduleDetection"`)+" must be "+y(`"force"`))
|
|
153
|
+
}
|
|
154
|
+
if opts.ModuleResolution != core.ModuleResolutionKindNode10 && !ts7Style {
|
|
155
|
+
errs = append(errs, y(`"moduleResolution"`)+" must be "+y(`"Node"`))
|
|
156
|
+
}
|
|
157
|
+
if !opts.AllowSyntheticDefaultImports.IsTrue() {
|
|
158
|
+
errs = append(errs, y(`"allowSyntheticDefaultImports"`)+" must be "+y("true"))
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
rbxtsModules := path.Join(projectPath, shared.NodeModules, shared.RbxtsScope)
|
|
162
|
+
if opts.TypeRoots == nil || !validateTypeRoots(rbxtsModules, typeRootStrings(opts)) {
|
|
163
|
+
errs = append(errs, y(`"typeRoots"`)+" must contain "+y(rbxtsModules))
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
for _, typesLocation := range opts.Types {
|
|
167
|
+
// "*" is how TS 6 and later ask for every package of the type roots, which was the default before.
|
|
168
|
+
if typesLocation == "*" {
|
|
169
|
+
continue
|
|
170
|
+
}
|
|
171
|
+
// Technically checked to exist above. But if that's an error, we still want this error too,
|
|
172
|
+
// to avoid "fix one error, get a new one on the next compile".
|
|
173
|
+
typeRoots := typeRootStrings(opts)
|
|
174
|
+
if typeRoots == nil {
|
|
175
|
+
typeRoots = []string{"node_modules/@rbxts"}
|
|
176
|
+
}
|
|
177
|
+
found := slices.ContainsFunc(typeRoots, func(typeRoot string) bool {
|
|
178
|
+
typesPath := path.Resolve(projectPath, typeRoot, typesLocation)
|
|
179
|
+
return exists(typesPath) || exists(typesPath+shared.DTSExt)
|
|
180
|
+
})
|
|
181
|
+
if !found {
|
|
182
|
+
errs = append(errs, y(`"types"`)+" "+y(typesLocation)+" were not found. Make sure the path is relative to `typeRoots`")
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// configurable compiler options
|
|
187
|
+
if opts.RootDir == "" && opts.RootDirs == nil {
|
|
188
|
+
errs = append(errs, y(`"rootDir"`)+" or "+y(`"rootDirs"`)+" must be defined")
|
|
189
|
+
}
|
|
190
|
+
if opts.OutDir == "" {
|
|
191
|
+
errs = append(errs, y(`"outDir"`)+" must be defined")
|
|
192
|
+
}
|
|
193
|
+
if value, ok := rawCompilerOption(parsed, "importsNotUsedAsValues"); ok {
|
|
194
|
+
suggested := "false"
|
|
195
|
+
if text, isString := value.(string); isString && strings.EqualFold(text, "preserve") {
|
|
196
|
+
suggested = "true"
|
|
197
|
+
}
|
|
198
|
+
errs = append(errs, y(`"importsNotUsedAsValues"`)+" is no longer supported, use "+y(`"verbatimModuleSyntax": `+suggested)+" instead")
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if len(errs) > 0 {
|
|
202
|
+
var list strings.Builder
|
|
203
|
+
for _, err := range errs {
|
|
204
|
+
list.WriteString("- " + err + "\n")
|
|
205
|
+
}
|
|
206
|
+
return shared.NewProjectError(strings.Join([]string{
|
|
207
|
+
`Invalid "tsconfig.json" configuration!`,
|
|
208
|
+
"https://roblox-ts.com/docs/quick-start#project-folder-setup",
|
|
209
|
+
list.String(),
|
|
210
|
+
}, "\n"))
|
|
211
|
+
}
|
|
212
|
+
return nil
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// GetParsedCommandLine is getParsedCommandLine: parse, fail on parser errors, validate.
|
|
216
|
+
func GetParsedCommandLine(data *shared.ProjectData) (*tsbridge.ParsedConfig, error) {
|
|
217
|
+
parsed, err := tsbridge.ParseConfig(data.TsConfigPath)
|
|
218
|
+
if err != nil {
|
|
219
|
+
return nil, shared.NewProjectError("Unable to load TS program!")
|
|
220
|
+
}
|
|
221
|
+
// An option that TS 5.5.3 still parses but TS 7 doesn't must get the validation message, not a parser error.
|
|
222
|
+
if _, removed := rawCompilerOption(parsed, "importsNotUsedAsValues"); removed {
|
|
223
|
+
if err := ValidateCompilerOptions(parsed, data.ProjectPath); err != nil {
|
|
224
|
+
return nil, err
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if len(parsed.Diagnostics) > 0 {
|
|
228
|
+
return nil, &shared.DiagnosticError{Diagnostics: shared.FromTSList(parsed.Diagnostics)}
|
|
229
|
+
}
|
|
230
|
+
if err := ValidateCompilerOptions(parsed, data.ProjectPath); err != nil {
|
|
231
|
+
return nil, err
|
|
232
|
+
}
|
|
233
|
+
return parsed, nil
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// GetRootDirs is getRootDirs.
|
|
237
|
+
func GetRootDirs(options *core.CompilerOptions) []string {
|
|
238
|
+
if options.RootDir != "" {
|
|
239
|
+
return []string{options.RootDir.AsString()}
|
|
240
|
+
}
|
|
241
|
+
if options.RootDirs == nil {
|
|
242
|
+
panic(shared.AssertionMessage(""))
|
|
243
|
+
}
|
|
244
|
+
result := make([]string, len(options.RootDirs))
|
|
245
|
+
for i, dir := range options.RootDirs {
|
|
246
|
+
result[i] = string(dir)
|
|
247
|
+
}
|
|
248
|
+
return result
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// CreatePathTranslator is createPathTranslator.
|
|
252
|
+
func CreatePathTranslator(p *tsbridge.Project, data *shared.ProjectData) *pathtranslator.Translator {
|
|
253
|
+
options := p.Options()
|
|
254
|
+
rootDir := shared.FindAncestorDir(append([]string{p.CommonSourceDirectory().AsString()}, GetRootDirs(options)...))
|
|
255
|
+
buildInfoPath := p.Config().GetBuildInfoFileName().AsString()
|
|
256
|
+
if buildInfoPath != "" {
|
|
257
|
+
buildInfoPath = path.Normalize(buildInfoPath)
|
|
258
|
+
}
|
|
259
|
+
return pathtranslator.New(rootDir, options.OutDir.AsString(), buildInfoPath, options.Declaration.IsTrue(), data.ProjectOptions.Luau)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// CheckFileName is checkFileName.
|
|
263
|
+
func CheckFileName(service *shared.DiagnosticService, filePath string) {
|
|
264
|
+
baseName := path.Basename(filePath, "")
|
|
265
|
+
if warning, ok := shared.FilenameWarnings[baseName]; ok {
|
|
266
|
+
service.AddDiagnostic(shared.Errors.IncorrectFileName(baseName, warning, filePath))
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// CheckRojoConfig is checkRojoConfig.
|
|
271
|
+
func CheckRojoConfig(service *shared.DiagnosticService, data *shared.ProjectData, resolver *rojoresolver.Resolver, rootDirs []string, translator *pathtranslator.Translator) {
|
|
272
|
+
if data.RojoConfigPath == "" {
|
|
273
|
+
return
|
|
274
|
+
}
|
|
275
|
+
for _, partition := range resolver.Partitions() {
|
|
276
|
+
for _, rootDir := range rootDirs {
|
|
277
|
+
if shared.IsPathDescendantOf(partition.FsPath, rootDir) {
|
|
278
|
+
rojoConfigDir := path.Dirname(data.RojoConfigPath)
|
|
279
|
+
outPath := translator.GetOutputPath(partition.FsPath)
|
|
280
|
+
inputPath := path.Relative(rojoConfigDir, partition.FsPath)
|
|
281
|
+
suggestedPath := path.Relative(rojoConfigDir, outPath)
|
|
282
|
+
service.AddDiagnostic(shared.Errors.RojoPathInSrc(inputPath, suggestedPath))
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// FileUsesCommentDirectives is fileUsesCommentDirectives, the one custom pre-emit check.
|
|
289
|
+
func FileUsesCommentDirectives(data *shared.ProjectData, file *ast.SourceFile) []*shared.Diagnostic {
|
|
290
|
+
if data.ProjectOptions.AllowCommentDirectives {
|
|
291
|
+
return nil
|
|
292
|
+
}
|
|
293
|
+
var diagnostics []*shared.Diagnostic
|
|
294
|
+
for _, directive := range file.CommentDirectives {
|
|
295
|
+
diagnostics = append(diagnostics, shared.Errors.NoCommentDirectives.At(shared.AtRange(shared.SourceRange{File: file, Pos: directive.Loc.Pos(), End: directive.Loc.End()})))
|
|
296
|
+
}
|
|
297
|
+
for _, pragma := range file.Pragmas {
|
|
298
|
+
if pragma.Name == "ts-nocheck" {
|
|
299
|
+
diagnostics = append(diagnostics, shared.Errors.NoCommentDirectives.At(shared.AtRange(shared.SourceRange{File: file, Pos: pragma.Pos(), End: pragma.End()})))
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return diagnostics
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// IsCompilableFile is isCompilableFile.
|
|
306
|
+
func IsCompilableFile(fsPath string) (bool, error) {
|
|
307
|
+
info, err := os.Stat(fsPath)
|
|
308
|
+
if err != nil {
|
|
309
|
+
return false, err
|
|
310
|
+
}
|
|
311
|
+
if info.IsDir() || strings.HasSuffix(fsPath, shared.DTSExt) {
|
|
312
|
+
return false, nil
|
|
313
|
+
}
|
|
314
|
+
return strings.HasSuffix(fsPath, shared.TSExt) || strings.HasSuffix(fsPath, shared.TSXExt), nil
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// CreateNodeModulesPathMapping is createNodeModulesPathMapping. Keys are canonical file names.
|
|
318
|
+
func CreateNodeModulesPathMapping(typeRoots []string, caseSensitivity tspath.CaseSensitivity) (map[string]string, error) {
|
|
319
|
+
mapping := map[string]string{}
|
|
320
|
+
for _, scopePath := range typeRoots {
|
|
321
|
+
if !exists(scopePath) {
|
|
322
|
+
continue
|
|
323
|
+
}
|
|
324
|
+
entries, err := os.ReadDir(scopePath)
|
|
325
|
+
if err != nil {
|
|
326
|
+
return nil, err
|
|
327
|
+
}
|
|
328
|
+
for _, entry := range entries {
|
|
329
|
+
pkgPath := path.Join(scopePath, entry.Name())
|
|
330
|
+
pkgJSONPath, ok := shared.RealPathExists(path.Join(pkgPath, "package.json"))
|
|
331
|
+
if !ok {
|
|
332
|
+
continue
|
|
333
|
+
}
|
|
334
|
+
data, err := os.ReadFile(pkgJSONPath)
|
|
335
|
+
if err != nil {
|
|
336
|
+
return nil, err
|
|
337
|
+
}
|
|
338
|
+
var pkgJSON struct {
|
|
339
|
+
Main *string `json:"main"`
|
|
340
|
+
Typings *string `json:"typings"`
|
|
341
|
+
Types *string `json:"types"`
|
|
342
|
+
}
|
|
343
|
+
if err := json.Unmarshal(data, &pkgJSON); err != nil {
|
|
344
|
+
return nil, err
|
|
345
|
+
}
|
|
346
|
+
// both "types" and "typings" are valid
|
|
347
|
+
typesPath := "index.d.ts"
|
|
348
|
+
if pkgJSON.Types != nil {
|
|
349
|
+
typesPath = *pkgJSON.Types
|
|
350
|
+
} else if pkgJSON.Typings != nil {
|
|
351
|
+
typesPath = *pkgJSON.Typings
|
|
352
|
+
}
|
|
353
|
+
if pkgJSON.Main != nil && *pkgJSON.Main != "" {
|
|
354
|
+
mapping[caseSensitivity.Canonicalize(path.Resolve(pkgPath, typesPath))] = path.Resolve(pkgPath, *pkgJSON.Main)
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return mapping, nil
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// inferProjectType is inferProjectType in compileFiles.ts.
|
|
362
|
+
func inferProjectType(data *shared.ProjectData, resolver *rojoresolver.Resolver) shared.ProjectType {
|
|
363
|
+
if data.IsPackage {
|
|
364
|
+
return shared.ProjectTypePackage
|
|
365
|
+
}
|
|
366
|
+
if resolver.IsGame {
|
|
367
|
+
return shared.ProjectTypeGame
|
|
368
|
+
}
|
|
369
|
+
return shared.ProjectTypeModel
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// CompileContext is what compileFiles.ts works out before transforming, and what TransformState needs.
|
|
373
|
+
type CompileContext struct {
|
|
374
|
+
RojoResolver *rojoresolver.Resolver
|
|
375
|
+
PkgRojoResolvers []*rojoresolver.Resolver
|
|
376
|
+
NodeModulesPathMapping map[string]string
|
|
377
|
+
// RuntimeLibRbxPath is nil where JS has undefined, which it is for packages.
|
|
378
|
+
RuntimeLibRbxPath rojoresolver.RbxPath
|
|
379
|
+
ProjectType shared.ProjectType
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// PrepareCompile is the part of compileFiles before the plugin and transform steps. Failure text matches
|
|
383
|
+
// emitResultFailure. A non-nil failure means stop with those diagnostics. Plain errors are what the JS throws.
|
|
384
|
+
func PrepareCompile(p *tsbridge.Project, data *shared.ProjectData, translator *pathtranslator.Translator, caseSensitivity tspath.CaseSensitivity) (*CompileContext, []*shared.Diagnostic, error) {
|
|
385
|
+
return PrepareCompileCached(p, data, translator, caseSensitivity, nil)
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// PrepareCache keeps what PrepareCompile reads from the Rojo project and the packages, so that a long-running process
|
|
389
|
+
// doesn't walk those folders for every compile. Whoever holds one must drop it when one of those changes.
|
|
390
|
+
type PrepareCache struct {
|
|
391
|
+
resolver *rojoresolver.Resolver
|
|
392
|
+
pkgResolvers []*rojoresolver.Resolver
|
|
393
|
+
mapping map[string]string
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// PrepareCompileCached is PrepareCompile. The cache can be nil.
|
|
397
|
+
func PrepareCompileCached(p *tsbridge.Project, data *shared.ProjectData, translator *pathtranslator.Translator, caseSensitivity tspath.CaseSensitivity, cache *PrepareCache) (*CompileContext, []*shared.Diagnostic, error) {
|
|
398
|
+
options := p.Options()
|
|
399
|
+
outDir := options.OutDir.AsString()
|
|
400
|
+
|
|
401
|
+
var resolver *rojoresolver.Resolver
|
|
402
|
+
var err error
|
|
403
|
+
switch {
|
|
404
|
+
case cache != nil && cache.resolver != nil:
|
|
405
|
+
resolver = cache.resolver
|
|
406
|
+
case data.RojoConfigPath != "":
|
|
407
|
+
resolver, err = rojoresolver.FromPath(data.RojoConfigPath)
|
|
408
|
+
default:
|
|
409
|
+
resolver, err = rojoresolver.Synthetic(outDir)
|
|
410
|
+
}
|
|
411
|
+
if err != nil {
|
|
412
|
+
return nil, nil, err
|
|
413
|
+
}
|
|
414
|
+
for _, warning := range resolver.Warnings() {
|
|
415
|
+
shared.LogService.Warn(warning)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
service := &shared.DiagnosticService{}
|
|
419
|
+
CheckRojoConfig(service, data, resolver, GetRootDirs(options), translator)
|
|
420
|
+
for _, file := range p.Program().SourceFiles() {
|
|
421
|
+
if !strings.HasPrefix(path.Normalize(file.FileName().AsString()), data.NodeModulesPath) {
|
|
422
|
+
CheckFileName(service, file.FileName().AsString())
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if options.TypeRoots == nil {
|
|
427
|
+
panic("typeRoots must be set: ValidateCompilerOptions checks this")
|
|
428
|
+
}
|
|
429
|
+
var pkgResolvers []*rojoresolver.Resolver
|
|
430
|
+
var mapping map[string]string
|
|
431
|
+
if cache != nil && cache.resolver != nil {
|
|
432
|
+
pkgResolvers, mapping = cache.pkgResolvers, cache.mapping
|
|
433
|
+
} else {
|
|
434
|
+
pkgResolvers = make([]*rojoresolver.Resolver, len(options.TypeRoots))
|
|
435
|
+
for i, typeRoot := range options.TypeRoots {
|
|
436
|
+
pkgResolvers[i], err = rojoresolver.Synthetic(string(typeRoot))
|
|
437
|
+
if err != nil {
|
|
438
|
+
return nil, nil, err
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
mapping, err = CreateNodeModulesPathMapping(typeRootStrings(options), caseSensitivity)
|
|
442
|
+
if err != nil {
|
|
443
|
+
return nil, nil, err
|
|
444
|
+
}
|
|
445
|
+
if cache != nil {
|
|
446
|
+
cache.resolver, cache.pkgResolvers, cache.mapping = resolver, pkgResolvers, mapping
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
projectType := data.ProjectOptions.Type
|
|
451
|
+
if projectType == "" {
|
|
452
|
+
projectType = inferProjectType(data, resolver)
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
failure := func(message string) []*shared.Diagnostic {
|
|
456
|
+
return []*shared.Diagnostic{shared.TextDiagnostic(message, shared.CategoryError)}
|
|
457
|
+
}
|
|
458
|
+
if projectType != shared.ProjectTypePackage && data.RojoConfigPath == "" {
|
|
459
|
+
return nil, failure("Non-package projects must have a Rojo project file!"), nil
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
var runtimeLibRbxPath rojoresolver.RbxPath
|
|
463
|
+
if projectType != shared.ProjectTypePackage {
|
|
464
|
+
runtimeLibRbxPath = resolver.GetRbxPathFromFilePath(path.Join(data.ProjectOptions.IncludePath, "RuntimeLib.lua"))
|
|
465
|
+
if runtimeLibRbxPath == nil {
|
|
466
|
+
return nil, failure("Rojo project contained no data for include folder!"), nil
|
|
467
|
+
}
|
|
468
|
+
if resolver.GetNetworkType(runtimeLibRbxPath) != rojoresolver.NetworkTypeUnknown {
|
|
469
|
+
return nil, failure("Runtime library cannot be in a server-only or client-only container!"), nil
|
|
470
|
+
}
|
|
471
|
+
if resolver.IsIsolated(runtimeLibRbxPath) {
|
|
472
|
+
return nil, failure("Runtime library cannot be in an isolated container!"), nil
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if service.HasErrors() {
|
|
477
|
+
return nil, service.Flush(), nil
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return &CompileContext{
|
|
481
|
+
RojoResolver: resolver,
|
|
482
|
+
PkgRojoResolvers: pkgResolvers,
|
|
483
|
+
NodeModulesPathMapping: mapping,
|
|
484
|
+
RuntimeLibRbxPath: runtimeLibRbxPath,
|
|
485
|
+
ProjectType: projectType,
|
|
486
|
+
}, nil, nil
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
func typeRootStrings(options *core.CompilerOptions) []string {
|
|
490
|
+
if options.TypeRoots == nil {
|
|
491
|
+
return nil
|
|
492
|
+
}
|
|
493
|
+
result := make([]string, len(options.TypeRoots))
|
|
494
|
+
for i, typeRoot := range options.TypeRoots {
|
|
495
|
+
result[i] = string(typeRoot)
|
|
496
|
+
}
|
|
497
|
+
return result
|
|
498
|
+
}
|