diff --git a/src/jsutils/formatList.ts b/src/jsutils/formatList.ts index b00dd6591a..85d8a1738f 100644 --- a/src/jsutils/formatList.ts +++ b/src/jsutils/formatList.ts @@ -25,6 +25,6 @@ function formatList(conjunction: string, items: ReadonlyArray): string { } const allButLast = items.slice(0, -1); - const lastItem = items[items.length - 1]; + const lastItem = items.at(-1); return allButLast.join(', ') + ', ' + conjunction + ' ' + lastItem; } diff --git a/src/language/__tests__/lexer-test.ts b/src/language/__tests__/lexer-test.ts index 1160a703d9..8ec91df52f 100644 --- a/src/language/__tests__/lexer-test.ts +++ b/src/language/__tests__/lexer-test.ts @@ -1134,7 +1134,7 @@ describe('Lexer', () => { for (let tok: Token | null = startToken; tok; tok = tok.next) { if (tokens.length) { // Tokens are double-linked, prev should point to last seen token. - expect(tok.prev).to.equal(tokens[tokens.length - 1]); + expect(tok.prev).to.equal(tokens.at(-1)); } tokens.push(tok); } diff --git a/src/language/__tests__/visitor-test.ts b/src/language/__tests__/visitor-test.ts index 270c948225..67b584cc84 100644 --- a/src/language/__tests__/visitor-test.ts +++ b/src/language/__tests__/visitor-test.ts @@ -32,7 +32,7 @@ function checkVisitorFnArgs(ast: any, args: any, isEdited: boolean = false) { expect(parent).to.have.property(key); expect(path).to.be.an.instanceof(Array); - expect(path[path.length - 1]).to.equal(key); + expect(path.at(-1)).to.equal(key); expect(ancestors).to.be.an.instanceof(Array); expect(ancestors.length).to.equal(path.length - 1); diff --git a/src/language/visitor.ts b/src/language/visitor.ts index 5639fbceed..86fc48a826 100644 --- a/src/language/visitor.ts +++ b/src/language/visitor.ts @@ -296,7 +296,7 @@ export function visit( if (edits.length !== 0) { // New root - return edits[edits.length - 1][1]; + return edits.at(-1)[1]; } return root; diff --git a/src/utilities/TypeInfo.ts b/src/utilities/TypeInfo.ts index fe8ea1fbab..64a8041493 100644 --- a/src/utilities/TypeInfo.ts +++ b/src/utilities/TypeInfo.ts @@ -88,39 +88,27 @@ export class TypeInfo { } getType(): Maybe { - if (this._typeStack.length > 0) { - return this._typeStack[this._typeStack.length - 1]; - } + return this._typeStack.at(-1); } getParentType(): Maybe { - if (this._parentTypeStack.length > 0) { - return this._parentTypeStack[this._parentTypeStack.length - 1]; - } + return this._parentTypeStack.at(-1); } getInputType(): Maybe { - if (this._inputTypeStack.length > 0) { - return this._inputTypeStack[this._inputTypeStack.length - 1]; - } + return this._inputTypeStack.at(-1); } getParentInputType(): Maybe { - if (this._inputTypeStack.length > 1) { - return this._inputTypeStack[this._inputTypeStack.length - 2]; - } + return this._inputTypeStack.at(-2); } getFieldDef(): Maybe> { - if (this._fieldDefStack.length > 0) { - return this._fieldDefStack[this._fieldDefStack.length - 1]; - } + return this._fieldDefStack.at(-1); } getDefaultValue(): Maybe { - if (this._defaultValueStack.length > 0) { - return this._defaultValueStack[this._defaultValueStack.length - 1]; - } + return this._defaultValueStack.at(-1); } getDirective(): Maybe { diff --git a/src/validation/rules/KnownDirectivesRule.ts b/src/validation/rules/KnownDirectivesRule.ts index 57641b91e7..7170593ac6 100644 --- a/src/validation/rules/KnownDirectivesRule.ts +++ b/src/validation/rules/KnownDirectivesRule.ts @@ -72,8 +72,8 @@ export function KnownDirectivesRule( function getDirectiveLocationForASTPath( ancestors: ReadonlyArray>, ): DirectiveLocation | undefined { - const appliedTo = ancestors[ancestors.length - 1]; - invariant('kind' in appliedTo); + const appliedTo = ancestors.at(-1); + invariant(appliedTo != null && 'kind' in appliedTo); switch (appliedTo.kind) { case Kind.OPERATION_DEFINITION: @@ -114,8 +114,8 @@ function getDirectiveLocationForASTPath( case Kind.INPUT_OBJECT_TYPE_EXTENSION: return DirectiveLocation.INPUT_OBJECT; case Kind.INPUT_VALUE_DEFINITION: { - const parentNode = ancestors[ancestors.length - 3]; - invariant('kind' in parentNode); + const parentNode = ancestors.at(-3); + invariant(parentNode != null && 'kind' in parentNode); return parentNode.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ? DirectiveLocation.INPUT_FIELD_DEFINITION : DirectiveLocation.ARGUMENT_DEFINITION;