Skip to content

[SQL] Line breaks for template literal variables #152

@BrendanC23

Description

@BrendanC23

I'm using prettier-plugin-embed with sql-formatter and sql-template-tag, which has worked well. I've run into one instance where

sql``

template literals are not formatted properly. I use these in two different ways.

  1. To hold a SQL parameter
  2. To hold part of a sql query

In the first case, the template variable is on the same line, as expected. In the second case, the variable still appears on the same line, making the code harder to read.

function test() {
    const myId = 123;
    const otherClause = myId === 123 ? sql`AND id2 = 1234` : empty;

    const query = sql`
        SELECT *
        FROM table_name
        WHERE
            id = ${myId}
            ${otherClause}
    `;
}

After formatting, this becomes

const query = sql`
    SELECT
        *
    FROM
        table_name
    WHERE
        id = ${myId} ${otherClause}
`;

but I would like it to be

const query = sql`
    SELECT
        *
    FROM
        table_name
    WHERE
        id = ${myId}
        ${otherClause}
`;

Is there a way to improve the formatting of the otherClause here? For example, if a template literal variable is on the same line, keep it that way. If it is on a new line, insert a new line to the formatted output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions