after.js 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. const eslintTemplateVisitor = require('../..');
  3. const templates = eslintTemplateVisitor();
  4. const objectVariable = templates.variable();
  5. const argumentsVariable = templates.spreadVariable();
  6. const substrCallTemplate = templates.template`${objectVariable}.substr(${argumentsVariable})`;
  7. const create = context => {
  8. const sourceCode = context.getSourceCode();
  9. return templates.visitor({
  10. [substrCallTemplate](node) {
  11. const objectNode = substrCallTemplate.context.getMatch(objectVariable);
  12. const argumentNodes = substrCallTemplate.context.getMatch(argumentsVariable);
  13. const problem = {
  14. node,
  15. message: 'Prefer `String#slice()` over `String#substr()`.',
  16. };
  17. const canFix = argumentNodes.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. };