Skip to content

Commit 8020e24

Browse files
author
Arthur Ozga
committed
Merge branch 'vladima/textChanges' into createTypeNode
2 parents e185ebb + 323aa3a commit 8020e24

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/harness/unittests/textChanges.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,5 +650,22 @@ import {
650650
changeTracker.insertNodeInListAfter(sourceFile, findChild("x", sourceFile), createImportSpecifier(undefined, createIdentifier("a")));
651651
})
652652
}
653+
{
654+
const text = `
655+
class A {
656+
x;
657+
}`;
658+
runSingleFileTest("insertNodeAfterMultipleNodes", noop, text, /*validateNodes*/ false, (sourceFile, changeTracker) => {
659+
let newNodes = [];
660+
for (let i = 0; i < 11 /*error doesn't occur with fewer nodes*/; ++i) {
661+
newNodes.push(
662+
createProperty(undefined, undefined, i + "", undefined, undefined, undefined));
663+
}
664+
const insertAfter = findChild("x", sourceFile);
665+
for (const newNode of newNodes) {
666+
changeTracker.insertNodeAfter(sourceFile, insertAfter, newNode, { suffix: newLineCharacter });
667+
}
668+
});
669+
}
653670
});
654671
}

src/services/textChanges.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ namespace ts.textChanges {
190190
public deleteNodeInList(sourceFile: SourceFile, node: Node) {
191191
const containingList = formatting.SmartIndenter.getContainingList(node, sourceFile);
192192
if (!containingList) {
193+
Debug.fail("node is not a list element");
193194
return this;
194195
}
195196
const index = containingList.indexOf(node);
@@ -260,6 +261,7 @@ namespace ts.textChanges {
260261
public insertNodeInListAfter(sourceFile: SourceFile, after: Node, newNode: Node) {
261262
const containingList = formatting.SmartIndenter.getContainingList(after, sourceFile);
262263
if (!containingList) {
264+
Debug.fail("node is not a list element");
263265
return this;
264266
}
265267
const index = containingList.indexOf(after);
@@ -407,10 +409,8 @@ namespace ts.textChanges {
407409
changesPerFile.forEachValue(path => {
408410
const changesInFile = changesPerFile.get(path);
409411
const sourceFile = changesInFile[0].sourceFile;
410-
ChangeTracker.normalize(changesInFile);
411-
412412
const fileTextChanges: FileTextChanges = { fileName: sourceFile.fileName, textChanges: [] };
413-
for (const c of changesInFile) {
413+
for (const c of ChangeTracker.normalize(changesInFile)) {
414414
fileTextChanges.textChanges.push({
415415
span: this.computeSpan(c, sourceFile),
416416
newText: this.computeNewText(c, sourceFile)
@@ -463,11 +463,15 @@ namespace ts.textChanges {
463463

464464
private static normalize(changes: Change[]) {
465465
// order changes by start position
466-
changes.sort((a, b) => a.range.pos - b.range.pos);
466+
const normalized = changes
467+
.map((c, i) => ({ c, i }))
468+
.sort(({ c: a, i: i1 }, { c: b, i: i2 }) => (a.range.pos - b.range.pos) || i1 - i2)
469+
.map(({ c }) => c);
467470
// verify that end position of the change is less than start position of the next change
468-
for (let i = 0; i < changes.length - 2; i++) {
469-
Debug.assert(changes[i].range.end <= changes[i + 1].range.pos);
471+
for (let i = 0; i < normalized.length - 2; i++) {
472+
Debug.assert(normalized[i].range.end <= normalized[i + 1].range.pos);
470473
}
474+
return normalized;
471475
}
472476
}
473477

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
===ORIGINAL===
2+
3+
class A {
4+
x;
5+
}
6+
===MODIFIED===
7+
8+
class A {
9+
x;
10+
0;
11+
1;
12+
2;
13+
3;
14+
4;
15+
5;
16+
6;
17+
7;
18+
8;
19+
9;
20+
10;
21+
}

0 commit comments

Comments
 (0)