Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
14 commits
Select commitHold shift + click to select a range
e2f2e7f
fix: precedence concider with parentheses
developer-bandiMar 14, 2024
f4c697e
refactor: seperate replaceNodeText fn
developer-bandiMar 27, 2024
5a9f630
refactor: make getWrappingCode fn
developer-bandiApr 10, 2024
67f19bd
fix: make new fn
developer-bandiMay 12, 2024
92db97f
fix: revert getWrappingFixer and make getWrappingCode
developer-bandiMay 12, 2024
28ca1d0
fix: lint error fix
developer-bandiMay 12, 2024
8290e5a
fix: lint error fix
developer-bandiMay 12, 2024
90e8748
fix: lint error fix
developer-bandiMay 12, 2024
0bd9dcd
fix: lint error fix
developer-bandiMay 12, 2024
d7fc54f
refactor: change getMovedNodeCode function
developer-bandiJul 14, 2024
4bb0876
fix: confict resolve
developer-bandiAug 18, 2024
9503808
Merge branch 'main' of https://github.com/developer-bandi/typescript-…
developer-bandiAug 18, 2024
e811164
feat: copy work to no-unnessary-template-expression
developer-bandiAug 18, 2024
1d536b6
Merge branch 'main' into fix/no-useless-template-literals
kirkwaiblingerAug 19, 2024
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,7 @@ import * as ts from 'typescript';
import {
createRule,
getConstrainedTypeAtLocation,
getMovedNodeCode,
getParserServices,
isTypeFlagSet,
isUndefinedIdentifier,
Expand DownExpand 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 deletionspackages/eslint-plugin/src/util/getWrappingFixer.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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)) {

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

/**
* 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp