Skip to content

Commit

Permalink
refactor(typeEvaluator): remove resolve condition, use walk to determ…
Browse files Browse the repository at this point in the history
…ine conditions
  • Loading branch information
sgulseth committed Aug 20, 2024
1 parent aedb463 commit 8ad70a8
Show file tree
Hide file tree
Showing 7 changed files with 477 additions and 399 deletions.
10 changes: 10 additions & 0 deletions src/typeEvaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ export function handleFuncCallNode(node: FuncCallNode, scope: Scope): TypeNode {
case 'global.defined': {
return {type: 'boolean'}
}
case 'global.path': {
const arg = walk({node: node.args[0], scope})
return mapConcrete(arg, scope, (arg) => {
if (arg.type === 'string') {
return {type: 'string'}
}

return {type: 'null'}
})
}
case 'global.coalesce': {
if (node.args.length === 0) {
return {type: 'null'} satisfies NullTypeNode
Expand Down
79 changes: 79 additions & 0 deletions src/typeEvaluator/matching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
matchAnalyzePattern,
matchText,
matchTokenize,
type Pattern,
type Token,
} from '../evaluator/matching'
import type {ConcreteTypeNode} from './typeHelpers'

export function match(left: ConcreteTypeNode, right: ConcreteTypeNode): boolean | undefined {
let tokens: Token[] = []
let patterns: Pattern[] = []
if (left.type === 'string') {
if (left.value === undefined) {
return undefined
}
tokens = tokens.concat(matchTokenize(left.value))
}
if (left.type === 'array') {
if (left.of.type === 'unknown') {
return undefined
}
if (left.of.type === 'string') {
// eslint-disable-next-line max-depth
if (left.of.value === undefined) {
return undefined
}

tokens = tokens.concat(matchTokenize(left.of.value))
}
if (left.of.type === 'union') {
// eslint-disable-next-line max-depth
for (const node of left.of.of) {
// eslint-disable-next-line max-depth
if (node.type === 'string' && node.value !== undefined) {
tokens = tokens.concat(matchTokenize(node.value))
}
}
}
}

if (right.type === 'string') {
if (right.value === undefined) {
return undefined
}
patterns = patterns.concat(matchAnalyzePattern(right.value))
}
if (right.type === 'array') {
if (right.of.type === 'unknown') {
return undefined
}
if (right.of.type === 'string') {
// eslint-disable-next-line max-depth
if (right.of.value === undefined) {
return undefined
}
patterns = patterns.concat(matchAnalyzePattern(right.of.value))
}
if (right.of.type === 'union') {
// eslint-disable-next-line max-depth
for (const node of right.of.of) {
// eslint-disable-next-line max-depth
if (node.type === 'string') {
// eslint-disable-next-line max-depth
if (node.value === undefined) {
return undefined
}
patterns = patterns.concat(matchAnalyzePattern(node.value))
}

// eslint-disable-next-line max-depth
if (node.type !== 'string') {
return false
}
}
}
}
return matchText(tokens, patterns)
}
Loading

0 comments on commit 8ad70a8

Please sign in to comment.