Skip to content

Commit

Permalink
Use shouldAddParenthesesToMemberExpressionObject
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 8, 2021
1 parent c198e83 commit 5675ee8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 9 deletions.
9 changes: 2 additions & 7 deletions rules/prefer-regexp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {isParenthesized} = require('eslint-utils');
const getDocumentationUrl = require('./utils/get-documentation-url');
const methodSelector = require('./utils/method-selector');
const {isBooleanNode} = require('./utils/boolean');
const shouldAddParenthesesToMemberExpressionObject = require('./utils/should-add-parentheses-to-member-expression-object');

const MESSAGE_ID_REGEXP_EXEC = 'regexp-exec';
const MESSAGE_ID_STRING_MATCH = 'string-match';
Expand Down Expand Up @@ -70,13 +71,7 @@ const create = context => {
let regexpText = sourceCode.getText(regexpNode);
if (
!isParenthesized(stringNode, sourceCode) &&
!(
regexpNode.type === 'Literal' ||
regexpNode.type === 'Identifier' ||
regexpNode.type === 'MemberExpression' ||
regexpNode.type === 'CallExpression' ||
(regexpNode.type === 'NewExpression' && regexpText.endsWith(')'))
)
shouldAddParenthesesToMemberExpressionObject(regexpNode, sourceCode)
) {
regexpText = `(${regexpText})`;
}
Expand Down
4 changes: 2 additions & 2 deletions test/snapshots/prefer-regexp-test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ Generated by [AVA](https://avajs.dev).
1 | if (foo.match(bar?.baz)) {}␊
Output:␊
1 | if ((bar?.baz).test(foo)) {}␊
1 | if (bar?.baz.test(foo)) {}␊
Error 1/1:␊
> 1 | if (foo.match(bar?.baz)) {}␊
Expand All @@ -429,7 +429,7 @@ Generated by [AVA](https://avajs.dev).
1 | if (foo.match(bar?.baz())) {}␊
Output:␊
1 | if ((bar?.baz()).test(foo)) {}␊
1 | if (bar?.baz().test(foo)) {}␊
Error 1/1:␊
> 1 | if (foo.match(bar?.baz())) {}␊
Expand Down
Binary file modified test/snapshots/prefer-regexp-test.js.snap
Binary file not shown.

0 comments on commit 5675ee8

Please sign in to comment.