@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
package/luau/create.go
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
package luau
|
|
2
|
+
|
|
3
|
+
import "sync/atomic"
|
|
4
|
+
|
|
5
|
+
// Ported from src/LuauAST/impl/create.ts.
|
|
6
|
+
|
|
7
|
+
// Create finishes a node built as a struct literal: it gives every child
|
|
8
|
+
// (including list elements) this node as its parent and returns the node.
|
|
9
|
+
//
|
|
10
|
+
// Same rule as upstream `create`: a child that already has a parent is
|
|
11
|
+
// replaced by a shallow clone whose parent is the new node (so the original
|
|
12
|
+
// keeps its old parent); a child without a parent is adopted in place.
|
|
13
|
+
//
|
|
14
|
+
// luau.Create(&luau.BinaryExpression{Left: a, Operator: "+", Right: b})
|
|
15
|
+
func Create[T Node](node T) T {
|
|
16
|
+
node.adopt()
|
|
17
|
+
return node
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// adopt for a single child node field.
|
|
21
|
+
func adopt[T Node](parent Node, child T) T {
|
|
22
|
+
var c Node = child
|
|
23
|
+
if c == nil {
|
|
24
|
+
return child
|
|
25
|
+
}
|
|
26
|
+
if c.Parent() != nil {
|
|
27
|
+
clone := c.shallowClone()
|
|
28
|
+
clone.SetParent(parent)
|
|
29
|
+
return clone.(T)
|
|
30
|
+
}
|
|
31
|
+
c.SetParent(parent)
|
|
32
|
+
return child
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// adoptList adopts every element of a list field. A nil list is skipped.
|
|
36
|
+
func adoptList(parent Node, list *List) {
|
|
37
|
+
if list == nil {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
for ln := list.Head; ln != nil; ln = ln.Next {
|
|
41
|
+
if ln.Value.Parent() != nil {
|
|
42
|
+
clone := ln.Value.shallowClone()
|
|
43
|
+
clone.SetParent(parent)
|
|
44
|
+
ln.Value = clone
|
|
45
|
+
} else {
|
|
46
|
+
ln.Value.SetParent(parent)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// adoptNodeOrList adopts a field that is either a node or a list of nodes.
|
|
52
|
+
func adoptNodeOrList(parent Node, v NodeOrList) NodeOrList {
|
|
53
|
+
switch x := v.(type) {
|
|
54
|
+
case nil:
|
|
55
|
+
return nil
|
|
56
|
+
case *List:
|
|
57
|
+
adoptList(parent, x)
|
|
58
|
+
return x
|
|
59
|
+
case Node:
|
|
60
|
+
return adopt(parent, x)
|
|
61
|
+
}
|
|
62
|
+
return v
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ---------------------------------------------------------------------------
|
|
66
|
+
// shallowClone: `{ ...node }`
|
|
67
|
+
|
|
68
|
+
func (n *None) shallowClone() Node { c := *n; return &c }
|
|
69
|
+
func (n *NilLiteral) shallowClone() Node { c := *n; return &c }
|
|
70
|
+
func (n *FalseLiteral) shallowClone() Node { c := *n; return &c }
|
|
71
|
+
func (n *TrueLiteral) shallowClone() Node { c := *n; return &c }
|
|
72
|
+
func (n *NumberLiteral) shallowClone() Node { c := *n; return &c }
|
|
73
|
+
func (n *StringLiteral) shallowClone() Node { c := *n; return &c }
|
|
74
|
+
func (n *VarArgsLiteral) shallowClone() Node { c := *n; return &c }
|
|
75
|
+
func (n *FunctionExpression) shallowClone() Node { c := *n; return &c }
|
|
76
|
+
func (n *Identifier) shallowClone() Node { c := *n; return &c }
|
|
77
|
+
func (n *TemporaryIdentifier) shallowClone() Node { c := *n; return &c }
|
|
78
|
+
func (n *ComputedIndexExpression) shallowClone() Node { c := *n; return &c }
|
|
79
|
+
func (n *PropertyAccessExpression) shallowClone() Node { c := *n; return &c }
|
|
80
|
+
func (n *CallExpression) shallowClone() Node { c := *n; return &c }
|
|
81
|
+
func (n *MethodCallExpression) shallowClone() Node { c := *n; return &c }
|
|
82
|
+
func (n *ParenthesizedExpression) shallowClone() Node { c := *n; return &c }
|
|
83
|
+
func (n *BinaryExpression) shallowClone() Node { c := *n; return &c }
|
|
84
|
+
func (n *UnaryExpression) shallowClone() Node { c := *n; return &c }
|
|
85
|
+
func (n *IfExpression) shallowClone() Node { c := *n; return &c }
|
|
86
|
+
func (n *InterpolatedString) shallowClone() Node { c := *n; return &c }
|
|
87
|
+
func (n *Array) shallowClone() Node { c := *n; return &c }
|
|
88
|
+
func (n *Map) shallowClone() Node { c := *n; return &c }
|
|
89
|
+
func (n *Set) shallowClone() Node { c := *n; return &c }
|
|
90
|
+
func (n *MixedTable) shallowClone() Node { c := *n; return &c }
|
|
91
|
+
func (n *Assignment) shallowClone() Node { c := *n; return &c }
|
|
92
|
+
func (n *BreakStatement) shallowClone() Node { c := *n; return &c }
|
|
93
|
+
func (n *CallStatement) shallowClone() Node { c := *n; return &c }
|
|
94
|
+
func (n *ContinueStatement) shallowClone() Node { c := *n; return &c }
|
|
95
|
+
func (n *DoStatement) shallowClone() Node { c := *n; return &c }
|
|
96
|
+
func (n *WhileStatement) shallowClone() Node { c := *n; return &c }
|
|
97
|
+
func (n *RepeatStatement) shallowClone() Node { c := *n; return &c }
|
|
98
|
+
func (n *IfStatement) shallowClone() Node { c := *n; return &c }
|
|
99
|
+
func (n *NumericForStatement) shallowClone() Node { c := *n; return &c }
|
|
100
|
+
func (n *ForStatement) shallowClone() Node { c := *n; return &c }
|
|
101
|
+
func (n *FunctionDeclaration) shallowClone() Node { c := *n; return &c }
|
|
102
|
+
func (n *MethodDeclaration) shallowClone() Node { c := *n; return &c }
|
|
103
|
+
func (n *VariableDeclaration) shallowClone() Node { c := *n; return &c }
|
|
104
|
+
func (n *ReturnStatement) shallowClone() Node { c := *n; return &c }
|
|
105
|
+
func (n *Comment) shallowClone() Node { c := *n; return &c }
|
|
106
|
+
func (n *MapField) shallowClone() Node { c := *n; return &c }
|
|
107
|
+
func (n *InterpolatedStringPart) shallowClone() Node { c := *n; return &c }
|
|
108
|
+
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
// adopt: one per kind, children in declaration order (see doc.go)
|
|
111
|
+
|
|
112
|
+
func (n *None) adopt() {}
|
|
113
|
+
func (n *NilLiteral) adopt() {}
|
|
114
|
+
func (n *FalseLiteral) adopt() {}
|
|
115
|
+
func (n *TrueLiteral) adopt() {}
|
|
116
|
+
func (n *NumberLiteral) adopt() {}
|
|
117
|
+
func (n *StringLiteral) adopt() {}
|
|
118
|
+
func (n *VarArgsLiteral) adopt() {}
|
|
119
|
+
func (n *Identifier) adopt() {}
|
|
120
|
+
func (n *TemporaryIdentifier) adopt() {}
|
|
121
|
+
func (n *BreakStatement) adopt() {}
|
|
122
|
+
func (n *ContinueStatement) adopt() {}
|
|
123
|
+
func (n *Comment) adopt() {}
|
|
124
|
+
|
|
125
|
+
func (n *InterpolatedStringPart) adopt() {}
|
|
126
|
+
|
|
127
|
+
func (n *FunctionExpression) adopt() {
|
|
128
|
+
adoptList(n, n.Parameters)
|
|
129
|
+
adoptList(n, n.Statements)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
func (n *ComputedIndexExpression) adopt() {
|
|
133
|
+
n.Expression = adopt(n, n.Expression)
|
|
134
|
+
n.Index = adopt(n, n.Index)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
func (n *PropertyAccessExpression) adopt() {
|
|
138
|
+
n.Expression = adopt(n, n.Expression)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
func (n *CallExpression) adopt() {
|
|
142
|
+
n.Expression = adopt(n, n.Expression)
|
|
143
|
+
adoptList(n, n.Args)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
func (n *MethodCallExpression) adopt() {
|
|
147
|
+
n.Expression = adopt(n, n.Expression)
|
|
148
|
+
adoptList(n, n.Args)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
func (n *ParenthesizedExpression) adopt() {
|
|
152
|
+
n.Expression = adopt(n, n.Expression)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
func (n *BinaryExpression) adopt() {
|
|
156
|
+
n.Left = adopt(n, n.Left)
|
|
157
|
+
n.Right = adopt(n, n.Right)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
func (n *UnaryExpression) adopt() {
|
|
161
|
+
n.Expression = adopt(n, n.Expression)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
func (n *IfExpression) adopt() {
|
|
165
|
+
n.Condition = adopt(n, n.Condition)
|
|
166
|
+
n.Expression = adopt(n, n.Expression)
|
|
167
|
+
n.Alternative = adopt(n, n.Alternative)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
func (n *InterpolatedString) adopt() { adoptList(n, n.Parts) }
|
|
171
|
+
func (n *Array) adopt() { adoptList(n, n.Members) }
|
|
172
|
+
func (n *Map) adopt() { adoptList(n, n.Fields) }
|
|
173
|
+
func (n *Set) adopt() { adoptList(n, n.Members) }
|
|
174
|
+
func (n *MixedTable) adopt() { adoptList(n, n.Fields) }
|
|
175
|
+
|
|
176
|
+
func (n *Assignment) adopt() {
|
|
177
|
+
n.Left = adoptNodeOrList(n, n.Left)
|
|
178
|
+
n.Right = adoptNodeOrList(n, n.Right)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
func (n *CallStatement) adopt() {
|
|
182
|
+
n.Expression = adopt(n, n.Expression)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
func (n *DoStatement) adopt() { adoptList(n, n.Statements) }
|
|
186
|
+
|
|
187
|
+
func (n *WhileStatement) adopt() {
|
|
188
|
+
n.Condition = adopt(n, n.Condition)
|
|
189
|
+
adoptList(n, n.Statements)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
func (n *RepeatStatement) adopt() {
|
|
193
|
+
n.Condition = adopt(n, n.Condition)
|
|
194
|
+
adoptList(n, n.Statements)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
func (n *IfStatement) adopt() {
|
|
198
|
+
n.Condition = adopt(n, n.Condition)
|
|
199
|
+
adoptList(n, n.Statements)
|
|
200
|
+
n.ElseBody = adoptNodeOrList(n, n.ElseBody)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
func (n *NumericForStatement) adopt() {
|
|
204
|
+
n.ID = adopt(n, n.ID)
|
|
205
|
+
n.Start = adopt(n, n.Start)
|
|
206
|
+
n.End = adopt(n, n.End)
|
|
207
|
+
n.Step = adopt(n, n.Step)
|
|
208
|
+
adoptList(n, n.Statements)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
func (n *ForStatement) adopt() {
|
|
212
|
+
adoptList(n, n.IDs)
|
|
213
|
+
n.Expression = adopt(n, n.Expression)
|
|
214
|
+
adoptList(n, n.Statements)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
func (n *FunctionDeclaration) adopt() {
|
|
218
|
+
n.Name = adopt(n, n.Name)
|
|
219
|
+
adoptList(n, n.Parameters)
|
|
220
|
+
adoptList(n, n.Statements)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
func (n *MethodDeclaration) adopt() {
|
|
224
|
+
n.Expression = adopt(n, n.Expression)
|
|
225
|
+
adoptList(n, n.Parameters)
|
|
226
|
+
adoptList(n, n.Statements)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
func (n *VariableDeclaration) adopt() {
|
|
230
|
+
n.Left = adoptNodeOrList(n, n.Left)
|
|
231
|
+
n.Right = adoptNodeOrList(n, n.Right)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
func (n *ReturnStatement) adopt() {
|
|
235
|
+
n.Expression = adoptNodeOrList(n, n.Expression)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
func (n *MapField) adopt() {
|
|
239
|
+
n.Index = adopt(n, n.Index)
|
|
240
|
+
n.Value = adopt(n, n.Value)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ---------------------------------------------------------------------------
|
|
244
|
+
// Helper constructors
|
|
245
|
+
|
|
246
|
+
var lastTempID atomic.Int64
|
|
247
|
+
|
|
248
|
+
// NewTempID creates a new temporary identifier. Each call takes the next id
|
|
249
|
+
// from a process-wide counter (upstream tempId; the name may be "").
|
|
250
|
+
func NewTempID(name string) *TemporaryIdentifier {
|
|
251
|
+
return Create(&TemporaryIdentifier{Name: name, ID: int(lastTempID.Add(1) - 1)})
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// NewNone creates a new None node.
|
|
255
|
+
func NewNone() *None { return Create(&None{}) }
|
|
256
|
+
|
|
257
|
+
// NewNil creates a new nil literal node.
|
|
258
|
+
func NewNil() *NilLiteral { return Create(&NilLiteral{}) }
|
|
259
|
+
|
|
260
|
+
// NewBool creates a *TrueLiteral or *FalseLiteral.
|
|
261
|
+
func NewBool(value bool) Expression {
|
|
262
|
+
if value {
|
|
263
|
+
return Create(&TrueLiteral{})
|
|
264
|
+
}
|
|
265
|
+
return Create(&FalseLiteral{})
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// NewNumber creates a number literal, or a negated one for negative values,
|
|
269
|
+
// using the JavaScript String(number) text (upstream number). NaN panics (upstream
|
|
270
|
+
// recurses forever).
|
|
271
|
+
func NewNumber(value float64) Expression {
|
|
272
|
+
if value >= 0 {
|
|
273
|
+
return Create(&NumberLiteral{Value: jsNumberToString(value)})
|
|
274
|
+
}
|
|
275
|
+
if value != value {
|
|
276
|
+
panic("luau.NewNumber: NaN")
|
|
277
|
+
}
|
|
278
|
+
abs := -value
|
|
279
|
+
return Create(&UnaryExpression{Operator: UnaryMinus, Expression: NewNumber(abs)})
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// NewString creates a string literal node. The value is rendered verbatim, so
|
|
283
|
+
// it must already be escaped.
|
|
284
|
+
func NewString(value string) *StringLiteral { return Create(&StringLiteral{Value: value}) }
|
|
285
|
+
|
|
286
|
+
// NewID creates a new identifier node.
|
|
287
|
+
func NewID(name string) *Identifier { return Create(&Identifier{Name: name}) }
|
|
288
|
+
|
|
289
|
+
// NewComment creates a new comment node.
|
|
290
|
+
func NewComment(text string) *Comment { return Create(&Comment{Text: text}) }
|
|
291
|
+
|
|
292
|
+
// NewArray creates a new array node.
|
|
293
|
+
func NewArray(members ...Expression) *Array {
|
|
294
|
+
return Create(&Array{Members: MakeList(members...)})
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// NewSet creates a new set node.
|
|
298
|
+
func NewSet(members ...Expression) *Set {
|
|
299
|
+
return Create(&Set{Members: MakeList(members...)})
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// KeyValue is one key/value mapping for NewMap.
|
|
303
|
+
type KeyValue struct {
|
|
304
|
+
Index Expression
|
|
305
|
+
Value Expression
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// NewMap creates a new map node from key/value mappings.
|
|
309
|
+
func NewMap(fields ...KeyValue) *Map {
|
|
310
|
+
nodes := make([]Node, len(fields))
|
|
311
|
+
for i, f := range fields {
|
|
312
|
+
nodes[i] = Create(&MapField{Index: f.Index, Value: f.Value})
|
|
313
|
+
}
|
|
314
|
+
return Create(&Map{Fields: MakeList(nodes...)})
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// MixedField is one entry for NewMixedTable: a key/value mapping when Key is
|
|
318
|
+
// not nil, otherwise a positional value.
|
|
319
|
+
type MixedField struct {
|
|
320
|
+
Key Expression
|
|
321
|
+
Value Expression
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// NewMixedTable creates a new mixed table node.
|
|
325
|
+
func NewMixedTable(fields ...MixedField) *MixedTable {
|
|
326
|
+
nodes := make([]Node, len(fields))
|
|
327
|
+
for i, f := range fields {
|
|
328
|
+
if f.Key != nil {
|
|
329
|
+
nodes[i] = Create(&MapField{Index: f.Key, Value: f.Value})
|
|
330
|
+
} else {
|
|
331
|
+
nodes[i] = f.Value
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return Create(&MixedTable{Fields: MakeList(nodes...)})
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
func NewBinary(left Expression, operator BinaryOperator, right Expression) *BinaryExpression {
|
|
338
|
+
return Create(&BinaryExpression{Left: left, Operator: operator, Right: right})
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
func NewUnary(operator UnaryOperator, expression Expression) *UnaryExpression {
|
|
342
|
+
return Create(&UnaryExpression{Operator: operator, Expression: expression})
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
func NewProperty(expression IndexableExpression, name string) *PropertyAccessExpression {
|
|
346
|
+
return Create(&PropertyAccessExpression{Expression: expression, Name: name})
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
func NewCall(expression IndexableExpression, args ...Expression) *CallExpression {
|
|
350
|
+
return Create(&CallExpression{Expression: expression, Args: MakeList(args...)})
|
|
351
|
+
}
|
package/luau/doc.go
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Package luau is a Go port of @roblox-ts/luau-ast 2.0.0: the Luau AST used by
|
|
2
|
+
// the roblox-ts compiler, plus the renderer that turns it into Luau source.
|
|
3
|
+
//
|
|
4
|
+
// Upstream: https://github.com/roblox-ts/luau-ast at commit
|
|
5
|
+
// ef18d8bccbe5a31435ab56683a81817a7f22ac51 (tag 2.0.0, the npm gitHead of
|
|
6
|
+
// @roblox-ts/luau-ast@2.0.0). Output must match the JS renderer byte for byte.
|
|
7
|
+
//
|
|
8
|
+
// # File map
|
|
9
|
+
//
|
|
10
|
+
// Go file upstream source (src/...)
|
|
11
|
+
// kinds.go LuauAST/impl/enums.ts, LuauAST/util/getKindName.ts
|
|
12
|
+
// operators.go LuauAST/types/operators.ts
|
|
13
|
+
// nodes.go LuauAST/types/nodes.ts, LuauAST/types/mapping.ts
|
|
14
|
+
// create.go LuauAST/impl/create.ts
|
|
15
|
+
// list.go LuauAST/impl/List.ts
|
|
16
|
+
// typeguards.go LuauAST/impl/typeGuards.ts
|
|
17
|
+
// globals.go LuauAST/impl/globals.ts
|
|
18
|
+
// strings.go LuauAST/impl/strings.ts
|
|
19
|
+
// util.go LuauAST/util/{assert,isMetamethod,isReservedClassField,
|
|
20
|
+
// isReservedIdentifier,isValidIdentifier,isValidNumberLiteral}.ts
|
|
21
|
+
// render.go LuauRenderer/render.ts
|
|
22
|
+
// render_state.go LuauRenderer/RenderState.ts
|
|
23
|
+
// solve_temp_ids.go LuauRenderer/solveTempIds.ts
|
|
24
|
+
// visit.go LuauRenderer/util/visit.ts
|
|
25
|
+
// render_util.go LuauRenderer/util/{getEnding,getSafeBracketEquals,needsParentheses,
|
|
26
|
+
// renderArguments,renderParameters,renderStatements}.ts
|
|
27
|
+
// render_indexable.go LuauRenderer/nodes/expressions/indexable/*.ts
|
|
28
|
+
// render_expressions.go LuauRenderer/nodes/expressions/*.ts
|
|
29
|
+
// render_statements.go LuauRenderer/nodes/statements/*.ts
|
|
30
|
+
// render_fields.go LuauRenderer/nodes/fields/*.ts
|
|
31
|
+
// jsnum.go no upstream file: ECMAScript Number() and String(number)
|
|
32
|
+
//
|
|
33
|
+
// # Naming map (upstream to Go)
|
|
34
|
+
//
|
|
35
|
+
// luau.create(kind, fields) Create(&Kind{...})
|
|
36
|
+
// luau.tempId, none, nil, bool, number, string, id, comment, array, set, map,
|
|
37
|
+
// mixedTable, binary, unary, property, call
|
|
38
|
+
// NewTempID, NewNone, NewNil, NewBool, NewNumber, NewString,
|
|
39
|
+
// NewID, NewComment, NewArray, NewSet, NewMap, NewMixedTable,
|
|
40
|
+
// NewBinary, NewUnary, NewProperty, NewCall
|
|
41
|
+
// luau.list.x(list, ...) methods on *List (list.Push(v)), plus MakeList and NewList
|
|
42
|
+
// luau.globals, luau.strings Globals, Strings
|
|
43
|
+
// luau.SyntaxKind.X KindX
|
|
44
|
+
// luau.isX(node) IsX(node)
|
|
45
|
+
// render, renderAST, renderStatements, solveTempIds, RenderState
|
|
46
|
+
// Render, RenderAST, RenderStatements, SolveTempIDs, RenderState
|
|
47
|
+
//
|
|
48
|
+
// # Semantics preserved on purpose
|
|
49
|
+
//
|
|
50
|
+
// Nodes are mutable pointers that carry a parent pointer. Create adopts its
|
|
51
|
+
// children: a child that already has a parent is shallow-cloned (struct copy,
|
|
52
|
+
// so lists and grandchildren stay shared) and the clone is stored in the new
|
|
53
|
+
// node; a child without a parent is adopted in place. List helpers mutate and
|
|
54
|
+
// share list nodes exactly like upstream (PushList marks the other list
|
|
55
|
+
// read-only but leaves its head and tail pointing into the merged chain, Shift
|
|
56
|
+
// does not clear the removed node's Next, and so on). The transformer depends
|
|
57
|
+
// on this, so do not "fix" it.
|
|
58
|
+
//
|
|
59
|
+
// Fields are adopted in the order they are declared in nodes.go. Upstream uses
|
|
60
|
+
// the property order of the object literal at each call site. This only
|
|
61
|
+
// matters when the same parentless node appears in two fields of one Create
|
|
62
|
+
// call, and in that case both orders render identically.
|
|
63
|
+
//
|
|
64
|
+
// # Concurrency
|
|
65
|
+
//
|
|
66
|
+
// Globals and Strings are shared mutable nodes, as upstream: the first Create
|
|
67
|
+
// that uses one adopts it in place, later uses clone it. Concurrent Create
|
|
68
|
+
// calls that use them race on that parent pointer. A parallel transformer
|
|
69
|
+
// should either serialise first use (use each global once at start-up) or
|
|
70
|
+
// clone with ShallowClone before use.
|
|
71
|
+
package luau
|
package/luau/globals.go
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
package luau
|
|
2
|
+
|
|
3
|
+
import "reflect"
|
|
4
|
+
|
|
5
|
+
// Ported from src/LuauAST/impl/globals.ts and src/LuauAST/impl/strings.ts.
|
|
6
|
+
//
|
|
7
|
+
// These are shared mutable nodes, exactly like upstream: the first Create
|
|
8
|
+
// that receives one adopts it in place, and every later use is cloned (see the
|
|
9
|
+
// concurrency note in doc.go). The initialisation order below mirrors the
|
|
10
|
+
// upstream file so the same use is the "first".
|
|
11
|
+
|
|
12
|
+
var (
|
|
13
|
+
coroutineID = NewID("coroutine")
|
|
14
|
+
mathID = NewID("math")
|
|
15
|
+
stringID = NewID("string")
|
|
16
|
+
tableID = NewID("table")
|
|
17
|
+
utf8ID = NewID("utf8")
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
type globalsType struct {
|
|
21
|
+
G *Identifier
|
|
22
|
+
TS *Identifier
|
|
23
|
+
Assert *Identifier
|
|
24
|
+
Bit32 *Identifier
|
|
25
|
+
Coroutine struct{ Yield *PropertyAccessExpression }
|
|
26
|
+
Error *Identifier
|
|
27
|
+
Exports *Identifier
|
|
28
|
+
Getmetatable *Identifier
|
|
29
|
+
Ipairs *Identifier
|
|
30
|
+
Next *Identifier
|
|
31
|
+
Pairs *Identifier
|
|
32
|
+
Pcall *Identifier
|
|
33
|
+
Require *Identifier
|
|
34
|
+
Script *Identifier
|
|
35
|
+
Select *Identifier
|
|
36
|
+
Self *Identifier
|
|
37
|
+
Setmetatable *Identifier
|
|
38
|
+
String struct {
|
|
39
|
+
Byte, Find, Format, Gmatch, Gsub, Lower, Match, Rep, Reverse, Split, Sub, Upper *PropertyAccessExpression
|
|
40
|
+
}
|
|
41
|
+
Super *Identifier
|
|
42
|
+
Table struct {
|
|
43
|
+
Clear, Clone, Concat, Create, Find, Freeze, Insert, Isfrozen, Maxn, Move, Pack, Remove, Sort,
|
|
44
|
+
Unpack *PropertyAccessExpression
|
|
45
|
+
}
|
|
46
|
+
Utf8 struct{ Charpattern, Codes *PropertyAccessExpression }
|
|
47
|
+
Math struct{ Min *PropertyAccessExpression }
|
|
48
|
+
Tostring *Identifier
|
|
49
|
+
Type *Identifier
|
|
50
|
+
Typeof *Identifier
|
|
51
|
+
Unpack *Identifier
|
|
52
|
+
|
|
53
|
+
// roblox
|
|
54
|
+
Game *Identifier
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Globals holds the commonly used Luau global identifiers and members.
|
|
58
|
+
var Globals = newGlobals()
|
|
59
|
+
|
|
60
|
+
func newGlobals() *globalsType {
|
|
61
|
+
g := &globalsType{}
|
|
62
|
+
g.G = NewID("_G")
|
|
63
|
+
g.TS = NewID("TS")
|
|
64
|
+
g.Assert = NewID("assert")
|
|
65
|
+
g.Bit32 = NewID("bit32")
|
|
66
|
+
g.Coroutine.Yield = NewProperty(coroutineID, "yield")
|
|
67
|
+
g.Error = NewID("error")
|
|
68
|
+
g.Exports = NewID("exports")
|
|
69
|
+
g.Getmetatable = NewID("getmetatable")
|
|
70
|
+
g.Ipairs = NewID("ipairs")
|
|
71
|
+
g.Next = NewID("next")
|
|
72
|
+
g.Pairs = NewID("pairs")
|
|
73
|
+
g.Pcall = NewID("pcall")
|
|
74
|
+
g.Require = NewID("require")
|
|
75
|
+
g.Script = NewID("script")
|
|
76
|
+
g.Select = NewID("select")
|
|
77
|
+
g.Self = NewID("self")
|
|
78
|
+
g.Setmetatable = NewID("setmetatable")
|
|
79
|
+
g.String.Byte = NewProperty(stringID, "byte")
|
|
80
|
+
g.String.Find = NewProperty(stringID, "find")
|
|
81
|
+
g.String.Format = NewProperty(stringID, "format")
|
|
82
|
+
g.String.Gmatch = NewProperty(stringID, "gmatch")
|
|
83
|
+
g.String.Gsub = NewProperty(stringID, "gsub")
|
|
84
|
+
g.String.Lower = NewProperty(stringID, "lower")
|
|
85
|
+
g.String.Match = NewProperty(stringID, "match")
|
|
86
|
+
g.String.Rep = NewProperty(stringID, "rep")
|
|
87
|
+
g.String.Reverse = NewProperty(stringID, "reverse")
|
|
88
|
+
g.String.Split = NewProperty(stringID, "split")
|
|
89
|
+
g.String.Sub = NewProperty(stringID, "sub")
|
|
90
|
+
g.String.Upper = NewProperty(stringID, "upper")
|
|
91
|
+
g.Super = NewID("super")
|
|
92
|
+
g.Table.Clear = NewProperty(tableID, "clear")
|
|
93
|
+
g.Table.Clone = NewProperty(tableID, "clone")
|
|
94
|
+
g.Table.Concat = NewProperty(tableID, "concat")
|
|
95
|
+
g.Table.Create = NewProperty(tableID, "create")
|
|
96
|
+
g.Table.Find = NewProperty(tableID, "find")
|
|
97
|
+
g.Table.Freeze = NewProperty(tableID, "freeze")
|
|
98
|
+
g.Table.Insert = NewProperty(tableID, "insert")
|
|
99
|
+
g.Table.Isfrozen = NewProperty(tableID, "isfrozen")
|
|
100
|
+
g.Table.Maxn = NewProperty(tableID, "maxn")
|
|
101
|
+
g.Table.Move = NewProperty(tableID, "move")
|
|
102
|
+
g.Table.Pack = NewProperty(tableID, "pack")
|
|
103
|
+
g.Table.Remove = NewProperty(tableID, "remove")
|
|
104
|
+
g.Table.Sort = NewProperty(tableID, "sort")
|
|
105
|
+
g.Table.Unpack = NewProperty(tableID, "unpack")
|
|
106
|
+
g.Utf8.Charpattern = NewProperty(utf8ID, "charpattern")
|
|
107
|
+
g.Utf8.Codes = NewProperty(utf8ID, "codes")
|
|
108
|
+
g.Math.Min = NewProperty(mathID, "min")
|
|
109
|
+
g.Tostring = NewID("tostring")
|
|
110
|
+
g.Type = NewID("type")
|
|
111
|
+
g.Typeof = NewID("typeof")
|
|
112
|
+
g.Unpack = NewID("unpack")
|
|
113
|
+
g.Game = NewID("game")
|
|
114
|
+
return g
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
type stringsType struct {
|
|
118
|
+
// metamethods
|
|
119
|
+
Index *StringLiteral // "__index"
|
|
120
|
+
Tostring *StringLiteral // "__tostring"
|
|
121
|
+
Mode *StringLiteral // "__mode"
|
|
122
|
+
K *StringLiteral // "k", used for __mode
|
|
123
|
+
|
|
124
|
+
// types
|
|
125
|
+
Number *StringLiteral // "number"
|
|
126
|
+
Table *StringLiteral // "table"
|
|
127
|
+
|
|
128
|
+
// opcall
|
|
129
|
+
Success *StringLiteral // "success"
|
|
130
|
+
Value *StringLiteral // "value"
|
|
131
|
+
Error *StringLiteral // "error"
|
|
132
|
+
|
|
133
|
+
// other
|
|
134
|
+
Comma *StringLiteral // ", ", used for ReadonlyArray.join()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// PinShared gives every node in Globals and Strings a parent, so Create always clones them and never writes to them.
|
|
138
|
+
// Call it once before starting workers, and those nodes are then safe to use from any goroutine. See doc.go. It changes
|
|
139
|
+
// nothing in the output: upstream's first Create adopts the node in place, and that node renders the same as a clone.
|
|
140
|
+
func PinShared() {
|
|
141
|
+
pinned := &Comment{}
|
|
142
|
+
var visit func(value reflect.Value)
|
|
143
|
+
visit = func(value reflect.Value) {
|
|
144
|
+
switch value.Kind() {
|
|
145
|
+
case reflect.Pointer:
|
|
146
|
+
if value.IsNil() {
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
if node, ok := value.Interface().(Node); ok {
|
|
150
|
+
if node.Parent() == nil {
|
|
151
|
+
node.SetParent(pinned)
|
|
152
|
+
}
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
visit(value.Elem())
|
|
156
|
+
case reflect.Struct:
|
|
157
|
+
for i := range value.NumField() {
|
|
158
|
+
visit(value.Field(i))
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
visit(reflect.ValueOf(Globals))
|
|
163
|
+
visit(reflect.ValueOf(Strings))
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Strings holds commonly used string literal nodes.
|
|
167
|
+
var Strings = &stringsType{
|
|
168
|
+
Index: NewString("__index"),
|
|
169
|
+
Tostring: NewString("__tostring"),
|
|
170
|
+
Mode: NewString("__mode"),
|
|
171
|
+
K: NewString("k"),
|
|
172
|
+
Number: NewString("number"),
|
|
173
|
+
Table: NewString("table"),
|
|
174
|
+
Success: NewString("success"),
|
|
175
|
+
Value: NewString("value"),
|
|
176
|
+
Error: NewString("error"),
|
|
177
|
+
Comma: NewString(", "),
|
|
178
|
+
}
|