sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.32k stars 398 forks source link

Feature Request: Remove unnecessary parentheses #684

Closed karlhorky closed 10 months ago

karlhorky commented 10 months ago

Describe the Feature

New default behavior in sql-formatter to remove unnecessary parentheses in SQL (maybe with a way to disable):

-- Before
SELECT
  sessions.id
FROM
  sessions
WHERE
  (
    (sessions.token = 'abc123')
    AND (sessions.user_id = 17)
    AND (sessions.expiry_timestamp > now())
  );

-- After
SELECT
  sessions.id
FROM
  sessions
WHERE
  sessions.token = 'abc123'
  AND sessions.user_id = 17
  AND sessions.expiry_timestamp > now();

Why do you want this feature?

Removing extra unnecessary parentheses can make code simpler and less nested

Prior art

Prettier does this:

Screenshot 2023-12-05 at 12 24 44

Other projects also have had similar requests / implementation:

Keywords for Search

Operator precedence, operators, parenthesis

nene commented 10 months ago

This is pretty much out of scope for SQL Formatter. In the sense that implementing this would require proper parsing of SQL which SQL Formatter doesn't do.

Elimination of extra parenthesis is at least partly implemented by the prettier-plugin-sql-cst. Like converting ((a + b)) to (a + b). I don't recall right now if I also implemented something else besides this most basic stuff.

I suggest you open this issue in prettier-plugin-sql-cst.

I'm closing this, as there's really no hope for it happening in SQL Formatter.

karlhorky commented 10 months ago

Ok thanks for the answer!

I suggest you open this issue in prettier-plugin-sql-cst.

Duplicated issue open here: