Skip to content

Latest commit

 

History

History
56 lines (44 loc) · 1.26 KB

prefer-replace-text.md

File metadata and controls

56 lines (44 loc) · 1.26 KB

Require using replaceText() instead of replaceTextRange() (eslint-plugin/prefer-replace-text)

Rule Details

The rule reports an error if replaceTextRange()'s first argument is an array of identical array elements. It can be easily replaced by replaceText() to improve readability.

Examples of incorrect code for this rule:

/* eslint eslint-plugin/prefer-replace-text: error */

module.exports = {
  create(context) {
    context.report({
      fix(fixer) {
        // error, can be written: return fixer.replaceText(node, '');
        return fixer.replaceTextRange([node.range[0], node.range[1]], '');
      },
    });
  },
};

Examples of correct code for this rule:

/* eslint eslint-plugin/prefer-replace-text: error */

module.exports = {
  create(context) {
    context.report({
      fix(fixer) {
        return fixer.replaceText(node, '');
      },
    });
  },
};

module.exports = {
  create(context) {
    context.report({
      fix(fixer) {
        // start = ...
        // end = ...
        return fixer.replaceTextRange([start, end], '');
      },
    });
  },
};

Further Reading