Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch indexOf to includes whenever possible #2989

Merged
merged 1 commit into from Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jsutils/inspect.js
Expand Up @@ -31,7 +31,7 @@ function formatObjectValue(
return 'null';
}

if (previouslySeenValues.indexOf(value) !== -1) {
if (previouslySeenValues.includes(value)) {
return '[Circular]';
}

Expand Down
2 changes: 1 addition & 1 deletion src/language/blockString.js
Expand Up @@ -95,7 +95,7 @@ export function printBlockString(
value: string,
preferMultipleLines: boolean = false,
): string {
const isSingleLine = value.indexOf('\n') === -1;
const isSingleLine = !value.includes('\n');
const hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
const hasTrailingQuote = value[value.length - 1] === '"';
const hasTrailingSlash = value[value.length - 1] === '\\';
Expand Down
7 changes: 2 additions & 5 deletions src/language/printer.js
Expand Up @@ -332,10 +332,7 @@ function indent(str: string): string {
return wrap(' ', str.replace(/\n/g, '\n '));
}

function isMultiline(str: string): boolean {
return str.indexOf('\n') !== -1;
}

function hasMultilineItems(maybeArray: ?Array<string>): boolean {
return maybeArray != null && maybeArray.some(isMultiline);
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
return maybeArray?.some((str) => str.includes('\n')) ?? false;
}
2 changes: 1 addition & 1 deletion src/type/validate.js
Expand Up @@ -441,7 +441,7 @@ function validateTypeImplementsAncestors(
): void {
const ifaceInterfaces = type.getInterfaces();
for (const transitive of iface.getInterfaces()) {
if (ifaceInterfaces.indexOf(transitive) === -1) {
if (!ifaceInterfaces.includes(transitive)) {
context.reportError(
transitive === type
? `Type ${type.name} cannot implement ${iface.name} because it would create a circular reference.`
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__tests__/stripIgnoredCharacters-test.js
Expand Up @@ -370,7 +370,7 @@ describe('stripIgnoredCharacters', () => {
expectStripped('""",|"""').toStayTheSame();

const ignoredTokensWithoutFormatting = ignoredTokens.filter(
(token) => ['\n', '\r', '\r\n', '\t', ' '].indexOf(token) === -1,
(token) => !['\n', '\r', '\r\n', '\t', ' '].includes(token),
);
for (const ignored of ignoredTokensWithoutFormatting) {
expectStripped('"""|' + ignored + '|"""').toStayTheSame();
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/findBreakingChanges.js
Expand Up @@ -156,7 +156,7 @@ function findDirectiveChanges(
}

for (const location of oldDirective.locations) {
if (newDirective.locations.indexOf(location) === -1) {
if (!newDirective.locations.includes(location)) {
schemaChanges.push({
type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED,
description: `${location} was removed from ${oldDirective.name}.`,
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/KnownArgumentNamesRule.js
Expand Up @@ -78,7 +78,7 @@ export function KnownArgumentNamesOnDirectivesRule(
if (directiveNode.arguments && knownArgs) {
for (const argNode of directiveNode.arguments) {
const argName = argNode.name.value;
if (knownArgs.indexOf(argName) === -1) {
if (!knownArgs.includes(argName)) {
const suggestions = suggestionList(argName, knownArgs);
context.reportError(
new GraphQLError(
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/KnownDirectivesRule.js
Expand Up @@ -55,7 +55,7 @@ export function KnownDirectivesRule(
}

const candidateLocation = getDirectiveLocationForASTPath(ancestors);
if (candidateLocation && locations.indexOf(candidateLocation) === -1) {
if (candidateLocation && !locations.includes(candidateLocation)) {
context.reportError(
new GraphQLError(
`Directive "@${name}" may not be used on ${candidateLocation}.`,
Expand Down
6 changes: 1 addition & 5 deletions src/validation/rules/KnownTypeNamesRule.js
Expand Up @@ -48,7 +48,7 @@ export function KnownTypeNamesRule(
if (!existingTypesMap[typeName] && !definedTypes[typeName]) {
const definitionNode = ancestors[2] ?? parent;
const isSDL = definitionNode != null && isSDLNode(definitionNode);
if (isSDL && isStandardTypeName(typeName)) {
if (isSDL && standardTypeNames.includes(typeName)) {
return;
}

Expand All @@ -71,10 +71,6 @@ const standardTypeNames = [...specifiedScalarTypes, ...introspectionTypes].map(
(type) => type.name,
);

function isStandardTypeName(typeName: string): boolean {
return standardTypeNames.indexOf(typeName) !== -1;
}

function isSDLNode(value: ASTNode | $ReadOnlyArray<ASTNode>): boolean {
return (
!Array.isArray(value) &&
Expand Down