Skip to content

Commit 5d5d120

Browse files
committed
feat(typeEvaluator): add support for parsing and type evaluating media::aspect
1 parent e74f381 commit 5d5d120

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/evaluator/functions/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {type ExprNode} from '../../nodeTypes'
2-
import {StreamValue, type ArrayValue} from '../../values'
2+
import {type ArrayValue, StreamValue} from '../../values'
33
import type {Executor} from '../types'
44
import array from './array'
55
import dateTime from './dateTime'
@@ -9,6 +9,7 @@ import documents from './documents'
99
import geo from './geo'
1010
import _global from './global'
1111
import math from './math'
12+
import media from './media'
1213
import pt from './pt'
1314
import releases from './releases'
1415
import sanity from './sanity'
@@ -47,6 +48,7 @@ export const namespaces: NamespaceSet = {
4748
pt,
4849
delta,
4950
diff,
51+
media,
5052
sanity,
5153
math,
5254
dateTime,

src/evaluator/functions/media.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {constantExecutor} from '../evaluate'
2+
import type {FunctionSet} from '.'
3+
4+
const media: FunctionSet = {}
5+
media['aspect'] = constantExecutor(() => {
6+
throw new Error('not implemented')
7+
})
8+
media['aspect'].arity = 2
9+
10+
export default media

src/typeEvaluator/functions.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,21 @@ export function handleFuncCallNode(node: FuncCallNode, scope: Scope): TypeNode {
514514
case 'documents.incomingGlobalDocumentReferenceCount': {
515515
return {type: 'number'}
516516
}
517+
case 'media.aspect': {
518+
return mapNode(walk({node: node.args[0], scope}), scope, (fieldNode) => {
519+
if (fieldNode.type === 'null') {
520+
return {type: 'null'}
521+
}
522+
523+
return mapNode(walk({node: node.args[1], scope}), scope, (aspectNode) => {
524+
if (aspectNode.type !== 'string') {
525+
return {type: 'null'}
526+
}
527+
528+
return {type: 'unknown'}
529+
})
530+
})
531+
}
517532
default: {
518533
return {type: 'unknown'}
519534
}

test/typeEvaluate.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,6 +3027,44 @@ t.test('function: documents::*', (t) => {
30273027
t.end()
30283028
})
30293029

3030+
t.test('function: media::*', (t) => {
3031+
const query = `*[_type == "post"] {
3032+
"missing": media::aspect(missing, "license"),
3033+
"number": media::aspect(author, 1234),
3034+
"aspect": media::aspect(author, "license"),
3035+
}`
3036+
const ast = parse(query)
3037+
const res = typeEvaluate(ast, schemas)
3038+
3039+
t.strictSame(res, {
3040+
type: 'array',
3041+
of: {
3042+
type: 'object',
3043+
attributes: {
3044+
missing: {
3045+
type: 'objectAttribute',
3046+
value: {
3047+
type: 'null',
3048+
},
3049+
},
3050+
number: {
3051+
type: 'objectAttribute',
3052+
value: {
3053+
type: 'null',
3054+
},
3055+
},
3056+
aspect: {
3057+
type: 'objectAttribute',
3058+
value: {
3059+
type: 'unknown',
3060+
},
3061+
},
3062+
},
3063+
},
3064+
})
3065+
t.end()
3066+
})
3067+
30303068
t.test('scoping', (t) => {
30313069
const ast = parse(`*[_type == "mainDocument" && _id == $id]{
30323070
_id,

0 commit comments

Comments
 (0)