Skip to content

Commit

Permalink
feat: add salTag option (fixes #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 27, 2023
1 parent 7e57f2b commit 40d887f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .README/rules/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ The first option is an object with the following configuration.
|`ignoreBaseIndent`|boolean|`false`|Does not leave base indent before linting.|
|`ignoreExpressions`|boolean|`false`|Does not format template literals that contain expressions.|
|`ignoreInline`|boolean|`true`|Does not format queries that are written on a single line.|
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
|`ignoreStartWithNewLine`|boolean|`true`|Does not remove `\n` at the beginning of queries.|
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
|`sqlTag`|string|`sql`|Template tag name for SQL.|

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

Expand Down
1 change: 1 addition & 0 deletions .README/rules/no-unsafe-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ The first option is an object with the following configuration.
|configuration|format|default|description|
|---|---|---|---|
|`allowLiteral`|boolean|`false`|Controls whether `sql` tag is required for template literals containing literal queries, i.e. template literals without expressions.|
|`sqlTag`|string|`sql`|Template tag name for SQL.|

<!-- assertions noUnsafeQuery -->
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ The first option is an object with the following configuration.
|`ignoreBaseIndent`|boolean|`false`|Does not leave base indent before linting.|
|`ignoreExpressions`|boolean|`false`|Does not format template literals that contain expressions.|
|`ignoreInline`|boolean|`true`|Does not format queries that are written on a single line.|
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
|`ignoreStartWithNewLine`|boolean|`true`|Does not remove `\n` at the beginning of queries.|
|`ignoreTagless`|boolean|`true`|Does not format queries that are written without using `sql` tag.|
|`sqlTag`|string|`sql`|Template tag name for SQL.|

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

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



7 changes: 6 additions & 1 deletion src/rules/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const create = (context) => {

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

const sqlTag = pluginOptions.sqlTag;
const ignoreExpressions = pluginOptions.ignoreExpressions === true;
const ignoreInline = pluginOptions.ignoreInline !== false;
const ignoreTagless = pluginOptions.ignoreTagless !== false;
Expand All @@ -21,7 +22,7 @@ const create = (context) => {
node.parent.tag?.object?.name ??
node.parent.tag?.callee?.object?.name;

const sqlTagIsPresent = tagName === 'sql';
const sqlTagIsPresent = tagName === sqlTag;

if (ignoreTagless && !sqlTagIsPresent) {
return;
Expand Down Expand Up @@ -131,6 +132,10 @@ export = {
default: true,
type: 'boolean',
},
sqlTag: {
default: 'sql',
type: 'string',
},
},
type: 'object',
},
Expand Down
15 changes: 12 additions & 3 deletions src/rules/noUnsafeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ const debug = createDebug('eslint-plugin-sql:rule:no-unsafe-query');

const defaultOptions = {
allowLiteral: false,
sqlTag: 'sql',
};

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

const { allowLiteral } = context.options[0] ?? defaultOptions;
const pluginOptions = context.options?.[0] || {};

const sqlTag = pluginOptions.sqlTag ?? defaultOptions.sqlTag;
const allowLiteral =
pluginOptions.allowLiteral ?? defaultOptions.allowLiteral;

return {
TemplateLiteral(node) {
Expand Down Expand Up @@ -41,9 +46,9 @@ const create = (context) => {

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

if (legacyTagName !== 'sql' && tagName !== 'sql') {
if (legacyTagName !== sqlTag && tagName !== sqlTag) {
context.report({
message: 'Use "sql" tag',
message: `Use "${sqlTag}" tag`,
node,
});
}
Expand All @@ -68,6 +73,10 @@ export = {
default: false,
type: 'boolean',
},
sqlTag: {
default: 'sql',
type: 'string',
},
},
type: 'object',
},
Expand Down
15 changes: 15 additions & 0 deletions test/rules/assertions/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ export default {
output:
" const code = sql`\nSELECT\n ${'foo'}\nFROM\n ${'bar'}\n`",
},
{
code: 'SQL`SELECT 1`',
errors: [
{
message: 'Format the query',
},
],
options: [
{
ignoreInline: false,
sqlTag: 'SQL',
},
],
output: 'SQL`\nSELECT\n 1\n`',
},
],
valid: [
{
Expand Down
21 changes: 21 additions & 0 deletions test/rules/assertions/noUnsafeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ export default {
},
},
},
{
code: "foo`SELECT ${'bar'}`",
errors: [
{
message: 'Use "SQL" tag',
},
],
options: [
{
sqlTag: 'SQL',
},
],
},
],
valid: [
{
Expand All @@ -56,5 +69,13 @@ export default {
{
code: "sql`SELECT ${'foo'}`",
},
{
code: "SQL`SELECT ${'bar'}`",
options: [
{
sqlTag: 'SQL',
},
],
},
],
};

0 comments on commit 40d887f

Please sign in to comment.