Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: Indentation of interpolated expression missing an indent level #103

Open
karlhorky opened this issue Apr 10, 2024 · 2 comments
Open
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@karlhorky
Copy link
Contributor

karlhorky commented Apr 10, 2024

Hi @Sec-ant 👋 Hope you're good!

I encountered a problem with prettier-plugin-sql (using sql-formatter) indentation with interpolation in a condition of a WHERE condition line:

Expected:

sql`
  SELECT
    *
  FROM
    longer_table_name_with_many_characters
  WHERE
    longer_table_name_with_many_characters.id IN ${sql(
      longerTableNameWithManyCharactersIds,
    )}
`

Actual:

(the 2 lines after sql( are missing 1 indent level)

sql`
  SELECT
    *
  FROM
    longer_table_name_with_many_characters
  WHERE
    longer_table_name_with_many_characters.id IN ${sql(
    longerTableNameWithManyCharactersIds,
  )}
`

This problem also extends to any other indented areas, eg. this FROM clause:

sql`
  SELECT
    *
  FROM
    ${sql(
    evenLongerTableNameWithManyCharactersKeepsGoingEvenLongerAndLongerAndLonger,
  )}
`

The sql-formatter demo seems to indent correctly, with a parenthesized expression spanning multiple lines (no ability to interpolate here, since it's only SQL, no JS template strings):

SELECT
  *
FROM
  longer_table_name_with_many_characters
WHERE
  longer_table_name_with_many_characters.id IN (
    111,
    222,
    333
  );
@karlhorky
Copy link
Contributor Author

Workaround

Wrapping the interpolation in parentheses works around the problem:

sql`
  SELECT
    *
  FROM
    longer_table_name_with_many_characters
  WHERE
    longer_table_name_with_many_characters.id IN (
      ${sql(longerTableNameWithManyCharactersIds)}
    )
`

But this workaround fails with a similar problem if the JS expression is too long and Prettier wraps it:

sql`
  SELECT
    *
  FROM
    longer_table_name_with_many_characters
  WHERE
    longer_table_name_with_many_characters.id IN (
      ${sql(
    evenLongerTableNameWithManyCharactersKeepsGoingEvenLongerAndLongerAndLongerIds,
  )}
    )
`;

@karlhorky karlhorky changed the title Indentation of interpolated expression missing an indent level SQL: Indentation of interpolated expression missing an indent level Apr 10, 2024
@Sec-ant Sec-ant added bug Something isn't working help wanted Extra attention is needed labels Apr 11, 2024
@Sec-ant
Copy link
Owner

Sec-ant commented Apr 11, 2024

I'll need some help on this one. If anyone can offer a general solution, I'd be happy to merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants