<commit_before>/* eslint-disable require-yield */

import {
  FieldNode,
  visit,
  FragmentDefinitionNode,
  BREAK,
  GraphQLResolveInfo,
} from "graphql"

export const hasFieldSelection = (
  resolveInfo: GraphQLResolveInfo,
  match: (nodeName: string) => boolean
): boolean => {
  if (!resolveInfo.fieldNodes) return false
  const firstFieldNode = resolveInfo.fieldNodes[0]

  let matched: boolean = false

  const visitor = (
    fieldNode: FieldNode | FragmentDefinitionNode,
    fragments?: { [key: string]: FragmentDefinitionNode }
  ): void => {
    visit(fieldNode, {
      Field(node, _key, _parent, path, _ancestors) {
        if (path.length > 3) {
          return false
        }
        if (path.length === 3 && match(node.name.value)) {
          matched = true
          return BREAK
        }
      },
      FragmentSpread(node) {
        const fragmentDef = resolveInfo.fragments[node.name.value]
        visitor(fragmentDef, fragments)
      },
    })
  }
  visitor(firstFieldNode)
  return matched
}

export const hasIntersectionWithSelectionSet = (
  resolveInfo: GraphQLResolveInfo,
  fields: string[]
): boolean => {
  return hasFieldSelection(resolveInfo, nodeName => fields.includes(nodeName))
}

export const includesFieldsOtherThanSelectionSet = (
  resolveInfo: GraphQLResolveInfo,
  fields: string[]
): boolean => {
  return hasFieldSelection(resolveInfo, nodeName => !fields.includes(nodeName))
}<commit_msg>Apply suggestions from code review<commit_after>/* eslint-disable require-yield */

import {
  FieldNode,
  visit,
  FragmentDefinitionNode,
  BREAK,
  GraphQLResolveInfo,
} from "graphql"

export const hasFieldSelection = (
  resolveInfo: GraphQLResolveInfo,
  match: (fieldName: string) => boolean
): boolean => {
  if (!resolveInfo.fieldNodes) return false
  // TODO: we only check first field node from the fields
  // this is fine in general but to fully support we may need to revisit later
  const firstFieldNode = resolveInfo.fieldNodes[0]

  let matched: boolean = false

  const visitor = (
    fieldNode: FieldNode | FragmentDefinitionNode,
    fragments?: { [key: string]: FragmentDefinitionNode }
  ): void => {
    visit(fieldNode, {
      Field(node, _key, _parent, path, _ancestors) {
        if (path.length > 3) {
          return false
        }
        if (path.length === 3 && match(node.name.value)) {
          matched = true
          return BREAK
        }
      },
      FragmentSpread(node) {
        const fragmentDef = resolveInfo.fragments[node.name.value]
        visitor(fragmentDef, fragments)
      },
    })
  }
  visitor(firstFieldNode)
  return matched
}

export const hasIntersectionWithSelectionSet = (
  resolveInfo: GraphQLResolveInfo,
  fieldNames: string[]
): boolean => {
  return hasFieldSelection(resolveInfo, nodeName =>
    fieldNames.includes(nodeName)
  )
}

export const includesFieldsOtherThanSelectionSet = (
  resolveInfo: GraphQLResolveInfo,
  fieldNames: string[]
): boolean => {
  return hasFieldSelection(
    resolveInfo,
    nodeName => !fieldNames.includes(nodeName)
  )
}
{'fraction_non_alphanumeric': 0.10384114583333333, 'fraction_numerical': 0.001953125, 'mean_word_length': 3.3465346534653464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1657042', 'n_tokens_mistral': 906, 'n_tokens_neox': 895, 'n_words': 218}