nene / prettier-plugin-sql-cst

Prettier SQL plugin that uses sql-parser-cst
https://nene.github.io/prettier-sql-playground/
GNU General Public License v3.0
123 stars 7 forks source link

Bug Report: Formatting moves comments unexpectedly #9

Open Anthony-Fiddes opened 11 months ago

Anthony-Fiddes commented 11 months ago

I'm experiencing a curious bug where comments don't end up where I expect. In addition, if I keep formatting the text, the comments keep moving until they all settle on the same line.

Language: BigQuery

Text to format:

CREATE TEMP TABLE blah
-- comments
-- more comments
-- even more comments
(
date DATE NOT NULL
) AS
(
SELECT
bleep
FROM
bloop
);

Expected output:

CREATE TEMP TABLE blah 
-- comments
-- more comments
-- even more comments
(
  date DATE NOT NULL
) AS
  (
    SELECT
      bleep
    FROM bloop
  );

Actual output:

CREATE TEMP TABLE blah -- comments
-- more comments
-- even more comments
(
  date DATE NOT NULL
) AS
  (
    SELECT
      bleep
    FROM bloop
  );
nene commented 11 months ago

Thanks for reporting.

Comments being moved slightly is one thing, but getting multiple line-comments on a single line and their order changed is a much more serious issue. Like currently this SQL:

CREATE TABLE foo -- comment1
-- comment2
(
  bar INT
);

gets formatted as:

CREATE TABLE foo -- comment2 -- comment1
(
  bar INT
);
nene commented 11 months ago

I don't really know why this happens, because I've left the handling of comments to the Prettier engine. Two line-comments one-after-another seems like a bug in Prettier itself. Though possibly my input of comments data might be incorrect for Prettier.

One hope is that perhaps this works better in Prettier 3.x. Should first upgrade to latest Prettier and then investigate further.

nene commented 11 months ago

Tried upgrading to Prettier 3. Bunch of problems there... but I did find out that it won't fix this comments formatting issue.

nene commented 7 months ago

Related to https://github.com/prettier/prettier/issues/15978