Skip to content

Commit a2ea65a

Browse files
Belco90ljharb
authored andcommitted
[Refactor] add isParenthesized AST util
1 parent 4b4bba9 commit a2ea65a

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2626
* [Refactor] [`no-deprecated`]: improve performance ([#3271][] @golopot)
2727
* [Refactor] [`no-did-mount-set-state`], [`no-did-update-set-state`], [`no-will-update-set-state`]: improve performance ([#3272][] @golopot)
2828
* [Refactor] improve performance by avoiding unnecessary `Components.detect` ([#3273][] @golopot)
29+
* [Refactor] add `isParenthesized` AST util ([#3203][] @Belco90)
2930

3031
[#3273]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3273
3132
[#3272]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3272
@@ -39,6 +40,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
3940
[#3258]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3258
4041
[#3254]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3254
4142
[#3251]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3251
43+
[#3203]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3203
4244
[#3248]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3248
4345
[#3244]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3244
4446
[#3235]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3235

lib/rules/jsx-wrap-multilines.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const has = require('object.hasown/polyfill')();
99
const docsUrl = require('../util/docsUrl');
1010
const jsxUtil = require('../util/jsx');
1111
const reportC = require('../util/report');
12+
const isParenthesized = require('../util/ast').isParenthesized;
1213

1314
// ------------------------------------------------------------------------------
1415
// Constants
@@ -89,20 +90,10 @@ module.exports = {
8990
return option && option !== 'ignore';
9091
}
9192

92-
function isParenthesised(node) {
93-
const sourceCode = context.getSourceCode();
94-
const previousToken = sourceCode.getTokenBefore(node);
95-
const nextToken = sourceCode.getTokenAfter(node);
96-
97-
return previousToken && nextToken
98-
&& previousToken.value === '(' && previousToken.range[1] <= node.range[0]
99-
&& nextToken.value === ')' && nextToken.range[0] >= node.range[1];
100-
}
101-
10293
function needsOpeningNewLine(node) {
10394
const previousToken = context.getSourceCode().getTokenBefore(node);
10495

105-
if (!isParenthesised(node)) {
96+
if (!isParenthesized(context, node)) {
10697
return false;
10798
}
10899

@@ -116,7 +107,7 @@ module.exports = {
116107
function needsClosingNewLine(node) {
117108
const nextToken = context.getSourceCode().getTokenAfter(node);
118109

119-
if (!isParenthesised(node)) {
110+
if (!isParenthesized(context, node)) {
120111
return false;
121112
}
122113

@@ -153,12 +144,12 @@ module.exports = {
153144
const sourceCode = context.getSourceCode();
154145
const option = getOption(type);
155146

156-
if ((option === true || option === 'parens') && !isParenthesised(node) && isMultilines(node)) {
147+
if ((option === true || option === 'parens') && !isParenthesized(context, node) && isMultilines(node)) {
157148
report(node, 'missingParens', (fixer) => fixer.replaceText(node, `(${sourceCode.getText(node)})`));
158149
}
159150

160151
if (option === 'parens-new-line' && isMultilines(node)) {
161-
if (!isParenthesised(node)) {
152+
if (!isParenthesized(context, node)) {
162153
const tokenBefore = sourceCode.getTokenBefore(node, { includeComments: true });
163154
const tokenAfter = sourceCode.getTokenAfter(node, { includeComments: true });
164155
const start = node.loc.start;

lib/util/ast.js

+18
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,23 @@ function getKeyValue(context, node) {
294294
return key.type === 'Identifier' ? key.name : key.value;
295295
}
296296

297+
/**
298+
* Checks if a node is surrounded by parenthesis.
299+
*
300+
* @param {object} context - Context from the rule
301+
* @param {ASTNode} node - Node to be checked
302+
* @returns {boolean}
303+
*/
304+
function isParenthesized(context, node) {
305+
const sourceCode = context.getSourceCode();
306+
const previousToken = sourceCode.getTokenBefore(node);
307+
const nextToken = sourceCode.getTokenAfter(node);
308+
309+
return !!previousToken && !!nextToken
310+
&& previousToken.value === '(' && previousToken.range[1] <= node.range[0]
311+
&& nextToken.value === ')' && nextToken.range[0] >= node.range[1];
312+
}
313+
297314
/**
298315
* Checks if a node is being assigned a value: props.bar = 'bar'
299316
* @param {ASTNode} node The AST node being checked.
@@ -410,6 +427,7 @@ module.exports = {
410427
getPropertyNameNode,
411428
getComponentProperties,
412429
getKeyValue,
430+
isParenthesized,
413431
isAssignmentLHS,
414432
isClass,
415433
isFunction,

0 commit comments

Comments
 (0)