nene / prettier-plugin-sql-cst

Prettier SQL plugin that uses sql-parser-cst

Home Page:https://nene.github.io/prettier-sql-playground/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Remove unnecessary parentheses

karlhorky opened this issue · comments

Copied from original sql-formatter issue: sql-formatter-org/sql-formatter#684

Describe the Feature

New default behavior in prettier-plugin-sql-cst 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:

288022812-3e896557-bb2e-49e4-9bdc-75a5de463228

Other projects also have had similar requests / implementation:

Keywords for Search

Operator precedence, operators, parenthesis

FYI: Currently there exists some very basic support for unnecessary parentheses removal, which covers cases like these:

-- removal of repeated parenthesis
a + ((b + c))  --> a + (b + c)

-- removal of parenthesis around function parameters
my_func(a, (b), ((c + d)))  -->  my_func(a, b, c + d)