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

@appguru/luafmt

v1.5.0

Published

A small Lua formatter / pretty printer

Downloads

31

Readme

Luafmt

A small Lua formatter / pretty printer. Designed for 5.1, may support newer versions.

About

Luafmt is licensed under the MIT license and written by Lars Mueller alias appgurueu.

Features

  • Fixes spacing & indentation
  • Neat literal formatting
  • Converts ({}) and ('')/("")/([[]]) calls to short forms
  • Moves comments to reasonable scopes (blocks or table constructors)
  • Table & function inlining
  • Extra newlines around function declarations

API

Installation

Install the NPM package luafmt:

npm install @appguru/luafmt

And import it like this:

const { ast, formatChunk, formatter } = require("@appguru/luafmt")

ast

Abstract syntax tree methods.

fixRanges

Makes luaparse ranges be from if to end and not from if to then for all IfClauses of the AST.

insertComments(chunk)

Inserts comments into the top-level abstract syntax tree provided by luaparse.

formatter(conf)

Provides a formatter which returns a function format(ast, [indent]) taking a luaparse AST & optionally the base indentation as number. Note that you have to call [fixRanges] and [insertComments] on the AST yourself before using it:

const { ast } = require("@appguru/luafmt")
const { parse } = require("luaparse") 
const srcAST = parse(`-- hello world
if true then print'hello world!'
else _ = _ end`)
const formatter = ast.formatter()
ast.fixRanges(srcAST)
ast.insertComments(srcAST)
console.log(formatter(srcAST))

formatChunk(text)

Takes a Lua chunk (think file / function body) as string and returns a formatted version, also as string.

formatter(conf)

const configuredFormatChunk = formatter({
  // string
  indent: "\t",
  // string
  newline: "\n",
  // boolean
  extra_newlines: true,
  // object
	inline: {
    // object or false
		block: {
			max_exp_length // number, in characters
    },
    // object or false
		table: {
			max_field_count // number
			max_field_length // number, in characters
		}
	}
})

Versions

  • v1.0.0
    • The initial release
  • v1.0.7
    • Lots of fixes
  • v1.0.8
    • Empty blocks are now just a single space, no space after #
    • Fixed dependency constraints
  • v1.0.9
    • Fixed operator precedence
  • v1.0.10
    • Fixed table index operators (adding parens around base)
  • v1.1.0
    • Fixed double minus (- -x)
    • Added inline tables & blocks
  • v1.1.1
    • Fixed if statement formatting
  • v1.1.2
    • Fixed local declarations without initializers (local name)
    • Tweaked table inlining
  • v1.2.0
    • Added extra newlines around function declarations feature
  • v1.2.1
    • Fixed table constructors containing comments
  • v1.2.2
    • Fixed repeat-until statement formatting
    • Bumped luon version to v1.2.6
  • v1.2.3
    • Fixed extra newlines
    • Improved tests
  • v1.2.4
    • Fixed extra newlines
  • v1.3.0, v1.4.0
    • Added configuration
  • v1.4.1
    • Fixed Readme
  • v1.4.2
    • Fixed (removed) trailing commas after fields followed by comments
    • Fixed (removed) obsolete (possibly invalid) bracketing (precedencies)
  • v1.4.3
    • Tweaked: Obsolete brackets are omitted in the case of concatenation, but not for exponentation
  • v1.5.0
    • Exposed AST-functions fixRanges & insertComments as well as lower-level AST formatter