dprint / dprint-plugin-typescript

TypeScript and JavaScript code formatting plugin for dprint.
https://dprint.dev/plugins/typescript
MIT License
258 stars 57 forks source link

Support de-indenting/indenting block comments according to their leading line indentation #678

Closed bartlomieju closed 3 days ago

bartlomieju commented 1 month ago

Describe the bug

dprint-plugin-typescript version: 0.93.0

Input Code


            /* some multiline comment
            with function below it */
            someFunc();

Expected Output

            /* some multiline comment
            with function below it */
            someFunc();

Actual Output

      /* some multiline comment
                  with function below it */
      someFunc();

Then after using the "Actual output" as input to check for format stability we get:

      /* some multiline comment
                        with function below it */
      someFunc();

and then again:

      /* some multiline comment
                              with function below it */
      someFunc();

And it continues...

This is the root cause for https://github.com/denoland/deno/issues/26560

bartlomieju commented 1 month ago

I guess one work-around that could be done in Deno itself is to split into lines, trim them and then join. That will however make some comments formatting off. I'm gonna apply it for now to get rid of panics, but I think it'd be better to fix it in dprint itself.

dsherret commented 1 month ago

dprint-plugin-typescript doesn't modify block comments. This is an issue somewhere else.

dsherret commented 1 month ago

After discussion, we're going to improve dprint-plugin-typescript to handle de-indenting/indenting these kind of block comments. Initially we were thinking markup_fmt should de-indent itself, but it wouldn't be able to handle scenarios like:

<script>
  const test = `
  test
`;
  console.log(test);
</script>
bartlomieju commented 3 weeks ago

A few more examples of inputs that cause unstable format:

  "{"; // This line cause panic
  `
    `;
  `{
  }`;
  ``;
bartlomieju commented 3 weeks ago

@dsherret FYI I opened an issue with markup_fmt and it appears these strings are passed in this way on purpose - https://github.com/g-plane/markup_fmt/issues/74#issuecomment-2456078219.

I'm beginning to think that dprint-plugin-typescript could use a separate API like format_text_for_external_block that would be aware of the indentation requirements, but I haven't figured it out yet.

bartlomieju commented 3 days ago

Closing as this can now be solved with https://github.com/dprint/dprint-plugin-typescript/pull/689.