Skip to content

Commit

Permalink
feat(typeEvaluation): add support for global::string
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth committed Apr 23, 2024
1 parent 8a6a05e commit 133d8af
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/typeEvaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ export function handleFuncCallNode(node: FuncCallNode, scope: Scope): TypeNode {
return {type: 'null'} satisfies NullTypeNode
})
}
case 'global.string': {
const arg = walk({node: node.args[0], scope})
return mapConcrete(arg, scope, (node) => {
if (node.type === 'string' || node.type === 'number' || node.type === 'boolean') {
if (node.value) {
return {
type: 'string',
value: node.value.toString(),
}
}

return {
type: 'string',
}
}

return {type: 'null'}
})
}

case 'pt.text': {
if (node.args.length === 0) {
return {type: 'null'} satisfies NullTypeNode
Expand Down
2 changes: 1 addition & 1 deletion src/typeEvaluator/typeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function mapConcrete(
return mapConcrete(resolvedInline, scope, mapper, mergeUnions)
}
default:
// @ts-expect-error - we should handle each type
// @ts-expect-error - all types should be handled
throw new Error(`Unknown type: ${node.type}`)
}
}
53 changes: 53 additions & 0 deletions test/typeEvaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,59 @@ t.test('function: count', (t) => {
t.end()
})

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

function findSchemaType(name: string): TypeNode {
const type = schemas.find((s) => s.name === name)
if (!type) {
Expand Down

0 comments on commit 133d8af

Please sign in to comment.