hedgedoc/eslint-local-rules.js
Tilman Vatteroth 2241a3faea feat(package): adjust packages to workspaces
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2022-12-04 20:59:46 +01:00

58 lines
1.7 KiB
JavaScript

/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
'use strict';
const loggerFunctions = ['error', 'log', 'warn', 'debug', 'verbose'];
module.exports = {
'correct-logger-context': {
meta: {
fixable: 'code',
type: 'problem',
docs: {
recommended: true
},
schema: [],
},
create: function (context) {
return {
CallExpression: function (node) {
if (
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'MemberExpression' &&
node.callee.object.property.name === 'logger' &&
loggerFunctions.includes(node.callee.property.name) &&
!!node.arguments &&
node.arguments.length === 2
) {
const usedContext = node.arguments[1].value;
let correctContext = 'undefined';
const ancestors = context.getAncestors();
for (let index = ancestors.length - 1; index >= 0; index--) {
if (ancestors[index].type === 'MethodDefinition') {
correctContext = ancestors[index].key.name;
break;
}
}
if (usedContext !== correctContext) {
context.report({
node: node,
message: `Used wrong context in log statement`,
fix: function (fixer) {
return fixer.replaceText(
node.arguments[1],
`'${correctContext}'`,
);
},
});
}
}
},
};
},
},
};