MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / tokenize

Function tokenize

src/resolution/ambient-modules.ts:413–571  ·  view source on GitHub ↗

* Feed `source`'s tokens to `visit`: words, string literals (by their * contents), template literals (as one token, their `${…}` expressions read as * code) and punctuation, each with the brace depth outside it — both braces of * a block carry the depth the block sits at.

(
  source: string,
  visit: (kind: Kind, text: string, line: number, column: number, depth: number) => void
)

Source from the content-addressed store, hash-verified

411 * a block carry the depth the block sits at.
412 */
413function tokenize(
414 source: string,
415 visit: (kind: Kind, text: string, line: number, column: number, depth: number) => void
416): void {
417 const n = source.length;
418 let i = 0;
419 let line = 1;
420 let lineStart = 0;
421 let depth = 0;
422 // The brace depth at each open `${`: the `}` that brings depth back to it ends the expression.
423 const templates: number[] = [];
424 // Whether a `/` here would start a regex: after an operator, a keyword or nothing.
425 let regexAllowed = true;
426
427 const newline = (at: number): void => {
428 line++;
429 lineStart = at + 1;
430 };
431
432 // Reads a template literal's text from `i` up to its closing backtick or a `${`.
433 const templateText = (): void => {
434 while (i < n) {
435 const c = source.charCodeAt(i);
436 if (c === 92 /* \ */) {
437 if (source.charCodeAt(i + 1) === 10) newline(i + 1);
438 i += 2;
439 continue;
440 }
441 if (c === 96 /* ` */) {
442 i++;
443 return;
444 }
445 if (c === 36 /* $ */ && source.charCodeAt(i + 1) === 123 /* { */) {
446 templates.push(depth);
447 i += 2;
448 return;
449 }
450 if (c === 10) newline(i);
451 i++;
452 }
453 };
454
455 while (i < n) {
456 const c = source.charCodeAt(i);
457 if (c === 10) {
458 newline(i);
459 i++;
460 continue;
461 }
462 if (c === 32 || c === 9 || c === 13 || c === 0xfeff || c === 0xa0) {
463 i++;
464 continue;
465 }
466 const column = i - lineStart;
467 if (c === 47 /* / */) {
468 const next = source.charCodeAt(i + 1);
469 if (next === 47) {
470 while (i < n && source.charCodeAt(i) !== 10) i++;

Callers 3

scanAmbientModulesFunction · 0.70
scanGlobalScopeFunction · 0.70
scanNamespaceBlocksFunction · 0.70

Calls 6

newlineFunction · 0.85
templateTextFunction · 0.85
hasMethod · 0.80
isWordCharFunction · 0.70
isWordStartFunction · 0.70
visitFunction · 0.50

Tested by

no test coverage detected