nextlua 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/main.js +39 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextlua",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "A luau beautifier and minifier.",
5
5
  "main": "index.js",
6
6
  "bin": {
package/src/main.js CHANGED
@@ -409,10 +409,11 @@ function needsSpace(prev, current) {
409
409
  }
410
410
 
411
411
  if (current === "(") {
412
- // Control-flow keywords read better with a space before the paren
413
- // (`if (`, `while (`, `elseif (`), but calls like `function(` and
414
- // `foo(` stay tight.
415
- return keywordsSpacedBeforeParen.has(prev);
412
+ // Control-flow keywords and binary operators read better with a space
413
+ // before the paren (`if (`, `while (`, `a * (`, `x = (`), but calls
414
+ // like `function(` and `foo(` stay tight. A unary sign before `(` is
415
+ // handled by joinTokens, which suppresses the space (`-(x)`).
416
+ return keywordsSpacedBeforeParen.has(prev) || binaryOperators.has(prev);
416
417
  }
417
418
 
418
419
  if (current === "[" && isValueEnd(prev)) {
@@ -429,7 +430,9 @@ function needsSpace(prev, current) {
429
430
  return false;
430
431
  }
431
432
 
432
- if (prev === "#" || current === "#") {
433
+ if (prev === "#") {
434
+ // The length operator binds tightly to its operand: `#h`, never `# h`.
435
+ // The space *before* `#` still follows the normal rules (`= #h`).
433
436
  return false;
434
437
  }
435
438
 
@@ -454,6 +457,18 @@ function isGenericOpen(tokens, index) {
454
457
  !reservedKeywords.has(prev);
455
458
  }
456
459
 
460
+ function isUnarySign(tokens, index) {
461
+ // tokens[index] is "-" or "+". It is unary (binding tightly to its operand
462
+ // with no following space: `-1`, `-x`, `-(y)`, `-#t`) when it does not
463
+ // follow a complete value. Otherwise it is the binary operator (`a - 1`).
464
+ const token = tokens[index];
465
+ if (token !== "-" && token !== "+") {
466
+ return false;
467
+ }
468
+ const before = tokens[index - 1];
469
+ return before === undefined || !isValueEnd(before);
470
+ }
471
+
457
472
  function joinTokens(tokens) {
458
473
  let text = "";
459
474
  let prev = null;
@@ -465,7 +480,14 @@ function joinTokens(tokens) {
465
480
  const inGeneric = genericDepth > 0;
466
481
  const opensGeneric = !inGeneric && token === "<" && isGenericOpen(tokens, idx);
467
482
 
468
- if (prev !== null && !inGeneric && !opensGeneric && needsSpace(prev, token)) {
483
+ // A unary +/- binds to its operand, so suppress the space after it.
484
+ // (Skip the suppression when the next token is itself a +/- so we never
485
+ // fuse two signs into a `--` comment or a `+-` run.)
486
+ const prevIsUnarySign = prev !== null &&
487
+ token !== "-" && token !== "+" &&
488
+ isUnarySign(tokens, idx - 1);
489
+
490
+ if (prev !== null && !inGeneric && !opensGeneric && !prevIsUnarySign && needsSpace(prev, token)) {
469
491
  text += " ";
470
492
  }
471
493
  text += token;
@@ -571,6 +593,17 @@ function layout(input) {
571
593
  flushCurrent();
572
594
  }
573
595
 
596
+ if (token === "else" && inlineIfStack.length && inlineIfStack[inlineIfStack.length - 1] === "else") {
597
+ // The `else` of an inline `if ... then ... else ...` expression.
598
+ // It can appear nested inside parens/brackets (e.g. `x = (if c then
599
+ // a else b)`), so it must be matched regardless of bracket depth —
600
+ // otherwise inlineIfStack never empties and statement-splitting
601
+ // stays disabled for the rest of the block.
602
+ inlineIfStack.pop();
603
+ current.push(token);
604
+ continue;
605
+ }
606
+
574
607
  if (blockMiddle.has(token) && atStatementLevel()) {
575
608
  if (token === "else" && inlineIfStack.length) {
576
609
  inlineIfStack.pop();