before.js 731 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. const create = context => {
  3. const sourceCode = context.getSourceCode();
  4. return {
  5. CallExpression(node) {
  6. if (node.callee.type !== 'MemberExpression'
  7. || node.callee.property.type !== 'Identifier'
  8. || node.callee.property.name !== 'substr'
  9. ) {
  10. return;
  11. }
  12. const objectNode = node.callee.object;
  13. const problem = {
  14. node,
  15. message: 'Prefer `String#slice()` over `String#substr()`.',
  16. };
  17. const canFix = node.arguments.length === 0;
  18. if (canFix) {
  19. problem.fix = fixer => fixer.replaceText(node, sourceCode.getText(objectNode) + '.slice()');
  20. }
  21. context.report(problem);
  22. },
  23. };
  24. };
  25. module.exports = {
  26. create,
  27. meta: {
  28. type: 'suggestion',
  29. fixable: 'code',
  30. },
  31. };