mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-24 20:44:53 -05:00
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:
@@ -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 });
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
===ORIGINAL===
|
||||
import {
|
||||
x1,
|
||||
x2
|
||||
} from "bar";
|
||||
===MODIFIED===
|
||||
import {
|
||||
x1,
|
||||
x2,
|
||||
x3,
|
||||
x4,
|
||||
x5
|
||||
} from "bar";
|
||||
@@ -0,0 +1,13 @@
|
||||
===ORIGINAL===
|
||||
import {
|
||||
x1,
|
||||
x2
|
||||
} from "bar";
|
||||
===MODIFIED===
|
||||
import {
|
||||
x1,
|
||||
x2,
|
||||
x3,
|
||||
x4,
|
||||
x5
|
||||
} from "bar";
|
||||
Reference in New Issue
Block a user