Skip to content

Commit 1f484a9

Browse files
authored
Merge pull request #13966 from zhengbli/13110
Delete the following new line when removing unused locals
2 parents f1e9fe3 + e238b38 commit 1f484a9

File tree

7 files changed

+35
-18
lines changed

7 files changed

+35
-18
lines changed

src/harness/fourslash.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,7 +2108,7 @@ namespace FourSlash {
21082108
* Because codefixes are only applied on the working file, it is unsafe
21092109
* to apply this more than once (consider a refactoring across files).
21102110
*/
2111-
public verifyRangeAfterCodeFix(expectedText: string, errorCode?: number) {
2111+
public verifyRangeAfterCodeFix(expectedText: string, errorCode?: number, includeWhiteSpace?: boolean) {
21122112
const ranges = this.getRanges();
21132113
if (ranges.length !== 1) {
21142114
this.raiseError("Exactly one range should be specified in the testfile.");
@@ -2120,7 +2120,11 @@ namespace FourSlash {
21202120

21212121
const actualText = this.rangeText(ranges[0]);
21222122

2123-
if (this.removeWhitespace(actualText) !== this.removeWhitespace(expectedText)) {
2123+
const result = includeWhiteSpace
2124+
? actualText === expectedText
2125+
: this.removeWhitespace(actualText) === this.removeWhitespace(expectedText)
2126+
2127+
if (!result) {
21242128
this.raiseError(`Actual text doesn't match expected text. Actual:\n'${actualText}'\nExpected:\n'${expectedText}'`);
21252129
}
21262130
}
@@ -3517,8 +3521,8 @@ namespace FourSlashInterface {
35173521
this.DocCommentTemplate(/*expectedText*/ undefined, /*expectedOffset*/ undefined, /*empty*/ true);
35183522
}
35193523

3520-
public rangeAfterCodeFix(expectedText: string, errorCode?: number): void {
3521-
this.state.verifyRangeAfterCodeFix(expectedText, errorCode);
3524+
public rangeAfterCodeFix(expectedText: string, errorCode?: number, includeWhiteSpace?: boolean): void {
3525+
this.state.verifyRangeAfterCodeFix(expectedText, errorCode, includeWhiteSpace);
35223526
}
35233527

35243528
public importFixAtPosition(expectedTextArray: string[], errorCode?: number): void {

src/services/codefixes/unusedIdentifierFixes.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,20 @@ namespace ts.codefix {
150150
}
151151

152152
function createCodeFixToRemoveNode(node: Node) {
153-
return createCodeFix("", node.getStart(), node.getWidth());
153+
let end = node.getEnd();
154+
const endCharCode = sourceFile.text.charCodeAt(end);
155+
const afterEndCharCode = sourceFile.text.charCodeAt(end + 1);
156+
if (isLineBreak(endCharCode)) {
157+
end += 1;
158+
}
159+
// in the case of CR LF, you could have two consecutive new line characters for one new line.
160+
// this needs to be differenciated from two LF LF chars that actually mean two new lines.
161+
if (isLineBreak(afterEndCharCode) && endCharCode !== afterEndCharCode) {
162+
end += 1;
163+
}
164+
165+
const start = node.getStart();
166+
return createCodeFix("", start, end - start);
154167
}
155168

156169
function findFirstNonSpaceCharPosStarting(start: number) {

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ declare namespace FourSlashInterface {
225225
noMatchingBracePositionInCurrentFile(bracePosition: number): void;
226226
DocCommentTemplate(expectedText: string, expectedOffset: number, empty?: boolean): void;
227227
noDocCommentTemplate(): void;
228-
rangeAfterCodeFix(expectedText: string, errorCode?: number): void;
228+
rangeAfterCodeFix(expectedText: string, errorCode?: number, includeWhiteSpace?: boolean): void;
229229
importFixAtPosition(expectedTextArray: string[], errorCode?: number): void;
230230

231231
navigationBar(json: any): void;

tests/cases/fourslash/unusedImports2FS.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
// @noUnusedLocals: true
44
// @Filename: file2.ts
5-
//// [|import {Calculator} from "./file1"
6-
//// import {test} from "./file1"|]
5+
//// [|import {test} from "./file1"
6+
//// import {Calculator} from "./file1"|]
77

88
//// var x = new Calculator();
99
//// x.handleChar();
@@ -16,4 +16,4 @@
1616
////
1717
//// }
1818

19-
verify.rangeAfterCodeFix(`import {Calculator} from "./file1"`);
19+
verify.rangeAfterCodeFix(`import {Calculator} from "./file1"`, /*errorCode*/ undefined, /*includeWhiteSpace*/ true);

tests/cases/fourslash/unusedVariableInForLoop7FS.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
//// function f1 () {
55
//// for (const elem of ["a", "b", "c"]) {
66
//// elem;
7-
//// [|var x = 20;|]
8-
//// }
7+
//// [|var x = 20;
8+
//// }|]
99
////}
1010
////
1111

12-
verify.rangeAfterCodeFix("");
12+
verify.rangeAfterCodeFix("}");

tests/cases/fourslash/unusedVariableInModule1.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @noUnusedLocals: true
44
// @noUnusedParameters: true
55
//// export {}
6-
//// [|var x: string;|]
7-
//// export var y: string;
6+
//// [|var x: string;
7+
//// export var y: string;|]
88

9-
verify.rangeAfterCodeFix("");
9+
verify.rangeAfterCodeFix("export var y: string;");

tests/cases/fourslash/unusedVariableInModule3.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @noUnusedLocals: true
44
// @noUnusedParameters: true
55
//// export {}
6-
//// [|var x = function f1() {}|]
7-
//// export var y: string;
6+
//// [|var x = function f1() {}
7+
//// export var y: string;|]
88

9-
verify.rangeAfterCodeFix("");
9+
verify.rangeAfterCodeFix("export var y: string;");

0 commit comments

Comments
 (0)