Skip to content

Apply various formatting fixes - #80

Open
joelmukuthu wants to merge 5 commits into
nene:masterfrom
joelmukuthu:fix/formatting-fixes
Open

Apply various formatting fixes#80
joelmukuthu wants to merge 5 commits into
nene:masterfrom
joelmukuthu:fix/formatting-fixes

Conversation

@joelmukuthu

@joelmukuthu joelmukuthu commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Fixes:

Comment thread src/syntax/expr.ts
Comment on lines +343 to +347
!expr.distinctKw &&
!expr.nullHandlingKw &&
!expr.orderBy &&
!expr.limit &&
!expr.having

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to avoid handling each case individually here?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment I think not much can be done here. This needs some changes on the parser side to make it easier to handle within the prettier plugin.

@joelmukuthu
joelmukuthu force-pushed the fix/formatting-fixes branch 2 times, most recently from fe0acba to d26d012 Compare August 11, 2026 06:38
@joelmukuthu
joelmukuthu force-pushed the fix/formatting-fixes branch from d26d012 to 120b664 Compare August 11, 2026 06:45

@nene nene left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request. I've been busy with other stuff. Finally got to reviewing this.

In general it looks good:

  • Break long WHEN/THEN clauses into separate lines
    • Needs some more thought on how to actually indent these.
  • Break long COMMENT ON clauses into separate lines
    • Doesn't really match with the indentation suggested in #76
  • Break long CREATE INDEX clauses into separate lines ✅
  • Break long binary expressions into separate lines ✅
  • Avoid line-break between empty parenthesis
    • This is the trickiest change. The general logic looks sound. I do have some recommendations though.

Comment thread test/expr/expr.test.ts
Comment on lines +212 to +218
SELECT
CASE
WHEN column_name = 1
THEN result_name
WHEN column_name = 2
THEN other_result
ELSE foo

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this not so easy to read, because WHEN and THEN blocks are indented the same amount. IMHO it's sort of like formatting if-else in some other language like so:

if x > 10
return 15
else if x < 10
return 20

It also doesn't match with how the procedural version of CASE expression gets formatted:

CASE
  WHEN column_name = 1 THEN
    SELECT \good'
  WHEN columne_name = 2 THEN
    SELECT 'bad'
  ELSE
    SELECT 'other'
END CASE

See case.test.ts

I would go with similar indentation for the long WHEN..THEN blocks in general:

CASE
  WHEN column_name = 1 THEN
    result_name
  WHEN columne_name = 2 THEN
    other_result
  ELSE
    foo
END CASE

Comment on lines +13 to +15
COMMENT ON
CONSTRAINT constraint_name ON DOMAIN domain_name
IS 'This is a really nice comment here.'

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indentation doesn't quite match with what was proposed in #76. According to that issue, it really should be:

COMMENT ON
  CONSTRAINT constraint_name ON DOMAIN domain_name
  IS 'This is a really nice comment here.'

Though I admit that the version you implemented might better align with the current overall indentation style that the formatter produces. But on another hand I'm not really so happy with that overall style. I'd like to move it more towards a bit more indentation. For example I'm not too happy with the indentation of ALTER TABLE statements:

-- current
ALTER TABLE foo
ALTER COLUMN bar
SET DEFAULT 'hello';

-- I'd rather have it like:
ALTER TABLE foo
  ALTER COLUMN bar
    SET DEFAULT 'hello';

Anyway... this COMMENT ON formatting is a minor thing. I don't mind if you leave it like this or change it.

Comment thread src/syntax/expr.ts
Comment on lines +356 to +357
const hasComments = (node: Node): boolean =>
Boolean((node as Node & { comments?: unknown[] }).comments?.length);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better way to implement this, would be:

const hasComments = (node: Node): boolean =>
  (node.leading?.length ?? 0) > 0 || (node.trailing?.length ?? 0) > 0;

The comments field is added to nodes by the Prettier engine. But the leading & trailing fields come directly from our parser.

Given that in here we really are interested from where the comments were in the original source code, we're better off using the leading & trailing. Plus we don't need the type-casts. Additionally it should be more resilient against changes in Prettier.

Comment thread src/syntax/expr.ts

const isCompactOp = (op: string) => op === "->" || op === "->>";

const isEmptyParenContent = (expr: Node): boolean => {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would turn this function into:

const isEmptyParenExpr = (expr: ParenExpr): boolean => {

Then all that logic for determining whether it's an empty parenthesis would live in one place, and also the name of the function would IMHO be easier to understand.

@joelmukuthu

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Everything you write makes sense! I'm going to be AFK for the next 2 weeks so I'll respond/make updates after :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants