Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate function createTextRange #23346

Merged
9 commits merged into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ namespace ts {
// for the statement list we synthesize when we down-level an arrow function with
// an expression function body. This prevents both comments and source maps from
// being emitted for the end position only.
statementsLocation = moveRangeEnd(body, -1);
statementsLocation = moveRangeEnd(body, -1, /*noAssert*/ true);

const equalsGreaterThanToken = (<ArrowFunction>node).equalsGreaterThanToken;
if (!nodeIsSynthesized(equalsGreaterThanToken) && !nodeIsSynthesized(body)) {
Expand Down Expand Up @@ -2328,7 +2328,7 @@ namespace ts {
node.initializer
)
),
moveRangeEnd(node.initializer, -1)
moveRangeEnd(node.initializer, -1, /*noAssert*/ true)
)
);
}
Expand All @@ -2343,7 +2343,7 @@ namespace ts {
}
else {
assignment.end = node.initializer.end;
statements.push(setTextRange(createStatement(visitNode(assignment, visitor, isExpression)), moveRangeEnd(node.initializer, -1)));
statements.push(setTextRange(createStatement(visitNode(assignment, visitor, isExpression)), moveRangeEnd(node.initializer, -1, /*noAssert*/ true)));
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3686,7 +3686,8 @@ namespace ts {
* @param pos The start position.
* @param end The end position.
*/
export function createRange(pos: number, end: number): TextRange {
export function createRange(pos: number, end: number = pos, noAssert = false): TextRange {
Debug.assert(noAssert || end >= pos);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We treat -1 as a special value to indicate a synthetic position. Rather than adding a noAssert, we could just write:

Debug.assert(pos === -1 || end === -1 || end >= pos);

return { pos, end };
}

Expand All @@ -3696,8 +3697,8 @@ namespace ts {
* @param range A TextRange.
* @param end The new end position.
*/
export function moveRangeEnd(range: TextRange, end: number): TextRange {
return createRange(range.pos, end);
export function moveRangeEnd(range: TextRange, end: number, noAssert = false): TextRange {
return createRange(range.pos, end, noAssert);
}

/**
Expand Down Expand Up @@ -4111,12 +4112,6 @@ namespace ts {
return { start, length };
}

/* @internal */
export function createTextRange(pos: number, end: number = pos): TextRange {
Debug.assert(end >= pos);
return { pos, end };
}

export function createTextSpanFromBounds(start: number, end: number) {
return createTextSpan(start, end - start);
}
Expand Down
14 changes: 7 additions & 7 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ namespace ts.textChanges {
}

private insertNodeAt(sourceFile: SourceFile, pos: number, newNode: Node, options: InsertNodeOptions = {}) {
this.replaceRange(sourceFile, createTextRange(pos), newNode, options);
this.replaceRange(sourceFile, createRange(pos), newNode, options);
}

private insertNodesAt(sourceFile: SourceFile, pos: number, newNodes: ReadonlyArray<Node>, options: InsertNodeOptions = {}): void {
Expand Down Expand Up @@ -457,11 +457,11 @@ namespace ts.textChanges {
// check if previous statement ends with semicolon
// if not - insert semicolon to preserve the code from changing the meaning due to ASI
if (sourceFile.text.charCodeAt(after.end - 1) !== CharacterCodes.semicolon) {
this.replaceRange(sourceFile, createTextRange(after.end), createToken(SyntaxKind.SemicolonToken));
this.replaceRange(sourceFile, createRange(after.end), createToken(SyntaxKind.SemicolonToken));
}
}
const endPosition = getAdjustedEndPosition(sourceFile, after, {});
return this.replaceRange(sourceFile, createTextRange(endPosition), newNode, this.getInsertNodeAfterOptions(after));
return this.replaceRange(sourceFile, createRange(endPosition), newNode, this.getInsertNodeAfterOptions(after));
}

private getInsertNodeAfterOptions(node: Node): InsertNodeOptions {
Expand Down Expand Up @@ -550,7 +550,7 @@ namespace ts.textChanges {

// write separator and leading trivia of the next element as suffix
const suffix = `${tokenToString(nextToken.kind)}${sourceFile.text.substring(nextToken.end, containingList[index + 1].getStart(sourceFile))}`;
this.replaceRange(sourceFile, createTextRange(startPos, containingList[index + 1].getStart(sourceFile)), newNode, { prefix, suffix });
this.replaceRange(sourceFile, createRange(startPos, containingList[index + 1].getStart(sourceFile)), newNode, { prefix, suffix });
}
}
else {
Expand Down Expand Up @@ -584,18 +584,18 @@ namespace ts.textChanges {
}
if (multilineList) {
// insert separator immediately following the 'after' node to preserve comments in trailing trivia
this.replaceRange(sourceFile, createTextRange(end), createToken(separator));
this.replaceRange(sourceFile, createRange(end), createToken(separator));
// use the same indentation as 'after' item
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))) {
insertPos--;
}
this.replaceRange(sourceFile, createTextRange(insertPos), newNode, { indentation, prefix: this.newLineCharacter });
this.replaceRange(sourceFile, createRange(insertPos), newNode, { indentation, prefix: this.newLineCharacter });
}
else {
this.replaceRange(sourceFile, createTextRange(end), newNode, { prefix: `${tokenToString(separator)} ` });
this.replaceRange(sourceFile, createRange(end), newNode, { prefix: `${tokenToString(separator)} ` });
}
}
return this;
Expand Down