Skip to content

Commit

Permalink
Handle only knex client
Browse files Browse the repository at this point in the history
  • Loading branch information
octo-topi committed Sep 22, 2023
1 parent 9ced3a8 commit 5e08f3f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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')
}
}
26 changes: 26 additions & 0 deletions rules/avoid-updating-all-rows.js
Original file line number Diff line number Diff line change
@@ -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 };
25 changes: 25 additions & 0 deletions rules/avoid-updating-all-rows.test.js
Original file line number Diff line number Diff line change
@@ -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" },
]),
],
});

0 comments on commit 5e08f3f

Please sign in to comment.