10000 fix(eslint-plugin): [no-unnecessary-template-expression] add missing parentheses in autofix by developer-bandi · Pull Request #8673 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as ts from 'typescript';
import {
createRule,
getConstrainedTypeAtLocation,
getMovedNodeCode,
getParserServices,
isTypeFlagSet,
isUndefinedIdentifier,
Expand Down 10000 Expand Up @@ -106,21 +107,14 @@ export default createRule<[], MessageId>({
context.report({
node: node.expressions[0],
messageId: 'noUnnecessaryTemplateExpression',
fix(fixer): TSESLint.RuleFix[] {
const [prevQuasi, nextQuasi] = node.quasis;

// Remove the quasis and backticks.
return [
fixer.removeRange([
prevQuasi.range[1] - 3,
node.expressions[0].range[0],
]),

fixer.removeRange([
node.expressions[0].range[1],
nextQuasi.range[0] + 2,
]),
];
fix(fixer): TSESLint.RuleFix | null {
const wrappingCode = getMovedNodeCode({
sourceCode: context.sourceCode,
nodeToMove: node.expressions[0],
destinationNode: node,
});

return fixer.replaceText(node, wrappingCode);
},
});

Expand Down
33 changes: 31 additions & 2 deletions packages/eslint-plugin/src/util/getWrappingFixer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The 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})`;
}

/* DA09 *
* Check if a node will always have the same precedence if it's parent changes.
Expand All @@ -98,8 +125,10 @@
* Check if a node's parent could have different precedence if the node changes.
*/
function isWeakPrecedenceParent(node: TSESTree.Node): boolean {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const parent = node.parent!;
const parent = node.parent;
if (!parent) {
return false;

Check warning on line 130 in packages/eslint-plugin/src/util/getWrappingFixer.ts

View check run for this annotation

Codecov / codecov/patch

packages/eslint-plugin/src/util/getWrappingFixer.ts#L130

Added line #L130 was not covered by tests
}

if (
parent.type === AST_NODE_TYPES.UpdateExpression ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1127,5 +1127,14 @@ declare const nested: string, interpolation: string;
},
],
},
{
code: "true ? `${'test' || ''}`.trim() : undefined;",
output: "true ? ('test' || '').trim() : undefined;",
errors: [
{
messageId: 'noUnnecessaryTemplateExpression',
},
],
},
],
});
Loading
0