Report messages in rules can have placeholders surrounded by curly brackets.
context.report({
node,
message: '{{disallowedNode}} nodes are not allowed.',
data: { disallowedNode: node.type },
});
Using placeholders is often preferred over using dynamic report messages, for a few reasons:
- They can help enforce a separation of the message and the data.
- It will be easier to migrate when ESLint starts supporting placing lint messages in metadata (see #6740)
This rule aims to report string concatenation and template literals in report messages.
Examples of incorrect code for this rule:
/* eslint eslint-plugin/prefer-placeholders: error */
module.exports = {
create(context) {
context.report({
node,
message: `The node ${node.name} is not allowed to be used.`,
});
context.report({
node,
message: 'The node ' + node.name + ' is not allowed to be used.',
});
},
};
Examples of correct code for this rule:
/* eslint eslint-plugin/prefer-placeholders: error */
module.exports = {
create(context) {
context.report({
node,
message: 'The node {{name}} is not allowed to be used.',
data: { name: node.name },
});
},
};
If you need to use string concatenation in your report messages for some reason, don't turn on this rule.