Skip to content

Commit

Permalink
refactor: remove resolve condition, use walk to determine conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth committed Aug 19, 2024
1 parent aedb463 commit aa37dab
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 381 deletions.
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 aa37dab

Please sign in to comment.