From 5e08f3f75bae722958667ca9092c2245e8380d86 Mon Sep 17 00:00:00 2001 From: "Pierre TOP (TOPI)" Date: Fri, 22 Sep 2023 16:13:27 +0200 Subject: [PATCH] Handle only knex client --- index.js | 3 ++- rules/avoid-updating-all-rows.js | 26 ++++++++++++++++++++++++++ rules/avoid-updating-all-rows.test.js | 25 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 rules/avoid-updating-all-rows.js create mode 100644 rules/avoid-updating-all-rows.test.js diff --git a/index.js b/index.js index a73ac9c..5e3215e 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ module.exports = { rules: { - 'avoid-injections': require('./rules/avoid-injections') + 'avoid-injections': require('./rules/avoid-injections'), + 'avoid-updating-all-rows': require('./rules/avoid-updating-all-rows') } } diff --git a/rules/avoid-updating-all-rows.js b/rules/avoid-updating-all-rows.js new file mode 100644 index 0000000..18de8cf --- /dev/null +++ b/rules/avoid-updating-all-rows.js @@ -0,0 +1,26 @@ +const meta = { + type: "problem", + docs: { + description: "Avoid updating all rows", + }, + messages: { + avoid: `Avoid updating all rows`, + }, +}; + +const create = function (context) { + return { + [`CallExpression[callee.property.name='update'][callee.object.callee.name='knex']`](node) { + check(context, node); + }, + }; +}; +const check = function (context, node) { + + context.report({ + node, + messageId: "avoid" + }); +}; + +module.exports = { meta, create }; diff --git a/rules/avoid-updating-all-rows.test.js b/rules/avoid-updating-all-rows.test.js new file mode 100644 index 0000000..4bfca35 --- /dev/null +++ b/rules/avoid-updating-all-rows.test.js @@ -0,0 +1,25 @@ +const { RuleTester } = require("eslint"); +const rule = require("./avoid-updating-all-rows"); + +function invalidCase(code, errors = [], others = {}) { + return Object.assign( + { + code, + errors, + }, + others, + ); +} + +const tester = new RuleTester({ + parserOptions: { ecmaVersion: 2015 }, +}); + +tester.run("avoid-updating-all-rows", rule, { + valid: ["knex('books').where({id:1}).update({'status': 'archived'})"], + invalid: [ + invalidCase("knex('books').update({'status': 'archived'})", [ + { messageId: "avoid" }, + ]), + ], +});