Skip to content

Instantly share code, notes, and snippets.

@SquidDev
Created February 3, 2019 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SquidDev/2fb2ea604ff3f79ea09be018b9038c93 to your computer and use it in GitHub Desktop.
Save SquidDev/2fb2ea604ff3f79ea09be018b9038c93 to your computer and use it in GitHub Desktop.
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