@data-vegle/rbxts-go 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +181 -2
- package/README.rbxtsgo.md +169 -0
- package/THIRD_PARTY_NOTICES.md +745 -0
- package/cli/cli.go +454 -0
- package/compile/compile.go +388 -0
- package/compile/declarations.go +221 -0
- package/compile/incremental.go +349 -0
- package/compile/times.go +9 -0
- package/docs/plugin-api.md +239 -0
- package/go.mod +20 -0
- package/go.sum +24 -0
- package/internal/difftest/difftest.go +530 -0
- package/internal/plugintest/plugintest.go +181 -0
- package/luau/create.go +351 -0
- package/luau/doc.go +71 -0
- package/luau/globals.go +178 -0
- package/luau/jsnum.go +188 -0
- package/luau/kinds.go +128 -0
- package/luau/list.go +227 -0
- package/luau/nodes.go +479 -0
- package/luau/operators.go +46 -0
- package/luau/render.go +134 -0
- package/luau/render_expressions.go +264 -0
- package/luau/render_fields.go +81 -0
- package/luau/render_indexable.go +81 -0
- package/luau/render_state.go +134 -0
- package/luau/render_statements.go +235 -0
- package/luau/render_util.go +296 -0
- package/luau/solve_temp_ids.go +212 -0
- package/luau/typeguards.go +175 -0
- package/luau/util.go +184 -0
- package/luau/visit.go +138 -0
- package/npm/index.d.ts +3 -0
- package/npm/rbxts-go.sh +187 -0
- package/package.json +45 -5
- package/patches/typescript/0001-checker-exports.patch +40 -0
- package/patches/typescript/0002-iterable-arity.patch +15 -0
- package/patches/typescript/0003-context-sensitive-this.patch +15 -0
- package/patches/typescript/0004-printer-keep-parse-tree-parentheses.patch +26 -0
- package/pluginapi/host.go +31 -0
- package/pluginapi/pluginapi.go +153 -0
- package/plugins/LICENSE-rbxts-transformer-setget +18 -0
- package/plugins/apply.go +342 -0
- package/plugins/config.go +431 -0
- package/plugins/fallback.go +84 -0
- package/plugins/fixemit.go +60 -0
- package/plugins/native.go +262 -0
- package/plugins/session.go +223 -0
- package/plugins/setget.go +478 -0
- package/project/copy.go +333 -0
- package/project/include/Promise.lua +2068 -0
- package/project/include/RuntimeLib.lua +260 -0
- package/project/nodepath/nodepath.go +917 -0
- package/project/pathtranslator/pathtranslator.go +214 -0
- package/project/project.go +498 -0
- package/project/rojoresolver/json.go +248 -0
- package/project/rojoresolver/rojoresolver.go +559 -0
- package/project/shared/constants.go +83 -0
- package/project/shared/diagnostic.go +238 -0
- package/project/shared/diagnostics.go +388 -0
- package/project/shared/elapsed.go +14 -0
- package/project/shared/eol_other.go +6 -0
- package/project/shared/eol_windows.go +4 -0
- package/project/shared/kleur.go +40 -0
- package/project/shared/util.go +183 -0
- package/scripts/build-with-plugins.sh +130 -0
- package/scripts/setup-third-party.sh +75 -0
- package/transformer/add_index_diagnostics.go +21 -0
- package/transformer/add_one_if_array_type.go +15 -0
- package/transformer/array_binding_pattern_contains_hoists.go +25 -0
- package/transformer/assert_never.go +24 -0
- package/transformer/assignment.go +64 -0
- package/transformer/call_macros.go +70 -0
- package/transformer/check_variable_hoist.go +125 -0
- package/transformer/clean_module_name.go +28 -0
- package/transformer/constructor_macros.go +122 -0
- package/transformer/convert_to_indexable_expression.go +12 -0
- package/transformer/create_binary_from_operator.go +86 -0
- package/transformer/create_continue_statements.go +68 -0
- package/transformer/create_get_service.go +15 -0
- package/transformer/create_hoist_declaration.go +27 -0
- package/transformer/create_import_expression.go +220 -0
- package/transformer/create_string_from_literal.go +195 -0
- package/transformer/create_truthiness_checks.go +63 -0
- package/transformer/create_type_check.go +9 -0
- package/transformer/ensure_transform_order.go +53 -0
- package/transformer/expression_chain.go +27 -0
- package/transformer/expression_might_mutate.go +60 -0
- package/transformer/find_constructor.go +15 -0
- package/transformer/fixup_whitespace_and_decode_entities.go +377 -0
- package/transformer/get_accessor_for_binding_type.go +160 -0
- package/transformer/get_add_iterable_to_array_builder.go +363 -0
- package/transformer/get_assignable_value.go +15 -0
- package/transformer/get_constant_value_literal.go +20 -0
- package/transformer/get_declared_variables.go +34 -0
- package/transformer/get_extends_node.go +23 -0
- package/transformer/get_flags.go +21 -0
- package/transformer/get_kind_name.go +18 -0
- package/transformer/get_original_symbol_of_node.go +16 -0
- package/transformer/get_source_file_from_module_specifier.go +68 -0
- package/transformer/get_statements.go +12 -0
- package/transformer/has_multiple_definitions.go +18 -0
- package/transformer/hoist_functions_past_final_statement.go +43 -0
- package/transformer/identifier_macros.go +17 -0
- package/transformer/is_blocked_by_try_statement.go +19 -0
- package/transformer/is_method.go +104 -0
- package/transformer/is_symbol_mutable.go +26 -0
- package/transformer/is_symbol_of_value.go +11 -0
- package/transformer/is_used_as_statement.go +25 -0
- package/transformer/is_valid_method_index_without_call.go +36 -0
- package/transformer/macro_manager.go +92 -0
- package/transformer/object_accessor.go +32 -0
- package/transformer/offset.go +58 -0
- package/transformer/pointer.go +53 -0
- package/transformer/property_call_macros.go +1035 -0
- package/transformer/state.go +606 -0
- package/transformer/transform_array_assignment_pattern.go +65 -0
- package/transformer/transform_array_binding_pattern.go +49 -0
- package/transformer/transform_array_literal_expression.go +97 -0
- package/transformer/transform_await_expression.go +12 -0
- package/transformer/transform_binary_expression.go +221 -0
- package/transformer/transform_binding_name.go +25 -0
- package/transformer/transform_block.go +14 -0
- package/transformer/transform_boolean_literal.go +13 -0
- package/transformer/transform_break_statement.go +26 -0
- package/transformer/transform_call_expression.go +266 -0
- package/transformer/transform_class_constructor.go +125 -0
- package/transformer/transform_class_declaration.go +12 -0
- package/transformer/transform_class_expression.go +14 -0
- package/transformer/transform_class_like_declaration.go +355 -0
- package/transformer/transform_conditional_expression.go +70 -0
- package/transformer/transform_continue_statement.go +30 -0
- package/transformer/transform_decorators.go +253 -0
- package/transformer/transform_delete_expression.go +17 -0
- package/transformer/transform_do_statement.go +39 -0
- package/transformer/transform_element_access_expression.go +114 -0
- package/transformer/transform_entity_name.go +21 -0
- package/transformer/transform_enum_declaration.go +135 -0
- package/transformer/transform_export_assignment.go +57 -0
- package/transformer/transform_export_declaration.go +150 -0
- package/transformer/transform_expression.go +120 -0
- package/transformer/transform_expression_statement.go +71 -0
- package/transformer/transform_for_of_statement.go +461 -0
- package/transformer/transform_for_statement.go +599 -0
- package/transformer/transform_function_declaration.go +84 -0
- package/transformer/transform_function_expression.go +48 -0
- package/transformer/transform_identifier.go +194 -0
- package/transformer/transform_if_statement.go +44 -0
- package/transformer/transform_import_declaration.go +146 -0
- package/transformer/transform_import_equals_declaration.go +46 -0
- package/transformer/transform_import_expression.go +36 -0
- package/transformer/transform_initializer.go +22 -0
- package/transformer/transform_interpolated_string_part.go +12 -0
- package/transformer/transform_jsx.go +41 -0
- package/transformer/transform_jsx_attributes.go +110 -0
- package/transformer/transform_jsx_children.go +50 -0
- package/transformer/transform_jsx_element.go +14 -0
- package/transformer/transform_jsx_expression.go +20 -0
- package/transformer/transform_jsx_fragment.go +42 -0
- package/transformer/transform_jsx_self_closing_element.go +13 -0
- package/transformer/transform_jsx_tag_name.go +53 -0
- package/transformer/transform_logical.go +160 -0
- package/transformer/transform_logical_or_coalescing_assignment_expression.go +103 -0
- package/transformer/transform_method_declaration.go +102 -0
- package/transformer/transform_module_declaration.go +143 -0
- package/transformer/transform_new_expression.go +27 -0
- package/transformer/transform_no_substitution_template_literal.go +16 -0
- package/transformer/transform_numeric_literal.go +15 -0
- package/transformer/transform_object_assignment_pattern.go +86 -0
- package/transformer/transform_object_binding_pattern.go +49 -0
- package/transformer/transform_object_literal_expression.go +107 -0
- package/transformer/transform_omitted_expression.go +9 -0
- package/transformer/transform_optional_chain.go +312 -0
- package/transformer/transform_parameters.go +124 -0
- package/transformer/transform_parenthesized_expression.go +17 -0
- package/transformer/transform_property_access_expression.go +37 -0
- package/transformer/transform_property_declaration.go +33 -0
- package/transformer/transform_property_name.go +20 -0
- package/transformer/transform_return_statement.go +83 -0
- package/transformer/transform_source_file.go +230 -0
- package/transformer/transform_spread_element.go +35 -0
- package/transformer/transform_statement.go +84 -0
- package/transformer/transform_statement_list.go +90 -0
- package/transformer/transform_string_literal.go +22 -0
- package/transformer/transform_super_keyword.go +9 -0
- package/transformer/transform_switch_statement.go +145 -0
- package/transformer/transform_tagged_template_expression.go +34 -0
- package/transformer/transform_template_expression.go +35 -0
- package/transformer/transform_this_expression.go +33 -0
- package/transformer/transform_throw_statement.go +18 -0
- package/transformer/transform_try_statement.go +192 -0
- package/transformer/transform_type_expression.go +12 -0
- package/transformer/transform_unary_expression.go +79 -0
- package/transformer/transform_variable_statement.go +178 -0
- package/transformer/transform_void_expression.go +13 -0
- package/transformer/transform_while_statement.go +34 -0
- package/transformer/transform_writable.go +69 -0
- package/transformer/transform_yield_expression.go +56 -0
- package/transformer/type_guards.go +30 -0
- package/transformer/types.go +381 -0
- package/transformer/validate_identifier.go +17 -0
- package/transformer/validate_method_assignment.go +106 -0
- package/transformer/validate_not_any.go +37 -0
- package/transformer/wrap_expression_statement.go +20 -0
- package/transformer/wrap_return_if_lua_tuple.go +63 -0
- package/transformer/wrap_statements_as_generator.go +22 -0
- package/tsbridge/affected.go +141 -0
- package/tsbridge/config.go +71 -0
- package/tsbridge/diagnostics.go +128 -0
- package/tsbridge/doc.go +16 -0
- package/tsbridge/macros.go +207 -0
- package/tsbridge/macrospec.go +30 -0
- package/tsbridge/pool.go +142 -0
- package/tsbridge/project.go +137 -0
- package/tsbridge/reprint.go +99 -0
- package/tsbridge/update.go +128 -0
- package/watch/poll.go +126 -0
- package/watch/session.go +651 -0
- package/watch/watch.go +180 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/isBlockedByTryStatement.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
6
|
+
|
|
7
|
+
func isReturnBlockedByTryStatement(node *ast.Node) bool {
|
|
8
|
+
ancestor := ast.FindAncestor(node, func(ancestor *ast.Node) bool {
|
|
9
|
+
return ast.IsTryStatement(ancestor) || ast.IsFunctionLikeDeclaration(ancestor)
|
|
10
|
+
})
|
|
11
|
+
return ancestor != nil && ast.IsTryStatement(ancestor)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
func isBreakBlockedByTryStatement(node *ast.Node) bool {
|
|
15
|
+
ancestor := ast.FindAncestor(node, func(ancestor *ast.Node) bool {
|
|
16
|
+
return ast.IsTryStatement(ancestor) || ast.IsIterationStatement(ancestor, false) || ast.IsSwitchStatement(ancestor)
|
|
17
|
+
})
|
|
18
|
+
return ancestor != nil && ast.IsTryStatement(ancestor)
|
|
19
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/isMethod.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
7
|
+
"github.com/microsoft/TypeScript/tsc/internal/checker"
|
|
8
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
func getThisParameter(parameters []*ast.Node) *ast.Node {
|
|
12
|
+
if len(parameters) > 0 {
|
|
13
|
+
name := parameters[0].Name()
|
|
14
|
+
if ast.IsIdentifier(name) && ast.IsThisIdentifier(name) {
|
|
15
|
+
return name
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return nil
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
func isMethodDeclaration(state *TransformState, node *ast.Node) bool {
|
|
22
|
+
if ast.IsFunctionLike(node) {
|
|
23
|
+
thisParam := getThisParameter(node.Parameters())
|
|
24
|
+
if thisParam != nil {
|
|
25
|
+
return state.GetType(thisParam).Flags()&checker.TypeFlagsVoid == 0
|
|
26
|
+
} else {
|
|
27
|
+
// namespace declare functions with `this` arg defined (i.e. utf8)
|
|
28
|
+
if ast.IsFunctionDeclaration(node) {
|
|
29
|
+
return false
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if ast.IsMethodDeclaration(node) || ast.IsMethodSignatureDeclaration(node) {
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// for some reason, FunctionExpressions within ObjectLiteralExpressions are implicitly methods
|
|
37
|
+
if ast.IsFunctionExpression(node) {
|
|
38
|
+
parent := SkipUpwards(node).Parent
|
|
39
|
+
if ast.IsPropertyAssignment(parent) {
|
|
40
|
+
grandparent := SkipUpwards(parent).Parent
|
|
41
|
+
if ast.IsObjectLiteralExpression(grandparent) {
|
|
42
|
+
return true
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
func isMethodInner(state *TransformState, node *ast.Node, t *checker.Type) bool {
|
|
54
|
+
hasMethodDefinition := false
|
|
55
|
+
hasCallbackDefinition := false
|
|
56
|
+
|
|
57
|
+
for _, callSignature := range state.Checker.GetCallSignatures(t) {
|
|
58
|
+
var thisValueDeclaration *ast.Node
|
|
59
|
+
if thisParameter := callSignature.ThisParameter(); thisParameter != nil {
|
|
60
|
+
thisValueDeclaration = thisParameter.ValueDeclaration()
|
|
61
|
+
}
|
|
62
|
+
if thisValueDeclaration != nil {
|
|
63
|
+
if state.GetType(thisValueDeclaration).Flags()&checker.TypeFlagsVoid == 0 {
|
|
64
|
+
hasMethodDefinition = true
|
|
65
|
+
} else {
|
|
66
|
+
hasCallbackDefinition = true
|
|
67
|
+
}
|
|
68
|
+
} else if callSignature.Declaration() != nil {
|
|
69
|
+
if isMethodDeclaration(state, callSignature.Declaration()) {
|
|
70
|
+
hasMethodDefinition = true
|
|
71
|
+
} else {
|
|
72
|
+
hasCallbackDefinition = true
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if hasMethodDefinition && hasCallbackDefinition {
|
|
78
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoMixedTypeCall.At(shared.AtNode(node)))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return hasMethodDefinition
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
func isMethodFromType(state *TransformState, node *ast.Node, t *checker.Type) bool {
|
|
85
|
+
result := false
|
|
86
|
+
|
|
87
|
+
walkTypes(t, func(u *checker.Type) {
|
|
88
|
+
// `result ||= getOrSetDefault(...)`: once result is true, later members are neither checked nor cached
|
|
89
|
+
if u.Symbol() != nil && !result {
|
|
90
|
+
value, ok := state.Multi.IsMethodCache[u.Symbol()]
|
|
91
|
+
if !ok {
|
|
92
|
+
value = isMethodInner(state, node, u)
|
|
93
|
+
state.Multi.IsMethodCache[u.Symbol()] = value
|
|
94
|
+
}
|
|
95
|
+
result = value
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
return result
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
func isMethod(state *TransformState, node *ast.Node) bool {
|
|
103
|
+
return isMethodFromType(state, node, state.GetType(node))
|
|
104
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/isSymbolMutable.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
6
|
+
|
|
7
|
+
func isSymbolMutable(state *TransformState, idSymbol *ast.Symbol) bool {
|
|
8
|
+
if value, ok := state.Multi.IsDefinedAsLetCache[idSymbol]; ok {
|
|
9
|
+
return value
|
|
10
|
+
}
|
|
11
|
+
value := func() bool {
|
|
12
|
+
if idSymbol.ValueDeclaration() != nil {
|
|
13
|
+
if ast.IsParameterDeclaration(idSymbol.ValueDeclaration()) {
|
|
14
|
+
return true
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
varDecList := GetAncestor(idSymbol.ValueDeclaration(), ast.IsVariableDeclarationList)
|
|
18
|
+
if varDecList != nil {
|
|
19
|
+
return varDecList.Flags&ast.NodeFlagsLet != 0
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return false
|
|
23
|
+
}()
|
|
24
|
+
state.Multi.IsDefinedAsLetCache[idSymbol] = value
|
|
25
|
+
return value
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/isSymbolOfValue.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
6
|
+
|
|
7
|
+
// isSymbolOfValue checks that the symbol represents something with a value at runtime.
|
|
8
|
+
// Thus excluding types and const enums.
|
|
9
|
+
func isSymbolOfValue(symbol *ast.Symbol) bool {
|
|
10
|
+
return symbol.Flags()&ast.SymbolFlagsValue != 0 && symbol.Flags()&ast.SymbolFlagsConstEnum == 0
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/isUsedAsStatement.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
6
|
+
|
|
7
|
+
func isUsedAsStatement(expression *ast.Node) bool {
|
|
8
|
+
child := SkipUpwards(expression)
|
|
9
|
+
parent := child.Parent
|
|
10
|
+
|
|
11
|
+
if ast.IsExpressionStatement(parent) {
|
|
12
|
+
return true
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// if part of for statement definition, except if used as the condition
|
|
16
|
+
if ast.IsForStatement(parent) && parent.AsForStatement().Condition != child {
|
|
17
|
+
return true
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if ast.IsDeleteExpression(parent) && isUsedAsStatement(parent) {
|
|
21
|
+
return true
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/isValidMethodIndexWithoutCall.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
6
|
+
|
|
7
|
+
func isValidMethodIndexWithoutCall(state *TransformState, node *ast.Node) bool {
|
|
8
|
+
parent := node.Parent
|
|
9
|
+
// a.b !== undefined
|
|
10
|
+
if ast.IsBinaryExpression(parent) {
|
|
11
|
+
return true
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// !a.b
|
|
15
|
+
if ast.IsPrefixUnaryExpression(parent) {
|
|
16
|
+
return true
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// typeIs/typeOf macros
|
|
20
|
+
if ast.IsCallExpression(parent) {
|
|
21
|
+
expType := state.Checker.GetNonOptionalType(state.GetType(parent.Expression()))
|
|
22
|
+
symbol := getFirstDefinedSymbol(state, expType)
|
|
23
|
+
if symbol != nil {
|
|
24
|
+
// Upstream compares against CALL_MACROS.typeIs and typeOf. Go funcs aren't comparable, so this compares names.
|
|
25
|
+
macro := state.Services.MacroManager.CallMacro(symbol)
|
|
26
|
+
if
|
|
27
|
+
// typeIs will be a TypeError if usage is not the first argument
|
|
28
|
+
macro == "typeIs" ||
|
|
29
|
+
macro == "typeOf" {
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/classes/MacroManager.ts, the lookup side, and of src/TSTransformer/macros/types.ts, which
|
|
4
|
+
// can't have a file of its own because types.go is util/types.ts. The symbol side is in tsbridge/macros.go: it maps a
|
|
5
|
+
// symbol to a macro's name, and the tables here map that name to the macro.
|
|
6
|
+
|
|
7
|
+
import (
|
|
8
|
+
"sync"
|
|
9
|
+
|
|
10
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
11
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
12
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
type identifierMacro func(state *TransformState, node *ast.Node) luau.Expression
|
|
16
|
+
|
|
17
|
+
type constructorMacro func(state *TransformState, node *ast.Node) luau.Expression
|
|
18
|
+
|
|
19
|
+
type callMacro func(state *TransformState, node *ast.Node, expression luau.Expression, args []luau.Expression) luau.Expression
|
|
20
|
+
|
|
21
|
+
// propertyCallMacro is an alias, so that runCallMacro can take either kind of macro, as upstream's does.
|
|
22
|
+
type propertyCallMacro = callMacro
|
|
23
|
+
|
|
24
|
+
// macroTables holds the macros by name. It's shared between workers and is read-only once built.
|
|
25
|
+
type macroTables struct {
|
|
26
|
+
identifiers map[string]identifierMacro
|
|
27
|
+
calls map[string]callMacro
|
|
28
|
+
constructors map[string]constructorMacro
|
|
29
|
+
propertyCalls map[string]propertyCallMacro
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// These have no initializer on purpose: the macros reach transformExpression, which reaches the lookups below, and
|
|
33
|
+
// that would be an initialization cycle.
|
|
34
|
+
var (
|
|
35
|
+
macroTablesOnce sync.Once
|
|
36
|
+
macroTablesValue *macroTables
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
func getMacroTables() *macroTables {
|
|
40
|
+
macroTablesOnce.Do(func() {
|
|
41
|
+
macroTablesValue = ¯oTables{
|
|
42
|
+
identifiers: identifierMacros(),
|
|
43
|
+
calls: callMacros(),
|
|
44
|
+
constructors: constructorMacros(),
|
|
45
|
+
propertyCalls: propertyCallMacros(),
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
return macroTablesValue
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
func getSymbolOrThrow(state *TransformState, name string) *ast.Symbol {
|
|
52
|
+
symbol := state.Services.MacroManager.Symbol(name)
|
|
53
|
+
if symbol == nil {
|
|
54
|
+
panic(shared.AssertionMessage(""))
|
|
55
|
+
}
|
|
56
|
+
return symbol
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
func getIdentifierMacro(state *TransformState, symbol *ast.Symbol) identifierMacro {
|
|
60
|
+
name := state.Services.MacroManager.IdentifierMacro(symbol)
|
|
61
|
+
if name == "" {
|
|
62
|
+
return nil
|
|
63
|
+
}
|
|
64
|
+
return getMacroTables().identifiers[name]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
func getCallMacro(state *TransformState, symbol *ast.Symbol) callMacro {
|
|
68
|
+
name := state.Services.MacroManager.CallMacro(symbol)
|
|
69
|
+
if name == "" {
|
|
70
|
+
return nil
|
|
71
|
+
}
|
|
72
|
+
return getMacroTables().calls[name]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
func getConstructorMacro(state *TransformState, symbol *ast.Symbol) constructorMacro {
|
|
76
|
+
name := state.Services.MacroManager.ConstructorMacro(symbol)
|
|
77
|
+
if name == "" {
|
|
78
|
+
return nil
|
|
79
|
+
}
|
|
80
|
+
return getMacroTables().constructors[name]
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
func getPropertyCallMacro(state *TransformState, symbol *ast.Symbol) propertyCallMacro {
|
|
84
|
+
name := state.Services.MacroManager.PropertyCallMacro(symbol)
|
|
85
|
+
if name == "" {
|
|
86
|
+
if state.Services.MacroManager.IsUnimplementedMacroMethod(symbol) {
|
|
87
|
+
panic(shared.AssertionMessage("Macro " + symbol.Parent().Name() + "." + symbol.Name() + "() is not implemented!"))
|
|
88
|
+
}
|
|
89
|
+
return nil
|
|
90
|
+
}
|
|
91
|
+
return getMacroTables().propertyCalls[name]
|
|
92
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/binding/objectAccessor.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
7
|
+
"github.com/microsoft/TypeScript/tsc/internal/checker"
|
|
8
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
9
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
func objectAccessor(state *TransformState, parentID luau.AnyIdentifier, t *checker.Type, name *ast.Node) luau.Expression {
|
|
13
|
+
addIndexDiagnostics(state, name, state.GetType(name))
|
|
14
|
+
|
|
15
|
+
if ast.IsIdentifier(name) {
|
|
16
|
+
return luau.NewProperty(parentID, name.Text())
|
|
17
|
+
} else if ast.IsComputedPropertyName(name) {
|
|
18
|
+
return luau.Create(&luau.ComputedIndexExpression{
|
|
19
|
+
Expression: parentID,
|
|
20
|
+
Index: addOneIfArrayType(state, t, transformExpression(state, name.Expression())),
|
|
21
|
+
})
|
|
22
|
+
} else if ast.IsNumericLiteral(name) || ast.IsStringLiteral(name) || ast.IsNoSubstitutionTemplateLiteral(name) {
|
|
23
|
+
return luau.Create(&luau.ComputedIndexExpression{
|
|
24
|
+
Expression: parentID,
|
|
25
|
+
Index: transformExpression(state, name),
|
|
26
|
+
})
|
|
27
|
+
} else if ast.IsPrivateIdentifier(name) {
|
|
28
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoPrivateIdentifier.At(shared.AtNode(name)))
|
|
29
|
+
return luau.NewNone()
|
|
30
|
+
}
|
|
31
|
+
return assertNever(name, "objectAccessor")
|
|
32
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/offset.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"math"
|
|
7
|
+
|
|
8
|
+
"github.com/microsoft/TypeScript/tsc/internal/jsnum"
|
|
9
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
// getLiteralNumberValue returns false where JS returns undefined. NaN is a value here, as in JS.
|
|
13
|
+
func getLiteralNumberValue(expression luau.Expression) (float64, bool) {
|
|
14
|
+
switch e := expression.(type) {
|
|
15
|
+
case *luau.NumberLiteral:
|
|
16
|
+
return float64(jsnum.FromString(e.Value)), true
|
|
17
|
+
case *luau.UnaryExpression:
|
|
18
|
+
if e.Operator == luau.UnaryMinus {
|
|
19
|
+
if innerValue, ok := getLiteralNumberValue(e.Expression); ok {
|
|
20
|
+
return -innerValue, true
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return 0, false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func offset(expression luau.Expression, value float64) luau.Expression {
|
|
28
|
+
if value == 0 {
|
|
29
|
+
return expression
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// this special case handles when the expression is a binary expression with a number literal on its right
|
|
33
|
+
// i.e. array[offset - 1 + 1] -> array[offset]
|
|
34
|
+
if binary, ok := expression.(*luau.BinaryExpression); ok && (binary.Operator == luau.BinaryAdd || binary.Operator == luau.BinarySub) {
|
|
35
|
+
if rightValue, ok := getLiteralNumberValue(binary.Right); ok {
|
|
36
|
+
sign := 1.0
|
|
37
|
+
if binary.Operator == luau.BinarySub {
|
|
38
|
+
sign = -1
|
|
39
|
+
}
|
|
40
|
+
newRightValue := rightValue + value*sign
|
|
41
|
+
if newRightValue == 0 {
|
|
42
|
+
return binary.Left
|
|
43
|
+
} else {
|
|
44
|
+
return luau.NewBinary(binary.Left, binary.Operator, luau.NewNumber(newRightValue))
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if literalValue, ok := getLiteralNumberValue(expression); ok {
|
|
50
|
+
return luau.NewNumber(literalValue + value)
|
|
51
|
+
} else {
|
|
52
|
+
operator := luau.BinarySub
|
|
53
|
+
if value > 0 {
|
|
54
|
+
operator = luau.BinaryAdd
|
|
55
|
+
}
|
|
56
|
+
return luau.NewBinary(expression, operator, luau.NewNumber(math.Abs(value)))
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/pointer.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
6
|
+
|
|
7
|
+
// pointer is Pointer<T>. value holds a *luau.Map, a *luau.Array or an identifier. disableMapInline and
|
|
8
|
+
// disableArrayInline change it, so pointers are passed by reference, as in JS.
|
|
9
|
+
type pointer struct {
|
|
10
|
+
name string
|
|
11
|
+
value luau.Expression
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type mapPointer = pointer
|
|
15
|
+
type arrayPointer = pointer
|
|
16
|
+
|
|
17
|
+
func createMapPointer(name string) *mapPointer {
|
|
18
|
+
return &mapPointer{name, luau.NewMap()}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
func createArrayPointer(name string) *arrayPointer {
|
|
22
|
+
return &arrayPointer{name, luau.NewArray()}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
func assignToMapPointer(state *TransformState, ptr *pointer, left luau.Expression, right luau.Expression) {
|
|
26
|
+
if value, ok := ptr.value.(*luau.Map); ok {
|
|
27
|
+
value.Fields.Push(luau.Create(&luau.MapField{
|
|
28
|
+
Index: left,
|
|
29
|
+
Value: right,
|
|
30
|
+
}))
|
|
31
|
+
} else {
|
|
32
|
+
state.Prereq(luau.Create(&luau.Assignment{
|
|
33
|
+
Left: luau.Create(&luau.ComputedIndexExpression{
|
|
34
|
+
Expression: ptr.value.(luau.IndexableExpression),
|
|
35
|
+
Index: left,
|
|
36
|
+
}),
|
|
37
|
+
Operator: luau.AssignEqual,
|
|
38
|
+
Right: right,
|
|
39
|
+
}))
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
func disableMapInline(state *TransformState, ptr *mapPointer) {
|
|
44
|
+
if luau.IsMap(ptr.value) {
|
|
45
|
+
ptr.value = state.PushToVar(ptr.value, ptr.name)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
func disableArrayInline(state *TransformState, ptr *arrayPointer) {
|
|
50
|
+
if luau.IsArray(ptr.value) {
|
|
51
|
+
ptr.value = state.PushToVar(ptr.value, ptr.name)
|
|
52
|
+
}
|
|
53
|
+
}
|