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 all commits
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
20 changes: 14 additions & 6 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2060,21 +2060,29 @@ namespace ts {
setTextRange(declarationList, node);
setCommentRange(declarationList, node);

// If the first or last declaration is a binding pattern, we need to modify
// the source map range for the declaration list.
if (node.transformFlags & TransformFlags.ContainsBindingPattern
&& (isBindingPattern(node.declarations[0].name) || isBindingPattern(last(node.declarations).name))) {
// If the first or last declaration is a binding pattern, we need to modify
// the source map range for the declaration list.
const firstDeclaration = firstOrUndefined(declarations);
if (firstDeclaration) {
setSourceMapRange(declarationList, createRange(firstDeclaration.pos, last(declarations).end));
}
setSourceMapRange(declarationList, getRangeUnion(declarations));
}

return declarationList;
}
return visitEachChild(node, visitor, context);
}

function getRangeUnion(declarations: ReadonlyArray<Node>): TextRange {
// declarations may not be sorted by position.
// pos should be the minimum* position over all nodes (that's not -1), end should be the maximum end over all nodes.
let pos = -1, end = -1;
for (const node of declarations) {
pos = pos === -1 ? node.pos : node.pos === -1 ? pos : Math.min(pos, node.pos);
end = Math.max(end, node.end);
}
return createRange(pos, end);
}

/**
* Gets a value indicating whether we should emit an explicit initializer for a variable
* declaration in a `let` declaration list.
Expand Down
9 changes: 2 additions & 7 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4056,7 +4056,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): TextRange {
Debug.assert(end >= pos || end === -1);
return { pos, end };
}

Expand Down Expand Up @@ -4520,12 +4521,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
2 changes: 1 addition & 1 deletion src/services/codefixes/convertToEs6Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ namespace ts.codefix {
return replacement[1];
}
else {
changes.replaceRangeWithText(sourceFile, createTextRange(left.getStart(sourceFile), right.pos), "export default");
changes.replaceRangeWithText(sourceFile, createRange(left.getStart(sourceFile), right.pos), "export default");
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/getEditsForFileRename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ namespace ts {
}

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

function forEachProperty(objectLiteral: Expression, cb: (property: PropertyAssignment, propertyName: string) => void) {
Expand Down
16 changes: 8 additions & 8 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ namespace ts.textChanges {
}

public 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: ReplaceWithMultipleNodesOptions = {}): void {
Expand Down Expand Up @@ -334,7 +334,7 @@ namespace ts.textChanges {
}

public insertText(sourceFile: SourceFile, pos: number, text: string): void {
this.replaceRangeWithText(sourceFile, createTextRange(pos), text);
this.replaceRangeWithText(sourceFile, createRange(pos), text);
}

/** Prefer this over replacing a node with another that has a type annotation, as it avoids reformatting the other parts of the node. */
Expand Down Expand Up @@ -456,7 +456,7 @@ 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, {});
Expand Down Expand Up @@ -595,7 +595,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 @@ -629,18 +629,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 All @@ -652,7 +652,7 @@ namespace ts.textChanges {
const [openBraceEnd, closeBraceEnd] = getClassBraceEnds(cls, sourceFile);
// For `class C { }` remove the whitespace inside the braces.
if (positionsAreOnSameLine(openBraceEnd, closeBraceEnd, sourceFile) && openBraceEnd !== closeBraceEnd - 1) {
this.deleteRange(sourceFile, createTextRange(openBraceEnd, closeBraceEnd - 1));
this.deleteRange(sourceFile, createRange(openBraceEnd, closeBraceEnd - 1));
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,15 +1165,15 @@ namespace ts {
}

export function createTextRangeFromNode(node: Node, sourceFile: SourceFile): TextRange {
return createTextRange(node.getStart(sourceFile), node.end);
return createRange(node.getStart(sourceFile), node.end);
}

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

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

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ sourceFile:sourceMapValidationDestructuringVariableStatement.ts
4 >
5 > name: nameC
6 > ,
7 > skill: skillC
8 > } = { name: "Edger", skill: "cutting edges" };
7 > skill: skillC } = { name: "Edger", skill: "cutting edges" }
8 > ;
1->Emitted(6, 1) Source(13, 5) + SourceIndex(0)
2 >Emitted(6, 5) Source(13, 5) + SourceIndex(0)
3 >Emitted(6, 51) Source(13, 79) + SourceIndex(0)
4 >Emitted(6, 53) Source(13, 7) + SourceIndex(0)
5 >Emitted(6, 68) Source(13, 18) + SourceIndex(0)
6 >Emitted(6, 70) Source(13, 20) + SourceIndex(0)
7 >Emitted(6, 87) Source(13, 33) + SourceIndex(0)
7 >Emitted(6, 87) Source(13, 79) + SourceIndex(0)
8 >Emitted(6, 88) Source(13, 80) + SourceIndex(0)
---
>>>if (nameA == nameB) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ sourceFile:sourceMapValidationDestructuringVariableStatement1.ts
6 >
7 > name: nameC
8 > ,
9 > skill: skillC
10> } = { name: "Edger", skill: "cutting edges" };
9 > skill: skillC } = { name: "Edger", skill: "cutting edges" }
10> ;
1->Emitted(6, 1) Source(13, 5) + SourceIndex(0)
2 >Emitted(6, 5) Source(13, 5) + SourceIndex(0)
3 >Emitted(6, 6) Source(13, 14) + SourceIndex(0)
Expand All @@ -207,7 +207,7 @@ sourceFile:sourceMapValidationDestructuringVariableStatement1.ts
6 >Emitted(6, 56) Source(13, 18) + SourceIndex(0)
7 >Emitted(6, 71) Source(13, 29) + SourceIndex(0)
8 >Emitted(6, 73) Source(13, 31) + SourceIndex(0)
9 >Emitted(6, 90) Source(13, 44) + SourceIndex(0)
9 >Emitted(6, 90) Source(13, 90) + SourceIndex(0)
10>Emitted(6, 91) Source(13, 91) + SourceIndex(0)
---
>>>var nameA = robotA.name, a = hello;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ sourceFile:sourceMapValidationDestructuringVariableStatementArrayBindingPattern.
6 > ,
7 > nameC
8 > ,
9 > skillC
10> ] = [3, "edging", "Trimming edges"];
9 > skillC] = [3, "edging", "Trimming edges"]
10> ;
1->Emitted(7, 1) Source(14, 5) + SourceIndex(0)
2 >Emitted(7, 5) Source(14, 5) + SourceIndex(0)
3 >Emitted(7, 41) Source(14, 63) + SourceIndex(0)
Expand All @@ -198,7 +198,7 @@ sourceFile:sourceMapValidationDestructuringVariableStatementArrayBindingPattern.
6 >Emitted(7, 60) Source(14, 15) + SourceIndex(0)
7 >Emitted(7, 73) Source(14, 20) + SourceIndex(0)
8 >Emitted(7, 75) Source(14, 22) + SourceIndex(0)
9 >Emitted(7, 89) Source(14, 28) + SourceIndex(0)
9 >Emitted(7, 89) Source(14, 63) + SourceIndex(0)
10>Emitted(7, 90) Source(14, 64) + SourceIndex(0)
---
>>>var numberA3 = robotA[0], robotAInfo = robotA.slice(1);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading