lukehaas / RunJS

RunJS is a JavaScript playground for macOS, Windows and Linux. Write code with instant feedback and access to Node.js and browser APIs.
https://runjs.app
2.01k stars 43 forks source link

Bug - Pretty code formatting bug #528

Closed pjchender closed 1 year ago

pjchender commented 1 year ago

I am unsure why the formatter sometimes breaks the code after prettifying it, which is annoying.

Take the code as an example,

function foo() {
    let bar = 1;

    while(true) {
      if (bar > 2) {
      } else if (bar <= 2) {
      }
    }

    return result
};

After formatting, however, it produces a duplicate line of return result:

function foo() {
  let bar = 1;

  while (true) {
    if (bar > 2) {
    } else if (bar <= 2) {
    }
  }
    return result;
  return result;
}

1

Another example is that,

function foo() {
    let bar = 1;

      if (bar > 2) {
      } else if (bar <= 2) {

      }

    return result;
};

It becomes to,

function foo() {
  let bar = 1;

  if (bar > 2) {
  } else if (bar <= 2) {
  }
      if (bar > 2) {
  return result;
}

2

I have no idea why this happened.

lukehaas commented 1 year ago

@pjchender, thanks for raising this. The reason this is happening is due to a diffing algorithm that attempts to update just the parts of the code that changed after formatting. Instead of the whole document being replaced on format.

It's clear from your observations that this diffing solution is deeply flawed. I'll see what I can do to resolve this in a future release.

scr2em commented 1 year ago

I came here to raise the same issue. Thanks for being proactive.

lukehaas commented 1 year ago

@pjchender this is resolved in the latest release.

pjchender commented 1 year ago

@lukehaas thank you so much!