Skip to content

Commit 3ff1410

Browse files
author
Elise DUBILLOT
committed
add rule limit-db-query-results
1 parent 2fdb01f commit 3ff1410

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- [#21](https://github.com/green-code-initiative/ecoCode-javascript/pull/21) Add rule `@ecocode/avoid-css-animations`
1313
- [#19](https://github.com/green-code-initiative/ecoCode-javascript/pull/19) add rule `@ecocode/no-empty-image-src-attribute`
14+
- [#18](https://github.com/green-code-initiative/ecoCode-javascript/pull/18) add rule `@ecocode/limit-db-query-results`
1415
- [#14](https://github.com/green-code-initiative/ecoCode-javascript/pull/14) Create SonarQube plugin
1516
- [#12](https://github.com/green-code-initiative/ecoCode-javascript/issues/12) Pack ESLint plugin into SonarQube plugin
1617
- [#16](https://github.com/green-code-initiative/ecoCode-javascript/pull/16) Use centralized rules specifications

eslint-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Add `@ecocode` to the `plugins` section of your `.eslintrc`, followed by rules c
6060
| :------------------------------------------------------------------------------------- | :--------------------------------------------------------- | :- |
6161
| [avoid-css-animations](docs/rules/avoid-css-animations.md) | Avoid usage of CSS animations ||
6262
| [avoid-high-accuracy-geolocation](docs/rules/avoid-high-accuracy-geolocation.md) | Avoid using high accuracy geolocation in web applications. ||
63+
| [limit-db-query-results](docs/rules/limit-db-query-results.md) | Should limit the number of returns for a SQL query ||
6364
| [no-empty-image-src-attribute](docs/rules/no-empty-image-src-attribute.md) | Disallow usage of image with empty source attribute ||
6465
| [no-import-all-from-library](docs/rules/no-import-all-from-library.md) | Should not import all from library ||
6566
| [no-multiple-access-dom-element](docs/rules/no-multiple-access-dom-element.md) | Disallow multiple access of same DOM element. ||
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Should limit the number of returns for a SQL query (`@ecocode/limit-db-query-results`)
2+
3+
⚠️ This rule _warns_ in the ✅ `recommended` config.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
## Rule Details
8+
9+
This rule aims at reducing CPU consumption by limiting the number of returns for a single SQL query.
10+
11+
## Examples
12+
13+
Examples of **non compliant** code for this rule:
14+
15+
```js
16+
const query = "SELECT * FROM clients";
17+
```
18+
19+
Examples of **compliant** code for this rule:
20+
21+
```js
22+
const query = "SELECT columns FROM table_name FETCH FIRST number ROWS ONLY";
23+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (C) 2023 Green Code Initiative
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
"use strict";
18+
19+
/** @type {import('eslint').Rule.RuleModule} */
20+
module.exports = {
21+
meta: {
22+
type: "suggestion",
23+
docs: {
24+
description: "Should limit the number of returns for a SQL query",
25+
category: "eco-design",
26+
recommended: "warn",
27+
},
28+
messages: {
29+
LimitTheNumberOfReturns:
30+
"Try and limit the number of data returned for a single query (by using the LIMIT keyword for example)",
31+
},
32+
schema: [],
33+
},
34+
create(context) {
35+
//list of limiting clauses to check against
36+
const limitingClauses = ["LIMIT", "TOP", "ROW_NUMBER", "FETCH FIRST"];
37+
return {
38+
Literal: function (node) {
39+
if (typeof node.value == "string") {
40+
const query = node.value.toUpperCase();
41+
if (
42+
query.includes("SELECT") &&
43+
query.includes("FROM") &&
44+
!limitingClauses.some((clause) => query.includes(clause))
45+
) {
46+
context.report({
47+
node: node,
48+
messageId: "LimitTheNumberOfReturns",
49+
});
50+
}
51+
}
52+
},
53+
};
54+
},
55+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (C) 2023 Green Code Initiative
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
"use strict";
18+
//------------------------------------------------------------------------------
19+
// Requirements
20+
//------------------------------------------------------------------------------
21+
22+
const rule = require("../../../lib/rules/limit-db-query-results"),
23+
RuleTester = require("eslint").RuleTester;
24+
25+
//------------------------------------------------------------------------------
26+
// Tests
27+
//------------------------------------------------------------------------------
28+
29+
const ruleTester = new RuleTester({
30+
parserOptions: {
31+
ecmaVersion: 6,
32+
sourceType: "module",
33+
},
34+
});
35+
const expectedError = {
36+
messageId: "LimitTheNumberOfReturns",
37+
type: "Literal",
38+
};
39+
40+
ruleTester.run("limit-db-query-results", rule, {
41+
valid: [
42+
`
43+
const query = "SELECT * FROM customers LIMIT 10;";
44+
`,
45+
`
46+
const query = "SELECT TOP 5 * FROM products;";
47+
`,
48+
`
49+
const query = "SELECT * FROM orders FETCH FIRST 20 ROWS ONLY;";
50+
`,
51+
`
52+
const query = "WITH numbered_customers AS (SELECT *, ROW_NUMBER() OVER (ORDER BY customer_id) AS row_num FROM customers) SELECT * FROM numbered_customers WHERE row_num <= 50;";
53+
`,
54+
],
55+
56+
invalid: [
57+
{
58+
code: `const query = "SELECT * FROM bikes;";`,
59+
errors: [expectedError],
60+
},
61+
],
62+
});

0 commit comments

Comments
 (0)