Skip to content

Commit 40d887f

Browse files
committed
feat: add salTag option (fixes #40)
1 parent 7e57f2b commit 40d887f

File tree

7 files changed

+60
-6
lines changed

7 files changed

+60
-6
lines changed

.README/rules/format.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ The first option is an object with the following configuration.
1515
|`ignoreBaseIndent`|boolean|`false`|Does not leave base indent before linting.|
1616
|`ignoreExpressions`|boolean|`false`|Does not format template literals that contain expressions.|
1717
|`ignoreInline`|boolean|`true`|Does not format queries that are written on a single line.|
18-
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
1918
|`ignoreStartWithNewLine`|boolean|`true`|Does not remove `\n` at the beginning of queries.|
19+
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
20+
|`sqlTag`|string|`sql`|Template tag name for SQL.|
2021

2122
The second option is an object with the [`pg-formatter` configuration](https://github.com/gajus/pg-formatter#configuration).
2223

.README/rules/no-unsafe-query.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ The first option is an object with the following configuration.
1414
|configuration|format|default|description|
1515
|---|---|---|---|
1616
|`allowLiteral`|boolean|`false`|Controls whether `sql` tag is required for template literals containing literal queries, i.e. template literals without expressions.|
17+
|`sqlTag`|string|`sql`|Template tag name for SQL.|
1718

1819
<!-- assertions noUnsafeQuery -->

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ The first option is an object with the following configuration.
109109
|`ignoreBaseIndent`|boolean|`false`|Does not leave base indent before linting.|
110110
|`ignoreExpressions`|boolean|`false`|Does not format template literals that contain expressions.|
111111
|`ignoreInline`|boolean|`true`|Does not format queries that are written on a single line.|
112-
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
113112
|`ignoreStartWithNewLine`|boolean|`true`|Does not remove `\n` at the beginning of queries.|
113+
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
114+
|`sqlTag`|string|`sql`|Template tag name for SQL.|
114115

115116
The second option is an object with the [`pg-formatter` configuration](https://github.com/gajus/pg-formatter#configuration).
116117

@@ -136,6 +137,7 @@ The first option is an object with the following configuration.
136137
|configuration|format|default|description|
137138
|---|---|---|---|
138139
|`allowLiteral`|boolean|`false`|Controls whether `sql` tag is required for template literals containing literal queries, i.e. template literals without expressions.|
140+
|`sqlTag`|string|`sql`|Template tag name for SQL.|
139141

140142

141143

src/rules/format.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const create = (context) => {
88

99
const pluginOptions = context.options?.[0] || {};
1010

11+
const sqlTag = pluginOptions.sqlTag;
1112
const ignoreExpressions = pluginOptions.ignoreExpressions === true;
1213
const ignoreInline = pluginOptions.ignoreInline !== false;
1314
const ignoreTagless = pluginOptions.ignoreTagless !== false;
@@ -21,7 +22,7 @@ const create = (context) => {
2122
node.parent.tag?.object?.name ??
2223
node.parent.tag?.callee?.object?.name;
2324

24-
const sqlTagIsPresent = tagName === 'sql';
25+
const sqlTagIsPresent = tagName === sqlTag;
2526

2627
if (ignoreTagless && !sqlTagIsPresent) {
2728
return;
@@ -131,6 +132,10 @@ export = {
131132
default: true,
132133
type: 'boolean',
133134
},
135+
sqlTag: {
136+
default: 'sql',
137+
type: 'string',
138+
},
134139
},
135140
type: 'object',
136141
},

src/rules/noUnsafeQuery.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ const debug = createDebug('eslint-plugin-sql:rule:no-unsafe-query');
55

66
const defaultOptions = {
77
allowLiteral: false,
8+
sqlTag: 'sql',
89
};
910

1011
const create = (context) => {
1112
const placeholderRule = context.settings?.sql?.placeholderRule;
1213

13-
const { allowLiteral } = context.options[0] ?? defaultOptions;
14+
const pluginOptions = context.options?.[0] || {};
15+
16+
const sqlTag = pluginOptions.sqlTag ?? defaultOptions.sqlTag;
17+
const allowLiteral =
18+
pluginOptions.allowLiteral ?? defaultOptions.allowLiteral;
1419

1520
return {
1621
TemplateLiteral(node) {
@@ -41,9 +46,9 @@ const create = (context) => {
4146

4247
const legacyTagName = node.parent.name?.toLowerCase();
4348

44-
if (legacyTagName !== 'sql' && tagName !== 'sql') {
49+
if (legacyTagName !== sqlTag && tagName !== sqlTag) {
4550
context.report({
46-
message: 'Use "sql" tag',
51+
message: `Use "${sqlTag}" tag`,
4752
node,
4853
});
4954
}
@@ -68,6 +73,10 @@ export = {
6873
default: false,
6974
type: 'boolean',
7075
},
76+
sqlTag: {
77+
default: 'sql',
78+
type: 'string',
79+
},
7180
},
7281
type: 'object',
7382
},

test/rules/assertions/format.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ export default {
9191
output:
9292
" const code = sql`\nSELECT\n ${'foo'}\nFROM\n ${'bar'}\n`",
9393
},
94+
{
95+
code: 'SQL`SELECT 1`',
96+
errors: [
97+
{
98+
message: 'Format the query',
99+
},
100+
],
101+
options: [
102+
{
103+
ignoreInline: false,
104+
sqlTag: 'SQL',
105+
},
106+
],
107+
output: 'SQL`\nSELECT\n 1\n`',
108+
},
94109
],
95110
valid: [
96111
{

test/rules/assertions/noUnsafeQuery.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ export default {
3737
},
3838
},
3939
},
40+
{
41+
code: "foo`SELECT ${'bar'}`",
42+
errors: [
43+
{
44+
message: 'Use "SQL" tag',
45+
},
46+
],
47+
options: [
48+
{
49+
sqlTag: 'SQL',
50+
},
51+
],
52+
},
4053
],
4154
valid: [
4255
{
@@ -56,5 +69,13 @@ export default {
5669
{
5770
code: "sql`SELECT ${'foo'}`",
5871
},
72+
{
73+
code: "SQL`SELECT ${'bar'}`",
74+
options: [
75+
{
76+
sqlTag: 'SQL',
77+
},
78+
],
79+
},
5980
],
6081
};

0 commit comments

Comments
 (0)