Skip to content

Commit 64d0d5a

Browse files
authoredOct 28, 2022
fix(51301): Fixing an unused import at the end of a line removes the newline (#51320)
* fix(51301): keep the line break after removing the unused identifier * preserve line breaks in import specifiers * preserve line breaks in parameters and destructuring elements * remove preserveLineBreak option
1 parent 754eeb2 commit 64d0d5a

23 files changed

+489
-1
lines changed
 

‎src/services/textChanges.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,27 @@ namespace ts.textChanges {
993993
return skipTrivia(sourceFile.text, getAdjustedStartPosition(sourceFile, node, { leadingTriviaOption: LeadingTriviaOption.IncludeAll }), /*stopAfterLineBreak*/ false, /*stopAtComments*/ true);
994994
}
995995

996+
function endPositionToDeleteNodeInList(sourceFile: SourceFile, node: Node, prevNode: Node | undefined, nextNode: Node): number {
997+
const end = startPositionToDeleteNodeInList(sourceFile, nextNode);
998+
if (prevNode === undefined || positionsAreOnSameLine(getAdjustedEndPosition(sourceFile, node, {}), end, sourceFile)) {
999+
return end;
1000+
}
1001+
const token = findPrecedingToken(nextNode.getStart(sourceFile), sourceFile);
1002+
if (isSeparator(node, token)) {
1003+
const prevToken = findPrecedingToken(node.getStart(sourceFile), sourceFile);
1004+
if (isSeparator(prevNode, prevToken)) {
1005+
const pos = skipTrivia(sourceFile.text, token.getEnd(), /*stopAfterLineBreak*/ true, /*stopAtComments*/ true);
1006+
if (positionsAreOnSameLine(prevToken.getStart(sourceFile), token.getStart(sourceFile), sourceFile)) {
1007+
return isLineBreak(sourceFile.text.charCodeAt(pos - 1)) ? pos - 1 : pos;
1008+
}
1009+
if (isLineBreak(sourceFile.text.charCodeAt(pos))) {
1010+
return pos;
1011+
}
1012+
}
1013+
}
1014+
return end;
1015+
}
1016+
9961017
function getClassOrObjectBraceEnds(cls: ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression, sourceFile: SourceFile): [number | undefined, number | undefined] {
9971018
const open = findChildOfKind(cls, SyntaxKind.OpenBraceToken, sourceFile);
9981019
const close = findChildOfKind(cls, SyntaxKind.CloseBraceToken, sourceFile);
@@ -1589,9 +1610,10 @@ namespace ts.textChanges {
15891610
// That's handled in the end by `finishTrailingCommaAfterDeletingNodesInList`.
15901611
Debug.assert(!deletedNodesInLists.has(node), "Deleting a node twice");
15911612
deletedNodesInLists.add(node);
1613+
15921614
changes.deleteRange(sourceFile, {
15931615
pos: startPositionToDeleteNodeInList(sourceFile, node),
1594-
end: index === containingList.length - 1 ? getAdjustedEndPosition(sourceFile, node, {}) : startPositionToDeleteNodeInList(sourceFile, containingList[index + 1]),
1616+
end: index === containingList.length - 1 ? getAdjustedEndPosition(sourceFile, node, {}) : endPositionToDeleteNodeInList(sourceFile, node, containingList[index - 1], containingList[index + 1]),
15951617
});
15961618
}
15971619
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f({
7+
//// a, b,
8+
//// c,
9+
////}: any)|] {
10+
//// a;
11+
//// c;
12+
////}
13+
14+
verify.codeFix({
15+
index: 0,
16+
description: "Remove unused declaration for: 'b'",
17+
newRangeContent:
18+
`function f({
19+
a,
20+
c,
21+
}: any)`
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f({
7+
//// a, b,
8+
////
9+
////
10+
//// c,
11+
////}: any)|] {
12+
//// a;
13+
//// c;
14+
////}
15+
16+
verify.codeFix({
17+
index: 0,
18+
description: "Remove unused declaration for: 'b'",
19+
newRangeContent:
20+
`function f({
21+
a,
22+
23+
24+
c,
25+
}: any)`
26+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f({
7+
//// a, b, /* comment related to c */
8+
//// c,
9+
////}: any)|] {
10+
//// a;
11+
//// c;
12+
////}
13+
14+
verify.codeFix({
15+
index: 0,
16+
description: "Remove unused declaration for: 'b'",
17+
newRangeContent:
18+
`function f({
19+
a, /* comment related to c */
20+
c,
21+
}: any)`
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f({
7+
//// a,
8+
//// b,
9+
//// c,
10+
////}: any)|] {
11+
//// b;
12+
//// c;
13+
////}
14+
15+
verify.codeFix({
16+
index: 0,
17+
description: "Remove unused declaration for: 'a'",
18+
newRangeContent:
19+
`function f({
20+
b,
21+
c,
22+
}: any)`
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f({
7+
//// a,
8+
//// b,
9+
////
10+
//// c,
11+
////}: any)|] {
12+
//// a;
13+
//// c;
14+
////}
15+
16+
verify.codeFix({
17+
index: 0,
18+
description: "Remove unused declaration for: 'b'",
19+
newRangeContent:
20+
`function f({
21+
a,
22+
23+
c,
24+
}: any)`
25+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
////[|import {
5+
//// a, b, c, d,
6+
//// e, f,
7+
////} from "fs";|]
8+
////
9+
////a;
10+
////b;
11+
////c;
12+
////e;
13+
////f;
14+
15+
verify.codeFix({
16+
index: 0,
17+
description: "Remove unused declaration for: 'd'",
18+
newRangeContent:
19+
`import {
20+
a, b, c,
21+
e, f,
22+
} from "fs";`
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
////[|import {
5+
//// a, b, c, d,
6+
////
7+
////
8+
////
9+
////
10+
//// e, f,
11+
////} from "fs";|]
12+
////
13+
////a;
14+
////b;
15+
////c;
16+
////e;
17+
////f;
18+
19+
verify.codeFix({
20+
index: 0,
21+
description: "Remove unused declaration for: 'd'",
22+
newRangeContent:
23+
`import {
24+
a, b, c,
25+
26+
27+
28+
29+
e, f,
30+
} from "fs";`
31+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
////[|import {
5+
//// a, b, c, d, /** comment related to e */
6+
//// e, f,
7+
////} from "fs";|]
8+
////
9+
////a;
10+
////b;
11+
////c;
12+
////e;
13+
////f;
14+
15+
verify.codeFix({
16+
index: 0,
17+
description: "Remove unused declaration for: 'd'",
18+
newRangeContent:
19+
`import {
20+
a, b, c, /** comment related to e */
21+
e, f,
22+
} from "fs";`
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
////[|import {
5+
//// a,
6+
//// b,
7+
//// c,
8+
////} from "fs";|]
9+
////
10+
////a;
11+
////c;
12+
13+
verify.codeFix({
14+
index: 0,
15+
description: "Remove unused declaration for: 'b'",
16+
newRangeContent:
17+
`import {
18+
a,
19+
c,
20+
} from "fs";`
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
////[|import {
5+
//// a,
6+
//// b,
7+
////
8+
////
9+
//// c,
10+
////} from "fs";|]
11+
////
12+
////a;
13+
////c;
14+
15+
verify.codeFix({
16+
index: 0,
17+
description: "Remove unused declaration for: 'b'",
18+
newRangeContent:
19+
`import {
20+
a,
21+
22+
23+
c,
24+
} from "fs";`
25+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f(
7+
//// a: number, b: number,
8+
//// c: number,
9+
////)|] {
10+
//// a;
11+
//// c;
12+
////}
13+
14+
verify.codeFix({
15+
index: 0,
16+
description: "Remove unused declaration for: 'b'",
17+
newRangeContent:
18+
`function f(
19+
a: number,
20+
c: number,
21+
)`
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f(
7+
//// a: number, b: number,
8+
////
9+
//// // comment
10+
//// c: number,
11+
////)|] {
12+
//// a;
13+
//// c;
14+
////}
15+
16+
verify.codeFix({
17+
index: 0,
18+
description: "Remove unused declaration for: 'b'",
19+
newRangeContent:
20+
`function f(
21+
a: number,
22+
23+
// comment
24+
c: number,
25+
)`
26+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f(
7+
//// a: number, b: number, /* comment related to c */
8+
//// c: number,
9+
////)|] {
10+
//// a;
11+
//// c;
12+
////}
13+
14+
verify.codeFix({
15+
index: 0,
16+
description: "Remove unused declaration for: 'b'",
17+
newRangeContent:
18+
`function f(
19+
a: number, /* comment related to c */
20+
c: number,
21+
)`
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f(
7+
//// a: number,
8+
//// b: number,
9+
//// c: number,
10+
////)|] {
11+
//// b;
12+
//// c;
13+
////}
14+
15+
verify.codeFix({
16+
index: 0,
17+
description: "Remove unused declaration for: 'a'",
18+
newRangeContent:
19+
`function f(
20+
b: number,
21+
c: number,
22+
)`
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////[|function f(
7+
//// a: number,
8+
//// b: number,
9+
////
10+
////
11+
//// c: number,
12+
////)|] {
13+
//// a;
14+
//// c;
15+
////}
16+
17+
verify.codeFix({
18+
index: 0,
19+
description: "Remove unused declaration for: 'b'",
20+
newRangeContent:
21+
`function f(
22+
a: number,
23+
24+
25+
c: number,
26+
)`
27+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////function f<
7+
//// T1, T2,
8+
//// T3,
9+
////>(a: T1, b: T3) {}
10+
11+
verify.codeFix({
12+
index: 0,
13+
description: "Remove unused declaration for: 'T2'",
14+
newFileContent:
15+
`function f<
16+
T1,
17+
T3,
18+
>(a: T1, b: T3) {}`
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////function f<
7+
//// T1, T2,
8+
////
9+
////
10+
//// T3,
11+
////>(a: T1, b: T3) {}
12+
13+
verify.codeFix({
14+
index: 0,
15+
description: "Remove unused declaration for: 'T2'",
16+
newFileContent:
17+
`function f<
18+
T1,
19+
20+
21+
T3,
22+
>(a: T1, b: T3) {}`
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////function f<
7+
//// T1, T2, /** comment related to T3 */
8+
//// T3,
9+
////>(a: T1, b: T3) {}
10+
11+
verify.codeFix({
12+
index: 0,
13+
description: "Remove unused declaration for: 'T2'",
14+
newFileContent:
15+
`function f<
16+
T1, /** comment related to T3 */
17+
T3,
18+
>(a: T1, b: T3) {}`
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////function f<
7+
//// T1,
8+
//// T2,
9+
//// T3,
10+
////>(a: T1, b: T3) {}
11+
12+
verify.codeFix({
13+
index: 0,
14+
description: "Remove unused declaration for: 'T2'",
15+
newFileContent:
16+
`function f<
17+
T1,
18+
T3,
19+
>(a: T1, b: T3) {}`
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
////function f<
7+
//// T1,
8+
//// T2,
9+
////
10+
////
11+
//// T3,
12+
////>(a: T1, b: T3) {}
13+
14+
verify.codeFix({
15+
index: 0,
16+
description: "Remove unused declaration for: 'T2'",
17+
newFileContent:
18+
`function f<
19+
T1,
20+
21+
22+
T3,
23+
>(a: T1, b: T3) {}`
24+
});

0 commit comments

Comments
 (0)
Please sign in to comment.