-
-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prefer-string-starts-ends-with
: add suggestions for safely handling…
… non-strings (#1277) Co-authored-by: fisker Cheung <lionkay@gmail.com>
- Loading branch information
Showing
6 changed files
with
785 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
rules/utils/should-add-parentheses-to-logical-expression-child.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
/** | ||
Check if parentheses should be added to a `node` when it's used as child of `LogicalExpression`. | ||
@param {Node} node - The AST node to check. | ||
@param {{operator: string, property: string}} options - Options | ||
@returns {boolean} | ||
*/ | ||
function shouldAddParenthesesToLogicalExpressionChild(node, {operator, property}) { | ||
/* istanbul ignore next: When operator or property is different, need check `LogicalExpression` operator precedence, not implemented */ | ||
if (operator !== '??' || property !== 'left') { | ||
throw new Error('Not supported.'); | ||
} | ||
|
||
// Not really needed, but more readable | ||
if ( | ||
node.type === 'AwaitExpression' || | ||
node.type === 'BinaryExpression' | ||
) { | ||
return true; | ||
} | ||
|
||
// Lower precedence than `LogicalExpression` | ||
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table | ||
if ( | ||
node.type === 'ConditionalExpression' || | ||
node.type === 'AssignmentExpression' || | ||
node.type === 'AssignmentExpression' || | ||
node.type === 'YieldExpression' || | ||
node.type === 'SequenceExpression' | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
module.exports = shouldAddParenthesesToLogicalExpressionChild; |
Oops, something went wrong.