ec0lint-plugin-ec0lint-plugin
Version:
An ec0lint plugin for linting ec0lint plugins
103 lines (88 loc) • 3.23 kB
JavaScript
/**
* @fileoverview require using placeholders for dynamic report messages
* @author Teddy Katz
*/
;
const utils = require('../utils');
const { findVariable } = require('eslint-utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require using placeholders for dynamic report messages',
category: 'Rules',
recommended: false,
url: 'https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/prefer-placeholders.md',
},
fixable: null,
schema: [],
messages: {
usePlaceholders:
'Use report message placeholders instead of string concatenation.',
},
},
create(context) {
let contextIdentifiers;
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
return {
Program(ast) {
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
},
CallExpression(node) {
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'report'
) {
const reportInfo = utils.getReportInfo(node.arguments, context);
if (!reportInfo) {
return;
}
const reportMessagesAndDataArray = utils
.collectReportViolationAndSuggestionData(reportInfo)
.filter((obj) => obj.message);
for (let { message: messageNode } of reportMessagesAndDataArray) {
if (messageNode.type === 'Identifier') {
// See if we can find the variable declaration.
const variable = findVariable(
scopeManager.acquire(messageNode) || scopeManager.globalScope,
messageNode
);
if (
!variable ||
!variable.defs ||
!variable.defs[0] ||
!variable.defs[0].node ||
variable.defs[0].node.type !== 'VariableDeclarator' ||
!variable.defs[0].node.init
) {
return;
}
messageNode = variable.defs[0].node.init;
}
if (
(messageNode.type === 'TemplateLiteral' &&
messageNode.expressions.length > 0) ||
(messageNode.type === 'BinaryExpression' &&
messageNode.operator === '+')
) {
context.report({
node: messageNode,
messageId: 'usePlaceholders',
});
}
}
}
},
};
},
};