File tree Expand file tree Collapse file tree 8 files changed +124
-4
lines changed Expand file tree Collapse file tree 8 files changed +124
-4
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,34 @@ format('SELECT * FROM tbl', {
61
61
});
62
62
```
63
63
64
+ ### Disabling the formatter
65
+
66
+ You can disable the formatter for a section of SQL by surrounding it with disable/enable comments:
67
+
68
+ ``` sql
69
+ /* sql-formatter-disable */
70
+ SELECT * FROM tbl1;
71
+ /* sql-formatter-enable */
72
+ SELECT * FROM tbl2;
73
+ ```
74
+
75
+ which produces:
76
+
77
+ ``` sql
78
+ /* sql-formatter-disable */
79
+ SELECT * FROM tbl1;
80
+ /* sql-formatter-enable */
81
+ SELECT
82
+ *
83
+ FROM
84
+ tbl2;
85
+ ```
86
+
87
+ The formatter doesn't even parse the code between these comments.
88
+ So in case there's some SQL that happens to crash SQL Formatter,
89
+ you can at comment the culprit out (at least until the issue gets
90
+ fixed in SQL Formatter).
91
+
64
92
### Placeholders replacement
65
93
66
94
In addition to formatting, this library can also perform placeholder replacement in prepared SQL statements:
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ import {
30
30
CaseElseNode ,
31
31
DataTypeNode ,
32
32
ParameterizedDataTypeNode ,
33
+ DisableCommentNode ,
33
34
} from '../parser/ast.js' ;
34
35
35
36
import Layout , { WS } from './Layout.js' ;
@@ -134,6 +135,8 @@ export default class ExpressionFormatter {
134
135
return this . formatLineComment ( node ) ;
135
136
case NodeType . block_comment :
136
137
return this . formatBlockComment ( node ) ;
138
+ case NodeType . disable_comment :
139
+ return this . formatBlockComment ( node ) ;
137
140
case NodeType . data_type :
138
141
return this . formatDataType ( node ) ;
139
142
case NodeType . keyword :
@@ -367,8 +370,8 @@ export default class ExpressionFormatter {
367
370
}
368
371
}
369
372
370
- private formatBlockComment ( node : BlockCommentNode ) {
371
- if ( this . isMultilineBlockComment ( node ) ) {
373
+ private formatBlockComment ( node : BlockCommentNode | DisableCommentNode ) {
374
+ if ( node . type === NodeType . block_comment && this . isMultilineBlockComment ( node ) ) {
372
375
this . splitBlockComment ( node . text ) . forEach ( line => {
373
376
this . layout . add ( WS . NEWLINE , WS . INDENT , line ) ;
374
377
} ) ;
Original file line number Diff line number Diff line change @@ -31,6 +31,11 @@ export default class Tokenizer {
31
31
// the Tokenizer config options specified for each SQL dialect
32
32
private buildRulesBeforeParams ( cfg : TokenizerOptions ) : TokenRule [ ] {
33
33
return this . validRules ( [
34
+ {
35
+ type : TokenType . BLOCK_COMMENT ,
36
+ regex :
37
+ / ( \/ \* * s q l - f o r m a t t e r - d i s a b l e * \* \/ [ \s \S ] * ?(?: \/ \* * s q l - f o r m a t t e r - e n a b l e * \* \/ | $ ) ) / uy,
38
+ } ,
34
39
{
35
40
type : TokenType . BLOCK_COMMENT ,
36
41
regex : cfg . nestedBlockComments ? new NestedComment ( ) : / ( \/ \* [ ^ ] * ?\* \/ ) / uy,
Original file line number Diff line number Diff line change @@ -33,6 +33,8 @@ export enum TokenType {
33
33
CLOSE_PAREN = 'CLOSE_PAREN' ,
34
34
LINE_COMMENT = 'LINE_COMMENT' ,
35
35
BLOCK_COMMENT = 'BLOCK_COMMENT' ,
36
+ // Text between /* sql-formatter-disable */ and /* sql-formatter-enable */
37
+ DISABLE_COMMENT = 'DISABLE_COMMENT' ,
36
38
NUMBER = 'NUMBER' ,
37
39
NAMED_PARAMETER = 'NAMED_PARAMETER' ,
38
40
QUOTED_PARAMETER = 'QUOTED_PARAMETER' ,
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ export enum NodeType {
24
24
comma = 'comma' ,
25
25
line_comment = 'line_comment' ,
26
26
block_comment = 'block_comment' ,
27
+ disable_comment = 'disable_comment' ,
27
28
}
28
29
29
30
interface BaseNode {
@@ -178,7 +179,13 @@ export interface BlockCommentNode extends BaseNode {
178
179
precedingWhitespace : string ;
179
180
}
180
181
181
- export type CommentNode = LineCommentNode | BlockCommentNode ;
182
+ export interface DisableCommentNode extends BaseNode {
183
+ type : NodeType . disable_comment ;
184
+ text : string ;
185
+ precedingWhitespace : string ;
186
+ }
187
+
188
+ export type CommentNode = LineCommentNode | BlockCommentNode | DisableCommentNode ;
182
189
183
190
export type AstNode =
184
191
| ClauseNode
@@ -202,4 +209,5 @@ export type AstNode =
202
209
| OperatorNode
203
210
| CommaNode
204
211
| LineCommentNode
205
- | BlockCommentNode ;
212
+ | BlockCommentNode
213
+ | DisableCommentNode ;
Original file line number Diff line number Diff line change @@ -380,3 +380,10 @@ comment -> %BLOCK_COMMENT {%
380
380
precedingWhitespace: token .precedingWhitespace ,
381
381
})
382
382
% }
383
+ comment - > % DISABLE_COMMENT {%
384
+ ([token ]) => ({
385
+ type: NodeType .disable_comment ,
386
+ text: token .text ,
387
+ precedingWhitespace: token .precedingWhitespace ,
388
+ })
389
+ % }
Original file line number Diff line number Diff line change @@ -17,11 +17,13 @@ import supportsLogicalOperatorNewline from './options/logicalOperatorNewline.js'
17
17
import supportsParamTypes from './options/paramTypes.js' ;
18
18
import supportsWindowFunctions from './features/windowFunctions.js' ;
19
19
import supportsFunctionCase from './options/functionCase.js' ;
20
+ import supportsDisableComment from './features/disableComment.js' ;
20
21
21
22
/**
22
23
* Core tests for all SQL formatters
23
24
*/
24
25
export default function behavesLikeSqlFormatter ( format : FormatFn ) {
26
+ supportsDisableComment ( format ) ;
25
27
supportsCase ( format ) ;
26
28
supportsNumbers ( format ) ;
27
29
supportsWith ( format ) ;
Original file line number Diff line number Diff line change
1
+ import dedent from 'dedent-js' ;
2
+
3
+ import { FormatFn } from '../../src/sqlFormatter.js' ;
4
+
5
+ export default function supportsDisableComment ( format : FormatFn ) {
6
+ it ( 'does not format text between /* sql-formatter-disable */ and /* sql-formatter-enable */' , ( ) => {
7
+ const result = format ( dedent `
8
+ SELECT foo FROM bar;
9
+ /* sql-formatter-disable */
10
+ SELECT foo FROM bar;
11
+ /* sql-formatter-enable */
12
+ SELECT foo FROM bar;
13
+ ` ) ;
14
+
15
+ expect ( result ) . toBe ( dedent `
16
+ SELECT
17
+ foo
18
+ FROM
19
+ bar;
20
+
21
+ /* sql-formatter-disable */
22
+ SELECT foo FROM bar;
23
+ /* sql-formatter-enable */
24
+ SELECT
25
+ foo
26
+ FROM
27
+ bar;
28
+ ` ) ;
29
+ } ) ;
30
+
31
+ it ( 'does not format text after /* sql-formatter-disable */ until end of file' , ( ) => {
32
+ const result = format ( dedent `
33
+ SELECT foo FROM bar;
34
+ /* sql-formatter-disable */
35
+ SELECT foo FROM bar;
36
+
37
+ SELECT foo FROM bar;
38
+ ` ) ;
39
+
40
+ expect ( result ) . toBe ( dedent `
41
+ SELECT
42
+ foo
43
+ FROM
44
+ bar;
45
+
46
+ /* sql-formatter-disable */
47
+ SELECT foo FROM bar;
48
+
49
+ SELECT foo FROM bar;
50
+ ` ) ;
51
+ } ) ;
52
+
53
+ it ( 'does not parse code between disable/enable comments' , ( ) => {
54
+ const result = format ( dedent `
55
+ SELECT /*sql-formatter-disable*/ ?!{}[] /*sql-formatter-enable*/ FROM bar;
56
+ ` ) ;
57
+
58
+ expect ( result ) . toBe ( dedent `
59
+ SELECT
60
+ /*sql-formatter-disable*/ ?!{}[] /*sql-formatter-enable*/
61
+ FROM
62
+ bar;
63
+ ` ) ;
64
+ } ) ;
65
+ }
You can’t perform that action at this time.
0 commit comments