@data-vegle/rbxts-go 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +181 -2
- package/README.rbxtsgo.md +169 -0
- package/THIRD_PARTY_NOTICES.md +745 -0
- package/cli/cli.go +454 -0
- package/compile/compile.go +388 -0
- package/compile/declarations.go +221 -0
- package/compile/incremental.go +349 -0
- package/compile/times.go +9 -0
- package/docs/plugin-api.md +243 -0
- package/go.mod +20 -0
- package/go.sum +24 -0
- package/internal/difftest/difftest.go +530 -0
- package/internal/plugintest/plugintest.go +181 -0
- package/luau/create.go +351 -0
- package/luau/doc.go +71 -0
- package/luau/globals.go +178 -0
- package/luau/jsnum.go +188 -0
- package/luau/kinds.go +128 -0
- package/luau/list.go +227 -0
- package/luau/nodes.go +479 -0
- package/luau/operators.go +46 -0
- package/luau/render.go +134 -0
- package/luau/render_expressions.go +264 -0
- package/luau/render_fields.go +81 -0
- package/luau/render_indexable.go +81 -0
- package/luau/render_state.go +134 -0
- package/luau/render_statements.go +235 -0
- package/luau/render_util.go +296 -0
- package/luau/solve_temp_ids.go +212 -0
- package/luau/typeguards.go +175 -0
- package/luau/util.go +184 -0
- package/luau/visit.go +138 -0
- package/npm/index.d.ts +3 -0
- package/npm/rbxts-go.sh +187 -0
- package/package.json +45 -5
- package/patches/typescript/0001-checker-exports.patch +40 -0
- package/patches/typescript/0002-iterable-arity.patch +15 -0
- package/patches/typescript/0003-context-sensitive-this.patch +15 -0
- package/patches/typescript/0004-printer-keep-parse-tree-parentheses.patch +26 -0
- package/pluginapi/host.go +31 -0
- package/pluginapi/pluginapi.go +153 -0
- package/plugins/LICENSE-rbxts-transformer-setget +18 -0
- package/plugins/apply.go +342 -0
- package/plugins/config.go +431 -0
- package/plugins/fallback.go +84 -0
- package/plugins/fixemit.go +60 -0
- package/plugins/native.go +271 -0
- package/plugins/session.go +236 -0
- package/plugins/setget.go +478 -0
- package/project/copy.go +333 -0
- package/project/include/Promise.lua +2068 -0
- package/project/include/RuntimeLib.lua +260 -0
- package/project/nodepath/nodepath.go +917 -0
- package/project/pathtranslator/pathtranslator.go +214 -0
- package/project/project.go +498 -0
- package/project/rojoresolver/json.go +248 -0
- package/project/rojoresolver/rojoresolver.go +559 -0
- package/project/shared/constants.go +83 -0
- package/project/shared/diagnostic.go +238 -0
- package/project/shared/diagnostics.go +388 -0
- package/project/shared/elapsed.go +14 -0
- package/project/shared/eol_other.go +6 -0
- package/project/shared/eol_windows.go +4 -0
- package/project/shared/kleur.go +40 -0
- package/project/shared/util.go +183 -0
- package/scripts/build-with-plugins.sh +130 -0
- package/scripts/setup-third-party.sh +75 -0
- package/transformer/add_index_diagnostics.go +21 -0
- package/transformer/add_one_if_array_type.go +15 -0
- package/transformer/array_binding_pattern_contains_hoists.go +25 -0
- package/transformer/assert_never.go +24 -0
- package/transformer/assignment.go +64 -0
- package/transformer/call_macros.go +70 -0
- package/transformer/check_variable_hoist.go +125 -0
- package/transformer/clean_module_name.go +28 -0
- package/transformer/constructor_macros.go +122 -0
- package/transformer/convert_to_indexable_expression.go +12 -0
- package/transformer/create_binary_from_operator.go +86 -0
- package/transformer/create_continue_statements.go +68 -0
- package/transformer/create_get_service.go +15 -0
- package/transformer/create_hoist_declaration.go +27 -0
- package/transformer/create_import_expression.go +220 -0
- package/transformer/create_string_from_literal.go +195 -0
- package/transformer/create_truthiness_checks.go +63 -0
- package/transformer/create_type_check.go +9 -0
- package/transformer/ensure_transform_order.go +53 -0
- package/transformer/expression_chain.go +27 -0
- package/transformer/expression_might_mutate.go +60 -0
- package/transformer/find_constructor.go +15 -0
- package/transformer/fixup_whitespace_and_decode_entities.go +377 -0
- package/transformer/get_accessor_for_binding_type.go +160 -0
- package/transformer/get_add_iterable_to_array_builder.go +363 -0
- package/transformer/get_assignable_value.go +15 -0
- package/transformer/get_constant_value_literal.go +20 -0
- package/transformer/get_declared_variables.go +34 -0
- package/transformer/get_extends_node.go +23 -0
- package/transformer/get_flags.go +21 -0
- package/transformer/get_kind_name.go +18 -0
- package/transformer/get_original_symbol_of_node.go +16 -0
- package/transformer/get_source_file_from_module_specifier.go +68 -0
- package/transformer/get_statements.go +12 -0
- package/transformer/has_multiple_definitions.go +18 -0
- package/transformer/hoist_functions_past_final_statement.go +43 -0
- package/transformer/identifier_macros.go +17 -0
- package/transformer/is_blocked_by_try_statement.go +19 -0
- package/transformer/is_method.go +104 -0
- package/transformer/is_symbol_mutable.go +26 -0
- package/transformer/is_symbol_of_value.go +11 -0
- package/transformer/is_used_as_statement.go +25 -0
- package/transformer/is_valid_method_index_without_call.go +36 -0
- package/transformer/macro_manager.go +92 -0
- package/transformer/object_accessor.go +32 -0
- package/transformer/offset.go +58 -0
- package/transformer/pointer.go +53 -0
- package/transformer/property_call_macros.go +1035 -0
- package/transformer/state.go +606 -0
- package/transformer/transform_array_assignment_pattern.go +65 -0
- package/transformer/transform_array_binding_pattern.go +49 -0
- package/transformer/transform_array_literal_expression.go +97 -0
- package/transformer/transform_await_expression.go +12 -0
- package/transformer/transform_binary_expression.go +221 -0
- package/transformer/transform_binding_name.go +25 -0
- package/transformer/transform_block.go +14 -0
- package/transformer/transform_boolean_literal.go +13 -0
- package/transformer/transform_break_statement.go +26 -0
- package/transformer/transform_call_expression.go +266 -0
- package/transformer/transform_class_constructor.go +125 -0
- package/transformer/transform_class_declaration.go +12 -0
- package/transformer/transform_class_expression.go +14 -0
- package/transformer/transform_class_like_declaration.go +355 -0
- package/transformer/transform_conditional_expression.go +70 -0
- package/transformer/transform_continue_statement.go +30 -0
- package/transformer/transform_decorators.go +253 -0
- package/transformer/transform_delete_expression.go +17 -0
- package/transformer/transform_do_statement.go +39 -0
- package/transformer/transform_element_access_expression.go +114 -0
- package/transformer/transform_entity_name.go +21 -0
- package/transformer/transform_enum_declaration.go +135 -0
- package/transformer/transform_export_assignment.go +57 -0
- package/transformer/transform_export_declaration.go +150 -0
- package/transformer/transform_expression.go +120 -0
- package/transformer/transform_expression_statement.go +71 -0
- package/transformer/transform_for_of_statement.go +461 -0
- package/transformer/transform_for_statement.go +599 -0
- package/transformer/transform_function_declaration.go +84 -0
- package/transformer/transform_function_expression.go +48 -0
- package/transformer/transform_identifier.go +194 -0
- package/transformer/transform_if_statement.go +44 -0
- package/transformer/transform_import_declaration.go +146 -0
- package/transformer/transform_import_equals_declaration.go +46 -0
- package/transformer/transform_import_expression.go +36 -0
- package/transformer/transform_initializer.go +22 -0
- package/transformer/transform_interpolated_string_part.go +12 -0
- package/transformer/transform_jsx.go +41 -0
- package/transformer/transform_jsx_attributes.go +110 -0
- package/transformer/transform_jsx_children.go +50 -0
- package/transformer/transform_jsx_element.go +14 -0
- package/transformer/transform_jsx_expression.go +20 -0
- package/transformer/transform_jsx_fragment.go +42 -0
- package/transformer/transform_jsx_self_closing_element.go +13 -0
- package/transformer/transform_jsx_tag_name.go +53 -0
- package/transformer/transform_logical.go +160 -0
- package/transformer/transform_logical_or_coalescing_assignment_expression.go +103 -0
- package/transformer/transform_method_declaration.go +102 -0
- package/transformer/transform_module_declaration.go +143 -0
- package/transformer/transform_new_expression.go +27 -0
- package/transformer/transform_no_substitution_template_literal.go +16 -0
- package/transformer/transform_numeric_literal.go +15 -0
- package/transformer/transform_object_assignment_pattern.go +86 -0
- package/transformer/transform_object_binding_pattern.go +49 -0
- package/transformer/transform_object_literal_expression.go +107 -0
- package/transformer/transform_omitted_expression.go +9 -0
- package/transformer/transform_optional_chain.go +312 -0
- package/transformer/transform_parameters.go +124 -0
- package/transformer/transform_parenthesized_expression.go +17 -0
- package/transformer/transform_property_access_expression.go +37 -0
- package/transformer/transform_property_declaration.go +33 -0
- package/transformer/transform_property_name.go +20 -0
- package/transformer/transform_return_statement.go +83 -0
- package/transformer/transform_source_file.go +230 -0
- package/transformer/transform_spread_element.go +35 -0
- package/transformer/transform_statement.go +84 -0
- package/transformer/transform_statement_list.go +90 -0
- package/transformer/transform_string_literal.go +22 -0
- package/transformer/transform_super_keyword.go +9 -0
- package/transformer/transform_switch_statement.go +145 -0
- package/transformer/transform_tagged_template_expression.go +34 -0
- package/transformer/transform_template_expression.go +35 -0
- package/transformer/transform_this_expression.go +33 -0
- package/transformer/transform_throw_statement.go +18 -0
- package/transformer/transform_try_statement.go +192 -0
- package/transformer/transform_type_expression.go +12 -0
- package/transformer/transform_unary_expression.go +79 -0
- package/transformer/transform_variable_statement.go +178 -0
- package/transformer/transform_void_expression.go +13 -0
- package/transformer/transform_while_statement.go +34 -0
- package/transformer/transform_writable.go +69 -0
- package/transformer/transform_yield_expression.go +56 -0
- package/transformer/type_guards.go +30 -0
- package/transformer/types.go +381 -0
- package/transformer/validate_identifier.go +17 -0
- package/transformer/validate_method_assignment.go +106 -0
- package/transformer/validate_not_any.go +37 -0
- package/transformer/wrap_expression_statement.go +20 -0
- package/transformer/wrap_return_if_lua_tuple.go +63 -0
- package/transformer/wrap_statements_as_generator.go +22 -0
- package/tsbridge/affected.go +141 -0
- package/tsbridge/config.go +71 -0
- package/tsbridge/diagnostics.go +128 -0
- package/tsbridge/doc.go +16 -0
- package/tsbridge/macros.go +207 -0
- package/tsbridge/macrospec.go +30 -0
- package/tsbridge/pool.go +142 -0
- package/tsbridge/project.go +137 -0
- package/tsbridge/reprint.go +120 -0
- package/tsbridge/update.go +128 -0
- package/watch/poll.go +126 -0
- package/watch/session.go +651 -0
- package/watch/watch.go +180 -0
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
// Package difftest compares rbxtsgo with the goldens that real JS roblox-ts@3.0.0 produced, in
|
|
2
|
+
// testdata/differential/golden. See testdata/differential/README.md. Every output file must match byte for byte,
|
|
3
|
+
// and each file's diagnostics must match in order. It's the scoreboard and the gate for the port.
|
|
4
|
+
package difftest
|
|
5
|
+
|
|
6
|
+
import (
|
|
7
|
+
"bytes"
|
|
8
|
+
"context"
|
|
9
|
+
"encoding/json"
|
|
10
|
+
"fmt"
|
|
11
|
+
"io/fs"
|
|
12
|
+
"os"
|
|
13
|
+
"path/filepath"
|
|
14
|
+
"regexp"
|
|
15
|
+
"slices"
|
|
16
|
+
"sort"
|
|
17
|
+
"strings"
|
|
18
|
+
"unicode/utf16"
|
|
19
|
+
"unicode/utf8"
|
|
20
|
+
|
|
21
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
22
|
+
"github.com/microsoft/TypeScript/tsc/internal/diagnostics"
|
|
23
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/compile"
|
|
24
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/plugins"
|
|
25
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project"
|
|
26
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
|
|
27
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/pathtranslator"
|
|
28
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/project/shared"
|
|
29
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/tsbridge"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
// Corpus is one entry of testdata/differential/corpora.json.
|
|
33
|
+
type Corpus struct {
|
|
34
|
+
Name string `json:"name"`
|
|
35
|
+
TsConfig string `json:"tsconfig"`
|
|
36
|
+
Mode string `json:"mode"`
|
|
37
|
+
Options map[string]any `json:"options"`
|
|
38
|
+
// Golden names the corpus whose goldens this one compares with, when JS can't read this one's config.
|
|
39
|
+
Golden string `json:"golden"`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// DiagnosticEntry is the structured form of a diagnostic in diagnostics.json. Column is in UTF-16 units on both sides.
|
|
43
|
+
type DiagnosticEntry struct {
|
|
44
|
+
Source string `json:"source"`
|
|
45
|
+
Code int `json:"code"`
|
|
46
|
+
ID int `json:"id"`
|
|
47
|
+
Category string `json:"category"`
|
|
48
|
+
Message string `json:"message"`
|
|
49
|
+
File string `json:"file"`
|
|
50
|
+
Line int `json:"line"`
|
|
51
|
+
Column int `json:"column"`
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
func (d DiagnosticEntry) String() string {
|
|
55
|
+
return fmt.Sprintf("%s:%d:%d %s %s code=%d id=%d %q", d.File, d.Line, d.Column, d.Source, d.Category, d.Code, d.ID, d.Message)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Result is the outcome for one case. Kind is "output" for one file under outDir and "diagnostics" for one source file's
|
|
59
|
+
// diagnostics, or the whole list in a whole-program corpus.
|
|
60
|
+
type Result struct {
|
|
61
|
+
Corpus string
|
|
62
|
+
Kind string
|
|
63
|
+
Name string
|
|
64
|
+
Pass bool
|
|
65
|
+
// Detail says why it failed.
|
|
66
|
+
Detail string
|
|
67
|
+
// Panic is the internal error behind a failure, if any. It's what the scoreboard groups by.
|
|
68
|
+
Panic string
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Key identifies a case in the baseline file.
|
|
72
|
+
func (r Result) Key() string { return r.Corpus + " " + r.Kind + " " + r.Name }
|
|
73
|
+
|
|
74
|
+
// Report is the result of one run over all corpora. Sources holds each corpus's output by path, for comparing runs.
|
|
75
|
+
type Report struct {
|
|
76
|
+
Results []Result
|
|
77
|
+
Outputs map[string]string
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Counts returns how many cases passed out of how many, for a kind.
|
|
81
|
+
func (r *Report) Counts(kind string) (pass, total int) {
|
|
82
|
+
for _, result := range r.Results {
|
|
83
|
+
if result.Kind == kind {
|
|
84
|
+
total++
|
|
85
|
+
if result.Pass {
|
|
86
|
+
pass++
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return pass, total
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// RepoRoot walks up from dir to the folder holding testdata/differential.
|
|
94
|
+
func RepoRoot(dir string) (string, error) {
|
|
95
|
+
for {
|
|
96
|
+
if _, err := os.Stat(filepath.Join(dir, "testdata", "differential", "corpora.json")); err == nil {
|
|
97
|
+
return dir, nil
|
|
98
|
+
}
|
|
99
|
+
parent := filepath.Dir(dir)
|
|
100
|
+
if parent == dir {
|
|
101
|
+
return "", fmt.Errorf("no testdata/differential above the current folder")
|
|
102
|
+
}
|
|
103
|
+
dir = parent
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// LoadCorpora reads corpora.json.
|
|
108
|
+
func LoadCorpora(repoRoot string) ([]Corpus, error) {
|
|
109
|
+
data, err := os.ReadFile(filepath.Join(repoRoot, "testdata", "differential", "corpora.json"))
|
|
110
|
+
if err != nil {
|
|
111
|
+
return nil, err
|
|
112
|
+
}
|
|
113
|
+
var corpora []Corpus
|
|
114
|
+
if err := json.Unmarshal(data, &corpora); err != nil {
|
|
115
|
+
return nil, err
|
|
116
|
+
}
|
|
117
|
+
return corpora, nil
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Options says how to run.
|
|
121
|
+
type Options struct {
|
|
122
|
+
Checkers int
|
|
123
|
+
// Only limits the run to these corpora, if not empty.
|
|
124
|
+
Only []string
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Run compiles each corpus and compares it with its goldens.
|
|
128
|
+
func Run(repoRoot string, options Options) (*Report, error) {
|
|
129
|
+
corpora, err := LoadCorpora(repoRoot)
|
|
130
|
+
if err != nil {
|
|
131
|
+
return nil, err
|
|
132
|
+
}
|
|
133
|
+
report := &Report{Outputs: map[string]string{}}
|
|
134
|
+
for _, corpus := range corpora {
|
|
135
|
+
if len(options.Only) > 0 && !slices.Contains(options.Only, corpus.Name) {
|
|
136
|
+
continue
|
|
137
|
+
}
|
|
138
|
+
if err := runCorpus(repoRoot, corpus, options, report); err != nil {
|
|
139
|
+
return nil, fmt.Errorf("corpus %s: %w", corpus.Name, err)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return report, nil
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
func projectOptions(corpus Corpus) shared.ProjectOptions {
|
|
146
|
+
options := shared.DefaultProjectOptions()
|
|
147
|
+
if value, ok := corpus.Options["allowCommentDirectives"].(bool); ok {
|
|
148
|
+
options.AllowCommentDirectives = value
|
|
149
|
+
}
|
|
150
|
+
if value, ok := corpus.Options["optimizedLoops"].(bool); ok {
|
|
151
|
+
options.OptimizedLoops = value
|
|
152
|
+
}
|
|
153
|
+
return options
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
func runCorpus(repoRoot string, corpus Corpus, options Options, report *Report) error {
|
|
157
|
+
tsConfig := nodepath.Native.Normalize(filepath.Join(repoRoot, filepath.FromSlash(corpus.TsConfig)))
|
|
158
|
+
if _, err := os.Stat(tsConfig); err != nil {
|
|
159
|
+
return fmt.Errorf("%s is missing. Run: bash scripts/setup-diff-corpus.sh", tsConfig)
|
|
160
|
+
}
|
|
161
|
+
goldenName := corpus.Name
|
|
162
|
+
if corpus.Golden != "" {
|
|
163
|
+
goldenName = corpus.Golden
|
|
164
|
+
}
|
|
165
|
+
goldenDir := filepath.Join(repoRoot, "testdata", "differential", "golden", goldenName)
|
|
166
|
+
|
|
167
|
+
data, err := project.CreateProjectData(tsConfig, projectOptions(corpus))
|
|
168
|
+
if err != nil {
|
|
169
|
+
return err
|
|
170
|
+
}
|
|
171
|
+
parsed, err := project.GetParsedCommandLine(data)
|
|
172
|
+
if err != nil {
|
|
173
|
+
return err
|
|
174
|
+
}
|
|
175
|
+
p := tsbridge.NewProject(parsed, tsbridge.ProjectOptions{Checkers: options.Checkers})
|
|
176
|
+
translator := project.CreatePathTranslator(p, data)
|
|
177
|
+
prepared, failure, err := project.PrepareCompile(p, data, translator, p.Program().CaseSensitivity())
|
|
178
|
+
if err != nil {
|
|
179
|
+
return err
|
|
180
|
+
}
|
|
181
|
+
if failure != nil {
|
|
182
|
+
return fmt.Errorf("prepare failed: %s", failure[0].Message())
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
files := p.SourceFilesToCompile()
|
|
186
|
+
decision := plugins.Decide(tsConfig, plugins.Options{})
|
|
187
|
+
if corpus.Mode == "plugin-exit" {
|
|
188
|
+
return comparePluginExits(corpus, goldenDir, tsConfig, p, decision, files, report)
|
|
189
|
+
}
|
|
190
|
+
applied, err := plugins.Apply(context.Background(), p, decision, files, nil)
|
|
191
|
+
if err != nil {
|
|
192
|
+
return err
|
|
193
|
+
}
|
|
194
|
+
if applied.Exit != nil {
|
|
195
|
+
return fmt.Errorf("the plugin exited: %s", strings.Join(applied.Exit, "\n"))
|
|
196
|
+
}
|
|
197
|
+
p, files = applied.Project, applied.Files
|
|
198
|
+
if err := compareReprints(corpus, goldenDir, applied, translator, p.Options().OutDir.AsString(), report); err != nil {
|
|
199
|
+
return err
|
|
200
|
+
}
|
|
201
|
+
mode := compile.ModeWhole
|
|
202
|
+
if corpus.Mode == "perfile" {
|
|
203
|
+
mode = compile.ModeIsolated
|
|
204
|
+
}
|
|
205
|
+
c := &compile.Context{Project: p, Data: data, Translator: translator, Prepared: prepared, Replay: shared.NewContextReplay()}
|
|
206
|
+
results, err := compile.Run(context.Background(), c, files, mode)
|
|
207
|
+
if err != nil {
|
|
208
|
+
return err
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
projectDir := filepath.ToSlash(filepath.Dir(tsConfig))
|
|
212
|
+
outDir := p.Options().OutDir.AsString()
|
|
213
|
+
relProject := func(name string) string {
|
|
214
|
+
return strings.TrimPrefix(strings.TrimPrefix(filepath.ToSlash(name), projectDir), "/")
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Outputs.
|
|
218
|
+
outputs := map[string]string{}
|
|
219
|
+
panics := map[string]string{} // output path to the internal error that kept it from existing
|
|
220
|
+
for _, result := range results {
|
|
221
|
+
outPath := filepath.ToSlash(translator.GetOutputPath(nodepath.Native.Normalize(result.File.FileName().AsString())))
|
|
222
|
+
rel := strings.TrimPrefix(strings.TrimPrefix(outPath, filepath.ToSlash(outDir)), "/")
|
|
223
|
+
if result.Panic != "" {
|
|
224
|
+
panics[rel] = result.Panic
|
|
225
|
+
}
|
|
226
|
+
if result.HasSource {
|
|
227
|
+
outputs[rel] = result.Source
|
|
228
|
+
report.Outputs[corpus.Name+"/"+rel] = result.Source
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
golden := map[string][]byte{}
|
|
232
|
+
goldenFiles := filepath.Join(goldenDir, "files")
|
|
233
|
+
if _, statErr := os.Stat(goldenFiles); statErr == nil {
|
|
234
|
+
err = filepath.WalkDir(goldenFiles, func(path string, entry fs.DirEntry, err error) error {
|
|
235
|
+
if err != nil || entry.IsDir() {
|
|
236
|
+
return err
|
|
237
|
+
}
|
|
238
|
+
content, err := os.ReadFile(path)
|
|
239
|
+
if err != nil {
|
|
240
|
+
return err
|
|
241
|
+
}
|
|
242
|
+
rel, err := filepath.Rel(goldenFiles, path)
|
|
243
|
+
if err != nil {
|
|
244
|
+
return err
|
|
245
|
+
}
|
|
246
|
+
golden[filepath.ToSlash(rel)] = content
|
|
247
|
+
return nil
|
|
248
|
+
})
|
|
249
|
+
if err != nil {
|
|
250
|
+
return err
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
names := make([]string, 0, len(golden)+len(outputs))
|
|
254
|
+
for name := range golden {
|
|
255
|
+
names = append(names, name)
|
|
256
|
+
}
|
|
257
|
+
for name := range outputs {
|
|
258
|
+
if _, ok := golden[name]; !ok {
|
|
259
|
+
names = append(names, name)
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
sort.Strings(names)
|
|
263
|
+
for _, name := range names {
|
|
264
|
+
result := Result{Corpus: corpus.Name, Kind: "output", Name: name, Panic: panics[name]}
|
|
265
|
+
want, inGolden := golden[name]
|
|
266
|
+
got, inGo := outputs[name]
|
|
267
|
+
switch {
|
|
268
|
+
case inGolden && inGo:
|
|
269
|
+
if bytes.Equal(want, []byte(got)) {
|
|
270
|
+
result.Pass = true
|
|
271
|
+
} else {
|
|
272
|
+
result.Detail = FirstDifference(want, []byte(got))
|
|
273
|
+
}
|
|
274
|
+
case inGolden:
|
|
275
|
+
result.Detail = "rbxtsgo wrote no file"
|
|
276
|
+
if result.Panic != "" {
|
|
277
|
+
result.Detail += ": " + result.Panic
|
|
278
|
+
}
|
|
279
|
+
default:
|
|
280
|
+
result.Detail = "JS wrote no file, and rbxtsgo did"
|
|
281
|
+
}
|
|
282
|
+
report.Results = append(report.Results, result)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Diagnostics.
|
|
286
|
+
var wantDiagnostics map[string][]DiagnosticEntry
|
|
287
|
+
raw, err := os.ReadFile(filepath.Join(goldenDir, "diagnostics.json"))
|
|
288
|
+
if err != nil {
|
|
289
|
+
return fmt.Errorf("no goldens: %w. Run: bash scripts/gen-diff-goldens.sh", err)
|
|
290
|
+
}
|
|
291
|
+
if err := json.Unmarshal(raw, &wantDiagnostics); err != nil {
|
|
292
|
+
return err
|
|
293
|
+
}
|
|
294
|
+
compareDiagnostics := func(name string, got []*shared.Diagnostic, panicText string) {
|
|
295
|
+
entries := make([]DiagnosticEntry, len(got))
|
|
296
|
+
for i, d := range got {
|
|
297
|
+
entries[i] = describe(d, projectDir, relProject)
|
|
298
|
+
}
|
|
299
|
+
result := Result{Corpus: corpus.Name, Kind: "diagnostics", Name: name, Panic: panicText}
|
|
300
|
+
want := wantDiagnostics[name]
|
|
301
|
+
switch {
|
|
302
|
+
case panicText != "":
|
|
303
|
+
result.Detail = "internal error: " + panicText
|
|
304
|
+
case slices.Equal(want, entries):
|
|
305
|
+
result.Pass = true
|
|
306
|
+
default:
|
|
307
|
+
result.Detail = firstDiagnosticDifference(want, entries)
|
|
308
|
+
}
|
|
309
|
+
report.Results = append(report.Results, result)
|
|
310
|
+
}
|
|
311
|
+
if mode == compile.ModeIsolated {
|
|
312
|
+
for _, result := range results {
|
|
313
|
+
compareDiagnostics(relProject(result.File.FileName().AsString()), result.Diagnostics, result.Panic)
|
|
314
|
+
}
|
|
315
|
+
} else {
|
|
316
|
+
var all []*shared.Diagnostic
|
|
317
|
+
panicText := ""
|
|
318
|
+
for _, result := range results {
|
|
319
|
+
all = append(all, result.Diagnostics...)
|
|
320
|
+
if result.Panic != "" && panicText == "" {
|
|
321
|
+
panicText = result.Panic
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
compareDiagnostics("<whole>", all, panicText)
|
|
325
|
+
}
|
|
326
|
+
return nil
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// comparePluginExits runs the plugin on each file alone, and compares what it wrote before exiting.
|
|
330
|
+
func comparePluginExits(corpus Corpus, goldenDir, tsConfig string, p *tsbridge.Project, decision plugins.Decision, files []*ast.SourceFile, report *Report) error {
|
|
331
|
+
var want map[string][]DiagnosticEntry
|
|
332
|
+
raw, err := os.ReadFile(filepath.Join(goldenDir, "diagnostics.json"))
|
|
333
|
+
if err != nil {
|
|
334
|
+
return fmt.Errorf("no goldens: %w. Run: bash scripts/gen-diff-goldens.sh", err)
|
|
335
|
+
}
|
|
336
|
+
if err := json.Unmarshal(raw, &want); err != nil {
|
|
337
|
+
return err
|
|
338
|
+
}
|
|
339
|
+
projectDir := filepath.ToSlash(filepath.Dir(tsConfig))
|
|
340
|
+
for _, file := range files {
|
|
341
|
+
name := strings.TrimPrefix(strings.TrimPrefix(file.FileName().AsString(), projectDir), "/")
|
|
342
|
+
applied, err := plugins.Apply(context.Background(), p, decision, []*ast.SourceFile{file}, nil)
|
|
343
|
+
if err != nil {
|
|
344
|
+
return err
|
|
345
|
+
}
|
|
346
|
+
var got []DiagnosticEntry
|
|
347
|
+
if applied.Exit != nil {
|
|
348
|
+
message := ansi.ReplaceAllString(strings.Join(applied.Exit, "\n"), "")
|
|
349
|
+
got = []DiagnosticEntry{{Source: "plugin", Code: 1, ID: -1, Category: "error", Message: message, Line: -1, Column: -1}}
|
|
350
|
+
}
|
|
351
|
+
result := Result{Corpus: corpus.Name, Kind: "diagnostics", Name: name, Pass: slices.Equal(want[name], got)}
|
|
352
|
+
if !result.Pass {
|
|
353
|
+
result.Detail = firstDiagnosticDifference(want[name], got)
|
|
354
|
+
}
|
|
355
|
+
report.Results = append(report.Results, result)
|
|
356
|
+
}
|
|
357
|
+
return nil
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// compareReprints compares each file's reprinted text with what JS wrote for the writeTransformedFiles option. The Luau
|
|
361
|
+
// comparison is the requirement. This one says where a difference starts, and diagnostic positions depend on it.
|
|
362
|
+
func compareReprints(corpus Corpus, goldenDir string, applied *plugins.Applied, translator *pathtranslator.Translator, outDir string, report *Report) error {
|
|
363
|
+
if applied.Printed == nil {
|
|
364
|
+
return nil
|
|
365
|
+
}
|
|
366
|
+
for _, file := range applied.Files {
|
|
367
|
+
name := file.FileName().AsString()
|
|
368
|
+
outPath := filepath.ToSlash(translator.GetOutputTransformedPath(nodepath.Native.Normalize(name)))
|
|
369
|
+
rel := strings.TrimPrefix(strings.TrimPrefix(outPath, filepath.ToSlash(outDir)), "/")
|
|
370
|
+
result := Result{Corpus: corpus.Name, Kind: "reprint", Name: rel}
|
|
371
|
+
want, err := os.ReadFile(filepath.Join(goldenDir, "transformed", filepath.FromSlash(rel)))
|
|
372
|
+
switch {
|
|
373
|
+
case err != nil:
|
|
374
|
+
result.Detail = "JS wrote no reprint"
|
|
375
|
+
case string(want) == applied.Printed[name]:
|
|
376
|
+
result.Pass = true
|
|
377
|
+
default:
|
|
378
|
+
result.Detail = FirstDifference(want, []byte(applied.Printed[name]))
|
|
379
|
+
}
|
|
380
|
+
report.Results = append(report.Results, result)
|
|
381
|
+
report.Outputs[corpus.Name+"/"+rel] = applied.Printed[name]
|
|
382
|
+
}
|
|
383
|
+
return nil
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
var ansi = regexp.MustCompile("\x1b\\[[0-9;]*m")
|
|
387
|
+
|
|
388
|
+
func describe(d *shared.Diagnostic, projectDir string, relProject func(string) string) DiagnosticEntry {
|
|
389
|
+
entry := DiagnosticEntry{Code: -1, ID: d.ID(), Line: -1, Column: -1}
|
|
390
|
+
if d.IsRobloxTS() {
|
|
391
|
+
entry.Source = "rbxts"
|
|
392
|
+
} else {
|
|
393
|
+
entry.Source = "ts"
|
|
394
|
+
entry.Code = int(d.Code())
|
|
395
|
+
}
|
|
396
|
+
switch d.Category() {
|
|
397
|
+
case diagnostics.CategoryError:
|
|
398
|
+
entry.Category = "error"
|
|
399
|
+
case diagnostics.CategoryWarning:
|
|
400
|
+
entry.Category = "warning"
|
|
401
|
+
case diagnostics.CategorySuggestion:
|
|
402
|
+
entry.Category = "suggestion"
|
|
403
|
+
default:
|
|
404
|
+
entry.Category = "message"
|
|
405
|
+
}
|
|
406
|
+
message := ansi.ReplaceAllString(d.Message(), "")
|
|
407
|
+
message = strings.ReplaceAll(message, filepath.FromSlash(projectDir), "<ROOT>")
|
|
408
|
+
entry.Message = strings.ReplaceAll(message, projectDir, "<ROOT>")
|
|
409
|
+
if file := d.File(); file != nil {
|
|
410
|
+
entry.File = relProject(file.FileName().AsString())
|
|
411
|
+
entry.Line, entry.Column = lineAndColumn(file, d.Start())
|
|
412
|
+
}
|
|
413
|
+
return entry
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// lineAndColumn is getLineAndCharacterOfPosition, with the column in UTF-16 units as JS has it.
|
|
417
|
+
func lineAndColumn(file *ast.SourceFile, position int) (line, column int) {
|
|
418
|
+
text := file.Text()
|
|
419
|
+
lineStart := 0
|
|
420
|
+
for i := 0; i < position && i < len(text); {
|
|
421
|
+
r, size := utf8.DecodeRuneInString(text[i:])
|
|
422
|
+
next := i + size
|
|
423
|
+
switch r {
|
|
424
|
+
case '\r':
|
|
425
|
+
if next < len(text) && text[next] == '\n' {
|
|
426
|
+
next++
|
|
427
|
+
}
|
|
428
|
+
fallthrough
|
|
429
|
+
case '\n', '
', '
':
|
|
430
|
+
if next <= position {
|
|
431
|
+
line++
|
|
432
|
+
lineStart = next
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
i = next
|
|
436
|
+
}
|
|
437
|
+
for _, r := range text[lineStart:min(position, len(text))] {
|
|
438
|
+
column += utf16.RuneLen(r)
|
|
439
|
+
}
|
|
440
|
+
return line, column
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
func firstDiagnosticDifference(want, got []DiagnosticEntry) string {
|
|
444
|
+
var b strings.Builder
|
|
445
|
+
fmt.Fprintf(&b, "want %d diagnostics, got %d", len(want), len(got))
|
|
446
|
+
different := 0
|
|
447
|
+
for i := range min(len(want), len(got)) {
|
|
448
|
+
if want[i] != got[i] {
|
|
449
|
+
different++
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
fmt.Fprintf(&b, ", %d differ", different)
|
|
453
|
+
for i := range max(len(want), len(got)) {
|
|
454
|
+
var w, g string
|
|
455
|
+
if i < len(want) {
|
|
456
|
+
w = want[i].String()
|
|
457
|
+
}
|
|
458
|
+
if i < len(got) {
|
|
459
|
+
g = got[i].String()
|
|
460
|
+
}
|
|
461
|
+
if w != g {
|
|
462
|
+
fmt.Fprintf(&b, "; first difference at #%d\n JS: %s\n Go: %s", i, w, g)
|
|
463
|
+
break
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return b.String()
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// FirstDifference describes the first place two outputs differ, with the lines around it.
|
|
470
|
+
func FirstDifference(want, got []byte) string {
|
|
471
|
+
i := 0
|
|
472
|
+
for i < len(want) && i < len(got) && want[i] == got[i] {
|
|
473
|
+
i++
|
|
474
|
+
}
|
|
475
|
+
line := bytes.Count(want[:i], []byte("\n")) + 1
|
|
476
|
+
context := func(text []byte) string {
|
|
477
|
+
start := i
|
|
478
|
+
for n := 0; start > 0 && n < 3; start-- {
|
|
479
|
+
if text[start-1] == '\n' {
|
|
480
|
+
n++
|
|
481
|
+
if n == 3 {
|
|
482
|
+
break
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
end := min(i, len(text))
|
|
487
|
+
for n := 0; end < len(text) && n < 3; end++ {
|
|
488
|
+
if text[end] == '\n' {
|
|
489
|
+
n++
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if start > len(text) {
|
|
493
|
+
start = len(text)
|
|
494
|
+
}
|
|
495
|
+
return string(text[start:end])
|
|
496
|
+
}
|
|
497
|
+
return fmt.Sprintf("first difference at byte %d, line %d\n--- JS\n%s\n--- Go\n%s", i, line, context(want), context(got))
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// PanicSummary groups failures by their internal error, largest first. An entry for a port that's missing a piece
|
|
501
|
+
// reads "not ported: X", which makes this the work list.
|
|
502
|
+
func (r *Report) PanicSummary() []string {
|
|
503
|
+
counts := map[string]int{}
|
|
504
|
+
seen := map[string]bool{}
|
|
505
|
+
for _, result := range r.Results {
|
|
506
|
+
key := result.Corpus + " " + result.Name
|
|
507
|
+
if result.Panic != "" && !seen[key] {
|
|
508
|
+
seen[key] = true
|
|
509
|
+
counts[firstLine(result.Panic)]++
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
lines := make([]string, 0, len(counts))
|
|
513
|
+
for panicText, count := range counts {
|
|
514
|
+
lines = append(lines, fmt.Sprintf("%4d %s", count, panicText))
|
|
515
|
+
}
|
|
516
|
+
sort.Slice(lines, func(i, j int) bool {
|
|
517
|
+
if a, b := lines[i][:4], lines[j][:4]; a != b {
|
|
518
|
+
return a > b
|
|
519
|
+
}
|
|
520
|
+
return lines[i] < lines[j]
|
|
521
|
+
})
|
|
522
|
+
return lines
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
func firstLine(text string) string {
|
|
526
|
+
if i := strings.IndexByte(text, '\n'); i >= 0 {
|
|
527
|
+
return text[:i]
|
|
528
|
+
}
|
|
529
|
+
return text
|
|
530
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// Package plugintest has two native plugins for this repository's own tests, registered in the test process. They are
|
|
2
|
+
// made to exercise the host: every part of docs/plugin-api.md has a call here that reaches it.
|
|
3
|
+
//
|
|
4
|
+
// The plugin that shows the real way to build a compiler with a plugin, as its own module, is in
|
|
5
|
+
// pluginapi/testdata/testplugin.
|
|
6
|
+
package plugintest
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
"errors"
|
|
10
|
+
"fmt"
|
|
11
|
+
"path"
|
|
12
|
+
"strings"
|
|
13
|
+
"sync"
|
|
14
|
+
|
|
15
|
+
"github.com/microsoft/TypeScript/tsc/internal/ast"
|
|
16
|
+
"github.com/microsoft/TypeScript/tsc/internal/diagnostics"
|
|
17
|
+
"github.com/microsoft/TypeScript/tsc/rbxtsgo/pluginapi"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
const (
|
|
21
|
+
// MacroTransform is the "transform" of the macro plugin. It rewrites calls:
|
|
22
|
+
//
|
|
23
|
+
// $upper("text") the string "TEXT", followed by the entry's "suffix" if it has one
|
|
24
|
+
// $typeName(x) the string of x's type, from the checker
|
|
25
|
+
// $fail() an error diagnostic at the call
|
|
26
|
+
// $warn() a warning diagnostic at the call, and the call becomes the string "warned"
|
|
27
|
+
// $exit() FileContext.Exit
|
|
28
|
+
// $panic() a panic
|
|
29
|
+
//
|
|
30
|
+
// Its transformer has no Update, so a watch session calls New for every program.
|
|
31
|
+
MacroTransform = "rbxtsgo-test-macros"
|
|
32
|
+
// MarkTransform is the "transform" of the mark plugin, which is meant to come after the macro plugin. It puts
|
|
33
|
+
// angle brackets around every string that $upper made, which it can only find through FileContext.Original.
|
|
34
|
+
// Its transformer has Update. An entry with "failNew": true makes New return an error, and "failFinish": true
|
|
35
|
+
// does the same for Finish.
|
|
36
|
+
MarkTransform = "rbxtsgo-test-mark"
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
var (
|
|
40
|
+
logMu sync.Mutex
|
|
41
|
+
log []string
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
func record(format string, args ...any) {
|
|
45
|
+
logMu.Lock()
|
|
46
|
+
defer logMu.Unlock()
|
|
47
|
+
log = append(log, fmt.Sprintf(format, args...))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// TakeLog returns the calls the host made into the plugins since the last TakeLog, in order.
|
|
51
|
+
func TakeLog() []string {
|
|
52
|
+
logMu.Lock()
|
|
53
|
+
defer logMu.Unlock()
|
|
54
|
+
taken := log
|
|
55
|
+
log = nil
|
|
56
|
+
return taken
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var registerOnce sync.Once
|
|
60
|
+
|
|
61
|
+
// Register registers both plugins, once per process.
|
|
62
|
+
func Register() {
|
|
63
|
+
registerOnce.Do(func() {
|
|
64
|
+
pluginapi.Register(pluginapi.Plugin{Name: "macros", Transforms: []string{MacroTransform}, New: newMacros})
|
|
65
|
+
pluginapi.Register(pluginapi.Plugin{Name: "mark", Transforms: []string{MarkTransform}, New: newMark})
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
func baseName(file *ast.SourceFile) string { return path.Base(file.FileName().AsString()) }
|
|
70
|
+
|
|
71
|
+
func baseNames(files []*ast.SourceFile) string {
|
|
72
|
+
names := make([]string, len(files))
|
|
73
|
+
for i, file := range files {
|
|
74
|
+
names[i] = baseName(file)
|
|
75
|
+
}
|
|
76
|
+
return strings.Join(names, ",")
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// calledName is the name of the function a call calls, when the call is name(...).
|
|
80
|
+
func calledName(node *ast.Node) string {
|
|
81
|
+
if node != nil && node.Kind == ast.KindCallExpression && node.Expression().Kind == ast.KindIdentifier {
|
|
82
|
+
return node.Expression().Text()
|
|
83
|
+
}
|
|
84
|
+
return ""
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
type macros struct {
|
|
88
|
+
host *pluginapi.Host
|
|
89
|
+
suffix string
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
func newMacros(host *pluginapi.Host, config map[string]any) (pluginapi.Transformer, error) {
|
|
93
|
+
if host.Program == nil || host.Options == nil || host.Data == nil || host.PathTranslator == nil || host.RojoResolver == nil || host.Checker == nil {
|
|
94
|
+
return nil, fmt.Errorf("macros: the host is incomplete: %+v", *host)
|
|
95
|
+
}
|
|
96
|
+
suffix, _ := config["suffix"].(string)
|
|
97
|
+
record("macros.New watch=%v files=%s transform=%v", host.Watch, baseNames(host.Files), config["transform"])
|
|
98
|
+
return ¯os{host: host, suffix: suffix}, nil
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
func (m *macros) TransformFile(ctx *pluginapi.FileContext, file *ast.SourceFile, node *ast.Node) *ast.Node {
|
|
102
|
+
record("macros.TransformFile %s", baseName(file))
|
|
103
|
+
var visitor *ast.NodeVisitor
|
|
104
|
+
replace := func(call *ast.Node, text string) *ast.Node {
|
|
105
|
+
literal := ctx.Factory.NewStringLiteral(text, ast.TokenFlagsNone)
|
|
106
|
+
ctx.EmitContext.SetOriginal(literal, call)
|
|
107
|
+
return literal
|
|
108
|
+
}
|
|
109
|
+
visitor = ctx.EmitContext.NewNodeVisitor(func(node *ast.Node) *ast.Node {
|
|
110
|
+
arguments := func() []*ast.Node { return node.AsCallExpression().Arguments.Nodes }
|
|
111
|
+
switch calledName(node) {
|
|
112
|
+
case "$upper":
|
|
113
|
+
if len(arguments()) == 1 && arguments()[0].Kind == ast.KindStringLiteral {
|
|
114
|
+
return replace(node, strings.ToUpper(arguments()[0].Text())+m.suffix)
|
|
115
|
+
}
|
|
116
|
+
case "$typeName":
|
|
117
|
+
if len(arguments()) == 1 {
|
|
118
|
+
// The checker is asked about the node of the file as written.
|
|
119
|
+
argument := ctx.Original(arguments()[0])
|
|
120
|
+
return replace(node, ctx.Checker.TypeToString(ctx.Checker.GetTypeAtLocation(argument)))
|
|
121
|
+
}
|
|
122
|
+
case "$fail":
|
|
123
|
+
ctx.AddDiagnostic(diagnostics.CategoryError, node, "$fail was called")
|
|
124
|
+
return node
|
|
125
|
+
case "$warn":
|
|
126
|
+
ctx.AddDiagnostic(diagnostics.CategoryWarning, node, "$warn was called")
|
|
127
|
+
return replace(node, "warned")
|
|
128
|
+
case "$exit":
|
|
129
|
+
ctx.Exit("[macros]: $exit was called", "[macros]: in "+baseName(file))
|
|
130
|
+
case "$panic":
|
|
131
|
+
panic("$panic was called")
|
|
132
|
+
}
|
|
133
|
+
return visitor.VisitEachChild(node)
|
|
134
|
+
})
|
|
135
|
+
return visitor.VisitSourceFile(node.AsSourceFile()).AsNode()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
func (m *macros) Finish() error {
|
|
139
|
+
record("macros.Finish")
|
|
140
|
+
return nil
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
type mark struct {
|
|
144
|
+
failFinish bool
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
func newMark(host *pluginapi.Host, config map[string]any) (pluginapi.Transformer, error) {
|
|
148
|
+
record("mark.New watch=%v", host.Watch)
|
|
149
|
+
if config["failNew"] == true {
|
|
150
|
+
return nil, errors.New("mark: failNew is set")
|
|
151
|
+
}
|
|
152
|
+
return &mark{failFinish: config["failFinish"] == true}, nil
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
func (m *mark) Update(host *pluginapi.Host, changed []*ast.SourceFile) error {
|
|
156
|
+
record("mark.Update changed=%s", baseNames(changed))
|
|
157
|
+
return nil
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
func (m *mark) TransformFile(ctx *pluginapi.FileContext, file *ast.SourceFile, node *ast.Node) *ast.Node {
|
|
161
|
+
record("mark.TransformFile %s", baseName(file))
|
|
162
|
+
var visitor *ast.NodeVisitor
|
|
163
|
+
visitor = ctx.EmitContext.NewNodeVisitor(func(node *ast.Node) *ast.Node {
|
|
164
|
+
// What the macro plugin left is a string. The call it was made from is its original.
|
|
165
|
+
if node.Kind == ast.KindStringLiteral && calledName(ctx.Original(node)) == "$upper" {
|
|
166
|
+
literal := ctx.Factory.NewStringLiteral("<"+node.Text()+">", ast.TokenFlagsNone)
|
|
167
|
+
ctx.EmitContext.SetOriginal(literal, node)
|
|
168
|
+
return literal
|
|
169
|
+
}
|
|
170
|
+
return visitor.VisitEachChild(node)
|
|
171
|
+
})
|
|
172
|
+
return visitor.VisitSourceFile(node.AsSourceFile()).AsNode()
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
func (m *mark) Finish() error {
|
|
176
|
+
record("mark.Finish")
|
|
177
|
+
if m.failFinish {
|
|
178
|
+
return errors.New("mark: failFinish is set")
|
|
179
|
+
}
|
|
180
|
+
return nil
|
|
181
|
+
}
|