Skip to content

Instantly share code, notes, and snippets.

@SquidDev
Created Feb 3, 2019
Embed
What would you like to do?
luafmt (https://github.com/trixnz/lua-fmt/) patch for binary operators
--- a/src/printer.ts
+++ b/src/printer.ts
@@ -101,6 +101,17 @@ function isLastStatement(path: FastPath) {
return body && body[body.length - 1] === node;
}
+const binOps = {
+ ["~"]: "__bxor",
+ ["|"]: "__bor",
+ ["<<"]: "__shl",
+ [">>"]: "__shr",
+}
+
+const unOps = {
+ ["~"]: "__bnot",
+}
+
function printNodeNoParens(path: FastPath, options: Options, print: PrintFn) {
const value = path.getValue();
@@ -111,6 +122,19 @@ function printNodeNoParens(path: FastPath, options: Options, print: PrintFn) {
const parts: Doc[] = [];
const node = value as luaparse.Node;
+ if(node.type == "BinaryExpression" && binOps[node.operator]) {
+ Object.assign(node, {
+ type: "CallExpression", loc: node.loc, range: node.range, inParens: false,
+ base: { type: "Identifier", loc: node.loc, range: node.range, inParens: false, name: binOps[node.operator] },
+ arguments: [node.left, node.right],
+ });
+ } else if (node.type == "UnaryExpression" && unOps[node.operator]) {
+ Object.assign(node, {
+ type: "CallExpression", loc: node.loc, range: node.range, inParens: false,
+ base: { type: "Identifier", loc: node.loc, range: node.range, inParens: false, name: binOps[node.operator] },
+ arguments: [node.argument],
+ });
+ }
switch (node.type) {
case 'Chunk':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment