Skip to content

Commit bad3a44

Browse files
author
Andy
authored
In insertNodeAfter, handle file with no trailing newline (#23814)
1 parent 94d94f6 commit bad3a44

File tree

9 files changed

+44
-13
lines changed

9 files changed

+44
-13
lines changed

src/harness/fourslash.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,10 +2662,10 @@ Actual: ${stringify(fullActual)}`);
26622662
public verifyImportFixAtPosition(expectedTextArray: string[], errorCode: number | undefined, preferences: ts.UserPreferences | undefined) {
26632663
const { fileName } = this.activeFile;
26642664
const ranges = this.getRanges().filter(r => r.fileName === fileName);
2665-
if (ranges.length !== 1) {
2665+
if (ranges.length > 1) {
26662666
this.raiseError("Exactly one range should be specified in the testfile.");
26672667
}
2668-
const range = ts.first(ranges);
2668+
const range = ts.firstOrUndefined(ranges);
26692669

26702670
const codeFixes = this.getCodeFixes(fileName, errorCode, preferences).filter(f => f.fixId === undefined); // TODO: GH#20315 filter out those that use the import fix ID;
26712671

@@ -2684,7 +2684,7 @@ Actual: ${stringify(fullActual)}`);
26842684
const change = ts.first(codeFix.changes);
26852685
ts.Debug.assert(change.fileName === fileName);
26862686
this.applyEdits(change.fileName, change.textChanges, /*isFormattingEdit*/ false);
2687-
const text = this.rangeText(range);
2687+
const text = range ? this.rangeText(range) : this.getFileContent(this.activeFile.fileName);
26882688
actualTextArray.push(text);
26892689
scriptInfo.updateContent(originalContent);
26902690
}

src/services/textChanges.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ namespace ts.textChanges {
177177
}
178178

179179
function getAdjustedEndPosition(sourceFile: SourceFile, node: Node, options: ConfigurableEnd) {
180+
const { end } = node;
180181
if (options.useNonAdjustedEndPosition || isExpression(node)) {
181-
return node.getEnd();
182+
return end;
182183
}
183-
const end = node.getEnd();
184184
const newEnd = skipTrivia(sourceFile.text, end, /*stopAfterLineBreak*/ true);
185185
return newEnd !== end && isLineBreak(sourceFile.text.charCodeAt(newEnd - 1))
186186
? newEnd
@@ -466,7 +466,11 @@ namespace ts.textChanges {
466466
}
467467
}
468468
const endPosition = getAdjustedEndPosition(sourceFile, after, {});
469-
return this.replaceRange(sourceFile, createTextRange(endPosition), newNode, this.getInsertNodeAfterOptions(after));
469+
const options = this.getInsertNodeAfterOptions(after);
470+
return this.replaceRange(sourceFile, createTextRange(endPosition), newNode, {
471+
...options,
472+
prefix: after.end === sourceFile.end && isStatement(after) ? (options.prefix ? `\n${options.prefix}` : "\n") : options.prefix,
473+
});
470474
}
471475

472476
private getInsertNodeAfterOptions(node: Node): InsertNodeOptions {

tests/baselines/reference/textChanges/insertNodeAfter2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace M {
2020
// comment 6
2121
var a = 4; // comment 7
2222
}
23+
2324
public class class1 implements interface1
2425
{
2526
property1: boolean;

tests/cases/fourslash/codeFixAddMissingMember5.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ verify.codeFix({
1818
()=>{ this.foo === 10 };
1919
}
2020
}
21+
2122
C.foo = undefined;
2223
`
2324
});

tests/cases/fourslash/codeFixAddMissingMember7.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ verify.codeFix({
1414
newFileContent: `class C {
1515
static p = ()=>{ this.foo === 10 };
1616
}
17+
1718
C.foo = undefined;
1819
`
1920
});

tests/cases/fourslash/convertFunctionToEs6Class_commentOnVariableDeclaration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
verify.codeFix({
99
description: "Convert function to an ES2015 class",
1010
newFileContent:
11-
`/** Doc */
11+
`
12+
/** Doc */
1213
class C {
1314
constructor() { this.x = 0; }
1415
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.ts
4+
////export const foo = 0;
5+
6+
// @Filename: /b.ts
7+
////export const bar = 0;
8+
9+
// @Filename: /c.ts
10+
////foo;
11+
////import { bar } from "./b";
12+
13+
goTo.file("/c.ts");
14+
verify.importFixAtPosition([
15+
`foo;
16+
import { bar } from "./b";
17+
import { foo } from "./a";
18+
`,
19+
]);

tests/cases/fourslash/importNameCodeFix_jsExtension.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
////import * as g from "global"; // Global imports skipped
1414
////import { a } from "./a.js";
1515
////import { a as a2 } from "./a"; // Ignored, only the first relative import is considered
16-
////[|b;|]
16+
////b;
1717

1818
goTo.file("/c.ts");
1919
verify.importFixAtPosition([
20-
`import { b } from "./b.js";
20+
`import * as g from "global"; // Global imports skipped
21+
import { a } from "./a.js";
22+
import { a as a2 } from "./a"; // Ignored, only the first relative import is considered
23+
import { b } from "./b.js";
2124
b;`,
2225
]);

tests/cases/fourslash/importNameCodeFix_jsx.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
// @Filename: /c.tsx
1515
////import { React } from "react";
16-
////[|<Foo />;|]
16+
////<Foo />;
1717

1818
// @Filename: /d.tsx
19-
////[|import { Foo } from "./Foo";
20-
////<Foo />;|]
19+
////import { Foo } from "./Foo";
20+
////<Foo />;
2121

2222
// Tests that we don't crash at non-identifier location.
2323
goTo.file("/a.tsx");
@@ -26,7 +26,8 @@ verify.importFixAtPosition([]);
2626
// When constructor is missing, provide fix for that
2727
goTo.file("/c.tsx");
2828
verify.importFixAtPosition([
29-
`import { Foo } from "./Foo";
29+
`import { React } from "react";
30+
import { Foo } from "./Foo";
3031
<Foo />;`]);
3132

3233
// When JSX namespace is missing, provide fix for that

0 commit comments

Comments
 (0)