From 925bb85d2f4794920ff1df7cf3ced0b1e33964cc Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 25 Mar 2023 14:55:06 +0100 Subject: [PATCH] build: add eslint rule forbidding TypeORM Equal constructor TypeORMs Equal constructor is buggy and should not be used. This introduces a ESLint rule that checks for that. Signed-off-by: David Mehren --- backend/.eslintrc.js | 5 ++--- eslint-local-rules.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/backend/.eslintrc.js b/backend/.eslintrc.js index f90551066..3b89fb6b2 100644 --- a/backend/.eslintrc.js +++ b/backend/.eslintrc.js @@ -52,10 +52,9 @@ module.exports = { jest: true, }, rules: { - "prettier/prettier": ["error", - require('./.prettierrc.json') - ], + 'prettier/prettier': ['error', require('./.prettierrc.json')], 'local-rules/correct-logger-context': 'error', + 'local-rules/no-typeorm-equal': 'error', 'func-style': ['error', 'declaration'], '@typescript-eslint/no-unused-vars': [ 'warn', diff --git a/eslint-local-rules.js b/eslint-local-rules.js index 7240cd998..faa5038d7 100644 --- a/eslint-local-rules.js +++ b/eslint-local-rules.js @@ -14,7 +14,7 @@ module.exports = { fixable: 'code', type: 'problem', docs: { - recommended: true + recommended: true, }, schema: [], }, @@ -45,7 +45,7 @@ module.exports = { fix: function (fixer) { return fixer.replaceText( node.arguments[1], - `'${correctContext}'`, + `'${correctContext}'` ); }, }); @@ -55,4 +55,32 @@ module.exports = { }; }, }, + 'no-typeorm-equal': { + meta: { + type: 'problem', + fixable: 'code', + messages: { + noEqual: + 'TypeORMs Equal constructor is buggy and therefore not allowed.', + }, + }, + create: function (context) { + return { + Identifier: function (node) { + if (node.name === 'Equal' && node.parent.type === 'CallExpression') { + context.report({ + node: node, + messageId: 'noEqual', + fix: function (fixer) { + return fixer.replaceText( + node.parent, + `{ id: ${node.parent.arguments[0].name}.id }` + ); + }, + }); + } + }, + }; + }, + }, };