Skip to content

Commit

Permalink
add rule limit-db-query-results
Browse files Browse the repository at this point in the history
  • Loading branch information
Elise DUBILLOT committed Aug 10, 2023
1 parent 5844b11 commit 1f02dfb
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 24 deletions.
25 changes: 12 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- [#19](https://github.com/green-code-initiative/ecoCode-javascript/pull/19) add rule `@ecocode/no-empty-image-src-attribute`
- [#18](https://github.com/green-code-initiative/ecoCode-javascript/pull/18) add rule `@ecocode/limit-db-query-results`
- [#14](https://github.com/green-code-initiative/ecoCode-javascript/pull/14) Create SonarQube plugin
- [#12](https://github.com/green-code-initiative/ecoCode-javascript/issues/12) Pack ESLint plugin into SonarQube plugin
- [#16](https://github.com/green-code-initiative/ecoCode-javascript/pull/16) Use centralized rules specifications
Expand All @@ -24,25 +25,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add support for TypeScript rules with **typescript-eslint**
- Add rule `@ecocode/avoid-high-accuracy-geolocation`
- Add rule `@ecocode/no-import-all-from-library`
- Add rule `@ecocode/no-multiple-style-changes`
- Add rule `@ecocode/prefer-collections-with-pagination`
- Add support for TypeScript rules with **typescript-eslint**
- Add rule `@ecocode/avoid-high-accuracy-geolocation`
- Add rule `@ecocode/no-import-all-from-library`
- Add rule `@ecocode/no-multiple-style-changes`
- Add rule `@ecocode/prefer-collections-with-pagination`

## [0.1.0] - 2023-03-24

### Added

- First alpha version of the ESLint plugin 🚀
- Add rule `@ecocode/no-multiple-access-dom-element`
- Create tooling script to generate SonarQube rules
- Setup mocha and nyc for tests and coverage
- Setup basic coding style tools
- Write complete contributing guide
- First alpha version of the ESLint plugin 🚀
- Add rule `@ecocode/no-multiple-access-dom-element`
- Create tooling script to generate SonarQube rules
- Setup mocha and nyc for tests and coverage
- Setup basic coding style tools
- Write complete contributing guide

[Unreleased]: https://github.com/green-code-initiative/ecoCode-linter/compare/eslint-plugin/0.2.0...HEAD

[0.2.0]: https://github.com/green-code-initiative/ecoCode-linter/compare/eslint-plugin/0.1.0...eslint-plugin/0.2.0

[0.1.0]: https://github.com/green-code-initiative/ecoCode-linter/compare/6d305511db82bf8faa4833528641535e605dbacf...eslint-plugin/0.1.0
18 changes: 7 additions & 11 deletions eslint-plugin/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
![Logo](https://github.com/green-code-initiative/ecoCode/blob/main/docs/resources/logo-large.png?raw=true)
======================================
# ![Logo](https://github.com/green-code-initiative/ecoCode/blob/main/docs/resources/logo-large.png?raw=true)

An ESLint plugin which provides JavaScript and TypeScript rules of the ecoCode project.

👉 See [ecoCode-javascript README](../README.md) to have more information.

> ⚠️ This plugin is in a very early stage and need improvements. Any contribution will be appreciated.
🚀 Getting started
------------------
## 🚀 Getting started

### Installation

Expand Down Expand Up @@ -48,17 +46,17 @@ Add `@ecocode` to the `plugins` section of your `.eslintrc`, followed by rules c
}
```

🔨 Rules
--------
## 🔨 Rules

<!-- begin auto-generated rules list -->

⚠️ Configurations set to warn in.\
✅ Set in the `recommended` configuration.

| Name | Description | ⚠️ |
| :------------------------------------------------------------------------------------- | :--------------------------------------------------------- | :- |
| Name | Description | ⚠️ |
| :------------------------------------------------------------------------------------- | :--------------------------------------------------------- | :-- |
| [avoid-high-accuracy-geolocation](docs/rules/avoid-high-accuracy-geolocation.md) | Avoid using high accuracy geolocation in web applications. ||
| [limit-db-query-results](docs/rules/limit-db-query-results.md) | Should limit the number of returns for a SQL query ||
| [no-empty-image-src-attribute](docs/rules/no-empty-image-src-attribute.md) | Disallow usage of image with empty source attribute ||
| [no-import-all-from-library](docs/rules/no-import-all-from-library.md) | Should not import all from library ||
| [no-multiple-access-dom-element](docs/rules/no-multiple-access-dom-element.md) | Disallow multiple access of same DOM element. ||
Expand All @@ -67,8 +65,6 @@ Add `@ecocode` to the `plugins` section of your `.eslintrc`, followed by rules c

<!-- end auto-generated rules list -->


🛒 Distribution
---------------
## 🛒 Distribution

You can follow changelog on [GitHub Releases page](https://github.com/green-code-initiative/ecoCode-linter/releases).
23 changes: 23 additions & 0 deletions eslint-plugin/docs/rules/limit-db-query-results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Should limit the number of returns for a SQL query (`@ecocode/limit-db-query-results`)

⚠️ This rule _warns_ in the ✅ `recommended` config.

<!-- end auto-generated rule header -->

## Rule Details

This rule aims at reducing CPU consumption by limiting the number of returns for a single SQL query.

## Examples

Examples of **non compliant** code for this rule:

```js
const query = "SELECT * FROM clients";
```

Examples of **compliant** code for this rule:

```js
const query = "SELECT columns FROM table_name FETCH FIRST number ROWS ONLY";
```
55 changes: 55 additions & 0 deletions eslint-plugin/lib/rules/limit-db-query-results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (C) 2023 Green Code Initiative
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Should limit the number of returns for a SQL query",
category: "eco-design",
recommended: "warn",
},
messages: {
LimitTheNumberOfReturns:
"Try and limit the number of data returned for a single query (by using the LIMIT keyword for example)",
},
schema: [],
},
create(context) {
//list of limiting clauses to check against
const limitingClauses = ["LIMIT", "TOP", "ROW_NUMBER", "FETCH FIRST"];
return {
Literal: function (node) {
if (typeof node.value == "string") {
const query = node.value.toUpperCase();
if (
query.includes("SELECT") &&
query.includes("FROM") &&
!limitingClauses.some((clause) => query.includes(clause))
) {
context.report({
node: node,
messageId: "LimitTheNumberOfReturns",
});
}
}
},
};
},
};
62 changes: 62 additions & 0 deletions eslint-plugin/tests/lib/rules/limit-db-query-results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (C) 2023 Green Code Initiative
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/limit-db-query-results"),
RuleTester = require("eslint").RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
},
});
const expectedError = {
messageId: "LimitTheNumberOfReturns",
type: "Literal",
};

ruleTester.run("limit-db-query-results", rule, {
valid: [
`
const query = "SELECT * FROM customers LIMIT 10;";
`,
`
const query = "SELECT TOP 5 * FROM products;";
`,
`
const query = "SELECT * FROM orders FETCH FIRST 20 ROWS ONLY;";
`,
`
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;";
`,
],

invalid: [
{
code: `const query = "SELECT * FROM bikes;";`,
errors: [expectedError],
},
],
});

0 comments on commit 1f02dfb

Please sign in to comment.