Skip to content

Latest commit

 

History

History
52 lines (43 loc) · 1.18 KB

prefer-replace-text.md

File metadata and controls

52 lines (43 loc) · 1.18 KB

prefer using replaceText instead of replaceTextRange. (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-text-range: 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-text-range: 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