# All files / src/parsers projection.js

100% Statements 35/35
100% Branches 11/11
100% Functions 16/16
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208

    108x

    129x

    19x

    288x

    108x

    129x

    51x

    19x

    19x
    18x

    6x

    342x
    348x

    18x

    174x

    18x

    18x

    174x

    18x

    15x

    18x

    112x

    19x

    241x

    19x
    18x
    18x

    108x

    18x

    19x
    19x

    19x

    3245x

     // @flow

    import P from 'parsimmon'
    import crap from './crap'
    import { StringParserRegExp } from './primitive'
    import IdentifierParser from './identifier'

    import type {
      ListNodeType,
      ProjectionNodeType,
      ObjectProjectionNodeType,
      SimpleObjectProjectionItemType,
      ValuePropNodeType,
      ProjectableNodeType,
      NodeType,
      ObjectProjectionItemType,
      FragmentObjectProjectionItemType
    } from '../types'

    type WrappedObjectProjectionNodeType = {
      name: 'objectProjection',
      value: Array<ObjectProjectionItemType>
    };
    type WrappedProjectionNodeType =
      | {name: 'projection', value: ListNodeType}
      | {name: 'valueProp', value: string}
      | WrappedObjectProjectionNodeType;

    type WrappedProjectionNodeWithOptionalType =
      | {
          name: 'projection',
          value: {name: 'projection', value: ListNodeType},
          optional: boolean
        }
      | {name: 'valueProp', value: string, optional: boolean}
      | {
          name: 'objectProjection',
          value: Array<ObjectProjectionItemType>,
          optional: boolean
        };

    const packList = (x: ListNodeType): WrappedProjectionNodeType => ({
      value: x,
      name: 'projection'
    })

    const packProperty = (x: string): WrappedProjectionNodeType => ({
      value: x,
      name: 'valueProp'
    })

    const unpackOne = (
      projectable: | ProjectableNodeType
        | ProjectionNodeType
        | ValuePropNodeType
        | ObjectProjectionNodeType,
      projection: WrappedProjectionNodeWithOptionalType
    ): | ProjectionNodeType
      | ValuePropNodeType
      | ProjectableNodeType
      | ObjectProjectionNodeType => {
      switch (projection.name) {
        case 'projection':
          return {
            name: 'projection',
            value: {
              optional: projection.optional,
              left: projectable,
              right: projection.value.value
            }
          }
        case 'valueProp':
          return {
            name: 'valueProp',
            value: {
              optional: projection.value[0] === '?',
              left: projectable,
              right:
                projection.value[0] === '?'
                  ? projection.value.slice(1)
                  : projection.value
            }
          }
        default:
          return {
            name: 'objectProjection',
            value: {
              left: projectable,
              optional: false,
              right: projection.value
            }
          }
      }
    }

    const PropertyParser = P.regexp(/\??(\.[$A-Z_][0-9A-Z_$]*)+/i).map(
      packProperty
    )

    const ObjectProjectionParser = P.lazy((): mixed => {
      const SimpleItemParser = P.alt(
        P.regex(StringParserRegExp).map((value: string): string =>
          value.slice(1, -1)
        ),
        IdentifierParser.map(({ value }: {value: string}): string => value)
      ).map((value: string): ObjectProjectionItemType => ({
        type: 'SimpleItem',
        value
      }))
      const RecursiveItemParser = P.seq(
        SimpleItemParser,
        crap.then(ObjectProjectionParser.atMost(1))
      ).map(
        (
          segments: [
            SimpleObjectProjectionItemType,
            [WrappedObjectProjectionNodeType]
          ]
        ): ObjectProjectionItemType =>
          segments[1].length === 0
            ? segments[0]
            : {
              type: 'RecursiveItem',
              name: segments[0].value,
              value: segments[1][0]
            }
      )

      const AliasParser = P.seq(SimpleItemParser, P.string(':').trim(crap))

      const AliasableItemParser = P.seq(
        AliasParser.atMost(1),
        RecursiveItemParser
      ).map(
        ([alias, item]: [
          [ObjectProjectionNodeType, string],
          ObjectProjectionNodeType
        ]): ObjectProjectionNodeType =>
          alias.length === 1 ? { ...item, alias: alias[0][0].value } : item
      )

      const FragmentParser = P.string('...')
        .then(crap)
        .then(IdentifierParser)
        .map((value: {value: string}): FragmentObjectProjectionItemType => ({
          type: 'FragmentItem',
          value: value.value
        }))

      return P.sepBy(
        P.alt(AliasableItemParser, FragmentParser),
        P.string(',')
          .atMost(1)
          .trim(crap)
      )
        .wrap(P.string('{').then(crap), crap.then(P.string('}')))
        .map(
          (value: Array<ObjectProjectionItemType>): WrappedProjectionNodeType => ({
            name: 'objectProjection',
            value
          })
        )
    })

    const unpack = ([projectable, projections]: [
      ProjectableNodeType,
      Array<WrappedProjectionNodeWithOptionalType>
    ]): | ProjectionNodeType
      | ValuePropNodeType
      | ProjectableNodeType
      | ObjectProjectionNodeType => projections.reduce(unpackOne, projectable)

    const SimpleProjectionParser = P.lazy((): mixed => {
      const ListParser = require('./collections/list').default.map(packList)
      const SimpleProjectionParser = P.seq(P.string('?').atMost(1), ListParser).map(
        ([optional, value]: [
          [] | [mixed],
          ListNodeType
        ]): WrappedProjectionNodeType => ({
          name: 'projection',
          optional: optional.length === 1,
          value: value
        })
      )

      return SimpleProjectionParser
    })

    const ProjectionParser = P.lazy((): mixed => {
      const ProjectableParser = require('./projectable').default

      return P.seq(
        ProjectableParser.skip(crap),
        P.alt(SimpleProjectionParser, PropertyParser, ObjectProjectionParser)
          .skip(crap)
          .many()
      )
        .map(
          ([left, right]: [
            ProjectableNodeType,
            Array<WrappedProjectionNodeWithOptionalType>
          ]): NodeType => (right.length === 0 ? left : unpack([left, right]))
        )
        .desc('projection')
    })

    export default ProjectionParser