Skip to content

Commit efa97f6

Browse files
Use ES2021 Array.at (#3857)
1 parent aa43fec commit efa97f6

File tree

6 files changed

+14
-26
lines changed

6 files changed

+14
-26
lines changed

src/jsutils/formatList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ function formatList(conjunction: string, items: ReadonlyArray<string>): string {
2525
}
2626

2727
const allButLast = items.slice(0, -1);
28-
const lastItem = items[items.length - 1];
28+
const lastItem = items.at(-1);
2929
return allButLast.join(', ') + ', ' + conjunction + ' ' + lastItem;
3030
}

src/language/__tests__/lexer-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ describe('Lexer', () => {
11341134
for (let tok: Token | null = startToken; tok; tok = tok.next) {
11351135
if (tokens.length) {
11361136
// Tokens are double-linked, prev should point to last seen token.
1137-
expect(tok.prev).to.equal(tokens[tokens.length - 1]);
1137+
expect(tok.prev).to.equal(tokens.at(-1));
11381138
}
11391139
tokens.push(tok);
11401140
}

src/language/__tests__/visitor-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function checkVisitorFnArgs(ast: any, args: any, isEdited: boolean = false) {
3232
expect(parent).to.have.property(key);
3333

3434
expect(path).to.be.an.instanceof(Array);
35-
expect(path[path.length - 1]).to.equal(key);
35+
expect(path.at(-1)).to.equal(key);
3636

3737
expect(ancestors).to.be.an.instanceof(Array);
3838
expect(ancestors.length).to.equal(path.length - 1);

src/language/visitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export function visit(
296296

297297
if (edits.length !== 0) {
298298
// New root
299-
return edits[edits.length - 1][1];
299+
return edits.at(-1)[1];
300300
}
301301

302302
return root;

src/utilities/TypeInfo.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,39 +88,27 @@ export class TypeInfo {
8888
}
8989

9090
getType(): Maybe<GraphQLOutputType> {
91-
if (this._typeStack.length > 0) {
92-
return this._typeStack[this._typeStack.length - 1];
93-
}
91+
return this._typeStack.at(-1);
9492
}
9593

9694
getParentType(): Maybe<GraphQLCompositeType> {
97-
if (this._parentTypeStack.length > 0) {
98-
return this._parentTypeStack[this._parentTypeStack.length - 1];
99-
}
95+
return this._parentTypeStack.at(-1);
10096
}
10197

10298
getInputType(): Maybe<GraphQLInputType> {
103-
if (this._inputTypeStack.length > 0) {
104-
return this._inputTypeStack[this._inputTypeStack.length - 1];
105-
}
99+
return this._inputTypeStack.at(-1);
106100
}
107101

108102
getParentInputType(): Maybe<GraphQLInputType> {
109-
if (this._inputTypeStack.length > 1) {
110-
return this._inputTypeStack[this._inputTypeStack.length - 2];
111-
}
103+
return this._inputTypeStack.at(-2);
112104
}
113105

114106
getFieldDef(): Maybe<GraphQLField<unknown, unknown>> {
115-
if (this._fieldDefStack.length > 0) {
116-
return this._fieldDefStack[this._fieldDefStack.length - 1];
117-
}
107+
return this._fieldDefStack.at(-1);
118108
}
119109

120110
getDefaultValue(): Maybe<unknown> {
121-
if (this._defaultValueStack.length > 0) {
122-
return this._defaultValueStack[this._defaultValueStack.length - 1];
123-
}
111+
return this._defaultValueStack.at(-1);
124112
}
125113

126114
getDirective(): Maybe<GraphQLDirective> {

src/validation/rules/KnownDirectivesRule.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export function KnownDirectivesRule(
7272
function getDirectiveLocationForASTPath(
7373
ancestors: ReadonlyArray<ASTNode | ReadonlyArray<ASTNode>>,
7474
): DirectiveLocation | undefined {
75-
const appliedTo = ancestors[ancestors.length - 1];
76-
invariant('kind' in appliedTo);
75+
const appliedTo = ancestors.at(-1);
76+
invariant(appliedTo != null && 'kind' in appliedTo);
7777

7878
switch (appliedTo.kind) {
7979
case Kind.OPERATION_DEFINITION:
@@ -114,8 +114,8 @@ function getDirectiveLocationForASTPath(
114114
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
115115
return DirectiveLocation.INPUT_OBJECT;
116116
case Kind.INPUT_VALUE_DEFINITION: {
117-
const parentNode = ancestors[ancestors.length - 3];
118-
invariant('kind' in parentNode);
117+
const parentNode = ancestors.at(-3);
118+
invariant(parentNode != null && 'kind' in parentNode);
119119
return parentNode.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION
120120
? DirectiveLocation.INPUT_FIELD_DEFINITION
121121
: DirectiveLocation.ARGUMENT_DEFINITION;

0 commit comments

Comments
 (0)