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

40 lines
731 B
JavaScript

'use strict';
const create = context => {
const sourceCode = context.getSourceCode();
return {
CallExpression(node) {
if (node.callee.type !== 'MemberExpression'
|| node.callee.property.type !== 'Identifier'
|| node.callee.property.name !== 'substr'
) {
return;
}
const objectNode = node.callee.object;
const problem = {
node,
message: 'Prefer `String#slice()` over `String#substr()`.',
};
const canFix = node.arguments.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',
},
};