Skip to content

Commit

Permalink
Merge pull request #2996 from Microsoft/lastOrUndefinedReplacement
Browse files Browse the repository at this point in the history
Replace awkward last-element selection pattern with 'lastOrUndefined'
  • Loading branch information
DanielRosenwasser committed May 4, 2015
2 parents 3eaf2cf + d5c9857 commit 4cc1848
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3182,7 +3182,7 @@ module ts {

function getRestTypeOfSignature(signature: Signature): Type {
if (signature.hasRestParameter) {
let type = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
let type = getTypeOfSymbol(lastOrUndefined(signature.parameters));
if (type.flags & TypeFlags.Reference && (<TypeReference>type).target === globalArrayType) {
return (<TypeReference>type).typeArguments[0];
}
Expand Down Expand Up @@ -5666,7 +5666,7 @@ module ts {
// If last parameter is contextually rest parameter get its type
if (indexOfParameter === (func.parameters.length - 1) &&
funcHasRestParameters && contextualSignature.hasRestParameter && func.parameters.length >= contextualSignature.parameters.length) {
return getTypeOfSymbol(contextualSignature.parameters[contextualSignature.parameters.length - 1]);
return getTypeOfSymbol(lastOrUndefined(contextualSignature.parameters));
}
}
}
Expand Down Expand Up @@ -7215,9 +7215,9 @@ module ts {
links.type = instantiateType(getTypeAtPosition(context, i), mapper);
}
if (signature.hasRestParameter && context.hasRestParameter && signature.parameters.length >= context.parameters.length) {
let parameter = signature.parameters[signature.parameters.length - 1];
let parameter = lastOrUndefined(signature.parameters);
let links = getSymbolLinks(parameter);
links.type = instantiateType(getTypeOfSymbol(context.parameters[context.parameters.length - 1]), mapper);
links.type = instantiateType(getTypeOfSymbol(lastOrUndefined(context.parameters)), mapper);
}
}

Expand Down Expand Up @@ -12829,7 +12829,7 @@ module ts {
function checkGrammarBindingElement(node: BindingElement) {
if (node.dotDotDotToken) {
let elements = (<BindingPattern>node.parent).elements;
if (node !== elements[elements.length - 1]) {
if (node !== lastOrUndefined(elements)) {
return grammarErrorOnNode(node, Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern);
}

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ module ts {
let normalized: string[] = [];
for (let part of parts) {
if (part !== ".") {
if (part === ".." && normalized.length > 0 && normalized[normalized.length - 1] !== "..") {
if (part === ".." && normalized.length > 0 && lastOrUndefined(normalized) !== "..") {
normalized.pop();
}
else {
Expand Down Expand Up @@ -586,7 +586,7 @@ module ts {
export function getRelativePathToDirectoryOrUrl(directoryPathOrUrl: string, relativeOrAbsolutePath: string, currentDirectory: string, getCanonicalFileName: (fileName: string) => string, isAbsolutePathAnUrl: boolean) {
let pathComponents = getNormalizedPathOrUrlComponents(relativeOrAbsolutePath, currentDirectory);
let directoryComponents = getNormalizedPathOrUrlComponents(directoryPathOrUrl, currentDirectory);
if (directoryComponents.length > 1 && directoryComponents[directoryComponents.length - 1] === "") {
if (directoryComponents.length > 1 && lastOrUndefined(directoryComponents) === "") {
// If the directory path given was of type test/cases/ then we really need components of directory to be only till its name
// that is ["test", "cases", ""] needs to be actually ["test", "cases"]
directoryComponents.length--;
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ var ts;
var sourceMapNameIndexMap = {};
var sourceMapNameIndices = [];
function getSourceMapNameIndex() {
return sourceMapNameIndices.length ? sourceMapNameIndices[sourceMapNameIndices.length - 1] : -1;
return sourceMapNameIndices.length ? lastOrUndefined(sourceMapNameIndices) : -1;
}
// Last recorded and encoded spans
var lastRecordedSourceMapSpan;
Expand Down Expand Up @@ -5084,11 +5084,11 @@ var ts;
}
}
function hasDetachedComments(pos) {
return detachedCommentsInfo !== undefined && detachedCommentsInfo[detachedCommentsInfo.length - 1].nodePos === pos;
return detachedCommentsInfo !== undefined && lastOrUndefined(detachedCommentsInfo).nodePos === pos;
}
function getLeadingCommentsWithoutDetachedComments() {
// get the leading comments from detachedPos
var leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
var leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos);
if (detachedCommentsInfo.length - 1) {
detachedCommentsInfo.pop();
}
Expand Down Expand Up @@ -5189,13 +5189,13 @@ var ts;
// All comments look like they could have been part of the copyright header. Make
// sure there is at least one blank line between it and the node. If not, it's not
// a copyright header.
var lastCommentLine = ts.getLineOfLocalPosition(currentSourceFile, detachedComments[detachedComments.length - 1].end);
var lastCommentLine = ts.getLineOfLocalPosition(currentSourceFile, lastOrUndefined(detachedComments).end);
var nodeLine = ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node.pos));
if (nodeLine >= lastCommentLine + 2) {
// Valid detachedComments
ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments);
ts.emitComments(currentSourceFile, writer, detachedComments, true, newLine, writeComment);
var currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: detachedComments[detachedComments.length - 1].end };
var currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: lastOrUndefined(detachedComments).end };
if (detachedCommentsInfo) {
detachedCommentsInfo.push(currentDetachedCommentInfo);
}
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
let sourceMapNameIndexMap: Map<number> = {};
let sourceMapNameIndices: number[] = [];
function getSourceMapNameIndex() {
return sourceMapNameIndices.length ? sourceMapNameIndices[sourceMapNameIndices.length - 1] : -1;
return sourceMapNameIndices.length ? lastOrUndefined(sourceMapNameIndices) : -1;
}

// Last recorded and encoded spans
Expand Down Expand Up @@ -5890,13 +5890,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
}

function hasDetachedComments(pos: number) {
return detachedCommentsInfo !== undefined && detachedCommentsInfo[detachedCommentsInfo.length - 1].nodePos === pos;
return detachedCommentsInfo !== undefined && lastOrUndefined(detachedCommentsInfo).nodePos === pos;
}

function getLeadingCommentsWithoutDetachedComments() {
// get the leading comments from detachedPos
let leadingComments = getLeadingCommentRanges(currentSourceFile.text,
detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos);
if (detachedCommentsInfo.length - 1) {
detachedCommentsInfo.pop();
}
Expand Down Expand Up @@ -6017,13 +6017,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
// All comments look like they could have been part of the copyright header. Make
// sure there is at least one blank line between it and the node. If not, it's not
// a copyright header.
let lastCommentLine = getLineOfLocalPosition(currentSourceFile, detachedComments[detachedComments.length - 1].end);
let lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastOrUndefined(detachedComments).end);
let nodeLine = getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node.pos));
if (nodeLine >= lastCommentLine + 2) {
// Valid detachedComments
emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments);
emitComments(currentSourceFile, writer, detachedComments, /*trailingSeparator*/ true, newLine, writeComment);
let currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: detachedComments[detachedComments.length - 1].end };
let currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: lastOrUndefined(detachedComments).end };
if (detachedCommentsInfo) {
detachedCommentsInfo.push(currentDetachedCommentInfo);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ module ts {
do {
templateSpans.push(parseTemplateSpan());
}
while (templateSpans[templateSpans.length - 1].literal.kind === SyntaxKind.TemplateMiddle)
while (lastOrUndefined(templateSpans).literal.kind === SyntaxKind.TemplateMiddle)

templateSpans.end = getNodeEnd();
template.templateSpans = templateSpans;
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ module ts {
}
collecting = true;
if (result && result.length) {
result[result.length - 1].hasTrailingNewLine = true;
lastOrUndefined(result).hasTrailingNewLine = true;
}
continue;
case CharacterCodes.tab:
Expand Down Expand Up @@ -569,7 +569,7 @@ module ts {
default:
if (ch > CharacterCodes.maxAsciiCharacter && (isWhiteSpace(ch) || isLineBreak(ch))) {
if (result && result.length && isLineBreak(ch)) {
result[result.length - 1].hasTrailingNewLine = true;
lastOrUndefined(result).hasTrailingNewLine = true;
}
pos++;
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ module ts {
}

export function hasRestParameters(s: SignatureDeclaration): boolean {
return s.parameters.length > 0 && s.parameters[s.parameters.length - 1].dotDotDotToken !== undefined;
return s.parameters.length > 0 && lastOrUndefined(s.parameters).dotDotDotToken !== undefined;
}

export function isLiteralKind(kind: SyntaxKind): boolean {
Expand Down Expand Up @@ -1362,7 +1362,7 @@ module ts {
let lineStartsOfS = computeLineStarts(s);
if (lineStartsOfS.length > 1) {
lineCount = lineCount + lineStartsOfS.length - 1;
linePos = output.length - s.length + lineStartsOfS[lineStartsOfS.length - 1];
linePos = output.length - s.length + lastOrUndefined(lineStartsOfS);
}
}
}
Expand Down Expand Up @@ -1727,8 +1727,8 @@ module ts {
output.push(((charCode >> 6) & 0B00111111) | 0B10000000);
output.push((charCode & 0B00111111) | 0B10000000);
}
else {
Debug.assert(false, "Unexpected code point");
else {
Debug.assert(false, "Unexpected code point");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/services/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,14 @@ module ts.BreakpointResolver {
// fall through.

case SyntaxKind.CatchClause:
return spanInNode((<Block>node.parent).statements[(<Block>node.parent).statements.length - 1]);;
return spanInNode(lastOrUndefined((<Block>node.parent).statements));;

case SyntaxKind.CaseBlock:
// breakpoint in last statement of the last clause
let caseBlock = <CaseBlock>node.parent;
let lastClause = caseBlock.clauses[caseBlock.clauses.length - 1];
let lastClause = lastOrUndefined(caseBlock.clauses);
if (lastClause) {
return spanInNode(lastClause.statements[lastClause.statements.length - 1]);
return spanInNode(lastOrUndefined(lastClause.statements));
}
return undefined;

Expand Down
2 changes: 1 addition & 1 deletion src/services/formatting/formattingScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module ts.formatting {
if (isStarted) {
if (trailingTrivia) {
Debug.assert(trailingTrivia.length !== 0);
wasNewLine = trailingTrivia[trailingTrivia.length - 1].kind === SyntaxKind.NewLineTrivia;
wasNewLine = lastOrUndefined(trailingTrivia).kind === SyntaxKind.NewLineTrivia;
}
else {
wasNewLine = false;
Expand Down
2 changes: 1 addition & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4058,7 +4058,7 @@ module ts {
return true;
}
else if (declarations.length) {
result.push(createDefinitionInfo(declarations[declarations.length - 1], symbolKind, symbolName, containerName));
result.push(createDefinitionInfo(lastOrUndefined(declarations), symbolKind, symbolName, containerName));
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ module ts {
function nodeEndsWith(n: Node, expectedLastToken: SyntaxKind, sourceFile: SourceFile): boolean {
let children = n.getChildren(sourceFile);
if (children.length) {
let last = children[children.length - 1];
let last = lastOrUndefined(children);
if (last.kind === expectedLastToken) {
return true;
}
Expand Down

0 comments on commit 4cc1848

Please sign in to comment.