Skip to content

Commit

Permalink
Use ES2021 Array.at (#3857)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Mar 29, 2023
1 parent aa43fec commit efa97f6
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/jsutils/formatList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ function formatList(conjunction: string, items: ReadonlyArray<string>): string {
}

const allButLast = items.slice(0, -1);
const lastItem = items[items.length - 1];
const lastItem = items.at(-1);
return allButLast.join(', ') + ', ' + conjunction + ' ' + lastItem;
}
2 changes: 1 addition & 1 deletion src/language/__tests__/lexer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/visitor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/language/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 6 additions & 18 deletions src/utilities/TypeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,27 @@ export class TypeInfo {
}

getType(): Maybe<GraphQLOutputType> {
if (this._typeStack.length > 0) {
return this._typeStack[this._typeStack.length - 1];
}
return this._typeStack.at(-1);
}

getParentType(): Maybe<GraphQLCompositeType> {
if (this._parentTypeStack.length > 0) {
return this._parentTypeStack[this._parentTypeStack.length - 1];
}
return this._parentTypeStack.at(-1);
}

getInputType(): Maybe<GraphQLInputType> {
if (this._inputTypeStack.length > 0) {
return this._inputTypeStack[this._inputTypeStack.length - 1];
}
return this._inputTypeStack.at(-1);
}

getParentInputType(): Maybe<GraphQLInputType> {
if (this._inputTypeStack.length > 1) {
return this._inputTypeStack[this._inputTypeStack.length - 2];
}
return this._inputTypeStack.at(-2);
}

getFieldDef(): Maybe<GraphQLField<unknown, unknown>> {
if (this._fieldDefStack.length > 0) {
return this._fieldDefStack[this._fieldDefStack.length - 1];
}
return this._fieldDefStack.at(-1);
}

getDefaultValue(): Maybe<unknown> {
if (this._defaultValueStack.length > 0) {
return this._defaultValueStack[this._defaultValueStack.length - 1];
}
return this._defaultValueStack.at(-1);
}

getDirective(): Maybe<GraphQLDirective> {
Expand Down
8 changes: 4 additions & 4 deletions src/validation/rules/KnownDirectivesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export function KnownDirectivesRule(
function getDirectiveLocationForASTPath(
ancestors: ReadonlyArray<ASTNode | ReadonlyArray<ASTNode>>,
): 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:
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit efa97f6

Please sign in to comment.