# laux-compiler

> LAUX superset of Lua which compiles LAUX code to Lua

Latest version **1.6.0** (published 2023-08-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install laux-compiler
pnpm add laux-compiler
yarn add laux-compiler
bun add laux-compiler
```

Provides the command `lauxc`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.6.0 |
| Published | 2023-08-08 |
| First published | 2023-04-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 16 |
| Unpacked size | 591.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Atlas/8char |
| Maintainers | 8char |
| Keywords | transpiler |

## Links

- npm: https://www.npmjs.com/package/laux-compiler
- Repository: https://github.com/8char/laux-compiler
- Homepage: https://github.com/8char/laux-compiler#readme
- Issues: https://github.com/8char/laux-compiler/issues
- npm.io page: https://npm.io/package/laux-compiler

## Dependencies (16)

- [chai](https://npm.io/package/chai.md) ^3.5.0
- [chalk](https://npm.io/package/chalk.md) ^1.1.3
- [mocha](https://npm.io/package/mocha.md) ^10.2.0
- [extend](https://npm.io/package/extend.md) ^3.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [luamin](https://npm.io/package/luamin.md) ^1.0.4
- [chokidar](https://npm.io/package/chokidar.md) ^3.5.3
- [commander](https://npm.io/package/commander.md) ^2.9.0
- [fast-glob](https://npm.io/package/fast-glob.md) ^3.2.4
- [fs-jetpack](https://npm.io/package/fs-jetpack.md) ^0.11.0
- [underscore](https://npm.io/package/underscore.md) ^1.8.3
- [detect-indent](https://npm.io/package/detect-indent.md) ^5.0.0
- [@semantic-release/git](https://npm.io/package/@semantic-release/git.md) ^10.0.1
- [@semantic-release/npm](https://npm.io/package/@semantic-release/npm.md) ^10.0.3
- [@semantic-release/commit-analyzer](https://npm.io/package/@semantic-release/commit-analyzer.md) ^9.0.2
- [@semantic-release/release-notes-generator](https://npm.io/package/@semantic-release/release-notes-generator.md) ^10.0.3

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.6.0 (latest) — 2023-08-08
- 1.5.0 — 2023-08-08
- 1.4.1 — 2023-08-08
- 1.4.0 — 2023-08-08
- 1.3.3 — 2023-08-08
- 1.3.2 — 2023-08-08
- 1.3.1 — 2023-08-08
- 1.3.0 — 2023-08-08
- 1.2.5 — 2023-08-07
- 1.2.4 — 2023-08-07
- 1.2.3 — 2023-08-05
- 1.2.2 — 2023-08-05
- 1.2.1 — 2023-07-06
- 1.2.0 — 2023-07-06
- 1.1.7 — 2023-07-05
- … 12 more at https://npm.io/package/laux-compiler/versions

## README

<br />
<div align="center">

  <img src="https://i.imgur.com/0SR2KVb.png" width="256" height="256"/>

  <h3 align="center" style="margin-bottom: 0;">LAUX - GLUA Transpiler</h3>
  <sup>( LAUX )</sup>
  <p align="center">
    This is a fork of LAU made by <a href="https://github.com/alexanderarvidsson" target="_blank">Metamist (Alexander Arvidsson)</a>
    LAU as a project is dead. This fork is an attempt to revitalise Metamist's project, with a few changes to syntax & functionality.
    <br />
    <br />
    <a href="https://github.com/8char/laux-compiler/issues">Report Bug</a>
    ·
    <a href="https://github.com/8char/laux-compiler/issues">Request Feature</a>
  </p>
</div>

# What is LAUX?

LAUX is a superset of Lua, adding syntax sugar on top of Lua. It does however still work with vanilla Lua.

# How to use

You will need to install [Node.js](https://nodejs.org) to use this.

Open up a terminal of your choice, and type this.

```bash
> npm i -g laux-compiler
```

To transpile you simply go the folder your project belongs to

```bash
> lauxc watch ./laux ./lua
```

## Workspaces

There's also an option to use workspaces, which simply means using a configuration file. The config file must be `lauxconfig.json` and be located in the root folder.

This allows you to use a lot more options, of which one of the intersting ones are merges. You can merge multiple files into 1 single output file, which are very useful for releases.

An example of a config file would be this.

```json
{
  "path": {
    "input": "./laux",
    "output": "./lua"
  },
  "merges": [
    {
      "filesGlob": ["atlas_jobcreator/fields/**/*.laux"],
      "output": "atlas_jobcreator/fields"
    },
    {
      "filesGlob": ["atlas_jobcreator/currencies/**/*.laux"],
      "output": "atlas_jobcreator/currencies"
    }
  ]
}
```

To actually use the merge features, you would have to use the release option, which is denoted as -r

### No options

```bash
> lauxc workspace
```

### Release that merges files

```bash
> lauxc workspace -r
```

# What does it add?

## Functions

LAUX adds fat arrows & thin arrows.

Example of fat arrow

```lua
local Foo = {}
Foo.Bar = (self) => print(self) end -- Foo.Bar = function(self) print(self) end
```

Example of thin arrow doing the same

```lua
local Foo = {}
Foo.Bar = () -> print(self) end -- Foo.Bar = function(self) print(self) end
```

Thin arrow is essentially just a fat arrow, but it automatically adds self, just like : adds self automatically in contrast to . in default Lua.

## Decorators

LAUX adds decorators which can mutate functions to allow for things such as deprecation, singeltons, registries, mixin functionality, memoization, validation, rate limiting, caching, authentication/authorization, logging, etc...
_You can currently only have one decorator per function, however you can combine them in to one larger decorator_

```lua
-- These deprecated functions can be defined in
-- your library & be imported using LAUX's
-- import statement
local function deprecated(func)
  return function(...)
    local name = nil
    local index = 1

    while true do
        local info = debug.getinfo(func, "S")
        if not info then
            break
        end

        if info.what == "Lua" and info.name then
            name = info.name
            break
        end

        func = debug.getupvalue(func, index)
        if not func then
            break
        end

        index = index + 1
    end

    MsgC(Color(255, 165, 0), "[WARN] The function " .. name .. " is deprecated. Please look to using alternatives as this will cease to exist!")
    func(...)
  end
end

-- Imagine we have this TestFunc that comes from SomeLibrary,
-- we want to warn users upon each use so that they use
-- something different
@deprecated
function PrintPlayerName(ply)
  print(ply:Nick())
end
```

## Types

**This is real time type checking, so don't run it in something that gets run A TON, like every frame**
_Currently doens't work with arrow functions, fixing later_

```lua
function Foo(bar: string)
  -- do nothing
end
```

Outputs to

```lua
function Foo(bar)
  local __lau_type = (istable(bar) and bar.__type and bar:__type()) or type(bar)
  assert(__lau_type == "string", "Expected parameter `bar` to be type `string` instead of `" .. __lau_type .. "`")
end
```

This also accepts multiple types by using the | (pipe) character

```lua
function Foo(bar: string|MyAddon.Bar)
  -- do nothing
end
```

Outputs to

```lua
function Foo(bar)
  local __lau_type = (istable(bar) and bar.__type and bar:__type()) or type(bar)
  assert(__lau_type == "string" or __lau_type == "MyAddon.Bar", "Expected parameter `bar` to be type `string|MyAddon.Bar` instead of `" .. __lau_type .. "`")
end
```

## Mutations

LAUX adds mutations. Those are just simple shortcuts

```lua
x += 5 -- x = x + 5
x *= 2 -- x = x * 2
x /= 2 -- x = x / 2
x++ -- x = x + 1
x -= 1 -- x = x - 1. Notice x-- doesn't exist
x ||= 2 -- x = x or 2
x ..= "me" -- x = x .. "me"
x %= 2 -- x = x % 2
```

## Shortcut Expressions

LAUX adds shortcuts for quickly doing a generic action.

```lua
stopif i > 5 -- if (i > 5) then return end
breakif i > 2 -- if (i > 2) then break end
continueif i > 8 -- if (i > 8) then continue end
```

## Classes

LAUX adds JavaScript like classes. I assume you already know what a class is.

Syntax

```lua
[public] Class name [extends Parent] end
```

Example

```lua
class Foo
  -- We can have static attributes
  static ENUMS = {}
  -- We can also non static attributes
  name = "Bar"

  constructor(foobar: string)
    self.foobar = foobar
  end

  setFoobar(val: string) self.foobar = val end
  getFoobar() return self.foobar end

  -- If we are not using a public class we need to add __type() function.
  -- This is to avoid overlapping names for type checking
  __type()
    return "MyAddon.Foo"
  end
end

-- Don't need new keyword.
local foo1 = Foo()
```

Now if we wish, we can extend upon it

```lua
class Bar extends Foo
  constructor(foobar)
    -- Because we're extending we need to use super()
    -- You need to pass the arguments your parent (extends) need
    super(foobar)

    -- Lets now get our name we got from Foo
    print(self.name)
  end

  -- Only on Bar, not Foo
  uniqueMethod() end
end
```

Classes by default are private, so we can make it public by using the public keyword

```lua
public class AtlasShop.Items.Health extends AtlasShop.Item
  -- Since this is a public class you don't need a type
  -- __type will automatically return the name of the class
end
```

## Safe member navigator

LAUX adds a safe member navigatior. This allows you to use index something in a table without having to check if an element exists.

LAUX code

```lua
if (groups?[groupKind]?.members?.name) then

end
```

Lua code

```lua
if (((groups and groups[groupKind]) and groups[groupKind].members) and groups[groupKind].members.name) then

end
```

## Import Statement

The import statement allows for importing of members of an object. The import statement itself doesn't transpile down in to anything, however what it does is it adds the parent object before each index of it in code. Heres an example:

```lua
import print, warn, log from AtlasUI

print("Hello there!")
warn("Hello there!")
log("Hello there!")
```

Will transpile down to:

```lua
AtlasUI.print("Hello there!")
AtlasUI.warn("Hello there!")
AtlasUI.log("Hello there!")
```

## Spread operator

This is the same operator found in JavaScript. It functions like table.concat/unpack.

```lua
local a = { 2, 3 }
local b = { 1, ...a, 4 }
PrintTable(b) -- { 1, 2, 3, 4 }
```

## Deconstructing

Same as in JavaScript.

```lua
local tbl = { a = 5, b = 3 }
local { a, b } = tbl -- local a, b = tbl.a, tbl.b
print(a) -- 5
print(b) -- 3
```

## For of statement

You can do a generic for loop, which is the equivalent of doing

```lua
for i, v in pairs(tbl) do

end
```

By doing this in LAUX

```lua
for i, v of tbl do

end
```

There is no ipairs equivalent of this.

## Formated string

You can make a formated string by using the <code>`</code> character and enclosing your variable names in braces :

```lua
print(string.format("there are %d %s", number, string)
```

By doing this in LAUX

```lua
print(`there are ${number} ${string}`)
```

---
_Source: https://npm.io/package/laux-compiler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
