eslint-plugin-sql-template 3.0.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +9 -5
- package/package.json +9 -13
- package/rules/no-unsafe-query.js +71 -40
package/index.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { name, version } = require('./package.json');
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Export rules.
|
|
7
|
+
* @type {import('eslint').ESLint.Plugin}
|
|
5
8
|
*/
|
|
6
|
-
|
|
7
9
|
module.exports = {
|
|
8
|
-
meta: {
|
|
9
|
-
|
|
10
|
+
meta: {
|
|
11
|
+
name,
|
|
12
|
+
version,
|
|
13
|
+
namespace: 'sql-template'
|
|
14
|
+
},
|
|
10
15
|
rules: {
|
|
11
16
|
'no-unsafe-query': require('./rules/no-unsafe-query')
|
|
12
|
-
}
|
|
13
|
-
processors: {}
|
|
17
|
+
}
|
|
14
18
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-sql-template",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "ESLint plugin with rules for using the `sql` template tag on raw SQL queries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"plugin",
|
|
@@ -21,9 +21,6 @@
|
|
|
21
21
|
"type": "git",
|
|
22
22
|
"url": "git+ssh://git@github.com/uphold/eslint-plugin-sql-template.git"
|
|
23
23
|
},
|
|
24
|
-
"dependencies": {
|
|
25
|
-
"sql-parse": "^0.1.5"
|
|
26
|
-
},
|
|
27
24
|
"engines": {
|
|
28
25
|
"node": ">=20"
|
|
29
26
|
},
|
|
@@ -37,17 +34,16 @@
|
|
|
37
34
|
"scripts": {
|
|
38
35
|
"lint": "eslint .",
|
|
39
36
|
"release": "release-it",
|
|
40
|
-
"test": "
|
|
37
|
+
"test": "node --test test/rules/no-unsafe-query_test.js"
|
|
41
38
|
},
|
|
42
39
|
"devDependencies": {
|
|
43
|
-
"@eslint/js": "^
|
|
44
|
-
"@uphold/github-changelog-generator": "^
|
|
45
|
-
"eslint": "^
|
|
46
|
-
"eslint-config-prettier": "^
|
|
47
|
-
"eslint-plugin-prettier": "^5.
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"release-it": "^17.9.0"
|
|
40
|
+
"@eslint/js": "^10.0.1",
|
|
41
|
+
"@uphold/github-changelog-generator": "^4.0.2",
|
|
42
|
+
"eslint": "^10.0.0",
|
|
43
|
+
"eslint-config-prettier": "^10.1.8",
|
|
44
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
45
|
+
"prettier": "^3.8.1",
|
|
46
|
+
"release-it": "^19.2.4"
|
|
51
47
|
},
|
|
52
48
|
"peerDependencies": {
|
|
53
49
|
"eslint": ">=9"
|
package/rules/no-unsafe-query.js
CHANGED
|
@@ -1,77 +1,108 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Helper function to check if an expression contains a variable.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
function containsVariableExpression(expression) {
|
|
8
|
+
if (!expression) return false;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
function isSqlQuery(literal) {
|
|
14
|
-
if (!literal) {
|
|
15
|
-
return false;
|
|
10
|
+
if (['Identifier', 'CallExpression', 'MemberExpression'].includes(expression.type)) {
|
|
11
|
+
return true;
|
|
16
12
|
}
|
|
17
13
|
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
if (expression.type === 'ConditionalExpression') {
|
|
15
|
+
return containsVariableExpression(expression.consequent) || containsVariableExpression(expression.alternate);
|
|
16
|
+
}
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return false;
|
|
18
|
+
if (expression.type === 'TemplateLiteral') {
|
|
19
|
+
return expression.expressions.some(containsVariableExpression);
|
|
24
20
|
}
|
|
25
21
|
|
|
26
|
-
return
|
|
22
|
+
return false;
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
/**
|
|
30
|
-
*
|
|
26
|
+
* Helper function to check if a node has a parent that is a template literal.
|
|
31
27
|
*/
|
|
32
28
|
|
|
33
|
-
function
|
|
34
|
-
if (!node)
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
29
|
+
function hasParentTemplateLiteral(node) {
|
|
30
|
+
if (!node?.parent) return false;
|
|
37
31
|
|
|
38
|
-
if (node.type === '
|
|
39
|
-
|
|
32
|
+
if (node.parent.type === 'TemplateLiteral') {
|
|
33
|
+
return true;
|
|
40
34
|
}
|
|
41
35
|
|
|
42
|
-
|
|
43
|
-
const literal = node.quasis.map(quasi => quasi.value.raw).join('x');
|
|
44
|
-
|
|
45
|
-
if (isSqlQuery(literal)) {
|
|
46
|
-
context.report({
|
|
47
|
-
node,
|
|
48
|
-
message: 'Use the `sql` tagged template literal for raw queries'
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}
|
|
36
|
+
return hasParentTemplateLiteral(node.parent);
|
|
52
37
|
}
|
|
53
38
|
|
|
54
39
|
/**
|
|
55
|
-
*
|
|
40
|
+
* SQL starting keywords to detect inside the template literal.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
const sqlKeywords = /^`\s*(SELECT|INSERT\s+INTO|UPDATE|DELETE\s+FROM|WITH|GRANT|BEGIN|DROP)\s/i;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Rule definition.
|
|
56
47
|
*/
|
|
57
48
|
|
|
58
49
|
module.exports = {
|
|
59
50
|
meta: {
|
|
60
51
|
type: 'suggestion',
|
|
52
|
+
hasSuggestions: true,
|
|
53
|
+
fixable: 'code',
|
|
61
54
|
docs: {
|
|
62
|
-
description: '
|
|
55
|
+
description: 'Enforce safe SQL query handling using tagged templates',
|
|
63
56
|
recommended: false,
|
|
64
57
|
url: 'https://github.com/uphold/eslint-plugin-sql-template#rules'
|
|
65
58
|
},
|
|
66
|
-
|
|
59
|
+
messages: {
|
|
60
|
+
missingSqlTag: 'Use the `sql` tagged template literal for raw queries'
|
|
61
|
+
},
|
|
62
|
+
schema: []
|
|
67
63
|
},
|
|
68
64
|
create(context) {
|
|
69
65
|
return {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
66
|
+
TemplateLiteral(node) {
|
|
67
|
+
// Only check interpolated template literals.
|
|
68
|
+
if (node?.type !== 'TemplateLiteral' || node.expressions.length === 0) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Skip if the template literal has in it's chain a parent that is a TemplateLiteral.
|
|
73
|
+
if (hasParentTemplateLiteral(node)) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Skip if the template literal is already tagged with `sql`.
|
|
78
|
+
if (node.parent.type === 'TaggedTemplateExpression' && node.parent.tag.name === 'sql') {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Check if the template literal has SQL.
|
|
83
|
+
const hasSQL = sqlKeywords.test(context.sourceCode.getText(node));
|
|
84
|
+
|
|
85
|
+
// Recursively check if any expression is a variable (Identifier, MemberExpression, or nested TemplateLiteral)
|
|
86
|
+
const hasVariableExpression = node.expressions.some(containsVariableExpression);
|
|
87
|
+
|
|
88
|
+
if (hasSQL && hasVariableExpression) {
|
|
89
|
+
context.report({
|
|
90
|
+
node,
|
|
91
|
+
messageId: 'missingSqlTag',
|
|
92
|
+
suggest: [
|
|
93
|
+
{
|
|
94
|
+
desc: 'Wrap with sql tag',
|
|
95
|
+
fix(fixer) {
|
|
96
|
+
if (node.parent?.type === 'TaggedTemplateExpression') {
|
|
97
|
+
return fixer.replaceText(node.parent.tag, 'sql');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return fixer.insertTextBefore(node, 'sql');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
});
|
|
105
|
+
}
|
|
75
106
|
}
|
|
76
107
|
};
|
|
77
108
|
}
|