@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,220 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/createImportExpression.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"slices"
|
|
7
|
+
"strings"
|
|
8
|
+
|
|
9
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
10
|
+
"github.com/microsoft/TypeScript/tsc/internal/tspath"
|
|
11
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
12
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
|
|
13
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/rojoresolver"
|
|
14
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
func getAbsoluteImport(moduleRbxPath rojoresolver.RbxPath) []luau.Expression {
|
|
18
|
+
pathExpressions := []luau.Expression{}
|
|
19
|
+
if len(moduleRbxPath) == 0 || moduleRbxPath[0] == "" {
|
|
20
|
+
panic(shared.AssertionMessage(""))
|
|
21
|
+
}
|
|
22
|
+
serviceName := moduleRbxPath[0]
|
|
23
|
+
pathExpressions = append(pathExpressions, createGetService(serviceName))
|
|
24
|
+
for i := 1; i < len(moduleRbxPath); i++ {
|
|
25
|
+
pathExpressions = append(pathExpressions, luau.NewString(moduleRbxPath[i]))
|
|
26
|
+
}
|
|
27
|
+
return pathExpressions
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
func getRelativeImport(sourceRbxPath rojoresolver.RbxPath, moduleRbxPath rojoresolver.RbxPath) []luau.Expression {
|
|
31
|
+
relativePath := rojoresolver.Relative(sourceRbxPath, moduleRbxPath)
|
|
32
|
+
|
|
33
|
+
// create descending path pieces
|
|
34
|
+
path := []string{}
|
|
35
|
+
i := 0
|
|
36
|
+
for i < len(relativePath) && relativePath[i] == rojoresolver.RbxPathParent {
|
|
37
|
+
path = append(path, shared.ParentField)
|
|
38
|
+
i++
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
pathExpressions := []luau.Expression{propertyAccessExpressionChain(luau.Globals.Script, path)}
|
|
42
|
+
|
|
43
|
+
// create descending path pieces
|
|
44
|
+
for ; i < len(relativePath); i++ {
|
|
45
|
+
pathPart := relativePath[i]
|
|
46
|
+
if pathPart == rojoresolver.RbxPathParent {
|
|
47
|
+
panic(shared.AssertionMessage(""))
|
|
48
|
+
}
|
|
49
|
+
pathExpressions = append(pathExpressions, luau.NewString(pathPart))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return pathExpressions
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func validateModule(state *TransformState, scope string) bool {
|
|
56
|
+
scopedModules := nodepath.Native.Join(state.Data.NodeModulesPath, scope)
|
|
57
|
+
for _, typeRoot := range state.Options.TypeRoots {
|
|
58
|
+
if nodepath.Native.Normalize(scopedModules) == nodepath.Native.Normalize(typeRoot.AsString()) {
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
func findRelativeRbxPath(moduleOutPath string, pkgRojoResolvers []*rojoresolver.Resolver) rojoresolver.RbxPath {
|
|
66
|
+
for _, pkgRojoResolver := range pkgRojoResolvers {
|
|
67
|
+
relativeRbxPath := pkgRojoResolver.GetRbxPathFromFilePath(moduleOutPath)
|
|
68
|
+
if relativeRbxPath != nil {
|
|
69
|
+
return relativeRbxPath
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return nil
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
func getNodeModulesImportParts(state *TransformState, sourceFile *ast.SourceFile, moduleSpecifier *ast.Node, moduleOutPath string) []luau.Expression {
|
|
76
|
+
moduleScope := strings.Split(nodepath.Native.Relative(state.Data.NodeModulesPath, moduleOutPath), nodepath.Native.Sep)[0]
|
|
77
|
+
if moduleScope == "" {
|
|
78
|
+
panic(shared.AssertionMessage(""))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if !strings.HasPrefix(moduleScope, "@") {
|
|
82
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoUnscopedModule.At(shared.AtNode(moduleSpecifier)))
|
|
83
|
+
return []luau.Expression{luau.NewNone()}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if !validateModule(state, moduleScope) {
|
|
87
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoInvalidModule.At(shared.AtNode(moduleSpecifier)))
|
|
88
|
+
return []luau.Expression{luau.NewNone()}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if state.Compile.ProjectType == shared.ProjectTypePackage {
|
|
92
|
+
relativeRbxPath := findRelativeRbxPath(moduleOutPath, state.Compile.PkgRojoResolvers)
|
|
93
|
+
if relativeRbxPath == nil {
|
|
94
|
+
state.Diagnostic.AddDiagnostic(
|
|
95
|
+
shared.Errors.NoRojoData.At(shared.AtNode(moduleSpecifier), nodepath.Native.Relative(state.Data.ProjectPath, moduleOutPath), true),
|
|
96
|
+
)
|
|
97
|
+
return []luau.Expression{luau.NewNone()}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if len(relativeRbxPath) == 0 || relativeRbxPath[0] == "" {
|
|
101
|
+
panic(shared.AssertionMessage(""))
|
|
102
|
+
}
|
|
103
|
+
moduleName := relativeRbxPath[0]
|
|
104
|
+
|
|
105
|
+
return []luau.Expression{
|
|
106
|
+
propertyAccessExpressionChain(
|
|
107
|
+
luau.NewCall(state.TS(moduleSpecifier.Parent, "getModule"),
|
|
108
|
+
luau.Globals.Script,
|
|
109
|
+
luau.NewString(moduleScope),
|
|
110
|
+
luau.NewString(moduleName),
|
|
111
|
+
),
|
|
112
|
+
relativeRbxPath[1:],
|
|
113
|
+
),
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
moduleRbxPath := state.RojoResolver().GetRbxPathFromFilePath(moduleOutPath)
|
|
117
|
+
if moduleRbxPath == nil {
|
|
118
|
+
state.Diagnostic.AddDiagnostic(
|
|
119
|
+
shared.Errors.NoRojoData.At(shared.AtNode(moduleSpecifier), nodepath.Native.Relative(state.Data.ProjectPath, moduleOutPath), true),
|
|
120
|
+
)
|
|
121
|
+
return []luau.Expression{luau.NewNone()}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
indexOfScope := slices.Index(moduleRbxPath, moduleScope)
|
|
125
|
+
if indexOfScope <= 0 || moduleRbxPath[indexOfScope-1] != shared.NodeModules {
|
|
126
|
+
state.Diagnostic.AddDiagnostic(
|
|
127
|
+
shared.Errors.NoPackageImportWithoutScope.At(
|
|
128
|
+
shared.AtNode(moduleSpecifier),
|
|
129
|
+
nodepath.Native.Relative(state.Data.ProjectPath, moduleOutPath),
|
|
130
|
+
moduleRbxPath,
|
|
131
|
+
),
|
|
132
|
+
)
|
|
133
|
+
return []luau.Expression{luau.NewNone()}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return getProjectImportParts(state, sourceFile, moduleSpecifier, moduleOutPath, moduleRbxPath)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
func getProjectImportParts(state *TransformState, sourceFile *ast.SourceFile, moduleSpecifier *ast.Node, moduleOutPath string, moduleRbxPath rojoresolver.RbxPath) []luau.Expression {
|
|
140
|
+
moduleRbxType := state.RojoResolver().GetRbxTypeFromFilePath(moduleOutPath)
|
|
141
|
+
if moduleRbxType == rojoresolver.RbxTypeScript || moduleRbxType == rojoresolver.RbxTypeLocalScript {
|
|
142
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoNonModuleImport.At(shared.AtNode(moduleSpecifier)))
|
|
143
|
+
return []luau.Expression{luau.NewNone()}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
sourceOutPath := state.PathTranslator.GetOutputPath(sourceFile.FileName().AsString())
|
|
147
|
+
sourceRbxPath := state.RojoResolver().GetRbxPathFromFilePath(sourceOutPath)
|
|
148
|
+
if sourceRbxPath == nil {
|
|
149
|
+
state.Diagnostic.AddDiagnostic(
|
|
150
|
+
shared.Errors.NoRojoData.At(shared.AtNode(sourceFile.AsNode()), nodepath.Native.Relative(state.Data.ProjectPath, sourceOutPath), false),
|
|
151
|
+
)
|
|
152
|
+
return []luau.Expression{luau.NewNone()}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if state.Compile.ProjectType == shared.ProjectTypeGame {
|
|
156
|
+
// in the case of `import("")`, don't do network type check
|
|
157
|
+
// as the call may be guarded by runtime RunService checks
|
|
158
|
+
if !ast.IsImportCall(moduleSpecifier.Parent) &&
|
|
159
|
+
state.RojoResolver().GetNetworkType(moduleRbxPath) == rojoresolver.NetworkTypeServer &&
|
|
160
|
+
state.RojoResolver().GetNetworkType(sourceRbxPath) != rojoresolver.NetworkTypeServer {
|
|
161
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoServerImport.At(shared.AtNode(moduleSpecifier)))
|
|
162
|
+
return []luau.Expression{luau.NewNone()}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
fileRelation := state.RojoResolver().GetFileRelation(sourceRbxPath, moduleRbxPath)
|
|
166
|
+
if fileRelation == rojoresolver.FileRelationOutToOut || fileRelation == rojoresolver.FileRelationInToOut {
|
|
167
|
+
return getAbsoluteImport(moduleRbxPath)
|
|
168
|
+
} else if fileRelation == rojoresolver.FileRelationInToIn {
|
|
169
|
+
return getRelativeImport(sourceRbxPath, moduleRbxPath)
|
|
170
|
+
}
|
|
171
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoIsolatedImport.At(shared.AtNode(moduleSpecifier)))
|
|
172
|
+
return []luau.Expression{luau.NewNone()}
|
|
173
|
+
}
|
|
174
|
+
return getRelativeImport(sourceRbxPath, moduleRbxPath)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// isInsideNodeModules is ts.isInsideNodeModules from TS 5.5.3.
|
|
178
|
+
func isInsideNodeModules(fileOrDirectory string) bool {
|
|
179
|
+
return slices.Contains(tspath.GetPathComponents(fileOrDirectory), "node_modules")
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
func getImportParts(state *TransformState, sourceFile *ast.SourceFile, moduleSpecifier *ast.Node) []luau.Expression {
|
|
183
|
+
moduleFile := getSourceFileFromModuleSpecifier(state, moduleSpecifier)
|
|
184
|
+
if moduleFile == nil {
|
|
185
|
+
state.Diagnostic.AddDiagnostic(shared.Errors.NoModuleSpecifierFile.At(shared.AtNode(moduleSpecifier)))
|
|
186
|
+
return []luau.Expression{luau.NewNone()}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
virtualPath := state.GuessVirtualPath(moduleFile.FileName().AsString())
|
|
190
|
+
if virtualPath == "" {
|
|
191
|
+
virtualPath = moduleFile.FileName().AsString()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if isInsideNodeModules(virtualPath) {
|
|
195
|
+
mappedPath, ok := state.Compile.NodeModulesPathMapping[state.CaseSensitivity.Canonicalize(nodepath.Native.Normalize(virtualPath))]
|
|
196
|
+
if !ok {
|
|
197
|
+
mappedPath = virtualPath
|
|
198
|
+
}
|
|
199
|
+
moduleOutPath := state.PathTranslator.GetImportPath(
|
|
200
|
+
mappedPath,
|
|
201
|
+
/* isNodeModule */ true,
|
|
202
|
+
)
|
|
203
|
+
return getNodeModulesImportParts(state, sourceFile, moduleSpecifier, moduleOutPath)
|
|
204
|
+
}
|
|
205
|
+
moduleOutPath := state.PathTranslator.GetImportPath(virtualPath, false)
|
|
206
|
+
moduleRbxPath := state.RojoResolver().GetRbxPathFromFilePath(moduleOutPath)
|
|
207
|
+
if moduleRbxPath == nil {
|
|
208
|
+
state.Diagnostic.AddDiagnostic(
|
|
209
|
+
shared.Errors.NoRojoData.At(shared.AtNode(moduleSpecifier), nodepath.Native.Relative(state.Data.ProjectPath, moduleOutPath), false),
|
|
210
|
+
)
|
|
211
|
+
return []luau.Expression{luau.NewNone()}
|
|
212
|
+
}
|
|
213
|
+
return getProjectImportParts(state, sourceFile, moduleSpecifier, moduleOutPath, moduleRbxPath)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
func createImportExpression(state *TransformState, sourceFile *ast.SourceFile, moduleSpecifier *ast.Node) luau.IndexableExpression {
|
|
217
|
+
parts := getImportParts(state, sourceFile, moduleSpecifier)
|
|
218
|
+
parts = slices.Insert(parts, 0, luau.Expression(luau.Globals.Script))
|
|
219
|
+
return luau.NewCall(state.TS(moduleSpecifier.Parent, "import"), parts...)
|
|
220
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/createStringFromLiteral.ts, with the escape handling of upstream's master (#3033,
|
|
4
|
+
// issue #2910) split in two. See docs/known-differences.md.
|
|
5
|
+
//
|
|
6
|
+
// Always on, because JS 3.0.0's Luau for these fails to parse, so no working output can change:
|
|
7
|
+
// - \uXXXX, which Luau rejects because it needs \u{XXXX}, including surrogate pairs, which become one code point
|
|
8
|
+
// - a backslash and a line break in a template, which the renderer turns into a raw line break in a backtick string
|
|
9
|
+
// - \{ in a template, which the renderer turns into \\{ and so starts an interpolation that never ends
|
|
10
|
+
//
|
|
11
|
+
// A literal with any of those gets all of the rewrite. It couldn't run before, and half a fix would turn its parse error
|
|
12
|
+
// into a wrong value.
|
|
13
|
+
//
|
|
14
|
+
// Only with fixEmit, because the Luau parses and has the wrong value:
|
|
15
|
+
// - a backslash and a line break in a plain string, which keeps the backslash or the line break
|
|
16
|
+
// - a backslash and U+2028 or U+2029
|
|
17
|
+
// - identity escapes that mean something else in Luau, like \a and \z
|
|
18
|
+
// - line breaks in the source of a template, normalized to LF as JS does
|
|
19
|
+
|
|
20
|
+
import (
|
|
21
|
+
"strconv"
|
|
22
|
+
"strings"
|
|
23
|
+
"unicode/utf8"
|
|
24
|
+
|
|
25
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
26
|
+
"github.com/microsoft/TypeScript/tsc/internal/scanner"
|
|
27
|
+
"github.com/microsoft/TypeScript/tsc/internal/stringutil"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const (
|
|
31
|
+
templateEdge = len("`")
|
|
32
|
+
templateExpStart = len("${")
|
|
33
|
+
templateExpEnd = len("}")
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
// createStringFromLiteral returns the transformed text from a `ts.TemplateLiteralToken | ts.StringLiteral`.
|
|
37
|
+
// Cannot just use `node.text` because that converts `\\n` to be `\n`.
|
|
38
|
+
func createStringFromLiteral(state *TransformState, node *ast.Node) string {
|
|
39
|
+
text := literalText(node)
|
|
40
|
+
if state.Data.ProjectOptions.FixEmit || literalFailsToParse(node) {
|
|
41
|
+
return rewriteStringEscapes(text, ast.IsStringLiteral(node))
|
|
42
|
+
}
|
|
43
|
+
return text
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
func literalText(node *ast.Node) string {
|
|
47
|
+
text := scanner.GetSourceTextOfNodeFromSourceFile(ast.GetSourceFileOfNode(node), node, false)
|
|
48
|
+
if ast.IsStringLiteral(node) || ast.IsNoSubstitutionTemplateLiteral(node) {
|
|
49
|
+
text = stringutil.StripQuotes(text)
|
|
50
|
+
} else if ast.IsTemplateHead(node) {
|
|
51
|
+
// remove starting ` and ending ${
|
|
52
|
+
text = trimLiteralEdges(text, templateEdge, templateExpStart)
|
|
53
|
+
} else if ast.IsTemplateMiddle(node) {
|
|
54
|
+
// remove starting } and ending ${
|
|
55
|
+
text = trimLiteralEdges(text, templateExpEnd, templateExpStart)
|
|
56
|
+
} else if ast.IsTemplateTail(node) {
|
|
57
|
+
// remove starting } and ending `
|
|
58
|
+
text = trimLiteralEdges(text, templateExpEnd, templateEdge)
|
|
59
|
+
}
|
|
60
|
+
return text
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// literalFailsToParse says whether JS 3.0.0's Luau for this literal fails to parse. A template's tokens render as one
|
|
64
|
+
// string, so if one of them can't, the others have to be fixed along with it.
|
|
65
|
+
func literalFailsToParse(node *ast.Node) bool {
|
|
66
|
+
if ast.IsStringLiteral(node) {
|
|
67
|
+
text := literalText(node)
|
|
68
|
+
return !rendersAsLongString(text) && textFailsToParse(text, true)
|
|
69
|
+
}
|
|
70
|
+
if ast.IsNoSubstitutionTemplateLiteral(node) {
|
|
71
|
+
return textFailsToParse(literalText(node), false)
|
|
72
|
+
}
|
|
73
|
+
expression := node.Parent
|
|
74
|
+
if ast.IsTemplateSpan(expression) {
|
|
75
|
+
expression = expression.Parent
|
|
76
|
+
}
|
|
77
|
+
template := expression.AsTemplateExpression()
|
|
78
|
+
if textFailsToParse(literalText(template.Head), false) {
|
|
79
|
+
return true
|
|
80
|
+
}
|
|
81
|
+
for _, span := range template.TemplateSpans.Nodes {
|
|
82
|
+
if textFailsToParse(literalText(span.AsTemplateSpan().Literal), false) {
|
|
83
|
+
return true
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return false
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// textFailsToParse goes through whole escapes, so that the second backslash of \\u0041 doesn't start one.
|
|
90
|
+
func textFailsToParse(text string, isStringLiteral bool) bool {
|
|
91
|
+
for i := 0; i+1 < len(text); i++ {
|
|
92
|
+
if text[i] != '\\' {
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
i++
|
|
96
|
+
switch text[i] {
|
|
97
|
+
case 'u':
|
|
98
|
+
if isHex4At(text, i+1) {
|
|
99
|
+
return true
|
|
100
|
+
}
|
|
101
|
+
case '\n', '\r', '{':
|
|
102
|
+
if !isStringLiteral {
|
|
103
|
+
return true
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return false
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// trimLiteralEdges is text.slice(start, -end). The edges are ASCII, so bytes and UTF-16 units agree.
|
|
111
|
+
func trimLiteralEdges(text string, start int, end int) string {
|
|
112
|
+
if start >= len(text)-end {
|
|
113
|
+
return ""
|
|
114
|
+
}
|
|
115
|
+
return text[start : len(text)-end]
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// rendersAsLongString says whether renderStringLiteral uses [[...]] for this text, where escapes mean nothing to Luau,
|
|
119
|
+
// and so none of them can fail to parse.
|
|
120
|
+
func rendersAsLongString(text string) bool {
|
|
121
|
+
return strings.IndexByte(text, '\n') >= 0 || (strings.IndexByte(text, '"') >= 0 && strings.IndexByte(text, '\'') >= 0)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// isHex4At says whether four hex digits start at text[i].
|
|
125
|
+
func isHex4At(text string, i int) bool {
|
|
126
|
+
if i+4 > len(text) {
|
|
127
|
+
return false
|
|
128
|
+
}
|
|
129
|
+
for _, c := range []byte(text[i : i+4]) {
|
|
130
|
+
if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F') {
|
|
131
|
+
return false
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return true
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// surrogatePairAt returns the code point of \uD8XX\uDCXX, whose high half's digits start at text[i], or 0.
|
|
138
|
+
func surrogatePairAt(text string, i int) rune {
|
|
139
|
+
if !isHex4At(text, i) || i+10 > len(text) || text[i+4] != '\\' || text[i+5] != 'u' || !isHex4At(text, i+6) {
|
|
140
|
+
return 0
|
|
141
|
+
}
|
|
142
|
+
high, _ := strconv.ParseUint(text[i:i+4], 16, 32)
|
|
143
|
+
low, _ := strconv.ParseUint(text[i+6:i+10], 16, 32)
|
|
144
|
+
if high < 0xD800 || high > 0xDBFF || low < 0xDC00 || low > 0xDFFF {
|
|
145
|
+
return 0
|
|
146
|
+
}
|
|
147
|
+
return rune((high-0xD800)<<10 + (low - 0xDC00) + 0x10000)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// luauSharedEscapes are the characters after a backslash that mean the same to TS and Luau. Anything else after a
|
|
151
|
+
// backslash is the character itself in TS. Luau has \a for a bell and \z to skip whitespace.
|
|
152
|
+
const luauSharedEscapes = "0bfnrtvux\\\"'"
|
|
153
|
+
|
|
154
|
+
// rewriteStringEscapes is upstream's createStringFromLiteral, as one pass over whole escapes, so that the second backslash
|
|
155
|
+
// of \\u0041 doesn't start one.
|
|
156
|
+
func rewriteStringEscapes(text string, isStringLiteral bool) string {
|
|
157
|
+
// as TS normalizes physical line endings in templates. A plain string has them only after a backslash, and normalizing
|
|
158
|
+
// first means every kind of line break is one case below.
|
|
159
|
+
text = strings.ReplaceAll(strings.ReplaceAll(text, "\r\n", "\n"), "\r", "\n")
|
|
160
|
+
var result strings.Builder
|
|
161
|
+
result.Grow(len(text))
|
|
162
|
+
for i := 0; i < len(text); {
|
|
163
|
+
c := text[i]
|
|
164
|
+
if c != '\\' || i+1 >= len(text) {
|
|
165
|
+
result.WriteByte(c)
|
|
166
|
+
i++
|
|
167
|
+
continue
|
|
168
|
+
}
|
|
169
|
+
next, size := utf8.DecodeRuneInString(text[i+1:])
|
|
170
|
+
escape := text[i : i+1+size]
|
|
171
|
+
end := i + 1 + size
|
|
172
|
+
switch {
|
|
173
|
+
case next == 'u' && isHex4At(text, end):
|
|
174
|
+
if codePoint := surrogatePairAt(text, end); codePoint != 0 {
|
|
175
|
+
result.WriteString(`\u{` + strconv.FormatInt(int64(codePoint), 16) + `}`)
|
|
176
|
+
end += 10
|
|
177
|
+
} else {
|
|
178
|
+
result.WriteString(`\u{` + text[end:end+4] + `}`)
|
|
179
|
+
end += 4
|
|
180
|
+
}
|
|
181
|
+
case next == '\\' && !isStringLiteral && strings.HasPrefix(text[end:], "u{"):
|
|
182
|
+
// the renderer would read the second backslash of \\u{41} as the start of \u{41}, which would make {41} an
|
|
183
|
+
// interpolation.
|
|
184
|
+
result.WriteString(`\x5C`)
|
|
185
|
+
case next == '\n' || next == 0x2028 || next == 0x2029:
|
|
186
|
+
// adds no characters in TS
|
|
187
|
+
case strings.ContainsRune(luauSharedEscapes, next) || (next == '`' && !isStringLiteral):
|
|
188
|
+
result.WriteString(escape)
|
|
189
|
+
default:
|
|
190
|
+
result.WriteRune(next)
|
|
191
|
+
}
|
|
192
|
+
i = end
|
|
193
|
+
}
|
|
194
|
+
return result.String()
|
|
195
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/createTruthinessChecks.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/luau"
|
|
11
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
func willCreateTruthinessChecks(t *checker.Type) bool {
|
|
15
|
+
return isPossiblyType(t, isNumberLiteralType(0)) ||
|
|
16
|
+
isPossiblyType(t, isNaNType) ||
|
|
17
|
+
isPossiblyType(t, isEmptyStringType)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
func createTruthinessChecks(state *TransformState, exp luau.Expression, node *ast.Node) luau.Expression {
|
|
21
|
+
t := state.GetType(node)
|
|
22
|
+
isAssignableToZero := isPossiblyType(t, isNumberLiteralType(0))
|
|
23
|
+
isAssignableToNaN := isPossiblyType(t, isNaNType)
|
|
24
|
+
isAssignableToEmptyString := isPossiblyType(t, isEmptyStringType)
|
|
25
|
+
|
|
26
|
+
if isAssignableToZero || isAssignableToNaN || isAssignableToEmptyString {
|
|
27
|
+
exp = state.PushToVarIfComplex(exp, "value")
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
checks := []luau.Expression{}
|
|
31
|
+
|
|
32
|
+
if isAssignableToZero {
|
|
33
|
+
checks = append(checks, luau.NewBinary(exp, luau.BinaryNotEqual, luau.NewNumber(0)))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// workaround for https://github.com/microsoft/TypeScript/issues/32778
|
|
37
|
+
if isAssignableToZero || isAssignableToNaN {
|
|
38
|
+
checks = append(checks, luau.NewBinary(exp, luau.BinaryEqual, exp))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if isAssignableToEmptyString {
|
|
42
|
+
checks = append(checks, luau.NewBinary(exp, luau.BinaryNotEqual, luau.NewString("")))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
checks = append(checks, exp)
|
|
46
|
+
|
|
47
|
+
if state.Data.ProjectOptions.LogTruthyChanges &&
|
|
48
|
+
(isAssignableToZero || isAssignableToNaN || isAssignableToEmptyString) {
|
|
49
|
+
checkStrs := []string{}
|
|
50
|
+
if isAssignableToZero {
|
|
51
|
+
checkStrs = append(checkStrs, "0")
|
|
52
|
+
}
|
|
53
|
+
if isAssignableToZero || isAssignableToNaN {
|
|
54
|
+
checkStrs = append(checkStrs, "NaN")
|
|
55
|
+
}
|
|
56
|
+
if isAssignableToEmptyString {
|
|
57
|
+
checkStrs = append(checkStrs, `""`)
|
|
58
|
+
}
|
|
59
|
+
state.Diagnostic.AddDiagnostic(shared.Warnings.TruthyChange(strings.Join(checkStrs, ", ")).At(shared.AtNode(node)))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return binaryExpressionChain(checks, luau.BinaryAnd)
|
|
63
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/createTypeCheck.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
6
|
+
|
|
7
|
+
func createTypeCheck(expression luau.Expression, typeName *luau.StringLiteral) *luau.BinaryExpression {
|
|
8
|
+
return luau.NewBinary(luau.NewCall(luau.Globals.Type, expression), luau.BinaryEqual, typeName)
|
|
9
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/ensureTransformOrder.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
7
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// ensureTransformOrder takes an array of `ts.Expression` and transforms each, capturing prereqs. Returns the
|
|
11
|
+
// transformed nodes. Ensures the `luau.Expression` nodes execute in the same order as the `ts.Expression` nodes.
|
|
12
|
+
// A nil transformer means transformExpression.
|
|
13
|
+
func ensureTransformOrder(state *TransformState, nodes []*ast.Node, transformer func(state *TransformState, node *ast.Node) luau.Expression) []luau.Expression {
|
|
14
|
+
if transformer == nil {
|
|
15
|
+
transformer = transformExpression
|
|
16
|
+
}
|
|
17
|
+
expressions := make([]luau.Expression, len(nodes))
|
|
18
|
+
prereqsList := make([]*luau.List, len(nodes))
|
|
19
|
+
for i, node := range nodes {
|
|
20
|
+
expressions[i], prereqsList[i] = Capture(state, func() luau.Expression { return transformer(state, node) })
|
|
21
|
+
}
|
|
22
|
+
lastArgWithPrereqsIndex := -1
|
|
23
|
+
for i := len(prereqsList) - 1; i >= 0; i-- {
|
|
24
|
+
if !prereqsList[i].IsEmpty() {
|
|
25
|
+
lastArgWithPrereqsIndex = i
|
|
26
|
+
break
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
result := []luau.Expression{}
|
|
30
|
+
for i := range expressions {
|
|
31
|
+
expression, prereqs := expressions[i], prereqsList[i]
|
|
32
|
+
state.PrereqList(prereqs)
|
|
33
|
+
|
|
34
|
+
isConstVar := false
|
|
35
|
+
exp := nodes[i]
|
|
36
|
+
if ast.IsIdentifier(exp) {
|
|
37
|
+
symbol := state.Checker.GetSymbolAtLocation(exp)
|
|
38
|
+
if symbol != nil && !isSymbolMutable(state, symbol) {
|
|
39
|
+
isConstVar = true
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if i < lastArgWithPrereqsIndex &&
|
|
44
|
+
!luau.IsSimplePrimitive(expression) &&
|
|
45
|
+
!luau.IsTemporaryIdentifier(expression) &&
|
|
46
|
+
!isConstVar {
|
|
47
|
+
result = append(result, state.PushToVar(expression, "exp"))
|
|
48
|
+
} else {
|
|
49
|
+
result = append(result, expression)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return result
|
|
53
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/expressionChain.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
6
|
+
|
|
7
|
+
// binaryExpressionChain combines multiple luau.Expression objects into a chain of luau.BinaryExpressions
|
|
8
|
+
//
|
|
9
|
+
// i.e. using `and` as our operator, `[a, b, c]` -> `a and b and c`
|
|
10
|
+
func binaryExpressionChain(expressions []luau.Expression, operator luau.BinaryOperator) luau.Expression {
|
|
11
|
+
acc := expressions[0]
|
|
12
|
+
for _, current := range expressions[1:] {
|
|
13
|
+
acc = luau.NewBinary(acc, operator, current)
|
|
14
|
+
}
|
|
15
|
+
return acc
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// propertyAccessExpressionChain combines multiple strings into a chain of property accesses
|
|
19
|
+
//
|
|
20
|
+
// i.e. `["a", "b", "c"]` -> `exp.a.b.c`
|
|
21
|
+
func propertyAccessExpressionChain(expression luau.Expression, names []string) luau.IndexableExpression {
|
|
22
|
+
acc := convertToIndexableExpression(expression)
|
|
23
|
+
for _, current := range names {
|
|
24
|
+
acc = luau.NewProperty(acc, current)
|
|
25
|
+
}
|
|
26
|
+
return acc
|
|
27
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/expressionMightMutate.ts.
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
7
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/luau"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
func expressionMightMutate(state *TransformState, expression luau.Expression, node *ast.Node) bool {
|
|
11
|
+
if luau.IsTemporaryIdentifier(expression) {
|
|
12
|
+
// Assume tempIds are never re-assigned after being returned
|
|
13
|
+
// TODO: Is this actually safe to assume?
|
|
14
|
+
return false
|
|
15
|
+
} else if parenthesized, ok := expression.(*luau.ParenthesizedExpression); ok {
|
|
16
|
+
return expressionMightMutate(state, parenthesized.Expression, nil)
|
|
17
|
+
} else if luau.IsSimplePrimitive(expression) {
|
|
18
|
+
return false
|
|
19
|
+
} else if luau.IsFunctionExpression(expression) {
|
|
20
|
+
return false
|
|
21
|
+
} else if luau.IsVarArgsLiteral(expression) {
|
|
22
|
+
return false
|
|
23
|
+
} else if ifExpression, ok := expression.(*luau.IfExpression); ok {
|
|
24
|
+
return expressionMightMutate(state, ifExpression.Condition, nil) ||
|
|
25
|
+
expressionMightMutate(state, ifExpression.Expression, nil) ||
|
|
26
|
+
expressionMightMutate(state, ifExpression.Alternative, nil)
|
|
27
|
+
} else if binary, ok := expression.(*luau.BinaryExpression); ok {
|
|
28
|
+
return expressionMightMutate(state, binary.Left, nil) || expressionMightMutate(state, binary.Right, nil)
|
|
29
|
+
} else if unary, ok := expression.(*luau.UnaryExpression); ok {
|
|
30
|
+
return expressionMightMutate(state, unary.Expression, nil)
|
|
31
|
+
} else if array, ok := expression.(*luau.Array); ok {
|
|
32
|
+
return array.Members.Some(func(member luau.Node) bool {
|
|
33
|
+
return expressionMightMutate(state, member.(luau.Expression), nil)
|
|
34
|
+
})
|
|
35
|
+
} else if set, ok := expression.(*luau.Set); ok {
|
|
36
|
+
return set.Members.Some(func(member luau.Node) bool {
|
|
37
|
+
return expressionMightMutate(state, member.(luau.Expression), nil)
|
|
38
|
+
})
|
|
39
|
+
} else if mapExpression, ok := expression.(*luau.Map); ok {
|
|
40
|
+
return mapExpression.Fields.Some(func(value luau.Node) bool {
|
|
41
|
+
field := value.(*luau.MapField)
|
|
42
|
+
return expressionMightMutate(state, field.Index, nil) || expressionMightMutate(state, field.Value, nil)
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
if node != nil {
|
|
46
|
+
node = SkipDownwards(node)
|
|
47
|
+
if ast.IsIdentifier(node) {
|
|
48
|
+
symbol := state.Checker.GetSymbolAtLocation(node)
|
|
49
|
+
if symbol != nil && !isSymbolMutable(state, symbol) {
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Identifier
|
|
55
|
+
// ComputedIndexExpression
|
|
56
|
+
// PropertyAccessExpression
|
|
57
|
+
// CallExpression
|
|
58
|
+
// MethodCallExpression
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
package transformer
|
|
2
|
+
|
|
3
|
+
// Port of src/TSTransformer/util/findConstructor.ts.
|
|
4
|
+
|
|
5
|
+
import "github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
6
|
+
|
|
7
|
+
// findConstructor returns nil where JS returns undefined.
|
|
8
|
+
func findConstructor(node *ast.Node) *ast.Node {
|
|
9
|
+
for _, member := range node.Members() {
|
|
10
|
+
if ast.IsConstructorDeclaration(member) && member.Body() != nil {
|
|
11
|
+
return member
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return nil
|
|
15
|
+
}
|