Coder Social home page Coder Social logo

ketrab2004-roblox / rbxts-transformer-simpleswitch Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 255 KB

Transformer for converting switch cases to simple ifelse blocks so luau can better optimize them

Home Page: https://www.npmjs.com/package/rbxts-transformer-switchcase

TypeScript 37.63% Lua 62.37%
rbxts roblox roblox-ts transformer typescript typescript-transformer

rbxts-transformer-simpleswitch's Introduction

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.

rbxts-transformer-simpleswitch's People

Contributors

ketrab2004 avatar

Watchers

 avatar

rbxts-transformer-simpleswitch's Issues

No tests for special edge cases

  • Nested switches
    • inner switch is missing a break
    • outer switch is missing a break
    • both are missing a break
    • (the above/previous but with more than 2 switches!)
  • With invalid code
    • multiple default clauses
    • code after a break statement

Example

Create an example to show how you can use it (and to test if it even works)

Return and continue should also work

Currently

let a = 2;

switch (a) {
    case 1:
        return "one";

    case 2:
        return "two";

    default:
        return "uhhmmm???";
}

doesn't get converted to if else while it should.

  • Return and continue should also work
  • Create tests for return and continue
  • Throw should also work

Test fails

image

TypeError: Cannot read properties of undefined (reading 'updateJsxExpression') in src/index.test.ts at line 19

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.