diff --git a/package.json b/package.json index 323b9ce..ca6d67e 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "src" ], "dependencies": { - "luaparse": "0.2.1" + "luaparse": "0.3.0" }, "devDependencies": { "cross-env": "^7.0.2", diff --git a/src/parser.js b/src/parser.js index c47aa8b..796b858 100644 --- a/src/parser.js +++ b/src/parser.js @@ -5,6 +5,7 @@ function parse(text, parsers, options) { return luaparse.parse(text, { ranges: true, luaVersion: "5.3", + encodingMode: "pseudo-latin1", }); } diff --git a/src/printer.js b/src/printer.js index e7c7fc9..a87457d 100644 --- a/src/printer.js +++ b/src/printer.js @@ -17,12 +17,15 @@ const { } = require("prettier").doc.builders; const { willBreak } = require("prettier").doc.utils; const { makeString, isNextLineEmpty } = require("prettier").util; -const { isValidIdentifier, isExpression } = require("./util"); +const { + isValidIdentifier, + isExpression, + lineShouldEndWithSemicolon, +} = require("./util"); const { printDanglingComments, isDanglingComment } = require("./comments"); function printNoParens(path, options, print) { const node = path.getValue(); - switch (node.type) { case "Chunk": { return printBody(path, options, print); @@ -443,7 +446,7 @@ function couldBeCallExpressionBase(node) { return false; } -function shouldHaveParens(node, parent) { +function pathNeedsParens(node, parent) { if (!node.inParens) { return false; } @@ -507,7 +510,7 @@ function shouldHaveParens(node, parent) { } function willHaveLeadingParen(node, parent) { - if (shouldHaveParens(node, parent)) { + if (pathNeedsParens(node, parent)) { return true; } @@ -539,7 +542,7 @@ function getRightmostNode(node, parent) { } case "BinaryExpression": case "LogicalExpression": { - if (shouldHaveParens(node, parent)) { + if (pathNeedsParens(node, parent)) { return node; } return getRightmostNode(node.right, node); @@ -760,12 +763,32 @@ function getLast(arr) { } module.exports = function genericPrint(path, options, print) { - const printed = printNoParens(path, options, print); + const printedWithoutParens = printNoParens(path, options, print); const node = path.getValue(); - if (shouldHaveParens(node, path.getParentNode())) { - return concat(["(", printed, ")"]); - } else { - return printed; + + if (!node) { + return ""; + } else if (typeof node === "string") { + return node; + } + + const parts = []; + const needsParens = pathNeedsParens(node, path.getParentNode()); + + if (needsParens) { + parts.unshift("("); } + + parts.push(printedWithoutParens); + + if (needsParens) { + parts.push(")"); + } + + if (options.semi && lineShouldEndWithSemicolon(path)) { + parts.push(";"); + } + + return concat(parts); }; diff --git a/src/util.js b/src/util.js index 05a5504..272f4b1 100644 --- a/src/util.js +++ b/src/util.js @@ -59,4 +59,23 @@ function isExpression(node) { } } -module.exports = { isValidIdentifier, isExpression }; +function lineShouldEndWithSemicolon(path) { + const node = path.getValue(); + const parentNode = path.getParentNode(); + if (!parentNode) { + return false; + } + + return [ + "ExpressionStatement", + "CallStatement", + "LocalStatement", + "AssignmentStatement", + ].includes(node.type); +} + +module.exports = { + isValidIdentifier, + isExpression, + lineShouldEndWithSemicolon, +}; diff --git a/tests/unnecessary-parens/main.lua b/tests/unnecessary-parens/main.lua index d08d665..957837f 100644 --- a/tests/unnecessary-parens/main.lua +++ b/tests/unnecessary-parens/main.lua @@ -110,7 +110,7 @@ function you_need_me_2(...) return (...), (b()) end -function you_need_me_3() +function you_need_me_3(...) return (...), (b()), (c()) end diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index dddc9e4..42fd6e9 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -116,6 +116,7 @@ function mergeDefaultOptions(parserConfig) { { printWidth: 80, trailingComma: "none", + semi: false, }, parserConfig ); diff --git a/yarn.lock b/yarn.lock index e9c5e7e..4fa7c1b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2489,10 +2489,10 @@ loose-envify@^1.0.0: dependencies: js-tokens "^3.0.0 || ^4.0.0" -luaparse@0.2.1: - version "0.2.1" - resolved "https://registry.yarnpkg.com/luaparse/-/luaparse-0.2.1.tgz#aa8f56132b0de97d37f3c991a9df42e0e17f656c" - integrity sha1-qo9WEysN6X0388mRqd9C4OF/ZWw= +luaparse@0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/luaparse/-/luaparse-0.3.0.tgz#80f8d2161a636dea1fdd39e77996277337df18a1" + integrity sha512-hrRXc3QJfZ0BfgOGgNfFaKM01zCjSP2y000WOG4jwl3s06vKzODIfqJ3QKSm64mS52ROpnRnfi756jJbDr59fw== make-dir@^2.1.0: version "2.1.0"