Skip to content
Open
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"src"
],
"dependencies": {
"luaparse": "0.2.1"
"luaparse": "0.3.0"
},
"devDependencies": {
"cross-env": "^7.0.2",
Expand Down
1 change: 1 addition & 0 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function parse(text, parsers, options) {
return luaparse.parse(text, {
ranges: true,
luaVersion: "5.3",
encodingMode: "pseudo-latin1",
});
}

Expand Down
43 changes: 33 additions & 10 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -443,7 +446,7 @@ function couldBeCallExpressionBase(node) {
return false;
}

function shouldHaveParens(node, parent) {
function pathNeedsParens(node, parent) {
if (!node.inParens) {
return false;
}
Expand Down Expand Up @@ -507,7 +510,7 @@ function shouldHaveParens(node, parent) {
}

function willHaveLeadingParen(node, parent) {
if (shouldHaveParens(node, parent)) {
if (pathNeedsParens(node, parent)) {
return true;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
};
21 changes: 20 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
2 changes: 1 addition & 1 deletion tests/unnecessary-parens/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests_config/run_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ function mergeDefaultOptions(parserConfig) {
{
printWidth: 80,
trailingComma: "none",
semi: false,
},
parserConfig
);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down