1.0.1 • Published 1 year ago

rbxts-transformer-switchcase v1.0.1

Weekly downloads
-
License
MPL-2.0
Repository
github
Last release
1 year ago

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

let a = 2

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

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

    default:
        print("uhh what??");
        break;
}
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
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.

{
    "compilerOptions": {
        ...
        "plugins": [
            {
                "transform": "rbxts-transformer-switchcase",
                "disableSwitchComments": false,
                "disableBreakComments": true
            }
        ]
    }
}
IdentifierTypeDefaultDescription
disableSwitchCommentsbooleanfalsewhether or not --switch should be added in front of if statements generated by the transformer
disableBreakCommentsbooleanfalsewhether 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.