MCPcopy Create free account
hub / github.com/arktypeio/arktype / parseRightBound

Function parseRightBound

ark/type/parser/shift/operator/bounds.ts:167–250  ·  view source on GitHub ↗
(
	s: RootedRuntimeState,
	comparator: Comparator
)

Source from the content-addressed store, hash-verified

165})
166
167export const parseRightBound = (
168 s: RootedRuntimeState,
169 comparator: Comparator
170): void => {
171 if (s.sc.unscanned.trimStart() === "")
172 return s.error(writeMissingRightOperandMessage(comparator))
173 // store the node that will be bounded
174 const previousRoot = s.unsetRoot()
175 if (s.rightBounded?.root === previousRoot) {
176 return s.error(
177 writeDoubleRightBoundMessage(s.rightBounded.operand.expression)
178 )
179 }
180 // TypeScript's Array<string> reads as the keyword Array bounded by a type
181 const followsArray =
182 comparator === "<" && /(?:^|[^\w$.])Array\s*<$/.test(s.sc.scanned)
183 const previousScannerIndex = s.sc.location
184 s.sc.skipSpace()
185 let limit: number | Date | undefined = limitOf(s, shiftUntilTerminator(s.sc))
186 let limitToken = ""
187 if (limit === undefined) {
188 s.sc.i = previousScannerIndex
189 s.parseOperand()
190 const limitNode = s.unsetRoot()
191 // after parsing the next operand, use the locations to get the
192 // token from which it was parsed
193 limitToken = s.sc.sliceChars(previousScannerIndex, s.sc.location).trim()
194 // ( and keyof are operands that don't set a root
195 if (
196 !limitNode?.is("unit") ||
197 (typeof limitNode.unit !== "number" &&
198 !(limitNode.unit instanceof Date)) ||
199 (typeof limitNode.unit === "number" && !Number.isFinite(limitNode.unit))
200 ) {
201 return s.error(
202 writeInvalidLimitMessage(comparator, limitToken, "right") +
203 (followsArray ? arrayIsNotGenericHint : "")
204 )
205 }
206 limit = limitNode.unit
207 }
208 s.root = previousRoot
209 // apply the newly-parsed right bound
210 const exclusive = comparator.length === 1
211
212 const boundKinds = getBoundKinds(
213 comparator,
214 typeof limit === "number" ? limit : (limitToken as DateLiteral),
215 previousRoot,
216 "right"
217 )
218
219 // a Date's limit may be numeric, as it is for its bounds
220 const rule =
221 (
222 comparator === "!=" &&
223 typeof limit === "number" &&
224 previousRoot.extends($ark.kw.Date)

Callers 1

parseBoundFunction · 0.85

Calls 15

limitOfFunction · 0.90
shiftUntilTerminatorFunction · 0.90
isKeyOfFunction · 0.90
writeInvalidLimitMessageFunction · 0.85
getBoundKindsFunction · 0.85
openLeftBoundToRootFunction · 0.85
unsetRootMethod · 0.80
testMethod · 0.80
skipSpaceMethod · 0.80

Tested by

no test coverage detected