-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6123e16
commit 66456c5
Showing
5 changed files
with
386 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { isClosingParenToken, isOpeningParenToken } from "./token-predicate" | ||
|
||
/** | ||
* Get the left parenthesis of the parent node syntax if it exists. | ||
* E.g., `if (a) {}` then the `(`. | ||
* @param {Node} node The AST node to check. | ||
* @param {SourceCode} sourceCode The source code object to get tokens. | ||
* @returns {Token|null} The left parenthesis of the parent node syntax | ||
*/ | ||
function getParentSyntaxParen(node, sourceCode) { | ||
const parent = node.parent | ||
|
||
switch (parent.type) { | ||
case "CallExpression": | ||
case "NewExpression": | ||
if (parent.arguments.length === 1 && parent.arguments[0] === node) { | ||
return sourceCode.getTokenAfter( | ||
parent.callee, | ||
isOpeningParenToken | ||
) | ||
} | ||
return null | ||
|
||
case "DoWhileStatement": | ||
if (parent.test === node) { | ||
return sourceCode.getTokenAfter( | ||
parent.body, | ||
isOpeningParenToken | ||
) | ||
} | ||
return null | ||
|
||
case "IfStatement": | ||
case "WhileStatement": | ||
if (parent.test === node) { | ||
return sourceCode.getFirstToken(parent, 1) | ||
} | ||
return null | ||
|
||
case "SwitchStatement": | ||
if (parent.discriminant === node) { | ||
return sourceCode.getFirstToken(parent, 1) | ||
} | ||
return null | ||
|
||
case "WithStatement": | ||
if (parent.object === node) { | ||
return sourceCode.getFirstToken(parent, 1) | ||
} | ||
return null | ||
|
||
default: | ||
return null | ||
} | ||
} | ||
|
||
/** | ||
* Check whether a given node is parenthesized or not. | ||
* @param {Node} node The AST node to check. | ||
* @param {SourceCode} sourceCode The source code object to get tokens. | ||
* @returns {boolean} `true` if the node is parenthesized. | ||
*/ | ||
export function isParenthesized(node, sourceCode) { | ||
if (node == null) { | ||
return false | ||
} | ||
|
||
const maybeLeftParen = sourceCode.getTokenBefore(node) | ||
const maybeRightParen = sourceCode.getTokenAfter(node) | ||
|
||
return ( | ||
maybeLeftParen != null && | ||
maybeRightParen != null && | ||
isOpeningParenToken(maybeLeftParen) && | ||
isClosingParenToken(maybeRightParen) && | ||
// Avoid false positive such as `if (a) {}` | ||
maybeLeftParen !== getParentSyntaxParen(node, sourceCode) | ||
) | ||
} |
Oops, something went wrong.