Skip to content

thinknathan/tstl-const-propagation

Repository files navigation

tstl-const-propagation

CI GitHub License

TypeScriptToLua plugin that performs constant propagation.

As of v2.0.0, 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.

❗ Use this and any code transformation plugin with caution. Mistakes are possible.

Example

const FOO = 60;
bar(FOO);
const result = FOO + 40;

Becomes:

bar(60)
local result = 100

Installation

Requires TSTL >= 1.22.0

  1. Install this plugin
yarn add tstl-const-propagation -D
# or
npm install tstl-const-propagation --save-dev
  1. Add tstl-const-propagation to tstl.luaPlugins in tsconfig.json
{
	"tstl": {
		"luaPlugins": [
+			{ "name": "tstl-const-propagation" }
		],
	}
}

License

CC0