Avoid reformatting body of arrow function with single unused parameter (#28217)

This commit is contained in:
Andy
2018-10-29 16:40:30 -07:00
committed by GitHub
parent 0879e163c9
commit 1c4590341f
4 changed files with 19 additions and 22 deletions

View File

@@ -276,11 +276,15 @@ namespace ts.textChanges {
return this.replaceRangeWithNodes(sourceFile, getAdjustedRange(sourceFile, oldNode, oldNode, options), newNodes, options);
}
public replaceNodeWithText(sourceFile: SourceFile, oldNode: Node, text: string): void {
this.replaceRangeWithText(sourceFile, getAdjustedRange(sourceFile, oldNode, oldNode, useNonAdjustedPositions), text);
}
public replaceNodeRangeWithNodes(sourceFile: SourceFile, startNode: Node, endNode: Node, newNodes: ReadonlyArray<Node>, options: ReplaceWithMultipleNodesOptions & ConfigurableStartEnd = useNonAdjustedPositions) {
return this.replaceRangeWithNodes(sourceFile, getAdjustedRange(sourceFile, startNode, endNode, options), newNodes, options);
}
private nextCommaToken (sourceFile: SourceFile, node: Node): Node | undefined {
private nextCommaToken(sourceFile: SourceFile, node: Node): Node | undefined {
const next = findNextToken(node, node.parent, sourceFile);
return next && next.kind === SyntaxKind.CommaToken ? next : undefined;
}
@@ -690,7 +694,7 @@ namespace ts.textChanges {
}
private finishDeleteDeclarations(): void {
const deletedNodesInLists = new NodeSet(); // Stores ids of nodes in lists that we already deleted. Used to avoid deleting `, ` twice in `a, b`.
const deletedNodesInLists = new NodeSet(); // Stores nodes in lists that we already deleted. Used to avoid deleting `, ` twice in `a, b`.
for (const { sourceFile, node } of this.deletedNodes) {
if (!this.deletedNodes.some(d => d.sourceFile === sourceFile && rangeContainsRangeExclusive(d.node, node))) {
if (isArray(node)) {
@@ -1053,25 +1057,13 @@ namespace ts.textChanges {
switch (node.kind) {
case SyntaxKind.Parameter: {
const oldFunction = node.parent;
if (isArrowFunction(oldFunction) && oldFunction.parameters.length === 1) {
if (isArrowFunction(oldFunction) &&
oldFunction.parameters.length === 1 &&
!findChildOfKind(oldFunction, SyntaxKind.OpenParenToken, sourceFile)) {
// Lambdas with exactly one parameter are special because, after removal, there
// must be an empty parameter list (i.e. `()`) and this won't necessarily be the
// case if the parameter is simply removed (e.g. in `x => 1`).
const newFunction = updateArrowFunction(
oldFunction,
oldFunction.modifiers,
oldFunction.typeParameters,
/*parameters*/ undefined!, // TODO: GH#18217
oldFunction.type,
oldFunction.equalsGreaterThanToken,
oldFunction.body);
// Drop leading and trailing trivia of the new function because we're only going
// to replace the span (vs the full span) of the old function - the old leading
// and trailing trivia will remain.
suppressLeadingAndTrailingTrivia(newFunction);
changes.replaceNode(sourceFile, oldFunction, newFunction);
changes.replaceNodeWithText(sourceFile, node, "()");
}
else {
deleteNodeInList(changes, deletedNodesInLists, sourceFile, node);

View File

@@ -31,6 +31,10 @@
////takesCb((x, y) => { x; });
////takesCb((x, y) => { y; });
////
////x => {
//// const y = 0;
////};
////
////{
//// let a, b;
////}
@@ -72,6 +76,9 @@ takesCb(() => {});
takesCb((x) => { x; });
takesCb((x, y) => { y; });
() => {
};
{
}
for (; ;) {}

View File

@@ -4,9 +4,8 @@
// @noUnusedParameters: true
////[|/*~a*/(/*~b*/x/*~c*/:/*~d*/number/*~e*/)/*~f*/ => /*~g*/{/*~h*/}/*~i*/|]
// In a perfect world, /*~f*/ and /*~h*/ would probably be retained.
verify.codeFix({
description: "Remove declaration for: 'x'",
index: 0,
newRangeContent: "/*~a*/() => /*~g*/ { }/*~i*/",
newRangeContent: "/*~a*/(/*~e*/)/*~f*/ => /*~g*/{/*~h*/}/*~i*/",
});

View File

@@ -4,9 +4,8 @@
// @noUnusedParameters: true
////[|/*~a*/x/*~b*/ /*~c*/=>/*~d*/ {/*~e*/}/*~f*/|]
// In a perfect world, /*~c*/ and /*~e*/ would probably be retained.
verify.codeFix({
description: "Remove declaration for: 'x'",
index: 0,
newRangeContent: "/*~a*/() => /*~d*/ { }/*~f*/",
newRangeContent: "/*~a*/()/*~b*/ /*~c*/=>/*~d*/ {/*~e*/}/*~f*/",
});