Skip to content

ketrab2004-Roblox/rbxts-transformer-simpleswitch

Repository files navigation

rbxts-transformer-switchcase

npm version npm license npm test

Converts switch cases into simple if else blocks, to make it faster and to make it more legible.

Example

Input typescript
let a = 2

switch (a) {
    case 1:
        print("one");
        break;

    case 2:
        print("two");
        break;

    default:
        print("uhh what??");
        break;
}
Output luau without the transformer
local a = 2
repeat
    if a == 1 then
        print("one")
        break
    end
    if a == 2 then
        print("two")
        break
    end
    print("uhh what??")
    break
until true
Output luau with the transformer
local a = 2
-- switch
if a == 1 then
    print("one")
    -- break
elseif a == 2 then
    print("two")
    -- break
else
    print("uhh what??")
    -- break
end

How to use

  1. Install by running npm i rbxts-transformer-switchcase --save-dev

  2. Add it as a plugin to your tsconfig.json, see options here

    {
        "compilerOptions": {
            ...
            "plugins": [
                {
                    "transform": "rbxts-transformer-switchcase",
                    ...
                }
            ]
        }
    }
  3. Try it out

Settings

In the tsconfig.json you can add settings for the plugin.

Example
{
    "compilerOptions": {
        ...
        "plugins": [
            {
                "transform": "rbxts-transformer-switchcase",
                "disableSwitchComments": false,
                "disableBreakComments": true
            }
        ]
    }
}
Identifier Type Default Description
disableSwitchComments boolean false whether or not --switch should be added in front of if statements generated by the transformer
disableBreakComments boolean false whether or not removed break statements should get a --break in their place

Building

Setup

Install dependencies using npm i.

Build the transformer

Run npm run build in the root of the project.

Build the example

Run npm run build in /example.

Run tests

Run npm run test for quick and simple tests and npm run test:fancy for more readable results.