Skip to content

Latest commit

 

History

History
108 lines (91 loc) · 2.5 KB

require-meta-default-options.md

File metadata and controls

108 lines (91 loc) · 2.5 KB

Require only rules with options to implement a meta.defaultOptions property (eslint-plugin/require-meta-default-options)

🔧 This rule is automatically fixable by the --fix CLI option.

Defining default options declaratively in a rule's meta.defaultOptions property enables ESLint v9.15.0+ to merge any user-provided options with the default options, simplifying the rule's implementation. It can also be useful for other tools like eslint-doc-generator to generate documentation for the rule's options.

Rule Details

This rule requires ESLint rules to have a valid meta.defaultOptions property if and only if the rule has options defined in its meta.schema property.

Examples of incorrect code for this rule:

/* eslint eslint-plugin/require-meta-default-options: error */

module.exports = {
  meta: {
    schema: [
      {
        type: 'object',
        /* ... */
      },
    ],
    // defaultOptions is missing
  },
  create(context) {
    /* ... */
  },
};

module.exports = {
  meta: {
    schema: [],
    defaultOptions: [{}], // defaultOptions is not needed when schema is empty
  },
  create(context) {
    /* ... */
  },
};

module.exports = {
  meta: {
    schema: [
      {
        /* ... */
      },
    ],
    defaultOptions: {}, // defaultOptions should be an array
  },
  create(context) {
    /* ... */
  },
};

module.exports = {
  meta: {
    schema: [
      {
        /* ... */
      },
    ],
    defaultOptions: [], // defaultOptions should not be empty
  },
  create(context) {
    /* ... */
  },
};

Examples of correct code for this rule:

/* eslint eslint-plugin/require-meta-default-options: error */

module.exports = {
  meta: { schema: [] }, // no defaultOptions needed when schema is empty
  create(context) {
    /* ... */
  },
};

module.exports = {
  meta: {
    schema: [
      {
        type: 'object',
        properties: {
          exceptRange: {
            type: 'boolean',
          },
        },
        additionalProperties: false,
      },
    ],
    defaultOptions: [{ exceptRange: false }],
  },
  create(context) {
    /* ... */
  },
};

Further Reading