Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [no-unnecessary-template-expression] add missing parentheses in autofix#8673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
e2f2e7f
f4c697e
5a9f630
67f19bd
92db97f
28ca1d0
8290e5a
90e8748
0bd9dcd
d7fc54f
4bb0876
9503808
e811164
1d536b6
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -74,6 +74,33 @@ | ||
return fixer.replaceText(node, code); | ||
}; | ||
} | ||
/** | ||
* If the node to be moved and the destination node require parentheses, include parentheses in the node to be moved. | ||
* @param sourceCode Source code of current file | ||
* @param nodeToMove Nodes that need to be moved | ||
* @param destinationNode Final destination node with nodeToMove | ||
* @returns If parentheses are required, code for the nodeToMove node is returned with parentheses at both ends of the code. | ||
*/ | ||
export function getMovedNodeCode(params: { | ||
sourceCode: Readonly<TSESLint.SourceCode>; | ||
nodeToMove: TSESTree.Node; | ||
destinationNode: TSESTree.Node; | ||
}): string { | ||
const { sourceCode, nodeToMove: existingNode, destinationNode } = params; | ||
const code = sourceCode.getText(existingNode); | ||
if (isStrongPrecedenceNode(existingNode)) { | ||
// Moved node never needs parens | ||
return code; | ||
} | ||
if (!isWeakPrecedenceParent(destinationNode)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. [Praise] Great reuse of the existing functions! | ||
// Destination would never needs parens, regardless what node moves there | ||
return code; | ||
} | ||
// Parens may be necessary | ||
return `(${code})`; | ||
} | ||
/** | ||
* Check if a node will always have the same precedence if it's parent changes. | ||
@@ -98,8 +125,10 @@ | ||
* Check if a node's parent could have different precedence if the node changes. | ||
*/ | ||
function isWeakPrecedenceParent(node: TSESTree.Node): boolean { | ||
const parent = node.parent; | ||
if (!parent) { | ||
return false; | ||
} | ||
if ( | ||
parent.type === AST_NODE_TYPES.UpdateExpression || | ||
Uh oh!
There was an error while loading.Please reload this page.