Skip to content

Commit 15de546

Browse files
authored
Add support for custom quote types (#516)
2 parents 039f655 + a0aca28 commit 15de546

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/lexer/TokenizerOptions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ export interface PrefixedQuoteType {
2222
requirePrefix?: boolean; // True when prefix is required
2323
}
2424

25-
export type QuoteType = PlainQuoteType | PrefixedQuoteType;
26-
27-
export interface VariableRegex {
25+
export interface RegexPattern {
2826
regex: string;
2927
}
3028

31-
export type VariableType = VariableRegex | PrefixedQuoteType;
29+
export type QuoteType = PlainQuoteType | PrefixedQuoteType | RegexPattern;
30+
31+
export type VariableType = RegexPattern | PrefixedQuoteType;
3232

3333
export interface ParamTypes {
3434
// True to allow for positional "?" parameter placeholders

src/lexer/regexFactory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ export const quotePatterns = {
127127
const singleQuotePattern = (quoteTypes: QuoteType): string => {
128128
if (typeof quoteTypes === 'string') {
129129
return quotePatterns[quoteTypes];
130+
} else if ('regex' in quoteTypes) {
131+
return quoteTypes.regex;
130132
} else {
131133
return prefixesPattern(quoteTypes) + quotePatterns[quoteTypes.quote];
132134
}

test/sqlFormatter.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dedent from 'dedent-js';
22

3-
import { format, formatDialect, SqlLanguage, sqlite } from '../src/index.js';
3+
import { format, formatDialect, SqlLanguage, sqlite, DialectOptions } from '../src/index.js';
44

55
describe('sqlFormatter', () => {
66
it('throws error when unsupported language parameter specified', () => {
@@ -63,5 +63,28 @@ describe('sqlFormatter', () => {
6363
\`bar\`;
6464
`);
6565
});
66+
67+
it('allows use of regex-based custom string type', () => {
68+
// Extend SQLite dialect with additional string type
69+
const sqliteWithTemplates: DialectOptions = {
70+
tokenizerOptions: {
71+
...sqlite.tokenizerOptions,
72+
stringTypes: [...sqlite.tokenizerOptions.stringTypes, { regex: String.raw`\{\{.*?\}\}` }],
73+
},
74+
formatOptions: sqlite.formatOptions,
75+
};
76+
77+
expect(
78+
formatDialect(`SELECT {{template item}}, 'normal string' FROM {{tbl}};`, {
79+
dialect: sqliteWithTemplates,
80+
})
81+
).toBe(dedent`
82+
SELECT
83+
{{template item}},
84+
'normal string'
85+
FROM
86+
{{tbl}};
87+
`);
88+
});
6689
});
6790
});

0 commit comments

Comments
 (0)