forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-argument.js
31 lines (25 loc) · 976 Bytes
/
remove-argument.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict';
const {isCommaToken} = require('eslint-utils');
const {getParentheses} = require('../utils/parentheses.js');
function removeArgument(fixer, node, sourceCode) {
const callExpression = node.parent;
const index = callExpression.arguments.indexOf(node);
const parentheses = getParentheses(node, sourceCode);
const firstToken = parentheses[0] ?? node;
const lastToken = parentheses[parentheses.length - 1] ?? node;
let [start] = firstToken.range;
let [, end] = lastToken.range;
if (index !== 0) {
start = sourceCode.getTokenBefore(firstToken).range[0];
}
// If the removed argument is the only argument, the trailing comma must be removed too
/* istanbul ignore next: Not reachable for now */
if (callExpression.arguments.length === 1) {
const tokenAfter = sourceCode.getTokenBefore(lastToken);
if (isCommaToken(tokenAfter)) {
end = tokenAfter[1];
}
}
return fixer.replaceTextRange([start, end], '');
}
module.exports = removeArgument;