Skip to content

Commit bcb815b

Browse files
author
Andy
authored
Remove duplicate function createTextRange (#23346)
* Remove duplicate function createTextRange * Always allow end=-1 * Put noAssert back, pending #23370 * Use getRangeUnion helper * Update API (#24966)
1 parent 0677496 commit bcb815b

File tree

32 files changed

+100
-97
lines changed

32 files changed

+100
-97
lines changed

src/compiler/transformers/es2015.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -2060,21 +2060,29 @@ namespace ts {
20602060
setTextRange(declarationList, node);
20612061
setCommentRange(declarationList, node);
20622062

2063+
// If the first or last declaration is a binding pattern, we need to modify
2064+
// the source map range for the declaration list.
20632065
if (node.transformFlags & TransformFlags.ContainsBindingPattern
20642066
&& (isBindingPattern(node.declarations[0].name) || isBindingPattern(last(node.declarations).name))) {
2065-
// If the first or last declaration is a binding pattern, we need to modify
2066-
// the source map range for the declaration list.
2067-
const firstDeclaration = firstOrUndefined(declarations);
2068-
if (firstDeclaration) {
2069-
setSourceMapRange(declarationList, createRange(firstDeclaration.pos, last(declarations).end));
2070-
}
2067+
setSourceMapRange(declarationList, getRangeUnion(declarations));
20712068
}
20722069

20732070
return declarationList;
20742071
}
20752072
return visitEachChild(node, visitor, context);
20762073
}
20772074

2075+
function getRangeUnion(declarations: ReadonlyArray<Node>): TextRange {
2076+
// declarations may not be sorted by position.
2077+
// pos should be the minimum* position over all nodes (that's not -1), end should be the maximum end over all nodes.
2078+
let pos = -1, end = -1;
2079+
for (const node of declarations) {
2080+
pos = pos === -1 ? node.pos : node.pos === -1 ? pos : Math.min(pos, node.pos);
2081+
end = Math.max(end, node.end);
2082+
}
2083+
return createRange(pos, end);
2084+
}
2085+
20782086
/**
20792087
* Gets a value indicating whether we should emit an explicit initializer for a variable
20802088
* declaration in a `let` declaration list.

src/compiler/utilities.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -4056,7 +4056,8 @@ namespace ts {
40564056
* @param pos The start position.
40574057
* @param end The end position.
40584058
*/
4059-
export function createRange(pos: number, end: number): TextRange {
4059+
export function createRange(pos: number, end: number = pos): TextRange {
4060+
Debug.assert(end >= pos || end === -1);
40604061
return { pos, end };
40614062
}
40624063

@@ -4520,12 +4521,6 @@ namespace ts {
45204521
return { start, length };
45214522
}
45224523

4523-
/* @internal */
4524-
export function createTextRange(pos: number, end: number = pos): TextRange {
4525-
Debug.assert(end >= pos);
4526-
return { pos, end };
4527-
}
4528-
45294524
export function createTextSpanFromBounds(start: number, end: number) {
45304525
return createTextSpan(start, end - start);
45314526
}

src/services/codefixes/convertToEs6Module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace ts.codefix {
208208
return replacement[1];
209209
}
210210
else {
211-
changes.replaceRangeWithText(sourceFile, createTextRange(left.getStart(sourceFile), right.pos), "export default");
211+
changes.replaceRangeWithText(sourceFile, createRange(left.getStart(sourceFile), right.pos), "export default");
212212
return true;
213213
}
214214
}

src/services/getEditsForFileRename.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ namespace ts {
221221
}
222222

223223
function createStringRange(node: StringLiteralLike, sourceFile: SourceFileLike): TextRange {
224-
return createTextRange(node.getStart(sourceFile) + 1, node.end - 1);
224+
return createRange(node.getStart(sourceFile) + 1, node.end - 1);
225225
}
226226

227227
function forEachProperty(objectLiteral: Expression, cb: (property: PropertyAssignment, propertyName: string) => void) {

src/services/textChanges.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ namespace ts.textChanges {
291291
}
292292

293293
public insertNodeAt(sourceFile: SourceFile, pos: number, newNode: Node, options: InsertNodeOptions = {}) {
294-
this.replaceRange(sourceFile, createTextRange(pos), newNode, options);
294+
this.replaceRange(sourceFile, createRange(pos), newNode, options);
295295
}
296296

297297
private insertNodesAt(sourceFile: SourceFile, pos: number, newNodes: ReadonlyArray<Node>, options: ReplaceWithMultipleNodesOptions = {}): void {
@@ -334,7 +334,7 @@ namespace ts.textChanges {
334334
}
335335

336336
public insertText(sourceFile: SourceFile, pos: number, text: string): void {
337-
this.replaceRangeWithText(sourceFile, createTextRange(pos), text);
337+
this.replaceRangeWithText(sourceFile, createRange(pos), text);
338338
}
339339

340340
/** Prefer this over replacing a node with another that has a type annotation, as it avoids reformatting the other parts of the node. */
@@ -456,7 +456,7 @@ namespace ts.textChanges {
456456
// check if previous statement ends with semicolon
457457
// if not - insert semicolon to preserve the code from changing the meaning due to ASI
458458
if (sourceFile.text.charCodeAt(after.end - 1) !== CharacterCodes.semicolon) {
459-
this.replaceRange(sourceFile, createTextRange(after.end), createToken(SyntaxKind.SemicolonToken));
459+
this.replaceRange(sourceFile, createRange(after.end), createToken(SyntaxKind.SemicolonToken));
460460
}
461461
}
462462
const endPosition = getAdjustedEndPosition(sourceFile, after, {});
@@ -595,7 +595,7 @@ namespace ts.textChanges {
595595

596596
// write separator and leading trivia of the next element as suffix
597597
const suffix = `${tokenToString(nextToken.kind)}${sourceFile.text.substring(nextToken.end, containingList[index + 1].getStart(sourceFile))}`;
598-
this.replaceRange(sourceFile, createTextRange(startPos, containingList[index + 1].getStart(sourceFile)), newNode, { prefix, suffix });
598+
this.replaceRange(sourceFile, createRange(startPos, containingList[index + 1].getStart(sourceFile)), newNode, { prefix, suffix });
599599
}
600600
}
601601
else {
@@ -629,18 +629,18 @@ namespace ts.textChanges {
629629
}
630630
if (multilineList) {
631631
// insert separator immediately following the 'after' node to preserve comments in trailing trivia
632-
this.replaceRange(sourceFile, createTextRange(end), createToken(separator));
632+
this.replaceRange(sourceFile, createRange(end), createToken(separator));
633633
// use the same indentation as 'after' item
634634
const indentation = formatting.SmartIndenter.findFirstNonWhitespaceColumn(afterStartLinePosition, afterStart, sourceFile, this.formatContext.options);
635635
// insert element before the line break on the line that contains 'after' element
636636
let insertPos = skipTrivia(sourceFile.text, end, /*stopAfterLineBreak*/ true, /*stopAtComments*/ false);
637637
if (insertPos !== end && isLineBreak(sourceFile.text.charCodeAt(insertPos - 1))) {
638638
insertPos--;
639639
}
640-
this.replaceRange(sourceFile, createTextRange(insertPos), newNode, { indentation, prefix: this.newLineCharacter });
640+
this.replaceRange(sourceFile, createRange(insertPos), newNode, { indentation, prefix: this.newLineCharacter });
641641
}
642642
else {
643-
this.replaceRange(sourceFile, createTextRange(end), newNode, { prefix: `${tokenToString(separator)} ` });
643+
this.replaceRange(sourceFile, createRange(end), newNode, { prefix: `${tokenToString(separator)} ` });
644644
}
645645
}
646646
return this;
@@ -652,7 +652,7 @@ namespace ts.textChanges {
652652
const [openBraceEnd, closeBraceEnd] = getClassBraceEnds(cls, sourceFile);
653653
// For `class C { }` remove the whitespace inside the braces.
654654
if (positionsAreOnSameLine(openBraceEnd, closeBraceEnd, sourceFile) && openBraceEnd !== closeBraceEnd - 1) {
655-
this.deleteRange(sourceFile, createTextRange(openBraceEnd, closeBraceEnd - 1));
655+
this.deleteRange(sourceFile, createRange(openBraceEnd, closeBraceEnd - 1));
656656
}
657657
});
658658
}

src/services/utilities.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1165,15 +1165,15 @@ namespace ts {
11651165
}
11661166

11671167
export function createTextRangeFromNode(node: Node, sourceFile: SourceFile): TextRange {
1168-
return createTextRange(node.getStart(sourceFile), node.end);
1168+
return createRange(node.getStart(sourceFile), node.end);
11691169
}
11701170

11711171
export function createTextSpanFromRange(range: TextRange): TextSpan {
11721172
return createTextSpanFromBounds(range.pos, range.end);
11731173
}
11741174

11751175
export function createTextRangeFromSpan(span: TextSpan): TextRange {
1176-
return createTextRange(span.start, span.start + span.length);
1176+
return createRange(span.start, span.start + span.length);
11771177
}
11781178

11791179
export function createTextChangeFromStartLength(start: number, length: number, newText: string): TextChange {

tests/baselines/reference/sourceMapValidationDestructuringVariableStatement.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/sourceMapValidationDestructuringVariableStatement.sourcemap.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ sourceFile:sourceMapValidationDestructuringVariableStatement.ts
181181
4 >
182182
5 > name: nameC
183183
6 > ,
184-
7 > skill: skillC
185-
8 > } = { name: "Edger", skill: "cutting edges" };
184+
7 > skill: skillC } = { name: "Edger", skill: "cutting edges" }
185+
8 > ;
186186
1->Emitted(6, 1) Source(13, 5) + SourceIndex(0)
187187
2 >Emitted(6, 5) Source(13, 5) + SourceIndex(0)
188188
3 >Emitted(6, 51) Source(13, 79) + SourceIndex(0)
189189
4 >Emitted(6, 53) Source(13, 7) + SourceIndex(0)
190190
5 >Emitted(6, 68) Source(13, 18) + SourceIndex(0)
191191
6 >Emitted(6, 70) Source(13, 20) + SourceIndex(0)
192-
7 >Emitted(6, 87) Source(13, 33) + SourceIndex(0)
192+
7 >Emitted(6, 87) Source(13, 79) + SourceIndex(0)
193193
8 >Emitted(6, 88) Source(13, 80) + SourceIndex(0)
194194
---
195195
>>>if (nameA == nameB) {

tests/baselines/reference/sourceMapValidationDestructuringVariableStatement1.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/sourceMapValidationDestructuringVariableStatement1.sourcemap.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ sourceFile:sourceMapValidationDestructuringVariableStatement1.ts
197197
6 >
198198
7 > name: nameC
199199
8 > ,
200-
9 > skill: skillC
201-
10> } = { name: "Edger", skill: "cutting edges" };
200+
9 > skill: skillC } = { name: "Edger", skill: "cutting edges" }
201+
10> ;
202202
1->Emitted(6, 1) Source(13, 5) + SourceIndex(0)
203203
2 >Emitted(6, 5) Source(13, 5) + SourceIndex(0)
204204
3 >Emitted(6, 6) Source(13, 14) + SourceIndex(0)
@@ -207,7 +207,7 @@ sourceFile:sourceMapValidationDestructuringVariableStatement1.ts
207207
6 >Emitted(6, 56) Source(13, 18) + SourceIndex(0)
208208
7 >Emitted(6, 71) Source(13, 29) + SourceIndex(0)
209209
8 >Emitted(6, 73) Source(13, 31) + SourceIndex(0)
210-
9 >Emitted(6, 90) Source(13, 44) + SourceIndex(0)
210+
9 >Emitted(6, 90) Source(13, 90) + SourceIndex(0)
211211
10>Emitted(6, 91) Source(13, 91) + SourceIndex(0)
212212
---
213213
>>>var nameA = robotA.name, a = hello;

tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern.sourcemap.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ sourceFile:sourceMapValidationDestructuringVariableStatementArrayBindingPattern.
188188
6 > ,
189189
7 > nameC
190190
8 > ,
191-
9 > skillC
192-
10> ] = [3, "edging", "Trimming edges"];
191+
9 > skillC] = [3, "edging", "Trimming edges"]
192+
10> ;
193193
1->Emitted(7, 1) Source(14, 5) + SourceIndex(0)
194194
2 >Emitted(7, 5) Source(14, 5) + SourceIndex(0)
195195
3 >Emitted(7, 41) Source(14, 63) + SourceIndex(0)
@@ -198,7 +198,7 @@ sourceFile:sourceMapValidationDestructuringVariableStatementArrayBindingPattern.
198198
6 >Emitted(7, 60) Source(14, 15) + SourceIndex(0)
199199
7 >Emitted(7, 73) Source(14, 20) + SourceIndex(0)
200200
8 >Emitted(7, 75) Source(14, 22) + SourceIndex(0)
201-
9 >Emitted(7, 89) Source(14, 28) + SourceIndex(0)
201+
9 >Emitted(7, 89) Source(14, 63) + SourceIndex(0)
202202
10>Emitted(7, 90) Source(14, 64) + SourceIndex(0)
203203
---
204204
>>>var numberA3 = robotA[0], robotAInfo = robotA.slice(1);

tests/baselines/reference/sourceMapValidationDestructuringVariableStatementArrayBindingPattern2.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)