npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lua-features

v0.0.1

Published

Static analysis with extra features for lua

Downloads

3

Readme

lua-features

lua-features is a static analyser for lua. It can parse code in Lua 5.1, 5.2, 5.3 and LuaJIT. Its code is adapted from luaparse.

Features

On top of interpreting usual Lua, it is also a transpiler that can add additional features on top of the language. All features are then transpiled to plain lua (see CLI). Current features are:

Const variables

Wherever you could use local (variable or function declaration) you can instead use const, this ensures that such variable cannot be reassigned.

const x = 1
x = 2 -- error: cannot reassign a constant
function x() end -- error: cannot reassign a constant

Type checking

This feature is experimental and does not work properly, avoid using it. The plan is to add static type checking to lua, like flow and TypeScript do to Javascript.

local x : number = 1;
x = "1" -- error: cannot assign string to number
function add(x, y: number, number): number
    return x + y
end
add(1, 'a') -- error: cannot call function

Command line interface

lua-features provides a CLI (see the code). Use the help command to get instructions.

lua-features <command>

Commands:
  lua-features check <src-dir>              Check all .lua files in src-dir.
  lua-features transpile <src-dir>          Transpile all .lua files in src-dir
  <out-dir>                                 to out-dir

Options:
  --version  Show version number                                       [boolean]
  --help     Show help                                                 [boolean]

The check command tries to compile all files in a directory:

lua-features check <src-dir>

Check all .lua files in src-dir.

Positionals:
  src-dir  Input directory                                   [string] [required]

Options:
  --version         Show version number                                [boolean]
  --help            Show help                                          [boolean]
  --type-check, -t  Enables type-checking.                             [boolean]
  --const, -c       Enable const variables.                            [boolean]
  --with-lua, -l    Lua version to be compatible with.
                          [choices: "5.1", "5.2", "5.3", "JIT"] [default: "5.1"]

This will print nicely formatted errors if your files don't compile.

# example.lua:
const x = 1;
x = 2;

# cli
lua-features check . --const

# output
[.//example.lua:2:1-6] cannot reassign a constant
    x = 2;
    ^^^^^

The transpile command transpiles all files from one directory to plain lua, and copies them to another directory.

lua-features transpile <src-dir> <out-dir>

Transpile all .lua files in src-dir to out-dir

Positionals:
  src-dir  Input Directory                                   [string] [required]
  out-dir  Output directory                                  [string] [required]

Options:
  --version         Show version number                                [boolean]
  --help            Show help                                          [boolean]
  --type-check, -t  Enables type-checking.                             [boolean]
  --const, -c       Enable const variables.                            [boolean]
  --with-lua, -l    Lua version to be compatible with.
                          [choices: "5.1", "5.2", "5.3", "JIT"] [default: "5.1"]

If you're using the const feature, for example, all consts are changed to local. Notice it transpiles even if check errors for some feature. The spacing is messed up for now, but may be fixed in the future. I tried to keep at least the same line numbers for all operations, that way if you get an error on line X in your transpiled code, it surely came from line X in the original code. The transpile example.lua file looks like:

local  a = 1
 a = 2

Contributions

I accept PR's!

However, I will not lie: I'm not sure how much more time I'm gonna spend improving this repository :)

Possible feature ideas:

  • Compatibility: for example, compile Lua 5.3 bitops to use LuaJIT bit library.
  • continue: continue can probably be transpiled to some goto's