Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/formatter/ExpressionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,13 @@ export default class ExpressionFormatter {
}

private formatBlockComment(node: BlockCommentNode | DisableCommentNode) {
if (node.type === NodeType.block_comment && this.isStandaloneBlockComment(node)) {
if (node.type === NodeType.disable_comment) {
this.layout.add(node.text, WS.SPACE);
// Place following SQL on the next indented line after a multi-line region.
if (isMultiline(node.text)) {
this.layout.add(WS.NEWLINE, WS.INDENT);
}
} else if (this.isStandaloneBlockComment(node)) {
this.splitBlockComment(node.text).forEach(line => {
this.layout.add(WS.NEWLINE, WS.INDENT, line);
});
Expand Down
5 changes: 5 additions & 0 deletions src/lexer/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export default class Tokenizer {
regex:
/(\/\* *sql-formatter-disable *\*\/[\s\S]*?(?:\/\* *sql-formatter-enable *\*\/|$))/uy,
},
{
type: TokenType.DISABLE_COMMENT,
regex:
/(-- *sql-formatter-disable\b[^\n\r]*(?:(?:\r\n|\r|\n)[\s\S]*?(?:-- *sql-formatter-enable\b[^\n\r]*|$)|$))/uy,
},
{
type: TokenType.BLOCK_COMMENT,
regex: cfg.nestedBlockComments ? new NestedComment() : /(\/\*[^]*?\*\/)/uy,
Expand Down
3 changes: 2 additions & 1 deletion src/lexer/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export enum TokenType {
CLOSE_PAREN = 'CLOSE_PAREN',
LINE_COMMENT = 'LINE_COMMENT',
BLOCK_COMMENT = 'BLOCK_COMMENT',
// Text between /* sql-formatter-disable */ and /* sql-formatter-enable */
// Text between sql-formatter-disable and sql-formatter-enable comments
// (either /* ... */ or -- ... line comments)
DISABLE_COMMENT = 'DISABLE_COMMENT',
NUMBER = 'NUMBER',
NAMED_PARAMETER = 'NAMED_PARAMETER',
Expand Down
110 changes: 110 additions & 0 deletions test/features/disableComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,114 @@ export default function supportsDisableComment(format: FormatFn) {
bar;
`);
});

it('does not format text between -- sql-formatter-disable and -- sql-formatter-enable', () => {
const result = format(dedent`
SELECT foo FROM bar;
-- sql-formatter-disable
SELECT foo FROM bar;
-- sql-formatter-enable
SELECT foo FROM bar;
`);

expect(result).toBe(dedent`
SELECT
foo
FROM
bar;

-- sql-formatter-disable
SELECT foo FROM bar;
-- sql-formatter-enable
SELECT
foo
FROM
bar;
`);
});

// Issue #912
it('preserves indentation between -- sql-formatter-disable and -- sql-formatter-enable', () => {
const result = format(dedent`
-- sql-formatter-disable
SELECT
foo
FROM
bar;
-- sql-formatter-enable
`);

expect(result).toBe(dedent`
-- sql-formatter-disable
SELECT
foo
FROM
bar;
-- sql-formatter-enable
`);
});

it('does not format text after -- sql-formatter-disable until end of file', () => {
const result = format(dedent`
SELECT foo FROM bar;
-- sql-formatter-disable
SELECT foo FROM bar;

SELECT foo FROM bar;
`);

expect(result).toBe(dedent`
SELECT
foo
FROM
bar;

-- sql-formatter-disable
SELECT foo FROM bar;

SELECT foo FROM bar;
`);
});

it('does not parse code between -- disable/enable comments', () => {
const result = format(dedent`
SELECT
-- sql-formatter-disable
?!{}[]
-- sql-formatter-enable
FROM bar;
`);

expect(result).toBe(dedent`
SELECT
-- sql-formatter-disable
?!{}[]
-- sql-formatter-enable
FROM
bar;
`);
});

// Issue #912
it('does not format sqlc.embed() between -- sql-formatter-disable comments', () => {
const result = format(dedent`
SELECT
-- sql-formatter-disable
sqlc.embed(users),
-- sql-formatter-enable
customers.id
FROM
customers
`);

expect(result).toBe(dedent`
SELECT
-- sql-formatter-disable
sqlc.embed(users),
-- sql-formatter-enable
customers.id
FROM
customers
`);
});
}