@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,30 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/typeGuards.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
7
|
+
"github.com/microsoft/TypeScript/tsc/internal/checker"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
func isBlockLike(node *ast.Node) bool {
|
|
11
|
+
return node.Kind == ast.KindSourceFile ||
|
|
12
|
+
node.Kind == ast.KindBlock ||
|
|
13
|
+
node.Kind == ast.KindModuleBlock ||
|
|
14
|
+
node.Kind == ast.KindCaseClause ||
|
|
15
|
+
node.Kind == ast.KindDefaultClause
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
func isUnaryAssignmentOperator(operator ast.Kind) bool {
|
|
19
|
+
return operator == ast.KindPlusPlusToken || operator == ast.KindMinusMinusToken
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func isTemplateLiteralType(t *checker.Type) bool {
|
|
23
|
+
return t.Flags()&checker.TypeFlagsTemplateLiteral != 0
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// isNamespace tests the keyword, because TS 7 has no NodeFlags.Namespace. As with the flag, the inner parts of
|
|
27
|
+
// `namespace A.B {}` count too, and `module A {}` doesn't.
|
|
28
|
+
func isNamespace(node *ast.Node) bool {
|
|
29
|
+
return ast.IsModuleDeclaration(node) && node.AsModuleDeclaration().Keyword == ast.KindNamespaceKeyword
|
|
30
|
+
}
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/types.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"os"
|
|
7
|
+
"reflect"
|
|
8
|
+
"slices"
|
|
9
|
+
"strings"
|
|
10
|
+
"sync"
|
|
11
|
+
|
|
12
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
13
|
+
"github.com/microsoft/TypeScript/tsc/internal/checker"
|
|
14
|
+
"github.com/microsoft/TypeScript/tsc/internal/jsnum"
|
|
15
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
|
|
16
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
17
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/tsbridge"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
type typeCheck = func(*checker.Type) bool
|
|
21
|
+
|
|
22
|
+
// checkerOfType stands in for the checker that JS type objects carry, which upstream's `type.getConstraint()`,
|
|
23
|
+
// `type.getBaseTypes()`, `type.getProperties()` and so on use. The export is in patches/typescript/0001.
|
|
24
|
+
func checkerOfType(t *checker.Type) *checker.Checker {
|
|
25
|
+
return t.Checker()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func isClassOrInterface(t *checker.Type) bool {
|
|
29
|
+
return t.ObjectFlags()&checker.ObjectFlagsClassOrInterface != 0
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
func getRecursiveBaseTypesInner(result *[]*checker.Type, t *checker.Type) {
|
|
33
|
+
for _, baseType := range checkerOfType(t).GetBaseTypes(t) {
|
|
34
|
+
*result = append(*result, baseType)
|
|
35
|
+
if isClassOrInterface(baseType) {
|
|
36
|
+
getRecursiveBaseTypesInner(result, baseType)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
func getRecursiveBaseTypes(t *checker.Type) []*checker.Type {
|
|
42
|
+
var result []*checker.Type
|
|
43
|
+
getRecursiveBaseTypesInner(&result, t)
|
|
44
|
+
return result
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
func isDefinitelyTypeInner(t *checker.Type, callbacks []typeCheck) bool {
|
|
48
|
+
if t.IsUnion() {
|
|
49
|
+
return !slices.ContainsFunc(t.Types(), func(u *checker.Type) bool { return !isDefinitelyTypeInner(u, callbacks) })
|
|
50
|
+
} else if t.IsIntersection() {
|
|
51
|
+
return slices.ContainsFunc(t.Types(), func(u *checker.Type) bool { return isDefinitelyTypeInner(u, callbacks) })
|
|
52
|
+
} else {
|
|
53
|
+
if isClassOrInterface(t) && slices.ContainsFunc(getRecursiveBaseTypes(t), func(u *checker.Type) bool { return isDefinitelyTypeInner(u, callbacks) }) {
|
|
54
|
+
return true
|
|
55
|
+
}
|
|
56
|
+
return slices.ContainsFunc(callbacks, func(cb typeCheck) bool { return cb(t) })
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
func isDefinitelyType(t *checker.Type, callbacks ...typeCheck) bool {
|
|
61
|
+
constraint := checkerOfType(t).GetBaseConstraintOfType(t)
|
|
62
|
+
if constraint == nil {
|
|
63
|
+
constraint = t
|
|
64
|
+
}
|
|
65
|
+
return isDefinitelyTypeInner(constraint, callbacks)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
func isPossiblyTypeInner(t *checker.Type, callbacks []typeCheck) bool {
|
|
69
|
+
if t.Flags()&checker.TypeFlagsUnionOrIntersection != 0 {
|
|
70
|
+
return slices.ContainsFunc(t.Types(), func(u *checker.Type) bool { return isPossiblyTypeInner(u, callbacks) })
|
|
71
|
+
} else {
|
|
72
|
+
if isClassOrInterface(t) && slices.ContainsFunc(getRecursiveBaseTypes(t), func(u *checker.Type) bool { return isPossiblyTypeInner(u, callbacks) }) {
|
|
73
|
+
return true
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// type variable without constraint, any, or unknown
|
|
77
|
+
if t.Flags()&(checker.TypeFlagsTypeVariable|checker.TypeFlagsAnyOrUnknown) != 0 {
|
|
78
|
+
return true
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// defined type
|
|
82
|
+
if isDefinedType(t) {
|
|
83
|
+
// Go funcs aren't comparable, so this is `callbacks[0] === isUndefinedType` by code pointer.
|
|
84
|
+
if len(callbacks) == 1 && reflect.ValueOf(callbacks[0]).Pointer() == reflect.ValueOf(isUndefinedType).Pointer() {
|
|
85
|
+
// if only matching undefined, then defined means not possible
|
|
86
|
+
return false
|
|
87
|
+
}
|
|
88
|
+
return true
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return slices.ContainsFunc(callbacks, func(cb typeCheck) bool { return cb(t) })
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
func isPossiblyType(t *checker.Type, callbacks ...typeCheck) bool {
|
|
96
|
+
constraint := checkerOfType(t).GetBaseConstraintOfType(t)
|
|
97
|
+
if constraint == nil {
|
|
98
|
+
constraint = t
|
|
99
|
+
}
|
|
100
|
+
return isPossiblyTypeInner(constraint, callbacks)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
func isDefinedType(t *checker.Type) bool {
|
|
104
|
+
if t.Flags() != checker.TypeFlagsObject {
|
|
105
|
+
return false
|
|
106
|
+
}
|
|
107
|
+
c := checkerOfType(t)
|
|
108
|
+
return len(c.GetPropertiesOfType(t)) == 0 &&
|
|
109
|
+
len(c.GetCallSignatures(t)) == 0 &&
|
|
110
|
+
len(c.GetConstructSignatures(t)) == 0 &&
|
|
111
|
+
c.GetIndexTypeOfType(t, c.GetNumberType()) == nil &&
|
|
112
|
+
c.GetIndexTypeOfType(t, c.GetStringType()) == nil
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
func isAnyType(state *TransformState) typeCheck {
|
|
116
|
+
return func(t *checker.Type) bool { return t == state.Checker.GetAnyType() }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
func isBooleanType(t *checker.Type) bool {
|
|
120
|
+
return t.Flags()&(checker.TypeFlagsBoolean|checker.TypeFlagsBooleanLiteral) != 0
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
func isBooleanLiteralType(state *TransformState, value bool) typeCheck {
|
|
124
|
+
return func(t *checker.Type) bool {
|
|
125
|
+
if t.Flags()&checker.TypeFlagsBooleanLiteral != 0 {
|
|
126
|
+
valueType := state.Checker.GetFalseType()
|
|
127
|
+
if value {
|
|
128
|
+
valueType = state.Checker.GetTrueType()
|
|
129
|
+
}
|
|
130
|
+
return t == valueType
|
|
131
|
+
}
|
|
132
|
+
return isBooleanType(t)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
func isNumberType(t *checker.Type) bool {
|
|
137
|
+
return t.Flags()&(checker.TypeFlagsNumber|checker.TypeFlagsNumberLike|checker.TypeFlagsNumberLiteral) != 0
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
func isNumberLiteralType(value float64) typeCheck {
|
|
141
|
+
return func(t *checker.Type) bool {
|
|
142
|
+
if t.IsNumberLiteral() {
|
|
143
|
+
return t.AsLiteralType().Value() == any(jsnum.Number(value))
|
|
144
|
+
}
|
|
145
|
+
return isNumberType(t)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
func isNaNType(t *checker.Type) bool {
|
|
150
|
+
return isNumberType(t) && !t.IsNumberLiteral()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
func isStringType(t *checker.Type) bool {
|
|
154
|
+
return t.Flags()&(checker.TypeFlagsString|checker.TypeFlagsStringLike|checker.TypeFlagsStringLiteral) != 0
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
func isArrayType(state *TransformState) typeCheck {
|
|
158
|
+
return func(t *checker.Type) bool {
|
|
159
|
+
// typeChecker.isArrayLikeType() will return true for `any`, so rule it out here
|
|
160
|
+
if t.Flags()&checker.TypeFlagsAny != 0 {
|
|
161
|
+
return false
|
|
162
|
+
}
|
|
163
|
+
return checker.IsTupleType(t) ||
|
|
164
|
+
state.Checker.IsArrayLikeType(t) ||
|
|
165
|
+
t.Symbol() == getSymbolOrThrow(state, "ReadonlyArray") ||
|
|
166
|
+
t.Symbol() == getSymbolOrThrow(state, "Array") ||
|
|
167
|
+
t.Symbol() == getSymbolOrThrow(state, "ReadVoxelsArray") ||
|
|
168
|
+
t.Symbol() == getSymbolOrThrow(state, "TemplateStringsArray")
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
func isSetType(state *TransformState) typeCheck {
|
|
173
|
+
return func(t *checker.Type) bool {
|
|
174
|
+
return t.Symbol() == getSymbolOrThrow(state, "Set") ||
|
|
175
|
+
t.Symbol() == getSymbolOrThrow(state, "ReadonlySet") ||
|
|
176
|
+
t.Symbol() == getSymbolOrThrow(state, "WeakSet")
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
func isMapType(state *TransformState) typeCheck {
|
|
181
|
+
return func(t *checker.Type) bool {
|
|
182
|
+
return t.Symbol() == getSymbolOrThrow(state, "Map") ||
|
|
183
|
+
t.Symbol() == getSymbolOrThrow(state, "ReadonlyMap") ||
|
|
184
|
+
t.Symbol() == getSymbolOrThrow(state, "WeakMap")
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
func isGeneratorType(state *TransformState) typeCheck {
|
|
189
|
+
return func(t *checker.Type) bool { return t.Symbol() == getSymbolOrThrow(state, "Generator") }
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
func isIterableFunctionType(state *TransformState) typeCheck {
|
|
193
|
+
return func(t *checker.Type) bool { return t.Symbol() == getSymbolOrThrow(state, "IterableFunction") }
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
func isLuaTupleType(state *TransformState) typeCheck {
|
|
197
|
+
return func(t *checker.Type) bool {
|
|
198
|
+
return state.Checker.GetPropertyOfType(t, tsbridge.NominalLuaTupleName) ==
|
|
199
|
+
getSymbolOrThrow(state, tsbridge.NominalLuaTupleName)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
func isIterableFunctionLuaTupleType(state *TransformState) typeCheck {
|
|
204
|
+
return func(t *checker.Type) bool {
|
|
205
|
+
if isIterableFunctionType(state)(t) {
|
|
206
|
+
typeArguments := getTypeArguments(state, t)
|
|
207
|
+
return len(typeArguments) > 0 && isLuaTupleType(state)(typeArguments[0])
|
|
208
|
+
}
|
|
209
|
+
return false
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
func isIterableType(state *TransformState) typeCheck {
|
|
214
|
+
return func(t *checker.Type) bool { return t.Symbol() == getSymbolOrThrow(state, "Iterable") }
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
func isObjectType(t *checker.Type) bool {
|
|
218
|
+
return t.Flags()&checker.TypeFlagsObject != 0
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
func isUndefinedType(t *checker.Type) bool {
|
|
222
|
+
return t.Flags()&(checker.TypeFlagsUndefined|checker.TypeFlagsVoid) != 0
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
func isEmptyStringType(t *checker.Type) bool {
|
|
226
|
+
if t.IsStringLiteral() {
|
|
227
|
+
return t.AsLiteralType().Value() == any("")
|
|
228
|
+
}
|
|
229
|
+
if isTemplateLiteralType(t) {
|
|
230
|
+
texts := t.AsTemplateLiteralType().Texts()
|
|
231
|
+
return len(texts) == 0 || !slices.ContainsFunc(texts, func(v string) bool { return len(v) != 0 })
|
|
232
|
+
}
|
|
233
|
+
return isStringType(t)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
func isRobloxType(state *TransformState) typeCheck {
|
|
237
|
+
return func(t *checker.Type) bool {
|
|
238
|
+
symbol := t.Symbol()
|
|
239
|
+
if symbol == nil {
|
|
240
|
+
return false
|
|
241
|
+
}
|
|
242
|
+
return slices.ContainsFunc(symbol.Declarations(), func(d *ast.Node) bool {
|
|
243
|
+
sourceFile := ast.GetSourceFileOfNode(d)
|
|
244
|
+
return sourceFile != nil && state.isRobloxTypesFile(sourceFile)
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// isRobloxTypesFile reports whether a file is inside node_modules/@rbxts/types. The answer only depends on the file,
|
|
250
|
+
// and this is asked for every declaration of every type that reaches isRobloxType, so it is kept per worker.
|
|
251
|
+
func (s *TransformState) isRobloxTypesFile(sourceFile *ast.SourceFile) bool {
|
|
252
|
+
if answer, ok := s.Multi.IsRobloxTypesFileCache[sourceFile]; ok {
|
|
253
|
+
return answer
|
|
254
|
+
}
|
|
255
|
+
if s.Multi.robloxTypesPath == "" {
|
|
256
|
+
s.Multi.robloxTypesPath = nodepath.Native.Join(s.Data.NodeModulesPath, shared.RbxtsScope, "types")
|
|
257
|
+
}
|
|
258
|
+
answer := shared.IsPathDescendantOf(sourceFile.FileName().AsString(), s.Multi.robloxTypesPath)
|
|
259
|
+
s.Multi.IsRobloxTypesFileCache[sourceFile] = answer
|
|
260
|
+
return answer
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// type utilities
|
|
264
|
+
|
|
265
|
+
func walkTypes(t *checker.Type, callback func(*checker.Type)) {
|
|
266
|
+
if t.Flags()&checker.TypeFlagsUnionOrIntersection != 0 {
|
|
267
|
+
for _, u := range t.Types() {
|
|
268
|
+
walkTypes(u, callback)
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
// in template literal types, constraint === type and this causes infinite recursion
|
|
272
|
+
constraint := checkerOfType(t).GetBaseConstraintOfType(t)
|
|
273
|
+
if constraint != nil && constraint != t {
|
|
274
|
+
walkTypes(constraint, callback)
|
|
275
|
+
} else {
|
|
276
|
+
callback(t)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
func getFirstConstructSymbol(state *TransformState, expression *ast.Node) *ast.Symbol {
|
|
282
|
+
t := state.GetType(expression)
|
|
283
|
+
if t.Symbol() != nil {
|
|
284
|
+
for _, declaration := range t.Symbol().Declarations() {
|
|
285
|
+
if ast.IsInterfaceDeclaration(declaration) {
|
|
286
|
+
for _, member := range declaration.AsInterfaceDeclaration().Members.Nodes {
|
|
287
|
+
if ast.IsConstructSignatureDeclaration(member) {
|
|
288
|
+
return member.Symbol()
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return nil
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
var (
|
|
298
|
+
traceUnionOnce sync.Once
|
|
299
|
+
traceUnionPath string
|
|
300
|
+
traceUnionMutex sync.Mutex
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
// traceUnionMembers is not in upstream. It measures the union order difference between TS 7 and TS 5.5.3 (see "Union
|
|
304
|
+
// order" in the guide): with RBXTSGO_TRACE_UNION=path, it appends one line per call on a union or intersection whose
|
|
305
|
+
// members carry two or more distinct symbols. It only reads symbol names, and doesn't print types, because printing
|
|
306
|
+
// can make the checker create types. Failing to write is ignored, so the trace can't affect a compile.
|
|
307
|
+
func traceUnionMembers(state *TransformState, t *checker.Type) {
|
|
308
|
+
traceUnionOnce.Do(func() { traceUnionPath = os.Getenv("RBXTSGO_TRACE_UNION") })
|
|
309
|
+
if traceUnionPath == "" {
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
var names []string
|
|
313
|
+
distinct := map[*ast.Symbol]bool{}
|
|
314
|
+
for _, u := range t.Types() {
|
|
315
|
+
if symbol := u.Symbol(); symbol != nil {
|
|
316
|
+
names = append(names, ast.EscapeAllInternalSymbolNames(ast.SymbolName(symbol)))
|
|
317
|
+
distinct[symbol] = true
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if len(distinct) < 2 {
|
|
321
|
+
return
|
|
322
|
+
}
|
|
323
|
+
line := state.SourceFile.FileName().AsString() + ": " + strings.Join(names, " | ") + "\n"
|
|
324
|
+
|
|
325
|
+
traceUnionMutex.Lock()
|
|
326
|
+
defer traceUnionMutex.Unlock()
|
|
327
|
+
file, err := os.OpenFile(traceUnionPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
|
328
|
+
if err != nil {
|
|
329
|
+
return
|
|
330
|
+
}
|
|
331
|
+
defer file.Close()
|
|
332
|
+
_, _ = file.WriteString(line)
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
func getFirstDefinedSymbol(state *TransformState, t *checker.Type) *ast.Symbol {
|
|
336
|
+
if t.Flags()&checker.TypeFlagsUnionOrIntersection != 0 {
|
|
337
|
+
traceUnionMembers(state, t)
|
|
338
|
+
// See docs/union-order.md. TS 5.5.3 sorts a union by type id, which is creation order, and
|
|
339
|
+
// MacroManager creates the type of every method of a macro class before any file is checked. So in JS those
|
|
340
|
+
// come first in any union, in MacroManager's order. TS 7 sorts by name and position. Call macros come next:
|
|
341
|
+
// that part only matches what JS was measured to do, as docs/union-order.md says.
|
|
342
|
+
var best *ast.Symbol
|
|
343
|
+
bestOrder := -1
|
|
344
|
+
if t.Flags()&checker.TypeFlagsUnion != 0 {
|
|
345
|
+
for _, u := range t.Types() {
|
|
346
|
+
if u.Symbol() == nil {
|
|
347
|
+
continue
|
|
348
|
+
}
|
|
349
|
+
if order, ok := state.Services.MacroManager.MethodCreationOrder(u.Symbol()); ok && (best == nil || order < bestOrder) {
|
|
350
|
+
best = u.Symbol()
|
|
351
|
+
bestOrder = order
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if best != nil {
|
|
355
|
+
return best
|
|
356
|
+
}
|
|
357
|
+
for _, u := range t.Types() {
|
|
358
|
+
if u.Symbol() != nil && state.Services.MacroManager.CallMacro(u.Symbol()) != "" {
|
|
359
|
+
return u.Symbol()
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
for _, u := range t.Types() {
|
|
364
|
+
if u.Symbol() != nil && !state.Checker.IsUndefinedSymbol(u.Symbol()) {
|
|
365
|
+
return u.Symbol()
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return nil
|
|
369
|
+
} else {
|
|
370
|
+
return t.Symbol()
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// getTypeArguments returns nil for a type that isn't a reference. Upstream casts to TypeReference, and JS's
|
|
375
|
+
// getTypeArguments gives an empty array for those, where Go's panics.
|
|
376
|
+
func getTypeArguments(state *TransformState, t *checker.Type) []*checker.Type {
|
|
377
|
+
if t.ObjectFlags()&checker.ObjectFlagsReference == 0 {
|
|
378
|
+
return nil
|
|
379
|
+
}
|
|
380
|
+
return state.Checker.GetTypeArguments(t)
|
|
381
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/validateIdentifier.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
7
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
8
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
func validateIdentifier(state *TransformState, node *ast.Node) {
|
|
12
|
+
if !luau.IsValidIdentifier(node.Text()) {
|
|
13
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoInvalidIdentifier.At(shared.AtNode(node)))
|
|
14
|
+
} else if luau.IsReservedIdentifier(node.Text()) {
|
|
15
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoReservedIdentifier.At(shared.AtNode(node)))
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/validateMethodAssignment.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"strings"
|
|
7
|
+
|
|
8
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
9
|
+
"github.com/microsoft/TypeScript/tsc/internal/checker"
|
|
10
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
func hasCallSignatures(t *checker.Type) bool {
|
|
14
|
+
hasCallSignatures := false
|
|
15
|
+
walkTypes(t, func(u *checker.Type) {
|
|
16
|
+
hasCallSignatures = hasCallSignatures || len(checkerOfType(u).GetCallSignatures(u)) > 0
|
|
17
|
+
})
|
|
18
|
+
return hasCallSignatures
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
func validateTypes(state *TransformState, node *ast.Node, baseType *checker.Type, assignmentType *checker.Type) {
|
|
22
|
+
if hasCallSignatures(baseType) && hasCallSignatures(assignmentType) {
|
|
23
|
+
assignmentIsMethod := isMethodFromType(state, node, assignmentType)
|
|
24
|
+
if isMethodFromType(state, node, baseType) != assignmentIsMethod {
|
|
25
|
+
if assignmentIsMethod {
|
|
26
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.ExpectedMethodGotFunction.At(shared.AtNode(node)))
|
|
27
|
+
} else {
|
|
28
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.ExpectedFunctionGotMethod.At(shared.AtNode(node)))
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
func validateObjectLiteralElement(state *TransformState, node *ast.Node) {
|
|
35
|
+
t := state.GetType(node)
|
|
36
|
+
contextualType := state.Checker.GetContextualTypeForObjectLiteralElement(node, checker.ContextFlagsNone)
|
|
37
|
+
if contextualType != nil && contextualType != t {
|
|
38
|
+
validateTypes(state, node, t, contextualType)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func validateHeritageClause(state *TransformState, node *ast.Node, typeNode *ast.Node) {
|
|
43
|
+
name := ast.GetPropertyNameForPropertyNameNode(node.Name())
|
|
44
|
+
// JS returns undefined for a computed name that isn't a literal, where Go returns InternalSymbolNameMissing
|
|
45
|
+
if name == "" || name == ast.InternalSymbolNameMissing {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// In TS 5.5.3, getPropertyNameForPropertyNameNode returns an escaped name and the public getTypeOfPropertyOfType
|
|
50
|
+
// escapes it again. So a name starting with `__` looks up one with an extra `_`. Go's names are never escaped.
|
|
51
|
+
if strings.HasPrefix(name, "__") {
|
|
52
|
+
name = "_" + name
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
t := state.GetType(node)
|
|
56
|
+
propertyType := state.Checker.GetTypeOfPropertyOfType(state.GetType(typeNode), name)
|
|
57
|
+
if propertyType == nil {
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
validateTypes(state, node, t, propertyType)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
func validateSpread(state *TransformState, node *ast.Node) {
|
|
65
|
+
t := state.GetType(node.Expression())
|
|
66
|
+
contextualType := state.Checker.GetContextualType(node.Expression(), checker.ContextFlagsNone)
|
|
67
|
+
if contextualType == nil {
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// This order can differ from TS 5.5.3's on spread types, see "Property order on spread types" in the guide. It
|
|
72
|
+
// can only change the order of the diagnostics below.
|
|
73
|
+
for _, property := range state.Checker.GetPropertiesOfType(t) {
|
|
74
|
+
// JS passes property.name to the public getTypeOfPropertyOfType, which escapes it. For a symbol-keyed or
|
|
75
|
+
// private property that finds nothing, so upstream always reaches the `continue` below. Go's name finds it.
|
|
76
|
+
if strings.HasPrefix(property.Name(), ast.InternalSymbolNamePrefix) {
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
basePropertyType := state.Checker.GetTypeOfPropertyOfType(t, property.Name())
|
|
80
|
+
assignmentPropertyType := state.Checker.GetTypeOfPropertyOfType(contextualType, property.Name())
|
|
81
|
+
if basePropertyType == nil {
|
|
82
|
+
continue
|
|
83
|
+
}
|
|
84
|
+
if assignmentPropertyType == nil {
|
|
85
|
+
continue
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
validateTypes(state, node, basePropertyType, assignmentPropertyType)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
func validateMethodAssignment(state *TransformState, node *ast.Node) {
|
|
93
|
+
if ast.IsClassElement(node) && ast.IsClassLike(node.Parent) && node.Name() != nil {
|
|
94
|
+
for _, typeNode := range getAllSuperTypeNodes(node.Parent) {
|
|
95
|
+
validateHeritageClause(state, node, typeNode)
|
|
96
|
+
}
|
|
97
|
+
} else if ast.IsObjectLiteralElement(node) {
|
|
98
|
+
if ast.IsSpreadAssignment(node) {
|
|
99
|
+
if !ast.IsObjectLiteralExpression(node.Expression()) {
|
|
100
|
+
validateSpread(state, node)
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
validateObjectLiteralElement(state, node)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/validateNotAny.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
7
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
func validateNotAnyType(state *TransformState, node *ast.Node) {
|
|
11
|
+
if ast.IsSpreadElement(node) {
|
|
12
|
+
node = SkipDownwards(node.Expression())
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
t := state.GetType(node)
|
|
16
|
+
|
|
17
|
+
if isDefinitelyType(t, isArrayType(state)) {
|
|
18
|
+
// Array<T> -> T
|
|
19
|
+
indexType := state.Checker.GetIndexTypeOfType(t, state.Checker.GetNumberType())
|
|
20
|
+
if indexType != nil {
|
|
21
|
+
t = indexType
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if isDefinitelyType(t, isAnyType(state)) {
|
|
26
|
+
// given a type like `a: { [index: string]: any }`, `a["b"]` will not have a symbol
|
|
27
|
+
symbol := getOriginalSymbolOfNode(state.Checker, node)
|
|
28
|
+
if symbol != nil {
|
|
29
|
+
if !state.Multi.IsReportedByNoAnyCache[symbol] {
|
|
30
|
+
state.Multi.IsReportedByNoAnyCache[symbol] = true
|
|
31
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoAny.At(shared.AtNode(node)))
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoAny.At(shared.AtNode(node)))
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/wrapExpressionStatement.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
6
|
+
|
|
7
|
+
func wrapExpressionStatement(node luau.Expression) *luau.List {
|
|
8
|
+
if luau.IsTemporaryIdentifier(node) || luau.IsNone(node) {
|
|
9
|
+
return luau.NewList()
|
|
10
|
+
} else if luau.IsCall(node) {
|
|
11
|
+
return luau.MakeList(luau.Create(&luau.CallStatement{Expression: node.(luau.CallLike)}))
|
|
12
|
+
} else {
|
|
13
|
+
return luau.MakeList(
|
|
14
|
+
luau.Create(&luau.VariableDeclaration{
|
|
15
|
+
Left: luau.NewTempID(""),
|
|
16
|
+
Right: node,
|
|
17
|
+
}),
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/wrapReturnIfLuaTuple.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
7
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
func shouldWrapLuaTuple(state *TransformState, node *ast.Node, exp luau.Expression) bool {
|
|
11
|
+
if !luau.IsCall(exp) {
|
|
12
|
+
return true
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
child := SkipUpwards(node)
|
|
16
|
+
parent := child.Parent
|
|
17
|
+
|
|
18
|
+
// `foo();`
|
|
19
|
+
if ast.IsExpressionStatement(parent) {
|
|
20
|
+
return false
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// if part of for statement definition, except if used as the condition
|
|
24
|
+
if ast.IsForStatement(parent) && parent.AsForStatement().Condition != child {
|
|
25
|
+
return false
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// `const [a] = foo()`
|
|
29
|
+
if ast.IsVariableDeclaration(parent) &&
|
|
30
|
+
ast.IsArrayBindingPattern(parent.Name()) &&
|
|
31
|
+
!arrayBindingPatternContainsHoists(state, parent.Name()) {
|
|
32
|
+
return false
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// `[a] = foo()`
|
|
36
|
+
if ast.IsAssignmentExpression(parent, false) && ast.IsArrayLiteralExpression(parent.AsBinaryExpression().Left) {
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// `foo()[n]`
|
|
41
|
+
if ast.IsElementAccessExpression(parent) {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// `return foo()`
|
|
46
|
+
if ast.IsReturnStatement(parent) {
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// `void foo()`
|
|
51
|
+
if ast.IsVoidExpression(parent) {
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return true
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
func wrapReturnIfLuaTuple(state *TransformState, node *ast.Node, exp luau.Expression) luau.Expression {
|
|
59
|
+
if isLuaTupleType(state)(state.GetType(node)) && shouldWrapLuaTuple(state, node, exp) {
|
|
60
|
+
return luau.NewArray(exp)
|
|
61
|
+
}
|
|
62
|
+
return exp
|
|
63
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/wrapStatementsAsGenerator.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
7
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
func wrapStatementsAsGenerator(state *TransformState, node *ast.Node, statements *luau.List) *luau.List {
|
|
11
|
+
return luau.MakeList(
|
|
12
|
+
luau.Create(&luau.ReturnStatement{
|
|
13
|
+
Expression: luau.NewCall(state.TS(node, "generator"),
|
|
14
|
+
luau.Create(&luau.FunctionExpression{
|
|
15
|
+
HasDotDotDot: false,
|
|
16
|
+
Parameters: luau.NewList(),
|
|
17
|
+
Statements: statements,
|
|
18
|
+
}),
|
|
19
|
+
),
|
|
20
|
+
}),
|
|
21
|
+
)
|
|
22
|
+
}
|