Skip to content

Commit 4cc1848

Browse files
Merge pull request #2996 from Microsoft/lastOrUndefinedReplacement
Replace awkward last-element selection pattern with 'lastOrUndefined'
2 parents 3eaf2cf + d5c9857 commit 4cc1848

File tree

11 files changed

+30
-30
lines changed

11 files changed

+30
-30
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,7 +3182,7 @@ module ts {
31823182

31833183
function getRestTypeOfSignature(signature: Signature): Type {
31843184
if (signature.hasRestParameter) {
3185-
let type = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
3185+
let type = getTypeOfSymbol(lastOrUndefined(signature.parameters));
31863186
if (type.flags & TypeFlags.Reference && (<TypeReference>type).target === globalArrayType) {
31873187
return (<TypeReference>type).typeArguments[0];
31883188
}
@@ -5666,7 +5666,7 @@ module ts {
56665666
// If last parameter is contextually rest parameter get its type
56675667
if (indexOfParameter === (func.parameters.length - 1) &&
56685668
funcHasRestParameters && contextualSignature.hasRestParameter && func.parameters.length >= contextualSignature.parameters.length) {
5669-
return getTypeOfSymbol(contextualSignature.parameters[contextualSignature.parameters.length - 1]);
5669+
return getTypeOfSymbol(lastOrUndefined(contextualSignature.parameters));
56705670
}
56715671
}
56725672
}
@@ -7215,9 +7215,9 @@ module ts {
72157215
links.type = instantiateType(getTypeAtPosition(context, i), mapper);
72167216
}
72177217
if (signature.hasRestParameter && context.hasRestParameter && signature.parameters.length >= context.parameters.length) {
7218-
let parameter = signature.parameters[signature.parameters.length - 1];
7218+
let parameter = lastOrUndefined(signature.parameters);
72197219
let links = getSymbolLinks(parameter);
7220-
links.type = instantiateType(getTypeOfSymbol(context.parameters[context.parameters.length - 1]), mapper);
7220+
links.type = instantiateType(getTypeOfSymbol(lastOrUndefined(context.parameters)), mapper);
72217221
}
72227222
}
72237223

@@ -12829,7 +12829,7 @@ module ts {
1282912829
function checkGrammarBindingElement(node: BindingElement) {
1283012830
if (node.dotDotDotToken) {
1283112831
let elements = (<BindingPattern>node.parent).elements;
12832-
if (node !== elements[elements.length - 1]) {
12832+
if (node !== lastOrUndefined(elements)) {
1283312833
return grammarErrorOnNode(node, Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern);
1283412834
}
1283512835

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ module ts {
470470
let normalized: string[] = [];
471471
for (let part of parts) {
472472
if (part !== ".") {
473-
if (part === ".." && normalized.length > 0 && normalized[normalized.length - 1] !== "..") {
473+
if (part === ".." && normalized.length > 0 && lastOrUndefined(normalized) !== "..") {
474474
normalized.pop();
475475
}
476476
else {
@@ -586,7 +586,7 @@ module ts {
586586
export function getRelativePathToDirectoryOrUrl(directoryPathOrUrl: string, relativeOrAbsolutePath: string, currentDirectory: string, getCanonicalFileName: (fileName: string) => string, isAbsolutePathAnUrl: boolean) {
587587
let pathComponents = getNormalizedPathOrUrlComponents(relativeOrAbsolutePath, currentDirectory);
588588
let directoryComponents = getNormalizedPathOrUrlComponents(directoryPathOrUrl, currentDirectory);
589-
if (directoryComponents.length > 1 && directoryComponents[directoryComponents.length - 1] === "") {
589+
if (directoryComponents.length > 1 && lastOrUndefined(directoryComponents) === "") {
590590
// If the directory path given was of type test/cases/ then we really need components of directory to be only till its name
591591
// that is ["test", "cases", ""] needs to be actually ["test", "cases"]
592592
directoryComponents.length--;

src/compiler/emitter.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ var ts;
267267
var sourceMapNameIndexMap = {};
268268
var sourceMapNameIndices = [];
269269
function getSourceMapNameIndex() {
270-
return sourceMapNameIndices.length ? sourceMapNameIndices[sourceMapNameIndices.length - 1] : -1;
270+
return sourceMapNameIndices.length ? lastOrUndefined(sourceMapNameIndices) : -1;
271271
}
272272
// Last recorded and encoded spans
273273
var lastRecordedSourceMapSpan;
@@ -5084,11 +5084,11 @@ var ts;
50845084
}
50855085
}
50865086
function hasDetachedComments(pos) {
5087-
return detachedCommentsInfo !== undefined && detachedCommentsInfo[detachedCommentsInfo.length - 1].nodePos === pos;
5087+
return detachedCommentsInfo !== undefined && lastOrUndefined(detachedCommentsInfo).nodePos === pos;
50885088
}
50895089
function getLeadingCommentsWithoutDetachedComments() {
50905090
// get the leading comments from detachedPos
5091-
var leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
5091+
var leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos);
50925092
if (detachedCommentsInfo.length - 1) {
50935093
detachedCommentsInfo.pop();
50945094
}
@@ -5189,13 +5189,13 @@ var ts;
51895189
// All comments look like they could have been part of the copyright header. Make
51905190
// sure there is at least one blank line between it and the node. If not, it's not
51915191
// a copyright header.
5192-
var lastCommentLine = ts.getLineOfLocalPosition(currentSourceFile, detachedComments[detachedComments.length - 1].end);
5192+
var lastCommentLine = ts.getLineOfLocalPosition(currentSourceFile, lastOrUndefined(detachedComments).end);
51935193
var nodeLine = ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node.pos));
51945194
if (nodeLine >= lastCommentLine + 2) {
51955195
// Valid detachedComments
51965196
ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments);
51975197
ts.emitComments(currentSourceFile, writer, detachedComments, true, newLine, writeComment);
5198-
var currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: detachedComments[detachedComments.length - 1].end };
5198+
var currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: lastOrUndefined(detachedComments).end };
51995199
if (detachedCommentsInfo) {
52005200
detachedCommentsInfo.push(currentDetachedCommentInfo);
52015201
}

src/compiler/emitter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
336336
let sourceMapNameIndexMap: Map<number> = {};
337337
let sourceMapNameIndices: number[] = [];
338338
function getSourceMapNameIndex() {
339-
return sourceMapNameIndices.length ? sourceMapNameIndices[sourceMapNameIndices.length - 1] : -1;
339+
return sourceMapNameIndices.length ? lastOrUndefined(sourceMapNameIndices) : -1;
340340
}
341341

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

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

58965896
function getLeadingCommentsWithoutDetachedComments() {
58975897
// get the leading comments from detachedPos
58985898
let leadingComments = getLeadingCommentRanges(currentSourceFile.text,
5899-
detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
5899+
lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos);
59005900
if (detachedCommentsInfo.length - 1) {
59015901
detachedCommentsInfo.pop();
59025902
}
@@ -6017,13 +6017,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
60176017
// All comments look like they could have been part of the copyright header. Make
60186018
// sure there is at least one blank line between it and the node. If not, it's not
60196019
// a copyright header.
6020-
let lastCommentLine = getLineOfLocalPosition(currentSourceFile, detachedComments[detachedComments.length - 1].end);
6020+
let lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastOrUndefined(detachedComments).end);
60216021
let nodeLine = getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node.pos));
60226022
if (nodeLine >= lastCommentLine + 2) {
60236023
// Valid detachedComments
60246024
emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments);
60256025
emitComments(currentSourceFile, writer, detachedComments, /*trailingSeparator*/ true, newLine, writeComment);
6026-
let currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: detachedComments[detachedComments.length - 1].end };
6026+
let currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: lastOrUndefined(detachedComments).end };
60276027
if (detachedCommentsInfo) {
60286028
detachedCommentsInfo.push(currentDetachedCommentInfo);
60296029
}

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ module ts {
16911691
do {
16921692
templateSpans.push(parseTemplateSpan());
16931693
}
1694-
while (templateSpans[templateSpans.length - 1].literal.kind === SyntaxKind.TemplateMiddle)
1694+
while (lastOrUndefined(templateSpans).literal.kind === SyntaxKind.TemplateMiddle)
16951695

16961696
templateSpans.end = getNodeEnd();
16971697
template.templateSpans = templateSpans;

src/compiler/scanner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ module ts {
522522
}
523523
collecting = true;
524524
if (result && result.length) {
525-
result[result.length - 1].hasTrailingNewLine = true;
525+
lastOrUndefined(result).hasTrailingNewLine = true;
526526
}
527527
continue;
528528
case CharacterCodes.tab:
@@ -569,7 +569,7 @@ module ts {
569569
default:
570570
if (ch > CharacterCodes.maxAsciiCharacter && (isWhiteSpace(ch) || isLineBreak(ch))) {
571571
if (result && result.length && isLineBreak(ch)) {
572-
result[result.length - 1].hasTrailingNewLine = true;
572+
lastOrUndefined(result).hasTrailingNewLine = true;
573573
}
574574
pos++;
575575
continue;

src/compiler/utilities.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ module ts {
856856
}
857857

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

862862
export function isLiteralKind(kind: SyntaxKind): boolean {
@@ -1362,7 +1362,7 @@ module ts {
13621362
let lineStartsOfS = computeLineStarts(s);
13631363
if (lineStartsOfS.length > 1) {
13641364
lineCount = lineCount + lineStartsOfS.length - 1;
1365-
linePos = output.length - s.length + lineStartsOfS[lineStartsOfS.length - 1];
1365+
linePos = output.length - s.length + lastOrUndefined(lineStartsOfS);
13661366
}
13671367
}
13681368
}
@@ -1727,8 +1727,8 @@ module ts {
17271727
output.push(((charCode >> 6) & 0B00111111) | 0B10000000);
17281728
output.push((charCode & 0B00111111) | 0B10000000);
17291729
}
1730-
else {
1731-
Debug.assert(false, "Unexpected code point");
1730+
else {
1731+
Debug.assert(false, "Unexpected code point");
17321732
}
17331733
}
17341734

src/services/breakpoints.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,14 @@ module ts.BreakpointResolver {
446446
// fall through.
447447

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

451451
case SyntaxKind.CaseBlock:
452452
// breakpoint in last statement of the last clause
453453
let caseBlock = <CaseBlock>node.parent;
454-
let lastClause = caseBlock.clauses[caseBlock.clauses.length - 1];
454+
let lastClause = lastOrUndefined(caseBlock.clauses);
455455
if (lastClause) {
456-
return spanInNode(lastClause.statements[lastClause.statements.length - 1]);
456+
return spanInNode(lastOrUndefined(lastClause.statements));
457457
}
458458
return undefined;
459459

src/services/formatting/formattingScanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module ts.formatting {
5151
if (isStarted) {
5252
if (trailingTrivia) {
5353
Debug.assert(trailingTrivia.length !== 0);
54-
wasNewLine = trailingTrivia[trailingTrivia.length - 1].kind === SyntaxKind.NewLineTrivia;
54+
wasNewLine = lastOrUndefined(trailingTrivia).kind === SyntaxKind.NewLineTrivia;
5555
}
5656
else {
5757
wasNewLine = false;

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4058,7 +4058,7 @@ module ts {
40584058
return true;
40594059
}
40604060
else if (declarations.length) {
4061-
result.push(createDefinitionInfo(declarations[declarations.length - 1], symbolKind, symbolName, containerName));
4061+
result.push(createDefinitionInfo(lastOrUndefined(declarations), symbolKind, symbolName, containerName));
40624062
return true;
40634063
}
40644064

0 commit comments

Comments
 (0)