Skip to content

[FORMATTING] Inconsistent formatting for queries with $$ #682

Closed
@karlhorky

Description

@karlhorky

As I've learned in Sec-ant/prettier-plugin-embed#45 (comment), there is actually a small bug in sql-formatter with queries including $$:

The lines are not consistently indented - the leading space is stripped from the first line, but the others are untouched, by design.

Input data

Which SQL and options did you provide as input?

Note the leading spaces on the first line:

            create function dup (int) returns dup_result as $$ 
              select
                  $1,
                  cast($1 as text)||' is text'
            $$ language sql;

Expected Output

create function dup (int) returns dup_result as $$ 
  select
      $1,
      cast($1 as text)||' is text'
$$ language sql;

Alternative (no indentation formatting, including first line):

            create function dup (int) returns dup_result as $$ 
              select
                  $1,
                  cast($1 as text)||' is text'
            $$ language sql;

Actual Output

Leading spaces on the first line are removed, but indentation on the following lines are untouched, by design

create function dup (int) returns dup_result as $$ 
              select
                  $1,
                  cast($1 as text)||' is text'
            $$ language sql;

Usage

  • How are you calling / using the library? For Prettier formatting, via prettier-plugin-sql and prettier-plugin-embed
  • What SQL language(s) does this apply to? PostgreSQL
  • Which SQL Formatter version are you using? 14.1.0-beta.2

This appears to be causing the unstable formatting with prettier-plugin-embed outlined in the following issue:

Kapture.2023-12-03.at.19.08.00.mp4

Suggested Solution

Either of the following would resolve this issue:

  1. for queries with $$, format the rest of the string with indentation matching the delta from the first line (in this example, -12) à la dedent
  2. avoid any type of indentation with any queries including $$

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions