Skip to content

Commit 80b2c58

Browse files
committed
Eliminate replaceWithSingle in favor of replaceRange
1 parent 8d6e48a commit 80b2c58

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

src/services/textChanges.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -288,26 +288,15 @@ namespace ts.textChanges {
288288
}
289289

290290
public replaceNode(sourceFile: SourceFile, oldNode: Node, newNode: Node, options: ChangeNodeOptions = {}) {
291-
const startPosition = getAdjustedStartPosition(sourceFile, oldNode, options, Position.Start);
292-
const endPosition = getAdjustedEndPosition(sourceFile, oldNode, options);
293-
return this.replaceWithSingle(sourceFile, startPosition, endPosition, newNode, options);
291+
const pos = getAdjustedStartPosition(sourceFile, oldNode, options, Position.Start);
292+
const end = getAdjustedEndPosition(sourceFile, oldNode, options);
293+
return this.replaceRange(sourceFile, { pos, end }, newNode, options);
294294
}
295295

296296
public replaceNodeRange(sourceFile: SourceFile, startNode: Node, endNode: Node, newNode: Node, options: ChangeNodeOptions = {}) {
297-
const startPosition = getAdjustedStartPosition(sourceFile, startNode, options, Position.Start);
298-
const endPosition = getAdjustedEndPosition(sourceFile, endNode, options);
299-
return this.replaceWithSingle(sourceFile, startPosition, endPosition, newNode, options);
300-
}
301-
302-
private replaceWithSingle(sourceFile: SourceFile, startPosition: number, endPosition: number, newNode: Node, options: ChangeNodeOptions): this {
303-
this.changes.push({
304-
kind: ChangeKind.ReplaceWithSingleNode,
305-
sourceFile,
306-
options,
307-
node: newNode,
308-
range: { pos: startPosition, end: endPosition }
309-
});
310-
return this;
297+
const pos = getAdjustedStartPosition(sourceFile, startNode, options, Position.Start);
298+
const end = getAdjustedEndPosition(sourceFile, endNode, options);
299+
return this.replaceRange(sourceFile, { pos, end }, newNode, options);
311300
}
312301

313302
private replaceWithMultiple(sourceFile: SourceFile, startPosition: number, endPosition: number, newNodes: ReadonlyArray<Node>, options: ChangeMultipleNodesOptions): this {
@@ -343,18 +332,18 @@ namespace ts.textChanges {
343332
}
344333

345334
public insertNodeBefore(sourceFile: SourceFile, before: Node, newNode: Node, blankLineBetween = false) {
346-
const startPosition = getAdjustedStartPosition(sourceFile, before, {}, Position.Start);
347-
return this.replaceWithSingle(sourceFile, startPosition, startPosition, newNode, this.getOptionsForInsertNodeBefore(before, blankLineBetween));
335+
const pos = getAdjustedStartPosition(sourceFile, before, {}, Position.Start);
336+
return this.replaceRange(sourceFile, { pos, end: pos }, newNode, this.getOptionsForInsertNodeBefore(before, blankLineBetween));
348337
}
349338

350339
public insertModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void {
351340
const pos = before.getStart(sourceFile);
352-
this.replaceWithSingle(sourceFile, pos, pos, createToken(modifier), { suffix: " " });
341+
this.replaceRange(sourceFile, { pos, end: pos }, createToken(modifier), { suffix: " " });
353342
}
354343

355344
public changeIdentifierToPropertyAccess(sourceFile: SourceFile, prefix: string, node: Identifier): void {
356-
const startPosition = getAdjustedStartPosition(sourceFile, node, {}, Position.Start);
357-
this.replaceWithSingle(sourceFile, startPosition, startPosition, createPropertyAccess(createIdentifier(prefix), ""), {});
345+
const pos = getAdjustedStartPosition(sourceFile, node, {}, Position.Start);
346+
this.replaceRange(sourceFile, { pos, end: pos }, createPropertyAccess(createIdentifier(prefix), ""), {});
358347
}
359348

360349
private getOptionsForInsertNodeBefore(before: Node, doubleNewlines: boolean): ChangeNodeOptions {
@@ -392,8 +381,8 @@ namespace ts.textChanges {
392381
}
393382

394383
public insertNodeAtEndOfScope(sourceFile: SourceFile, scope: Node, newNode: Node): void {
395-
const startPosition = getAdjustedStartPosition(sourceFile, scope.getLastToken(), {}, Position.Start);
396-
this.replaceWithSingle(sourceFile, startPosition, startPosition, newNode, {
384+
const pos = getAdjustedStartPosition(sourceFile, scope.getLastToken(), {}, Position.Start);
385+
this.replaceRange(sourceFile, { pos, end: pos }, newNode, {
397386
prefix: isLineBreak(sourceFile.text.charCodeAt(scope.getLastToken().pos)) ? this.newLineCharacter : this.newLineCharacter + this.newLineCharacter,
398387
suffix: this.newLineCharacter
399388
});
@@ -435,7 +424,7 @@ namespace ts.textChanges {
435424
}
436425
}
437426
const endPosition = getAdjustedEndPosition(sourceFile, after, {});
438-
return this.replaceWithSingle(sourceFile, endPosition, endPosition, newNode, this.getInsertNodeAfterOptions(after));
427+
return this.replaceRange(sourceFile, { pos: endPosition, end: endPosition }, newNode, this.getInsertNodeAfterOptions(after));
439428
}
440429

441430
private getInsertNodeAfterOptions(node: Node): InsertNodeOptions {

0 commit comments

Comments
 (0)