In insertNodeAfter, handle file with no trailing newline (#23814)

This commit is contained in:
Andy
2018-05-03 12:58:42 -07:00
committed by GitHub
parent 94d94f6335
commit bad3a44bb2
9 changed files with 44 additions and 13 deletions
+3 -3
View File
@@ -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);
}
+7 -3
View File
@@ -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 {