Skip to content

Commit e59d9ee

Browse files
authored
Add utility getCallExpressionTokens (#2345)
1 parent b82542d commit e59d9ee

7 files changed

+92
-42
lines changed

rules/no-array-push-push.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict';
2-
const {hasSideEffect, isCommaToken, isSemicolonToken} = require('@eslint-community/eslint-utils');
3-
const getCallExpressionArgumentsText = require('./utils/get-call-expression-arguments-text.js');
2+
const {hasSideEffect, isSemicolonToken} = require('@eslint-community/eslint-utils');
3+
const {
4+
getCallExpressionTokens,
5+
getCallExpressionArgumentsText,
6+
} = require('./utils/index.js');
47
const isSameReference = require('./utils/is-same-reference.js');
58
const {isNodeMatches} = require('./utils/is-node-matches.js');
69
const getPreviousNode = require('./utils/get-previous-node.js');
@@ -78,13 +81,17 @@ function create(context) {
7881

7982
const fix = function * (fixer) {
8083
if (secondCallArguments.length > 0) {
81-
const text = getCallExpressionArgumentsText(secondCall, sourceCode);
84+
const text = getCallExpressionArgumentsText(sourceCode, secondCall);
85+
86+
const {
87+
trailingCommaToken,
88+
closingParenthesisToken,
89+
} = getCallExpressionTokens(sourceCode, firstCall);
8290

83-
const [penultimateToken, lastToken] = sourceCode.getLastTokens(firstCall, 2);
8491
yield (
85-
isCommaToken(penultimateToken)
86-
? fixer.insertTextAfter(penultimateToken, ` ${text}`)
87-
: fixer.insertTextBefore(lastToken, firstCall.arguments.length > 0 ? `, ${text}` : text)
92+
trailingCommaToken
93+
? fixer.insertTextAfter(trailingCommaToken, ` ${text}`)
94+
: fixer.insertTextBefore(closingParenthesisToken, firstCall.arguments.length > 0 ? `, ${text}` : text)
8895
);
8996
}
9097

rules/no-magic-array-flat-depth.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
const {isOpeningParenToken} = require('@eslint-community/eslint-utils');
32
const {isMethodCall, isNumberLiteral} = require('./ast/index.js');
3+
const {getCallExpressionTokens} = require('./utils/index.js');
44

55
const MESSAGE_ID = 'no-magic-array-flat-depth';
66
const messages = {
@@ -25,8 +25,10 @@ const create = context => ({
2525
}
2626

2727
const {sourceCode} = context;
28-
const openingParenthesisToken = sourceCode.getTokenAfter(callExpression.callee, isOpeningParenToken);
29-
const closingParenthesisToken = sourceCode.getLastToken(callExpression);
28+
const {
29+
openingParenthesisToken,
30+
closingParenthesisToken,
31+
} = getCallExpressionTokens(sourceCode, callExpression);
3032
if (sourceCode.commentsExistBetween(openingParenthesisToken, closingParenthesisToken)) {
3133
return;
3234
}

rules/prefer-module.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
2-
const {isOpeningParenToken} = require('@eslint-community/eslint-utils');
32
const isShadowed = require('./utils/is-shadowed.js');
43
const assertToken = require('./utils/assert-token.js');
4+
const {getCallExpressionTokens} = require('./utils/index.js');
55
const {isStaticRequire, isReferenceIdentifier, isFunction} = require('./ast/index.js');
66
const {
77
removeParentheses,
@@ -77,12 +77,12 @@ function fixRequireCall(node, sourceCode) {
7777
if (parent.type === 'ExpressionStatement' && parent.parent.type === 'Program') {
7878
return function * (fixer) {
7979
yield fixer.replaceText(callee, 'import');
80-
const openingParenthesisToken = sourceCode.getTokenAfter(
81-
callee,
82-
isOpeningParenToken,
83-
);
80+
81+
const {
82+
openingParenthesisToken,
83+
closingParenthesisToken,
84+
} = getCallExpressionTokens(sourceCode, requireCall);
8485
yield fixer.replaceText(openingParenthesisToken, ' ');
85-
const closingParenthesisToken = sourceCode.getLastToken(requireCall);
8686
yield fixer.remove(closingParenthesisToken);
8787

8888
for (const node of [callee, requireCall, source]) {
@@ -137,12 +137,12 @@ function fixRequireCall(node, sourceCode) {
137137
yield fixer.replaceText(equalToken, ' from ');
138138

139139
yield fixer.remove(callee);
140-
const openingParenthesisToken = sourceCode.getTokenAfter(
141-
callee,
142-
isOpeningParenToken,
143-
);
140+
141+
const {
142+
openingParenthesisToken,
143+
closingParenthesisToken,
144+
} = getCallExpressionTokens(sourceCode, requireCall);
144145
yield fixer.remove(openingParenthesisToken);
145-
const closingParenthesisToken = sourceCode.getLastToken(requireCall);
146146
yield fixer.remove(closingParenthesisToken);
147147

148148
for (const node of [callee, requireCall, source]) {

rules/prefer-structured-clone.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
2-
const {
3-
isCommaToken,
4-
isOpeningParenToken,
5-
} = require('@eslint-community/eslint-utils');
62
const {isCallExpression, isMethodCall} = require('./ast/index.js');
73
const {removeParentheses} = require('./fix/index.js');
8-
const {isNodeMatchesNameOrPath} = require('./utils/index.js');
4+
const {
5+
isNodeMatchesNameOrPath,
6+
getCallExpressionTokens,
7+
} = require('./utils/index.js');
98

109
const MESSAGE_ID_ERROR = 'prefer-structured-clone/error';
1110
const MESSAGE_ID_SUGGESTION = 'prefer-structured-clone/suggestion';
@@ -75,19 +74,17 @@ const create = context => {
7574
yield fixer.remove(jsonStringify.callee);
7675
yield * removeParentheses(jsonStringify.callee, fixer, sourceCode);
7776

78-
const openingParenthesisToken = sourceCode.getTokenAfter(jsonStringify.callee, isOpeningParenToken);
79-
yield fixer.remove(openingParenthesisToken);
80-
81-
const [
82-
penultimateToken,
77+
const {
78+
openingParenthesisToken,
8379
closingParenthesisToken,
84-
] = sourceCode.getLastTokens(jsonStringify, 2);
85-
86-
if (isCommaToken(penultimateToken)) {
87-
yield fixer.remove(penultimateToken);
88-
}
80+
trailingCommaToken,
81+
} = getCallExpressionTokens(sourceCode, jsonStringify);
8982

83+
yield fixer.remove(openingParenthesisToken);
9084
yield fixer.remove(closingParenthesisToken);
85+
if (trailingCommaToken) {
86+
yield fixer.remove(trailingCommaToken);
87+
}
9188
},
9289
},
9390
],
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
'use strict';
2-
const {isOpeningParenToken} = require('@eslint-community/eslint-utils');
2+
const getCallExpressionTokens = require('./get-call-expression-tokens.js');
3+
4+
/** @typedef {import('estree').CallExpression} CallExpression */
35

46
/**
57
Get the text of the arguments list of `CallExpression`.
68
7-
@param {Node} node - The `CallExpression` node.
9+
@param {import('eslint').SourceCode} sourceCode - The source code object.
10+
@param {CallExpression} callExpression - The `CallExpression` node.
811
@param {SourceCode} sourceCode - The source code object.
912
@returns {string}
1013
*/
11-
const getCallExpressionArgumentsText = (node, sourceCode) => {
12-
const openingParenthesisToken = sourceCode.getTokenAfter(node.callee, isOpeningParenToken);
13-
const closingParenthesisToken = sourceCode.getLastToken(node);
14+
function getCallExpressionArgumentsText(sourceCode, callExpression) {
15+
const {
16+
openingParenthesisToken,
17+
closingParenthesisToken,
18+
} = getCallExpressionTokens(sourceCode, callExpression);
1419

1520
return sourceCode.text.slice(
1621
openingParenthesisToken.range[1],
1722
closingParenthesisToken.range[0],
1823
);
19-
};
24+
}
2025

2126
module.exports = getCallExpressionArgumentsText;
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const {
4+
isOpeningParenToken,
5+
isCommaToken,
6+
} = require('@eslint-community/eslint-utils');
7+
8+
/** @typedef {import('estree').CallExpression} CallExpression */
9+
/** @typedef {import('eslint').AST.Token} Token */
10+
11+
/**
12+
Get the `openingParenthesisToken`, `closingParenthesisToken`, and `trailingCommaToken` of `CallExpression`.
13+
14+
@param {import('eslint').SourceCode} sourceCode - The source code object.
15+
@param {CallExpression} callExpression - The `CallExpression` node.
16+
@returns {{
17+
openingParenthesisToken: Token,
18+
closingParenthesisToken: Token,
19+
trailingCommaToken: Token | undefined,
20+
}}
21+
*/
22+
function getCallExpressionTokens(sourceCode, callExpression) {
23+
const openingParenthesisToken = sourceCode.getTokenAfter(callExpression.callee, isOpeningParenToken);
24+
const [
25+
penultimateToken,
26+
closingParenthesisToken,
27+
] = sourceCode.getLastTokens(callExpression, 2);
28+
const trailingCommaToken = isCommaToken(penultimateToken) ? penultimateToken : undefined;
29+
30+
return {
31+
openingParenthesisToken,
32+
closingParenthesisToken,
33+
trailingCommaToken,
34+
};
35+
}
36+
37+
module.exports = getCallExpressionTokens;

rules/utils/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module.exports = {
1818
avoidCapture: require('./avoid-capture.js'),
1919
escapeString: require('./escape-string.js'),
2020
getBooleanAncestor,
21+
getCallExpressionArgumentsText: require('./get-call-expression-arguments-text.js'),
22+
getCallExpressionTokens: require('./get-call-expression-tokens.js'),
2123
getParentheses,
2224
getParenthesizedRange,
2325
getParenthesizedText,

0 commit comments

Comments
 (0)