remove-argument.js 987 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. const {isCommaToken} = require('@eslint-community/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. /* c8 ignore start */
  17. if (callExpression.arguments.length === 1) {
  18. const tokenAfter = sourceCode.getTokenBefore(lastToken);
  19. if (isCommaToken(tokenAfter)) {
  20. end = tokenAfter[1];
  21. }
  22. }
  23. /* c8 ignore end */
  24. return fixer.replaceTextRange([start, end], '');
  25. }
  26. module.exports = removeArgument;