prefer-string-trim-start-end.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. const {methodCallSelector} = require('./selectors/index.js');
  3. const MESSAGE_ID = 'prefer-string-trim-start-end';
  4. const messages = {
  5. [MESSAGE_ID]: 'Prefer `String#{{replacement}}()` over `String#{{method}}()`.',
  6. };
  7. const selector = [
  8. methodCallSelector({
  9. methods: ['trimLeft', 'trimRight'],
  10. argumentsLength: 0,
  11. includeOptionalMember: true,
  12. }),
  13. ' > .callee',
  14. ' > .property',
  15. ].join(' ');
  16. /** @param {import('eslint').Rule.RuleContext} context */
  17. const create = () => ({
  18. [selector](node) {
  19. const method = node.name;
  20. const replacement = method === 'trimLeft' ? 'trimStart' : 'trimEnd';
  21. return {
  22. node,
  23. messageId: MESSAGE_ID,
  24. data: {method, replacement},
  25. fix: fixer => fixer.replaceText(node, replacement),
  26. };
  27. },
  28. });
  29. /** @type {import('eslint').Rule.RuleModule} */
  30. module.exports = {
  31. create,
  32. meta: {
  33. type: 'suggestion',
  34. docs: {
  35. description: 'Prefer `String#trimStart()` / `String#trimEnd()` over `String#trimLeft()` / `String#trimRight()`.',
  36. },
  37. fixable: 'code',
  38. messages,
  39. },
  40. };