Skip to content

Commit

Permalink
feat(typeEvaluator): add support for global::dateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth committed Jul 25, 2024
1 parent 4ee1b36 commit 8ed40ea
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/typeEvaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type {FuncCallNode} from '../nodeTypes'
import {Scope} from './scope'
import {walk} from './typeEvaluate'
import {mapConcrete} from './typeHelpers'
import {mapConcrete, nullUnion} from './typeHelpers'
import type {NullTypeNode, TypeNode} from './types'

function unionWithoutNull(unionTypeNode: TypeNode): TypeNode {
Expand Down Expand Up @@ -148,6 +148,18 @@ export function handleFuncCallNode(node: FuncCallNode, scope: Scope): TypeNode {
})
}

case 'global.dateTime': {
const arg = walk({node: node.args[0], scope})

return mapConcrete(arg, scope, (arg) => {
if (arg.type === 'string') {
return nullUnion({type: 'string'}) // we don't know wether the string is a valid date or not, so we return a [null, string]-union
}

return {type: 'null'} satisfies NullTypeNode
})
}

case 'global.references': {
return {type: 'boolean'}
}
Expand Down
51 changes: 51 additions & 0 deletions test/typeEvaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2230,6 +2230,57 @@ t.test('function: global::string', (t) => {
t.end()
})

t.test('function: global::dateTime', (t) => {
const query = `*[_type == "author"] {
"string": dateTime(name),
"constant": dateTime("const"),
"number": dateTime(age),
"boolean": dateTime(true),
"object": dateTime(object)
}`
const ast = parse(query)
const res = typeEvaluate(ast, schemas)
t.strictSame(res, {
type: 'array',
of: {
type: 'object',
attributes: {
string: {
type: 'objectAttribute',
value: nullUnion({
type: 'string',
}),
},
constant: {
type: 'objectAttribute',
value: nullUnion({
type: 'string',
}),
},
number: {
type: 'objectAttribute',
value: {
type: 'null',
},
},
boolean: {
type: 'objectAttribute',
value: {
type: 'null',
},
},
object: {
type: 'objectAttribute',
value: {
type: 'null',
},
},
},
},
})
t.end()
})

t.test('function: global::upper', (t) => {
const query = `*[_type == "author"] {
"_type": upper(_type),
Expand Down
1 change: 1 addition & 0 deletions test/typeEvaluateCompare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ const unaryFunctionTests: {namespace: string; funcName: string}[] = [
{namespace: 'math', funcName: 'avg'},
{namespace: 'array', funcName: 'compact'},
{namespace: 'array', funcName: 'unique'},
{namespace: 'global', funcName: 'dateTime'},
]

for (const {namespace, funcName} of unaryFunctionTests) {
Expand Down

0 comments on commit 8ed40ea

Please sign in to comment.