mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
In insertNodeAfter, handle file with no trailing newline (#23814)
This commit is contained in:
@@ -2662,10 +2662,10 @@ Actual: ${stringify(fullActual)}`);
|
||||
public verifyImportFixAtPosition(expectedTextArray: string[], errorCode: number | undefined, preferences: ts.UserPreferences | undefined) {
|
||||
const { fileName } = this.activeFile;
|
||||
const ranges = this.getRanges().filter(r => r.fileName === fileName);
|
||||
if (ranges.length !== 1) {
|
||||
if (ranges.length > 1) {
|
||||
this.raiseError("Exactly one range should be specified in the testfile.");
|
||||
}
|
||||
const range = ts.first(ranges);
|
||||
const range = ts.firstOrUndefined(ranges);
|
||||
|
||||
const codeFixes = this.getCodeFixes(fileName, errorCode, preferences).filter(f => f.fixId === undefined); // TODO: GH#20315 filter out those that use the import fix ID;
|
||||
|
||||
@@ -2684,7 +2684,7 @@ Actual: ${stringify(fullActual)}`);
|
||||
const change = ts.first(codeFix.changes);
|
||||
ts.Debug.assert(change.fileName === fileName);
|
||||
this.applyEdits(change.fileName, change.textChanges, /*isFormattingEdit*/ false);
|
||||
const text = this.rangeText(range);
|
||||
const text = range ? this.rangeText(range) : this.getFileContent(this.activeFile.fileName);
|
||||
actualTextArray.push(text);
|
||||
scriptInfo.updateContent(originalContent);
|
||||
}
|
||||
|
||||
@@ -177,10 +177,10 @@ namespace ts.textChanges {
|
||||
}
|
||||
|
||||
function getAdjustedEndPosition(sourceFile: SourceFile, node: Node, options: ConfigurableEnd) {
|
||||
const { end } = node;
|
||||
if (options.useNonAdjustedEndPosition || isExpression(node)) {
|
||||
return node.getEnd();
|
||||
return end;
|
||||
}
|
||||
const end = node.getEnd();
|
||||
const newEnd = skipTrivia(sourceFile.text, end, /*stopAfterLineBreak*/ true);
|
||||
return newEnd !== end && isLineBreak(sourceFile.text.charCodeAt(newEnd - 1))
|
||||
? newEnd
|
||||
@@ -466,7 +466,11 @@ namespace ts.textChanges {
|
||||
}
|
||||
}
|
||||
const endPosition = getAdjustedEndPosition(sourceFile, after, {});
|
||||
return this.replaceRange(sourceFile, createTextRange(endPosition), newNode, this.getInsertNodeAfterOptions(after));
|
||||
const options = this.getInsertNodeAfterOptions(after);
|
||||
return this.replaceRange(sourceFile, createTextRange(endPosition), newNode, {
|
||||
...options,
|
||||
prefix: after.end === sourceFile.end && isStatement(after) ? (options.prefix ? `\n${options.prefix}` : "\n") : options.prefix,
|
||||
});
|
||||
}
|
||||
|
||||
private getInsertNodeAfterOptions(node: Node): InsertNodeOptions {
|
||||
|
||||
Reference in New Issue
Block a user