Skip to content

Latest commit

 

History

History
64 lines (50 loc) · 1.86 KB

prefer-message-ids.md

File metadata and controls

64 lines (50 loc) · 1.86 KB

Require using messageId instead of message or desc to report rule violations (eslint-plugin/prefer-message-ids)

💼 This rule is enabled in the ✅ recommended config.

When reporting a rule violation, it's preferred to provide the violation message with the messageId property instead of the message property. Message IDs provide the following benefits:

  • Rule violation messages can be stored in a central meta.messages object for convenient management
  • Rule violation messages do not need to be repeated in both the rule file and rule test file
  • As a result, the barrier for changing rule violation messages is lower, encouraging more frequent contributions to improve and optimize them for the greatest clarity and usefulness

Rule Details

This rule catches usages of the message property when reporting a rule violation.

Examples of incorrect code for this rule:

/* eslint eslint-plugin/prefer-message-ids: error */

module.exports = {
  create(context) {
    return {
      CallExpression(node) {
        context.report({
          node,
          message: 'Some error message.',
        });
      },
    };
  },
};

Examples of correct code for this rule:

/* eslint eslint-plugin/prefer-message-ids: error */

module.exports = {
  meta: {
    messages: {
      someMessageId: 'Some error message',
    },
  },
  create(context) {
    return {
      CallExpression(node) {
        context.report({
          node,
          messageId: 'someMessageId',
        });
      },
    };
  },
};

Further Reading