fix(39515): fix getting a position for an element before the line break CRLF/LF on the line that contains 'after' element (#39554)

This commit is contained in:
Alexander T
2020-07-16 23:56:30 +03:00
committed by GitHub
parent 6430211f8f
commit c07c885151
4 changed files with 42 additions and 1 deletions

View File

@@ -794,7 +794,8 @@ namespace ts.textChanges {
const indentation = formatting.SmartIndenter.findFirstNonWhitespaceColumn(afterStartLinePosition, afterStart, sourceFile, this.formatContext.options);
// insert element before the line break on the line that contains 'after' element
let insertPos = skipTrivia(sourceFile.text, end, /*stopAfterLineBreak*/ true, /*stopAtComments*/ false);
if (insertPos !== end && isLineBreak(sourceFile.text.charCodeAt(insertPos - 1))) {
// find position before "\n" or "\r\n"
while (insertPos !== end && isLineBreak(sourceFile.text.charCodeAt(insertPos - 1))) {
insertPos--;
}
this.replaceRange(sourceFile, createRange(insertPos), newNode, { indentation, prefix: this.newLineCharacter });

View File

@@ -619,6 +619,20 @@ import {
changeTracker.insertNodeInListAfter(sourceFile, findChild("x", sourceFile), factory.createImportSpecifier(undefined, factory.createIdentifier("a")));
});
}
{
const runTest = (name: string, text: string) => runSingleFileTest(name, /*placeOpenBraceOnNewLineForFunctions*/ false, text, /*validateNodes*/ false, (sourceFile, changeTracker) => {
for (const specifier of ["x3", "x4", "x5"]) {
// eslint-disable-next-line boolean-trivia
changeTracker.insertNodeInListAfter(sourceFile, findChild("x2", sourceFile), factory.createImportSpecifier(undefined, factory.createIdentifier(specifier)));
}
});
const crlfText = "import {\r\nx1,\r\nx2\r\n} from \"bar\";";
runTest("insertNodeInListAfter19", crlfText);
const lfText = "import {\nx1,\nx2\n} from \"bar\";";
runTest("insertNodeInListAfter20", lfText);
}
{
const text = `
class A {

View File

@@ -0,0 +1,13 @@
===ORIGINAL===
import {
x1,
x2
} from "bar";
===MODIFIED===
import {
x1,
x2,
x3,
x4,
x5
} from "bar";

View File

@@ -0,0 +1,13 @@
===ORIGINAL===
import {
x1,
x2
} from "bar";
===MODIFIED===
import {
x1,
x2,
x3,
x4,
x5
} from "bar";