Skip to content

Commit 85f11cc

Browse files
author
Andy
authored
textChanges: Use replaceRange in more places (microsoft#22904)
1 parent ea6740f commit 85f11cc

2 files changed

Lines changed: 15 additions & 42 deletions

File tree

src/compiler/utilities.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4070,6 +4070,12 @@ namespace ts {
40704070
return { start, length };
40714071
}
40724072

4073+
/* @internal */
4074+
export function createTextRange(pos: number, end: number = pos): TextRange {
4075+
Debug.assert(end >= pos);
4076+
return { pos, end };
4077+
}
4078+
40734079
export function createTextSpanFromBounds(start: number, end: number) {
40744080
return createTextSpan(start, end - start);
40754081
}

src/services/textChanges.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ namespace ts.textChanges {
317317
}
318318

319319
private insertNodeAt(sourceFile: SourceFile, pos: number, newNode: Node, options: InsertNodeOptions = {}) {
320-
this.changes.push({ kind: ChangeKind.ReplaceWithSingleNode, sourceFile, options, node: newNode, range: { pos, end: pos } });
321-
return this;
320+
this.replaceRange(sourceFile, createTextRange(pos), newNode, options);
322321
}
323322

324323
private insertNodesAt(sourceFile: SourceFile, pos: number, newNodes: ReadonlyArray<Node>, options: InsertNodeOptions = {}): void {
@@ -446,17 +445,11 @@ namespace ts.textChanges {
446445
// check if previous statement ends with semicolon
447446
// if not - insert semicolon to preserve the code from changing the meaning due to ASI
448447
if (sourceFile.text.charCodeAt(after.end - 1) !== CharacterCodes.semicolon) {
449-
this.changes.push({
450-
kind: ChangeKind.ReplaceWithSingleNode,
451-
sourceFile,
452-
options: {},
453-
range: { pos: after.end, end: after.end },
454-
node: createToken(SyntaxKind.SemicolonToken)
455-
});
448+
this.replaceRange(sourceFile, createTextRange(after.end), createToken(SyntaxKind.SemicolonToken));
456449
}
457450
}
458451
const endPosition = getAdjustedEndPosition(sourceFile, after, {});
459-
return this.replaceRange(sourceFile, { pos: endPosition, end: endPosition }, newNode, this.getInsertNodeAfterOptions(after));
452+
return this.replaceRange(sourceFile, createTextRange(endPosition), newNode, this.getInsertNodeAfterOptions(after));
460453
}
461454

462455
private getInsertNodeAfterOptions(node: Node): InsertNodeOptions {
@@ -540,17 +533,9 @@ namespace ts.textChanges {
540533
startPos = getStartPositionOfLine(lineAndCharOfNextElement.line, sourceFile);
541534
}
542535

543-
this.changes.push({
544-
kind: ChangeKind.ReplaceWithSingleNode,
545-
sourceFile,
546-
range: { pos: startPos, end: containingList[index + 1].getStart(sourceFile) },
547-
node: newNode,
548-
options: {
549-
prefix,
550-
// write separator and leading trivia of the next element as suffix
551-
suffix: `${tokenToString(nextToken.kind)}${sourceFile.text.substring(nextToken.end, containingList[index + 1].getStart(sourceFile))}`
552-
}
553-
});
536+
// write separator and leading trivia of the next element as suffix
537+
const suffix = `${tokenToString(nextToken.kind)}${sourceFile.text.substring(nextToken.end, containingList[index + 1].getStart(sourceFile))}`;
538+
this.replaceRange(sourceFile, createTextRange(startPos, containingList[index + 1].getStart(sourceFile)), newNode, { prefix, suffix });
554539
}
555540
}
556541
else {
@@ -584,36 +569,18 @@ namespace ts.textChanges {
584569
}
585570
if (multilineList) {
586571
// insert separator immediately following the 'after' node to preserve comments in trailing trivia
587-
this.changes.push({
588-
kind: ChangeKind.ReplaceWithSingleNode,
589-
sourceFile,
590-
range: { pos: end, end },
591-
node: createToken(separator),
592-
options: {}
593-
});
572+
this.replaceRange(sourceFile, createTextRange(end), createToken(separator));
594573
// use the same indentation as 'after' item
595574
const indentation = formatting.SmartIndenter.findFirstNonWhitespaceColumn(afterStartLinePosition, afterStart, sourceFile, this.formatContext.options);
596575
// insert element before the line break on the line that contains 'after' element
597576
let insertPos = skipTrivia(sourceFile.text, end, /*stopAfterLineBreak*/ true, /*stopAtComments*/ false);
598577
if (insertPos !== end && isLineBreak(sourceFile.text.charCodeAt(insertPos - 1))) {
599578
insertPos--;
600579
}
601-
this.changes.push({
602-
kind: ChangeKind.ReplaceWithSingleNode,
603-
sourceFile,
604-
range: { pos: insertPos, end: insertPos },
605-
node: newNode,
606-
options: { indentation, prefix: this.newLineCharacter }
607-
});
580+
this.replaceRange(sourceFile, createTextRange(insertPos), newNode, { indentation, prefix: this.newLineCharacter });
608581
}
609582
else {
610-
this.changes.push({
611-
kind: ChangeKind.ReplaceWithSingleNode,
612-
sourceFile,
613-
range: { pos: end, end },
614-
node: newNode,
615-
options: { prefix: `${tokenToString(separator)} ` }
616-
});
583+
this.replaceRange(sourceFile, createTextRange(end), newNode, { prefix: `${tokenToString(separator)} ` });
617584
}
618585
}
619586
return this;

0 commit comments

Comments
 (0)