remove-argument.js 976 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. const {isCommaToken} = require('eslint-utils');
  3. const {getParentheses} = require('../utils/parentheses.js');
  4. function removeArgument(fixer, node, sourceCode) {
  5. const callExpression = node.parent;
  6. const index = callExpression.arguments.indexOf(node);
  7. const parentheses = getParentheses(node, sourceCode);
  8. const firstToken = parentheses[0] || node;
  9. const lastToken = parentheses[parentheses.length - 1] || node;
  10. let [start] = firstToken.range;
  11. let [, end] = lastToken.range;
  12. if (index !== 0) {
  13. start = sourceCode.getTokenBefore(firstToken).range[0];
  14. }
  15. // If the removed argument is the only argument, the trailing comma must be removed too
  16. /* istanbul ignore next: Not reachable for now */
  17. if (callExpression.arguments.length === 1) {
  18. const tokenAfter = sourceCode.getTokenBefore(lastToken);
  19. if (isCommaToken(tokenAfter)) {
  20. end = tokenAfter[1];
  21. }
  22. }
  23. return fixer.replaceTextRange([start, end], '');
  24. }
  25. module.exports = removeArgument;