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

Function tokensOf

ark/aot/unwrite.ts:293–324  ·  view source on GitHub ↗
(id: string, code: string)

Source from the content-addressed store, hash-verified

291// between them, which holds no quote or slash to misread. A comment is none,
292// as a transform printing the module drops it
293const tokensOf = (id: string, code: string): ModuleTokens | undefined => {
294 const { program, comments, errors } = parseModule(id, code)
295 if (errors.length) return
296 const named: (Token | Span)[] = [...comments]
297 collectNamed(program as never, code, named)
298 named.sort((l, r) => l.start - r.start)
299 const tokens: Token[] = []
300 let at = 0
301 for (const span of named) {
302 // e.g. a shorthand property's key and value
303 if (span.start < at) continue
304 readBetween(code, at, span.start, tokens)
305 if ("text" in span) tokens.push(span)
306 at = span.end
307 }
308 readBetween(code, at, code.length, tokens)
309 const statements: StatementTokens[] = []
310 let next = 0
311 for (const statement of program.body) {
312 const start = next
313 while (next < tokens.length && tokens[next].start < statement.end) next++
314 if (next > start) statements.push([start, next])
315 }
316 // a token past the last statement, which nothing a parser reads leaves
317 if (next < tokens.length) statements.push([next, tokens.length])
318 return {
319 tokens,
320 texts: tokens.map(token => token.text),
321 statements,
322 program
323 }
324}
325
326interface NamedNode extends Span {
327 type: string

Callers 1

unwriteFunction · 0.85

Calls 6

parseModuleFunction · 0.90
collectNamedFunction · 0.85
readBetweenFunction · 0.85
sortMethod · 0.80
mapMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected