prefer-string-trim-start-end.js 1009 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. }),
  12. ' > .callee',
  13. ' > .property',
  14. ].join(' ');
  15. /** @param {import('eslint').Rule.RuleContext} context */
  16. const create = () => ({
  17. [selector](node) {
  18. const method = node.name;
  19. const replacement = method === 'trimLeft' ? 'trimStart' : 'trimEnd';
  20. return {
  21. node,
  22. messageId: MESSAGE_ID,
  23. data: {method, replacement},
  24. fix: fixer => fixer.replaceText(node, replacement),
  25. };
  26. },
  27. });
  28. /** @type {import('eslint').Rule.RuleModule} */
  29. module.exports = {
  30. create,
  31. meta: {
  32. type: 'suggestion',
  33. docs: {
  34. description: 'Prefer `String#trimStart()` / `String#trimEnd()` over `String#trimLeft()` / `String#trimRight()`.',
  35. },
  36. fixable: 'code',
  37. messages,
  38. },
  39. };