Skip to content

Commit 37936c3

Browse files
committed
Expose lower-level AST functions
1 parent 8c46c4d commit 37936c3

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

Diff for: Readme.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,36 @@ npm install @appguru/luafmt
2828
And import it like this:
2929

3030
```javascript
31-
const { formatChunk, formatter } = require("@appguru/luafmt");
31+
const { ast, formatChunk, formatter } = require("@appguru/luafmt")
32+
```
33+
34+
#### `ast`
35+
36+
Abstract syntax tree methods.
37+
38+
##### `fixRanges`
39+
40+
Makes luaparse ranges be from `if` to `end` and not from `if` to `then` for all IfClauses of the AST.
41+
42+
##### `insertComments(chunk)`
43+
44+
Inserts comments into the top-level abstract syntax tree provided by luaparse.
45+
46+
##### `formatter(conf)`
47+
48+
Provides a formatter which returns a function `format(ast, [indent])` taking a luaparse AST & optionally the base indentation as number.
49+
Note that you have to call [`fixRanges`] and [`insertComments`] on the AST yourself before using it:
50+
51+
```javascript
52+
const { ast } = require("@appguru/luafmt")
53+
const { parse } = require("luaparse")
54+
const srcAST = parse(`-- hello world
55+
if true then print'hello world!'
56+
else _ = _ end`)
57+
const formatter = ast.formatter()
58+
ast.fixRanges(srcAST)
59+
ast.insertComments(srcAST)
60+
console.log(formatter(srcAST))
3261
```
3362

3463
#### `formatChunk(text)`
@@ -101,4 +130,6 @@ const configuredFormatChunk = formatter({
101130
* Fixed (removed) trailing commas after fields followed by comments
102131
* Fixed (removed) obsolete (possibly invalid) bracketing (precedencies)
103132
* `v1.4.3`
104-
* Tweaked: Obsolete brackets are omitted in the case of concatenation, but not for exponentation
133+
* Tweaked: Obsolete brackets are omitted in the case of concatenation, but not for exponentation
134+
* `v1.5.0`
135+
* Exposed AST-functions `fixRanges` & `insertComments` as well as lower-level AST formatter

Diff for: index.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,11 @@ const formatter = conf => {
199199
let left_pp = prettyPrint(left, indent)
200200
let right_pp = prettyPrint(right, indent)
201201
if (isBinaryExpression(left) && precedency[left.operator] < op_precedency) left_pp = "(" + left_pp + ")"
202-
if (isBinaryExpression(right) && precedency[right.operator] < op_precedency || (right.operator !== ".." && precedency[right.operator] == op_precedency)) right_pp = "(" + right_pp + ")"
202+
if (
203+
(isBinaryExpression(right) && precedency[right.operator] < op_precedency) ||
204+
(right.operator !== ".." && precedency[right.operator] == op_precedency)
205+
)
206+
right_pp = "(" + right_pp + ")"
203207
return left_pp + " " + operator + " " + right_pp
204208
}
205209

@@ -322,7 +326,7 @@ const formatter = conf => {
322326
const end_spacing = inline ? " " : indentationNewline(indent - 1)
323327
let table = [end_spacing + "}"]
324328
let beforeField = false
325-
for (let i = length -1; i > -1; i--) {
329+
for (let i = length - 1; i > -1; i--) {
326330
const field = node.fields[i]
327331
const isField = field.type !== "Comment"
328332
if (beforeField && isField) {
@@ -389,15 +393,23 @@ const formatter = conf => {
389393
return nodeFormatter(node, indent)
390394
}
391395

396+
return prettyPrint
397+
}
398+
399+
const astFormatter = conf => (ast, indent) => formatter(conf)(ast, indent === undefined ? 0 : indent)
400+
401+
const textFormatter = conf => {
402+
const formatter = astFormatter(conf)
392403
return text => {
393404
const ast = parse(text)
394405
fixRanges(ast)
395406
insertComments(ast)
396-
return prettyPrint(ast, 0)
407+
return formatter(ast)
397408
}
398409
}
399410

400411
module.exports = {
401-
formatChunk: formatter(),
402-
formatter
412+
ast: { fixRanges, insertComments, formatter: astFormatter },
413+
formatChunk: textFormatter(),
414+
formatter: textFormatter
403415
}

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@appguru/luafmt",
3-
"version": "1.4.3",
3+
"version": "1.5.0",
44
"esversion": 6,
55
"description": "A small Lua formatter / pretty printer",
66
"main": "./index.js",

0 commit comments

Comments
 (0)