Skip to content

Commit e67bd6d

Browse files
authored
Refactor parentheses related functions (sindresorhus#1169)
1 parent 8d97beb commit e67bd6d

File tree

6 files changed

+69
-73
lines changed

6 files changed

+69
-73
lines changed

rules/no-array-for-each.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const getDocumentationUrl = require('./utils/get-documentation-url');
1111
const methodSelector = require('./utils/method-selector');
1212
const needsSemicolon = require('./utils/needs-semicolon');
1313
const shouldAddParenthesesToExpressionStatementExpression = require('./utils/should-add-parentheses-to-expression-statement-expression');
14-
const getParenthesizedTimes = require('./utils/get-parenthesized-times');
14+
const {getParentheses} = require('./utils/parentheses');
1515
const extendFixRange = require('./utils/extend-fix-range');
1616
const isFunctionSelfUsedInside = require('./utils/is-function-self-used-inside');
1717
const {isNodeMatches} = require('./utils/is-node-matches');
@@ -155,18 +155,12 @@ function getFixFunction(callExpression, sourceCode, functionInfo) {
155155
};
156156

157157
function * removeCallbackParentheses(fixer) {
158-
const parenthesizedTimes = getParenthesizedTimes(callback, sourceCode);
159-
if (parenthesizedTimes > 0) {
160-
// Opening parenthesis tokens already included in `getForOfLoopHeadRange`
158+
// Opening parenthesis tokens already included in `getForOfLoopHeadRange`
159+
const closingParenthesisTokens = getParentheses(callback, sourceCode)
160+
.filter(token => isClosingParenToken(token));
161161

162-
const closingParenthesisTokens = sourceCode.getTokensAfter(
163-
callback,
164-
{count: parenthesizedTimes, filter: isClosingParenToken}
165-
);
166-
167-
for (const closingParenthesisToken of closingParenthesisTokens) {
168-
yield fixer.remove(closingParenthesisToken);
169-
}
162+
for (const closingParenthesisToken of closingParenthesisTokens) {
163+
yield fixer.remove(closingParenthesisToken);
170164
}
171165
}
172166

rules/prefer-spread.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const {isParenthesized, getStaticValue, isCommaToken, hasSideEffect} = require('
33
const getDocumentationUrl = require('./utils/get-documentation-url');
44
const methodSelector = require('./utils/method-selector');
55
const needsSemicolon = require('./utils/needs-semicolon');
6-
const getParentheses = require('./utils/get-parentheses');
6+
const {getParenthesizedRange} = require('./utils/parentheses');
77
const shouldAddParenthesesToSpreadElementArgument = require('./utils/should-add-parentheses-to-spread-element-argument');
88
const replaceNodeOrTokenAndSpacesBefore = require('./utils/replace-node-or-token-and-spaces-before');
99
const removeSpacesAfter = require('./utils/remove-spaces-after');
@@ -69,14 +69,6 @@ const isArrayLiteralHasTrailingComma = (node, sourceCode) => {
6969
return isCommaToken(sourceCode.getLastToken(node, 1));
7070
};
7171

72-
const getParenthesizedRange = (node, sourceCode) => {
73-
const [firstToken = node, lastToken = node] = getParentheses(node, sourceCode);
74-
75-
const [start] = firstToken.range;
76-
const [, end] = lastToken.range;
77-
return [start, end];
78-
};
79-
8072
const getRangeAfterCalleeObject = (node, sourceCode) => {
8173
const {object} = node.callee;
8274
const parenthesizedRange = getParenthesizedRange(object, sourceCode);

rules/utils/get-parentheses.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

rules/utils/get-parenthesized-times.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

rules/utils/parentheses.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
const {isParenthesized, isOpeningParenToken, isClosingParenToken} = require('eslint-utils');
3+
4+
/*
5+
Get how many times the node is parenthesized.
6+
7+
@param {Node} node - The node to be checked.
8+
@param {SourceCode} sourceCode - The source code object.
9+
@returns {number}
10+
*/
11+
function getParenthesizedTimes(node, sourceCode) {
12+
let times = 0;
13+
while (isParenthesized(times + 1, node, sourceCode)) {
14+
times++;
15+
}
16+
17+
return times;
18+
}
19+
20+
/*
21+
Get all parentheses tokens around the node.
22+
23+
@param {Node} node - The node to be checked.
24+
@param {SourceCode} sourceCode - The source code object.
25+
@returns {Token[]}
26+
*/
27+
function getParentheses(node, sourceCode) {
28+
const count = getParenthesizedTimes(node, sourceCode);
29+
30+
if (count === 0) {
31+
return [];
32+
}
33+
34+
return [
35+
...sourceCode.getTokensBefore(node, {count, filter: isOpeningParenToken}),
36+
...sourceCode.getTokensAfter(node, {count, filter: isClosingParenToken})
37+
];
38+
}
39+
40+
/*
41+
Get the parenthesized range of the node.
42+
43+
@param {Node} node - The node to be checked.
44+
@param {SourceCode} sourceCode - The source code object.
45+
@returns {number[]}
46+
*/
47+
function getParenthesizedRange(node, sourceCode) {
48+
const parentheses = getParentheses(node, sourceCode);
49+
const [start] = (parentheses[0] || node).range;
50+
const [, end] = (parentheses[parentheses.length - 1] || node).range;
51+
return [start, end];
52+
}
53+
54+
module.exports = {
55+
getParenthesizedTimes,
56+
getParentheses,
57+
getParenthesizedRange
58+
};

rules/utils/replace-node-or-token-and-spaces-before.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
'use strict';
2-
const {isOpeningParenToken, isClosingParenToken} = require('eslint-utils');
3-
const getParenthesizedTimes = require('./get-parenthesized-times');
2+
const {getParentheses} = require('./parentheses');
43

54
function * replaceNodeOrTokenAndSpacesBefore(nodeOrToken, replacement, fixer, sourceCode) {
6-
const parenthesizedTimes = getParenthesizedTimes(nodeOrToken, sourceCode);
5+
const tokens = getParentheses(nodeOrToken, sourceCode);
76

8-
if (parenthesizedTimes > 0) {
9-
let lastBefore = nodeOrToken;
10-
let lastAfter = nodeOrToken;
11-
for (let index = 0; index < parenthesizedTimes; index++) {
12-
const openingParenthesisToken = sourceCode.getTokenBefore(lastBefore, isOpeningParenToken);
13-
const closingParenthesisToken = sourceCode.getTokenAfter(lastAfter, isClosingParenToken);
14-
yield * replaceNodeOrTokenAndSpacesBefore(openingParenthesisToken, '', fixer, sourceCode);
15-
yield * replaceNodeOrTokenAndSpacesBefore(closingParenthesisToken, '', fixer, sourceCode);
16-
lastBefore = openingParenthesisToken;
17-
lastAfter = closingParenthesisToken;
18-
}
7+
for (const token of tokens) {
8+
yield * replaceNodeOrTokenAndSpacesBefore(token, '', fixer, sourceCode);
199
}
2010

2111
let [start, end] = nodeOrToken.range;

0 commit comments

Comments
 (0)