line_push/node_modules/eslint-template-visitor/examples/prefer-string-slice/after.js
2022-07-21 03:28:35 +00:00

42 lines
989 B
JavaScript

'use strict';
const eslintTemplateVisitor = require('../..');
const templates = eslintTemplateVisitor();
const objectVariable = templates.variable();
const argumentsVariable = templates.spreadVariable();
const substrCallTemplate = templates.template`${objectVariable}.substr(${argumentsVariable})`;
const create = context => {
const sourceCode = context.getSourceCode();
return templates.visitor({
[substrCallTemplate](node) {
const objectNode = substrCallTemplate.context.getMatch(objectVariable);
const argumentNodes = substrCallTemplate.context.getMatch(argumentsVariable);
const problem = {
node,
message: 'Prefer `String#slice()` over `String#substr()`.',
};
const canFix = argumentNodes.length === 0;
if (canFix) {
problem.fix = fixer => fixer.replaceText(node, sourceCode.getText(objectNode) + '.slice()');
}
context.report(problem);
},
});
};
module.exports = {
create,
meta: {
type: 'suggestion',
fixable: 'code',
},
};