UNPKG

@addon24/eslint-config

Version:

ESLint configuration rules for WorldOfTextcraft projects - Centralized configuration for all project types

72 lines (64 loc) 2.33 kB
/** * @fileoverview Enforce Pino logger format: logger.method(object, message) * @author WorldOfTextcraft Team */ "use strict"; export default { meta: { type: "problem", docs: { description: "Enforce Pino logger format: logger.method(object, message)", category: "Best Practices", recommended: true, }, fixable: "code", schema: [], messages: { wrongLoggerFormat: "Logger call must use Pino format: logger.{{method}}(object, message). Found: logger.{{method}}({{currentFormat}})", suggestFix: "Change to: logger.{{method}}({{object}}, {{message}})", }, }, create(context) { return { CallExpression(node) { // Check if this is a logger call if ( node.callee.type === "MemberExpression" && node.callee.object.name === "logger" && ["error", "info", "warn", "debug", "trace", "fatal", "log"].includes(node.callee.property.name) ) { const method = node.callee.property.name; const args = node.arguments; // Must have exactly 2 arguments if (args.length !== 2) { return; } const [firstArg, secondArg] = args; // Check if first argument is a string literal (wrong format) if (firstArg.type === "Literal" && typeof firstArg.value === "string") { // Check if second argument is an object or identifier (correct format would be reversed) if (secondArg.type === "ObjectExpression" || secondArg.type === "Identifier") { context.report({ node, messageId: "wrongLoggerFormat", data: { method, currentFormat: `"${firstArg.value}", ${context.getSourceCode().getText(secondArg)}`, }, fix(fixer) { // Swap the arguments const firstArgText = context.getSourceCode().getText(firstArg); const secondArgText = context.getSourceCode().getText(secondArg); return fixer.replaceText( node, `logger.${method}(${secondArgText}, ${firstArgText})` ); }, }); } } } }, }; }, };