https://github.com/thinknathan/tstl-const-propagation
TypeScriptToLua plugin that performs constant propagation and constant folding
https://github.com/thinknathan/tstl-const-propagation
constant-folding constant-propagation lua tstl tstl-extension tstl-plugin typescript
Last synced: 3 months ago
JSON representation
TypeScriptToLua plugin that performs constant propagation and constant folding
- Host: GitHub
- URL: https://github.com/thinknathan/tstl-const-propagation
- Owner: thinknathan
- License: cc0-1.0
- Created: 2023-12-06T01:26:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-02-01T17:52:55.000Z (4 months ago)
- Last Synced: 2026-02-02T02:01:18.052Z (4 months ago)
- Topics: constant-folding, constant-propagation, lua, tstl, tstl-extension, tstl-plugin, typescript
- Language: TypeScript
- Homepage:
- Size: 472 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tstl-const-propagation
[](https://github.com/thinknathan/tstl-const-propagation/actions/workflows/ci.yml) 
TypeScriptToLua plugin that performs [constant propagation](https://en.wikipedia.org/wiki/Constant_folding#Constant_propagation).
As of v2.0.0, [constant folding](https://en.wikipedia.org/wiki/Constant_folding) is also performed.
The variable label must be written in SNAKE_CASE to be eligible.
## Limitations
- Only applies to local variables that are declared in a single statement on their own line: multi-line declarations and variables that are declared and written in separate statements are not eligible
- Only operates on one file at a time: cannot copy variables between separate files
- Only applies to values that are string literals or numeric literals
- Only intended for use with actual constants (values that do not change at runtime)
Redundancy warning: Your target Lua runtime may already perform constant propagation and constant folding.
:exclamation: Use this and any code transformation plugin with caution. Mistakes are possible.
## Example
```ts
const FOO = 60;
bar(FOO);
const result = FOO + 40;
```
Becomes:
```lua
bar(60)
local result = 100
```
## Installation
Requires TSTL >= 1.22.0
1. Install this plugin
```bash
yarn add tstl-const-propagation -D
# or
npm install tstl-const-propagation --save-dev
```
2. Add `tstl-const-propagation` to `tstl.luaPlugins` in `tsconfig.json`
```diff
{
"tstl": {
"luaPlugins": [
+ { "name": "tstl-const-propagation" }
],
}
}
```
## License
CC0