@data-vegle/rbxts-go 1.0.0 → 1.1.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/README.md CHANGED
@@ -144,6 +144,13 @@ byte-identical to rbxtsgo's, and on rbxtsgo's `upstream-tests` corpus the printe
144
144
 
145
145
  Also new: `--memprofile <file>` writes a heap profile, next to the existing `--cpuprofile`.
146
146
 
147
+ Also new: `--obfuscate` runs [darklua](https://darklua.com) on the compiled files, and needs `darklua` on the PATH.
148
+ The compiled Luau goes to a temporary folder, and one darklua run writes the obfuscated files to the output folder, so
149
+ each output file is written once. Without a `.darklua.json` or `.darklua.json5` in the project folder, it uses the
150
+ rule list in `compile/obfuscate.go` (remove comments, spaces, types and unused code, rename locals, and more); with
151
+ one, that config decides. Only the files the compiler emits
152
+ are obfuscated: not the runtime library, and not copied `.lua` files.
153
+
147
154
  Also new: **native plugins**. A transformer plugin written in Go, as its own module, can be compiled into the compiler
148
155
  and then runs in place of the JavaScript transformer of the same name in `compilerOptions.plugins`, so the build no
149
156
  longer goes to the JS compiler. [docs/plugin-api.md](docs/plugin-api.md) is the contract for plugin authors, and
package/cli/cli.go CHANGED
@@ -81,6 +81,8 @@ func Run(args []string) int {
81
81
  memProfile := flags.String("memprofile", "", "write a heap profile to this file when the build ends")
82
82
  var fixEmit tristate
83
83
  flags.Var(&fixEmit, "fix-emit", "turn on fixes for output that is wrong in roblox-ts 3.0.0, which makes the output differ from it where it was wrong. Also tsconfig \"rbxtsgo\": { \"fixEmit\": true }")
84
+ var obfuscate tristate
85
+ flags.Var(&obfuscate, "obfuscate", "obfuscate the compiled files with darklua, which must be on the PATH. Uses .darklua.json or .darklua.json5 of the project when there is one")
84
86
  var setget tristate
85
87
  flags.Var(&setget, "setget", "turn getters and setters into methods, as rbxts-transformer-setget does")
86
88
  setgetPrefix := flags.String("setgetPrefix", "", "prefix of the methods setget makes (default __)")
@@ -138,6 +140,7 @@ func Run(args []string) int {
138
140
  setBool(allowCommentDirectives, &options.AllowCommentDirectives)
139
141
  setBool(luauFlag, &options.Luau)
140
142
  setBool(fixEmit, &options.FixEmit)
143
+ setBool(obfuscate, &options.Obfuscate)
141
144
  if *projectType != "" {
142
145
  options.Type = shared.ProjectType(*projectType)
143
146
  }
@@ -289,6 +292,9 @@ func build(projectPath string, applyFlags func(*shared.ProjectOptions), checkers
289
292
  if options.FixEmit {
290
293
  return errors.New(decision.Reason + ", and the JS compiler doesn't have --fix-emit.\nTo build this project, turn fixEmit off, or remove the plugin from compilerOptions.plugins.")
291
294
  }
295
+ if options.Obfuscate {
296
+ return errors.New(decision.Reason + ", and the JS compiler doesn't have --obfuscate.")
297
+ }
292
298
  code, err := plugins.Delegate(decision.Reason, tsConfigPath, commandLine, os.Stderr)
293
299
  exitCode = code
294
300
  return err
@@ -332,18 +332,31 @@ func CompileFiles(ctx context.Context, c *Context, files []*ast.SourceFile) (*Em
332
332
  // WriteResults writes each rendered file to its output path, as the emit loop of compileFiles does. The files are
333
333
  // independent, so several goroutines write them at once. The returned paths keep the order of the results, and the
334
334
  // error returned is the first one in that order, with the paths before it, as a sequential loop would give.
335
+ //
336
+ // With --obfuscate the files go to a temporary folder, and darklua writes them to the output folder. See obfuscate.go.
335
337
  func WriteResults(c *Context, results []*FileResult) ([]string, error) {
338
+ var obfuscate *obfuscation
339
+ if c.Data.ProjectOptions.Obfuscate {
340
+ var err error
341
+ if obfuscate, err = newObfuscation(); err != nil {
342
+ return nil, err
343
+ }
344
+ defer obfuscate.close()
345
+ }
336
346
  writeOne := func(result *FileResult) (string, error) {
337
347
  outPath := c.Translator.GetOutputPath(nodepath.Native.Normalize(result.File.FileName().AsString()))
338
- if c.Data.ProjectOptions.WriteOnlyChanged {
348
+ target := outPath
349
+ if obfuscate != nil {
350
+ target = obfuscate.path(c, outPath)
351
+ } else if c.Data.ProjectOptions.WriteOnlyChanged {
339
352
  if current, err := os.ReadFile(outPath); err == nil && string(current) == result.Source {
340
353
  return "", nil
341
354
  }
342
355
  }
343
- if err := os.MkdirAll(nodepath.Native.Dirname(outPath), 0o777); err != nil {
356
+ if err := os.MkdirAll(nodepath.Native.Dirname(target), 0o777); err != nil {
344
357
  return "", err
345
358
  }
346
- if err := os.WriteFile(outPath, []byte(result.Source), 0o666); err != nil {
359
+ if err := os.WriteFile(target, []byte(result.Source), 0o666); err != nil {
347
360
  return "", err
348
361
  }
349
362
  return outPath, nil
@@ -380,6 +393,11 @@ func WriteResults(c *Context, results []*FileResult) ([]string, error) {
380
393
  emitted = append(emitted, paths[i])
381
394
  }
382
395
  }
396
+ if obfuscate != nil && len(emitted) > 0 {
397
+ if err := obfuscate.run(c); err != nil {
398
+ return nil, err
399
+ }
400
+ }
383
401
  return emitted, nil
384
402
  }
385
403
 
@@ -0,0 +1,99 @@
1
+ package compile
2
+
3
+ import (
4
+ "errors"
5
+ "fmt"
6
+ "os"
7
+ "os/exec"
8
+ "strings"
9
+
10
+ "github.com/microsoft/TypeScript/tsc/rbxtsgo/project/nodepath"
11
+ )
12
+
13
+ // defaultDarkluaConfig is used when the project has no darklua config of its own. The generator keeps the tokens, so
14
+ // remove_spaces has something to do and one leading directive comment (--!native, --!strict, --!optimize) survives.
15
+ // "$roblox" keeps rename_variables from giving a local the name of a Roblox global.
16
+ const defaultDarkluaConfig = `{
17
+ "generator": "retain_lines",
18
+ "rules": [
19
+ "remove_spaces",
20
+ { "rule": "remove_comments", "except": ["^--!"] },
21
+ "remove_assertions",
22
+ "remove_debug_profiling",
23
+ "compute_expression",
24
+ "remove_unused_if_branch",
25
+ "remove_unused_while",
26
+ "filter_after_early_return",
27
+ "remove_empty_do",
28
+ "remove_unused_variable",
29
+ "remove_method_definition",
30
+ "convert_index_to_field",
31
+ "remove_nil_declaration",
32
+ "remove_interpolated_string",
33
+ "remove_function_call_parens",
34
+ "remove_types",
35
+ "remove_if_expression",
36
+ { "rule": "rename_variables", "globals": ["$default", "$roblox"], "include_functions": true },
37
+ "group_local_assignment",
38
+ "convert_square_root_call",
39
+ "convert_luau_number"
40
+ ]
41
+ }
42
+ `
43
+
44
+ // darkluaConfigNames are the files darklua itself looks for, in the project folder.
45
+ var darkluaConfigNames = []string{".darklua.json", ".darklua.json5"}
46
+
47
+ // obfuscation is --obfuscate for one WriteResults call. darklua reads files and writes files, so the compiled Luau
48
+ // is written to a temporary folder that mirrors the output folder, and one darklua run writes the obfuscated files
49
+ // to the output folder. Each output file is then written once, already obfuscated.
50
+ type obfuscation struct {
51
+ root string // the temporary folder
52
+ staging string // root/out, the mirror of the output folder
53
+ }
54
+
55
+ func newObfuscation() (*obfuscation, error) {
56
+ root, err := os.MkdirTemp("", "rbxts-go-obfuscate-")
57
+ if err != nil {
58
+ return nil, err
59
+ }
60
+ return &obfuscation{root: root, staging: nodepath.Native.Join(root, "out")}, nil
61
+ }
62
+
63
+ // path is where the compiled text of an output file goes before darklua runs.
64
+ func (o *obfuscation) path(c *Context, outPath string) string {
65
+ return nodepath.Native.Join(o.staging, nodepath.Native.Relative(c.Translator.OutDir, outPath))
66
+ }
67
+
68
+ // run has darklua write every staged file, obfuscated, to the output folder.
69
+ func (o *obfuscation) run(c *Context) error {
70
+ config := ""
71
+ for _, name := range darkluaConfigNames {
72
+ candidate := nodepath.Native.Join(c.Data.ProjectPath, name)
73
+ if _, err := os.Stat(candidate); err == nil {
74
+ config = candidate
75
+ break
76
+ }
77
+ }
78
+ if config == "" {
79
+ config = nodepath.Native.Join(o.root, "darklua.json")
80
+ if err := os.WriteFile(config, []byte(defaultDarkluaConfig), 0o666); err != nil {
81
+ return err
82
+ }
83
+ }
84
+ command := exec.Command("darklua", "process", o.staging, c.Translator.OutDir, "--config", config)
85
+ // The project folder, so that a tool manager (rokit, aftman) picks the project's darklua.
86
+ command.Dir = c.Data.ProjectPath
87
+ output, err := command.CombinedOutput()
88
+ if errors.Is(err, exec.ErrNotFound) {
89
+ return errors.New("--obfuscate needs darklua, and it was not found on the PATH")
90
+ }
91
+ if err != nil {
92
+ return fmt.Errorf("darklua failed: %w\n%s", err, strings.TrimSpace(string(output)))
93
+ }
94
+ return nil
95
+ }
96
+
97
+ func (o *obfuscation) close() {
98
+ _ = os.RemoveAll(o.root)
99
+ }
@@ -190,9 +190,13 @@ type Updater interface {
190
190
 
191
191
  What the host does today, which is more than the above asks of a plugin and can become less:
192
192
 
193
- - Every cycle calls `TransformFile` for every file of `Host.Files`, in program order, and then `Finish`. Nothing is
194
- kept from the last cycle on the plugins' side. Only the compile after it is incremental: a file whose printed text
195
- did not change is not compiled again.
193
+ - A cycle calls `TransformFile` only for the files the change can reach: the changed files and every file that
194
+ imports one of them, directly or through other files. The other files keep the text they were printed with last
195
+ time. A plugin that needs something from every file, such as a table of all the call sites of the program, must read
196
+ it from `Host.Program` (in `New`, `Update` or its first `TransformFile`), not gather it from the `TransformFile`
197
+ calls. `Finish` is called every cycle. Every file goes through again when files are added or removed, when a
198
+ declaration file or a script that can change the global scope changed, when a file resolves its imports
199
+ differently, and whenever setget is on.
196
200
  - `changed` is worked out against the last program the transformer was given, whether that cycle ended well or not.
197
201
  It holds any file of the program, declaration files and JSON files too, so it is not a subset of `Host.Files`.
198
202
  - `New` is called again, still with `Host.Watch` true, when the old transformers cannot be trusted or do not fit:
package/package.json CHANGED
@@ -1,46 +1,46 @@
1
1
  {
2
- "name": "@data-vegle/rbxts-go",
3
- "version": "1.0.0",
4
- "description": "The roblox-ts compiler in Go, on TypeScript 7, with native plugins. One dev dependency brings the compiler launcher, the roblox-ts types and TypeScript 7.",
5
- "license": "MIT",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/Vegle-Data-Apps/rbxts-go.git"
9
- },
10
- "bin": {
11
- "rbxts-go": "npm/rbxts-go.sh",
12
- "rbxtsc": "npm/rbxts-go.sh"
13
- },
14
- "types": "npm/index.d.ts",
15
- "files": [
16
- "npm",
17
- "scripts/build-with-plugins.sh",
18
- "scripts/setup-third-party.sh",
19
- "patches",
20
- "docs/plugin-api.md",
21
- "go.mod",
22
- "go.sum",
23
- "cli",
24
- "compile",
25
- "internal",
26
- "luau",
27
- "pluginapi",
28
- "plugins",
29
- "project",
30
- "transformer",
31
- "tsbridge",
32
- "watch",
33
- "!**/*_test.go",
34
- "!**/testdata",
35
- "THIRD_PARTY_NOTICES.md",
36
- "README.rbxtsgo.md"
37
- ],
38
- "publishConfig": {
39
- "access": "public"
40
- },
41
- "dependencies": {
42
- "@rbxts/compiler-types": "3.0.0-types.0",
43
- "@rbxts/types": "^1.0.915",
44
- "typescript": "7.1.0-beta"
45
- }
2
+ "name": "@data-vegle/rbxts-go",
3
+ "version": "1.1.0",
4
+ "description": "The roblox-ts compiler in Go, on TypeScript 7, with native plugins. One dev dependency brings the compiler launcher, the roblox-ts types and TypeScript 7.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Vegle-Data-Apps/rbxts-go.git"
9
+ },
10
+ "bin": {
11
+ "rbxts-go": "npm/rbxts-go.sh",
12
+ "rbxtsc": "npm/rbxts-go.sh"
13
+ },
14
+ "types": "npm/index.d.ts",
15
+ "files": [
16
+ "npm",
17
+ "scripts/build-with-plugins.sh",
18
+ "scripts/setup-third-party.sh",
19
+ "patches",
20
+ "docs/plugin-api.md",
21
+ "go.mod",
22
+ "go.sum",
23
+ "cli",
24
+ "compile",
25
+ "internal",
26
+ "luau",
27
+ "pluginapi",
28
+ "plugins",
29
+ "project",
30
+ "transformer",
31
+ "tsbridge",
32
+ "watch",
33
+ "!**/*_test.go",
34
+ "!**/testdata",
35
+ "THIRD_PARTY_NOTICES.md",
36
+ "README.rbxtsgo.md"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "dependencies": {
42
+ "@rbxts/compiler-types": "3.0.0-types.0",
43
+ "@rbxts/types": "^1.0.915",
44
+ "typescript": "7.1.0-beta"
45
+ }
46
46
  }
package/plugins/apply.go CHANGED
@@ -66,7 +66,7 @@ func ApplyWithStats(ctx context.Context, p *tsbridge.Project, decision Decision,
66
66
  }
67
67
  if decision.Action == ActionPlugins {
68
68
  run := &nativeRun{}
69
- result, err := run.transform(ctx, p, decision, files, build, false, func() *accessorNames { return collectAccessorNames(p) })
69
+ result, err := run.transform(ctx, p, decision, files, build, false, nil, func() *accessorNames { return collectAccessorNames(p) })
70
70
  if err != nil {
71
71
  return nil, stats, err
72
72
  }
@@ -16,7 +16,7 @@ const jsCompilerPath = "node_modules/roblox-ts/out/CLI/cli.js"
16
16
  // flagsOfOurs are the flags only rbxtsgo has that take a value, and boolFlagsOfOurs the ones that don't.
17
17
  var (
18
18
  flagsOfOurs = []string{"checkers", "cpuprofile", "setgetPrefix"}
19
- boolFlagsOfOurs = []string{"setget", "no-setget", "fix-emit", "no-fix-emit"}
19
+ boolFlagsOfOurs = []string{"setget", "no-setget", "fix-emit", "no-fix-emit", "obfuscate", "no-obfuscate"}
20
20
  )
21
21
 
22
22
  // passAlongArgs removes our own flags from the command line, because yargs in the JS compiler rejects unknown ones.
package/plugins/native.go CHANGED
@@ -89,9 +89,11 @@ func changedFiles(previous map[tspath.PathKey]*ast.SourceFile, program []*ast.So
89
89
  // transform runs the plugin pass over one program: New or Update, then every file through setget (if it is on) and
90
90
  // the plugins in order, then the printing, then Finish. It all happens on the calling goroutine with one checker,
91
91
  // apart from the printing, which calls no plugin. names gives setget's accessor names, and is only called for setget.
92
+ // reused holds, by path, the outcome of the files that are not transformed again: they keep that outcome. It is nil when
93
+ // every file goes through.
92
94
  //
93
95
  // An error of AddDiagnostic comes back as a *shared.DiagnosticError.
94
- func (r *nativeRun) transform(ctx context.Context, p *tsbridge.Project, decision Decision, files []*ast.SourceFile, build *Build, watch bool, names func() *accessorNames) (result *nativeResult, err error) {
96
+ func (r *nativeRun) transform(ctx context.Context, p *tsbridge.Project, decision Decision, files []*ast.SourceFile, build *Build, watch bool, reused map[tspath.PathKey]*fileOutcome, names func() *accessorNames) (result *nativeResult, err error) {
95
97
  // After a panic, an Exit or an error, nothing says what state a transformer is in. The next program starts with New.
96
98
  kept := false
97
99
  defer func() {
@@ -168,6 +170,9 @@ func (r *nativeRun) transform(ctx context.Context, p *tsbridge.Project, decision
168
170
  emits := make([]*printer.EmitContext, len(files))
169
171
  var reported []*shared.Diagnostic
170
172
  for i, file := range files {
173
+ if reused[file.PathKey()] != nil {
174
+ continue
175
+ }
171
176
  emit := printer.NewEmitContext()
172
177
  node := file.AsNode()
173
178
  doing := "while transforming " + file.FileName().AsString()
@@ -217,6 +222,10 @@ func (r *nativeRun) transform(ctx context.Context, p *tsbridge.Project, decision
217
222
  outcomes[i].panicText = fmt.Sprint(r)
218
223
  }
219
224
  }()
225
+ if old := reused[file.PathKey()]; old != nil {
226
+ outcomes[i] = *old
227
+ return
228
+ }
220
229
  printFile(file, transformed[i], emits[i], &outcomes[i])
221
230
  })
222
231
  for i := range outcomes {
@@ -129,8 +129,21 @@ func (s *Session) applyNative(ctx context.Context, p *tsbridge.Project, decision
129
129
  s.native = &nativeRun{}
130
130
  change = &tsbridge.Change{Full: true, Reason: "first run of the plugins"}
131
131
  }
132
- s.LastRedone = len(files)
133
- result, err := s.native.transform(ctx, p, decision, files, build, true, func() *accessorNames { return s.collectNames(p) })
132
+ // As in JS watch mode, the plugins only get the files a change can reach: the changed files and everything that
133
+ // imports them, directly or not. The others keep the text they were printed with last time. Plugins gather what
134
+ // they need from other files by reading the program, not from the calls to TransformFile. Setget's accessor names
135
+ // can change what any file prints, so with setget every file goes through again.
136
+ var kept map[tspath.PathKey]*fileOutcome
137
+ if !change.Full && !decision.Setget && s.outcomes != nil {
138
+ kept = make(map[tspath.PathKey]*fileOutcome, len(files))
139
+ for _, file := range files {
140
+ if outcome := s.outcomes[file.PathKey()]; outcome != nil && !change.Reaches(file) {
141
+ kept[file.PathKey()] = outcome
142
+ }
143
+ }
144
+ }
145
+ s.LastRedone = len(files) - len(kept)
146
+ result, err := s.native.transform(ctx, p, decision, files, build, true, kept, func() *accessorNames { return s.collectNames(p) })
134
147
  if err != nil {
135
148
  return nil, err
136
149
  }
@@ -63,6 +63,8 @@ type ProjectOptions struct {
63
63
  // FixEmit is rbxtsgo's own. It turns on the output fixes that differ from JS 3.0.0 for code that compiles there. See
64
64
  // docs/known-differences.md. BuildKey hashes these options, so changing it forces a rebuild.
65
65
  FixEmit bool
66
+ // Obfuscate is rbxts-go's own: darklua obfuscates the compiled files as they are written. See compile/obfuscate.go.
67
+ Obfuscate bool
66
68
  }
67
69
 
68
70
  // DefaultProjectOptions is DEFAULT_PROJECT_OPTIONS.
@@ -1,6 +1,8 @@
1
1
  package tsbridge
2
2
 
3
3
  import (
4
+ "os"
5
+
4
6
  "github.com/microsoft/TypeScript/tsc/internal/ast"
5
7
  "github.com/microsoft/TypeScript/tsc/internal/bundled"
6
8
  "github.com/microsoft/TypeScript/tsc/internal/compiler"
@@ -54,8 +56,24 @@ func (p *Project) UpdatePrinted(source *Project, changed map[tspath.PathKey]stri
54
56
  CompilerHost: compiler.NewCachedFSCompilerHost(bundled.WrapFS(osvfs.FS()), bundled.LibPath(), &tsc.ExtendedConfigCache{}, nil, nil),
55
57
  printed: changed,
56
58
  }
59
+ // The printed program can hold files the source program lacks: the ones only printed text imports, such as a
60
+ // module that a plugin's generated code brings in. They stay as they are while the file on disk has the text they
61
+ // were parsed from. Parsing one again would give a new object for the same declaration file, which reads as a
62
+ // change to the global scope and makes every file dirty.
63
+ extras := map[tspath.PathKey]*ast.SourceFile{}
64
+ extrasChanged := false
65
+ for path, old := range previous {
66
+ if _, ok := sourceFiles[path]; ok {
67
+ continue
68
+ }
69
+ if text, err := os.ReadFile(old.FileName().AsString()); err == nil && string(text) == old.Text() {
70
+ extras[path] = old
71
+ } else {
72
+ extrasChanged = true
73
+ }
74
+ }
57
75
  // Reuse needs the same files, with only printed ones different.
58
- reusable := !full && len(sourceFiles) == len(previous)
76
+ reusable := !full && !extrasChanged && len(sourceFiles)+len(extras) == len(previous)
59
77
  if reusable {
60
78
  for path, file := range sourceFiles {
61
79
  old, ok := previous[path]
@@ -93,6 +111,9 @@ func (p *Project) UpdatePrinted(source *Project, changed map[tspath.PathKey]stri
93
111
  merged[path] = file
94
112
  }
95
113
  }
114
+ for path, old := range extras {
115
+ merged[path] = old
116
+ }
96
117
  host.previous = merged
97
118
  program := compiler.NewProgram(compiler.ProgramOptions{Config: source.config, Host: host})
98
119
  return &Project{config: source.config, program: program, notes: source.notes}