Skip to content

Latest commit

 

History

History
66 lines (48 loc) · 1.69 KB

prefer-placeholders.md

File metadata and controls

66 lines (48 loc) · 1.69 KB

Require using placeholders for dynamic report messages (eslint-plugin/prefer-placeholders)

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)

Rule Details

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 },
    });
  },
};

When Not To Use It

If you need to use string concatenation in your report messages for some reason, don't turn on this rule.

Further Reading