Macros and extensions
Sloptor accepts all rbxtsc input and adds a few compile-time macros and language extensions. None of them change the output of code rbxtsc already accepts.
| Macro | Inlines |
|---|---|
$env("KEY", "fallback") · $env.KEY |
env vars from .env and .env.<NODE_ENV> |
$keys<T>() |
a type's string keys, using the checker |
$nameof(expr) |
the trailing identifier name as a string |
$file("data.json") |
a file's contents as a Luau table for JSON or a string for text |
$getModuleTree("shared/systems") |
a folder's module tree, with no index.ts required |
Extensions
Section titled “Extensions”These compile under sloptor but not under rbxtsc. Output for code that rbxtsc accepts is byte-identical, except where the fork changed behavior. Those cases follow the verified fork archive.
Loop labels
Section titled “Loop labels”Example: outer: for (...) { break outer; continue outer; }.
rbxtsc rejects every labeled statement with labels are not supported!. Sloptor compiles them.
Luau has no labels and no goto. A label that a plain break or continue can't satisfy lowers to a "none" | "break" | "continue" string flag. Sloptor emits if _outer == "break" then break end style checks after each enclosing loop to unwind outwards.
Labels work on these statements:
whileanddo/while- both
forshapes - every
for-ofshape, including$range switch
A label on a non-loop statement, such as a block or an if, gets wrapped in repeat ... until true so code can break out of it. Sloptor emits the flag variable and its per-iteration reset only when a label needs them.
Not supported:
- A label crossing a
try/catch. TheTS.TRY_BREAKandTS.TRY_CONTINUEreroute has no label. - A labeled non-loop statement that also contains an unlabeled
breakorcontinueaimed at an enclosing loop.
Both cases give a clear diagnostic, never wrong output.
Named function expressions
Section titled “Named function expressions”Example: useEffect(function onName() { ... }, [name]).
rbxtsc rejects every one with Function expression names are not supported!. Sloptor compiles them in any expression position:
- a call argument
- the right-hand side of an assignment
- a
constorletinitializer - an object-literal value
- a parameter default
- a ternary arm
- a short-circuit operand
- the callee of an immediately invoked function expression
Sloptor lifts the name into a local function <name>() ... end declaration right before the expression, and the expression becomes a reference to it. This is the only Luau form that gives a debug name in a traceback. A function expression assigned to anything else, including a plain local, compiles to an unnamed closure.
Recursion through the name works, because the name resolves to the lifted declaration and not to the outer binding. A name that collides with one already in scope gets a new one, such as letNamed_1 for letNamed. Lifting never shadows. A conditionally evaluated operand keeps its declaration inside the branch, so sloptor builds the closure only when the expression runs.
An async named function expression still lifts to local function <name>(), so the traceback keeps the debug name. Sloptor then assigns name = TS.async(name). This is ordinary Lua, since local function name is local name; name = function() ... end. The body already closes over the local, and the write points it at the wrapper. A self-call returns a Promise.
A generator named function expression lifts like a generator declaration: local function name() return TS.generator(function() ... end) end. A self-call then creates a new generator instead of re-entering the iterator callback.
Not supported: async generators. They already have their own diagnostic.
This also matters for transformer plugins. A plugin that moves a hook argument into a memo = function onName() {} assignment no longer breaks the build.
Module trees on folders
Section titled “Module trees on folders”rbxtsc requires the specifier to resolve as a module, so pointing it at a folder only works if the folder has an index.ts. Sloptor resolves folder specifiers directly:
- Relative ones, such as
"./systems", resolve against the importing file. - Non-relative ones, such as
"shared/systems", resolve againstbaseUrlandpaths, then against the project root, such as"src/shared/systems".
The usual server-import and isolation guards still apply.
Compile-time environment macro
Section titled “Compile-time environment macro”$env replaces the rbxts-transform-env plugin. You don't need a Node sidecar, plugin install, or typings package.
$env("GAME_NAME")inlines the variable's value as a Luau string literal, ornilwhen unset.$env("GAME_NAME", "fallback")inlines the value or the fallback.$env.GAME_NAMEand$env["GAME_NAME"]behave like the one-argument call.
Values resolve at compile time in this priority order: process environment, then .env.<NODE_ENV>, then .env. Keep the files next to your tsconfig.json. NODE_ENV itself resolves from the process environment, then .env. The .env format is KEY=VALUE lines with # comments and optional single or double quotes. Version 1 inlines strings only.
Sloptor injects the type declaration declare const $env: ... automatically, so you need no .d.ts. Names and fallbacks must be string literals so the compiler can inline the value. Dynamic names get a clear diagnostic. Editors never see the injected declaration, so sloptor writes an on-disk rotor.d.ts companion for the macros. See Editor types and rebuilds.
If you migrate from rbxts-transform-env, drop the plugin from your tsconfig. Its modern module-export form, import { $env } from "rbxts-transform-env", still works through the plugin sidecar and never collides with the built-in global.
Compile-time name macro
Section titled “Compile-time name macro”$nameof inlines the trailing identifier or property name of an expression as a Luau string literal:
$nameof(player.Humanoid.Health)becomes"Health".$nameof(foo)becomes"foo".
Sloptor reads the argument's source but never evaluates it, so it doesn't exist at runtime and stays in sync with refactors and renames. An expression with no statically known trailing name, such as an index, a call, or a literal, gets a clear diagnostic.
Type: declare function $nameof(item: unknown): string;
Compile-time keys macro
Section titled “Compile-time keys macro”$keys inlines an array of the string keys of T as a Luau array literal. It uses the type checker, like the rbxts-transformer-keys plugin. $keys<{ x: number; y: string }>() becomes { "x", "y" }.
Keys come from the apparent and declared string properties of the type, in declaration order. Sloptor skips number and symbol keys. A type with no enumerable string keys, such as {}, gives an empty array, which is a valid result. A missing type argument is a diagnostic.
Type: declare function $keys<T>(): string[];. The compiler consumes the generic at compile time and the macro fills the array.
Native declaration emit
Section titled “Native declaration emit”Sloptor emits every .d.ts from the native compiler instead of routing declaration emit through the Node worker. It also ports the two afterDeclarations transformers of rbxtsc to Go. One rescopes the types triple-slash reference. The other rewrites paths and baseUrl specifiers to relative ones.
The rewrite works from a resolution result driven by a reparse of the emitted text, never a regular expression. Sloptor shifts the .d.ts.map generated columns to follow each spliced specifier.
One deliberate divergence from rbxtsc comes with this. The JS transformer only resolved specifiers that the program had already resolved for that file. An import("alias/...") type that the declaration emitter synthesized for an inferred type kept its alias spelling and was unresolvable when published. Sloptor falls back to a real resolver pass for those, so it rewrites them too.
Not supported: an afterDeclarations transformer of your own. The native compiler has no custom-transformer hook. A project that declares one gets a clear per-build warning and no silent drop. No plugin in the @rbxts ecosystem includes one.
Editor types and rebuilds
Section titled “Editor types and rebuilds”Build and check refresh rotor.d.ts for $env, $keys, $nameof, and $file. Include it in your tsconfig for editor checking. $getModuleTree keeps the declaration from @rbxts/compiler-types.
Environment values and $file contents take part in incremental invalidation. Watch mode tracks their files, including missing files and importer-relative paths outside the source root. The next build picks up process environment changes. To change the inherited environment of a command-line watch process, restart it.
Supply version and build timestamps through explicit $env values or generated TypeScript sources.