All files code.ts

96.5% Statements 470/487
93.23% Branches 124/133
92% Functions 23/25
96.5% Lines 470/487

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 4881x 1x 1x 1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 36x 36x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x     27x 27x     27x 27x 27x 27x 27x 27x 27x 11x 1x 155x 155x 155x 155x 155x 155x 51x 51x 51x 51x 51x 51x 51x 51x 155x 27x 104x 77x 295x 295x 295x 295x 295x 295x 295x 295x 77x 155x 155x 155x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 90x 90x 16x 7x 16x 4x 4x 4x 18x 18x 18x 18x 18x 18x 4x 27x 27x 27x 27x 27x 295x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 295x 295x 295x 3x 3x 3x 3x 295x 295x 295x 295x 295x 295x 218x 218x 218x 295x 77x 10x 10x 295x 25x 2x 2x 25x 1x 1x 23x 4x 21x 4x 18x 3x 14x 3x 3x 25x 295x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 1x 26x 7x 25x   19x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 1x 1x 6x 6x 6x 6x 6x 1x 96x 96x 96x 96x 96x 96x 386x 386x 96x 96x 96x 96x 96x 96x 96x 96x 1x 4x 4x 4x 4x 4x 4x 4x 18x 18x 2x 2x 2x 2x 2x 2x 18x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 18x 7x 15x 3x 3x 18x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 13x 13x   1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 91x 91x 91x 9x 9x 8x 8x 8x 9x 1x 1x 91x 10x 10x 91x 9x 9x 9x 9x 9x 9x 9x 56x 25x 25x 3x 3x 3x 56x 8x 8x 6x 6x 8x 8x 56x 56x 56x 56x 56x 9x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 20x 20x 20x 6x 6x 6x 6x 6x 6x 20x 4x 4x 4x 4x 4x 14x 10x 10x 10x 10x 10x 10x 20x 20x 3x   1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 20x 20x                     20x 13x 3x 3x 13x 7x 7x 10x 3x 3x 3x 3x 13x 13x 20x 7x 1x 1x 1x 1x 1x 1x 7x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 7x 7x 7x 20x 3x 3x  
import { createStarryNight, all } from '@wooorm/starry-night';
import type { Root, RootContent } from 'hast';
import style from './dark-style.json';
 
const rules = new Map(
  Object.entries(style).map(([k, v]) => [k, new Map(Object.entries(v))]),
);
 
export function getColor(classList: string[]): string | undefined {
  console.assert(classList.length <= 1, `classList too long`);
  const styles =
    classList.length === 1 ? rules.get(`.${classList[0]}`) : new Map();
  console.assert((styles?.size ?? 0) <= 1, `more styles than just color`);
  const color = styles?.get('color');
  return color;
}
 
export type Parsable = [string, string] | { lang: string; code: string };
 
export interface Parsed<T extends Char = Char, L extends Line = Line> {
  language: string;
  lines: L[];
  chars: T[];
}
 
export interface Tokenized<T extends Token = Token, L extends Line = Line> {
  language: string;
  lines: L[];
  tokens: T[];
}
 
export interface Line {
  tags: string[];
  number?: number;
}
 
export interface Text {
  classList: string[];
  sections: string[];
}
 
export interface ColorCoded {
  color?: string;
  background?: string;
}
 
export interface Char extends Text {
  char: string;
  token: [number, number];
}
 
export interface RepChar extends Char {
  from: 'new' | 'old';
}
 
export interface FormChar extends Char {
  from: 'create' | 'keep' | 'delete';
}
 
export interface Token {
  token: string;
  location: [number, number];
}
 
export function parse(language: string, code: string): Parsed<Char> {
  if (starryNight == null) {
    throw new Error('you must await ready() to initialize package');
  }
  const scope = starryNight.flagToScope(language);
  if (typeof scope !== 'string') {
    throw new Error(`language ${language} not found`);
  }
  const parsed = starryNight.highlight(code, scope);
  const converted = recurse(parsed);
  return markTree({
    language,
    lines: [],
    chars: converted,
  });
}
 
function recurse(
  node: Root | RootContent,
  classes: string[] = [],
  result: Char[] = [],
) {
  if (node.type === 'element') {
    console.assert(node.tagName === 'span', `tag was not a span`);
    const className = (node.properties?.className ?? []) as string[];
    console.assert(Array.isArray(className), `className was not an array`);
    console.assert(className.length >= 1, `tag did not have a className`);
    console.assert(className.length <= 1, `tag had too many classNames`);
    node.children.forEach((child) =>
      recurse(child, [...classes, className[0]], result),
    );
  } else if (node.type === 'root') {
    node.children.forEach((child) => recurse(child, [], result));
  } else if (node.type === 'text') {
    for (let i = 0; i < node.value.length; i++) {
      console.assert(classes.length <= 1, `character has too many classes`);
      result.push({
        char: node.value[i],
        classList: classes,
        token: [i, node.value.length],
        sections: [],
      });
    }
  }
  return result;
}
 
const nextLineRegex = /^\/\/:[^\S\n]*next-line[^\S\n]+([^\n]+?)[^\S\n]*$/;
const thisLineRegex = /^\/\/:[^\S\n]*this-line[^\S\n]+([^\n]+?)[^\S\n]*$/;
const blockStartRegex = /^\/\/<<[^\S\n]*([^\n]+?)[^\S\n]*$/;
const blockEndRegex = /^\/\/>>[^\S\n]*$/;
const sectionStartRegex = /^\/\*<<[^\S\n]*([^\n]+?)[^\S\n]*\*\/$/;
const sectionEndRegex = /^\/\*>>[^\S\n]*\*\/$/;
const tagRegex = /^\/\*<[^\S\r\n]*(.*?)[^\S\r\n]*>\*\/$/;
 
const specialTypes: [RegExp, string[]][] = [
  [nextLineRegex, ['nextLine', 'line']],
  [thisLineRegex, ['thisLine', 'line']],
  [blockStartRegex, ['blockStart', 'line']],
  [blockEndRegex, ['blockEnd', 'line']],
  [sectionStartRegex, ['sectionStart', 'span']],
  [sectionEndRegex, ['sectionEnd', 'span']],
  [tagRegex, ['tag', 'span']],
];
function getSpecialType(span: string) {
  for (const [regex, result] of specialTypes) {
    if (regex.test(span)) return result;
  }
  return [];
}
 
function* spans(chars: Char[]): Generator<[Char[], number], void, void> {
  let i = 0;
  while (i < chars.length) {
    const char = chars[i];
    console.assert(char.token[0] === 0, `token was not beginning of span`);
    yield [chars.slice(i, i + char.token[1]), i];
    i += char.token[1];
  }
}
 
function markTree(parsed: Parsed<Char>): Parsed<Char> {
  const chars = parsed.chars;
  const lineCount =
    1 +
    chars.reduce((prior, { char }) => {
      return prior + (char === '\n' ? 1 : 0);
    }, 0);
  const lines: { tags: string[] }[] = new Array(lineCount)
    .fill(1)
    .map(() => ({ tags: [] }));
  let ln = 0;
  const blocks = [];
  const sections = [];
  let sectionHold = null;
  const result: Char[] = [];
  for (let i = 0; i < chars.length; i++) {
    const char = chars[i];
 
    if (char.token[0] === 0 && sectionHold != null) {
      sections.unshift(sectionHold);
      sectionHold = null;
    }
 
    result.push({
      ...char,
      sections: [...sections],
    });
 
    if (char.token[0] !== 0) {
      continue;
    }
 
    const span = getSpan(chars, i);
    if (char.char === '\n') {
      ln++;
      lines[ln].tags.push(...blocks);
    } else if (char.classList.length === 1 && char.classList[0] === 'pl-c') {
      if (nextLineRegex.test(span)) {
        const name = span.match(nextLineRegex)?.[1] as string;
        lines[ln + 1].tags.push(name);
      } else if (thisLineRegex.test(span)) {
        const name = span.match(thisLineRegex)?.[1] as string;
        lines[ln].tags.push(name);
      } else if (blockStartRegex.test(span)) {
        blocks.push(span.match(blockStartRegex)?.[1] as string);
      } else if (blockEndRegex.test(span)) {
        blocks.pop();
      } else if (sectionStartRegex.test(span)) {
        sectionHold = span.match(sectionStartRegex)?.[1] as string;
      } else if (sectionEndRegex.test(span)) {
        sections.shift();
      }
    }
  }
  return {
    ...parsed,
    lines,
    chars: result,
  };
}
 
/**
 * @internal
 * @param input - parsed or parsable variable
 * @returns parsed code
 */
export function ensureParsed(input: Parsed | Parsable): Parsed {
  if (Array.isArray(input)) {
    return parse(input[0], input[1]);
  } else if (typeof input === 'object' && 'code' in input) {
    return parse(input.lang, input.code);
  } else {
    return input as Parsed<Char>;
  }
}
 
let starryNight: {
  flagToScope: (s: string) => string | undefined;
  highlight: (c: string, s: string) => Root;
} | null = null;
const starryNightPromise = createStarryNight(all);
starryNightPromise.then((sn) => (starryNight = sn));
 
export function ready() {
  return starryNightPromise;
}
 
export function toString(code: Parsed<Char> | Parsable): string {
  const parsed = ensureParsed(code);
  const result: string[] = [];
  parsed.chars.forEach(({ char }) => result.push(char));
  return result.join('');
}
 
function getSpan(tree: Char[], at: number) {
  const [back, length] = tree[at].token;
  const start = at - back;
  const end = start + length;
  let result = '';
  for (let i = start; i < end; i++) {
    result += tree[i].char;
  }
  return result;
}
 
/**
 * Removes all code-fns tags.
 *
 * @param code - the parsed or parsable code to clean
 */
export function clean(code: Parsed<Char> | Parsable): Parsed<Char> {
  const parsed = ensureParsed(code);
  const result: Char[] = [];
  let lineNumber = 0;
  let finalNumber = 1;
  let lineSkip = false;
  const lines = [];
  for (const [span] of spans(parsed.chars)) {
    if (span.length === 1 && span[0].char === '\n') lineNumber++;
    if (lineSkip) {
      console.assert(
        span.length === 1 && span[0].char === '\n',
        `expected a new line`,
      );
      lineSkip = false;
      continue;
    } else if (span.length === 1 && span[0].char === '\n') {
      lines.push({
        ...parsed.lines[lineNumber],
        number: finalNumber,
      });
      finalNumber++;
    }
    console.assert(span[0].classList.length === 1, `multiple classes found`);
    const specialTypes = getSpecialType(
      span.reduce((text, { char }) => text + char, ''),
    );
    if (span[0].classList[0] !== 'pl-c' || specialTypes.length === 0) {
      result.push(...span);
    } else if (specialTypes.includes('line')) {
      lineSkip = true;
    }
  }
  lines.push({
    ...parsed.lines[lineNumber],
    number: finalNumber,
  });
  if (lineSkip) {
    lines.pop();
    console.assert(
      result[result.length - 1].char === '\n',
      `expected a new line`,
    );
    result.pop();
  }
  return {
    ...parsed,
    chars: result,
    lines,
  };
}
 
/**
 * @internal
 */
export function last<T>(arr: T[][]): T[] {
  if (arr.length === 0) arr.push([]);
  return arr[arr.length - 1];
}
 
export function substitute(
  code: Parsed<Char> | Parsable,
  subs: Record<string, string>,
): Parsed<RepChar> {
  const parsed = ensureParsed(code);
  const language = parsed.language;
  const tree = parsed.chars;
  const replacements: [number, number][] = [];
  let final = '';
  tree.forEach((char, at) => {
    if (char.token[0] !== 0) return;
    const span = getSpan(tree, at);
    if (char.classList[0] === 'pl-c' && tagRegex.test(span)) {
      const [, name] = span.match(tagRegex) as [string, string];
      if (name in subs) {
        const rep = subs[name];
        final += rep;
        if (rep !== '') replacements.push([at, rep.length]);
      } else {
        final += span;
      }
    } else {
      final += span;
    }
  });
  const reparsed = parse(language, final);
  let [r, ri] = [0, 0];
  let inReplacement = false;
  return {
    language,
    lines: [],
    chars: reparsed.chars.map((char: Char, at: number) => {
      if (inReplacement) {
        ri++;
        if (ri === replacements[r][1]) {
          inReplacement = false;
          r++;
        }
      } else if (r < replacements.length) {
        const [rat] = replacements[r];
        if (rat === at) {
          inReplacement = true;
        }
      }
 
      return {
        ...char,
        from: inReplacement ? 'new' : 'old',
      };
    }),
  };
}
 
export function transform(
  code: Parsed<Char> | Parsable,
  start: Record<string, string>,
  final: Record<string, string>,
): FormChar[] {
  const tree = ensureParsed(code);
  const before = substitute(tree, start);
  const after = substitute(tree, final);
  let [bat] = [0];
  let [aat] = [0];
  const chars: FormChar[] = [];
  while (bat < before.chars.length || aat < after.chars.length) {
    const bchar = before.chars[bat] ?? null;
    const achar = after.chars[aat] ?? null;
    if (bchar?.from === 'old' && achar?.from === 'old') {
      chars.push({
        ...achar,
        from: 'keep',
      });
      bat++;
      aat++;
    } else if (bchar?.from === 'new') {
      chars.push({
        ...bchar,
        from: 'delete',
      });
      bat++;
    } else if (achar?.from === 'new') {
      chars.push({
        ...achar,
        from: 'create',
      });
      aat++;
    }
  }
 
  return chars;
}
 
export interface Transition {
  delete: [string, [number, number], string?][];
  create: [string, [number, number], string?][];
  retain: [string, [number, number], [number, number], string?][];
}
 
export function transition(
  code: Parsed<Char> | Parsable,
  start: Record<string, string>,
  final: Record<string, string>,
): Transition {
  const tree = ensureParsed(code);
  const chars = transform(tree, start, final);
  const result: Transition = {
    delete: [],
    create: [],
    retain: [],
  };
  let [dln, dat] = [0, 0];
  let [cln, cat] = [0, 0];
  let lastColor: symbol | undefined | string = Symbol();
  let lastFrom: symbol | string = Symbol();
  chars.forEach((char) => {
    const color = getColor(char.classList);
    if (char.char === '\n') {
      if (char.from === 'keep' || char.from === 'create') {
        cln++;
        cat = 0;
      }
      if (char.from === 'keep' || char.from === 'delete') {
        dln++;
        dat = 0;
      }
      lastColor = Symbol();
      lastFrom = Symbol();
    } else if (color === lastColor && char.from === lastFrom) {
      if (char.from === 'delete') {
        last(result.delete)[0] += char.char;
        dat++;
      } else if (char.from === 'create') {
        last(result.create)[0] += char.char;
        cat++;
      } else if (char.from === 'keep') {
        last(result.retain)[0] += char.char;
        dat++;
        cat++;
      }
      lastFrom = char.from;
      lastColor = color;
    } else {
      if (char.from === 'delete') {
        result.delete.push([
          char.char,
          [dln, dat],
          ...((color ? [color] : []) as [string?]),
        ]);
        dat++;
      } else if (char.from === 'create') {
        result.create.push([
          char.char,
          [cln, cat],
          ...((color ? [color] : []) as [string?]),
        ]);
        cat++;
      } else if (char.from === 'keep') {
        result.retain.push([
          char.char,
          [dln, dat],
          [cln, cat],
          ...((color ? [color] : []) as [string?]),
        ]);
        dat++;
        cat++;
      }
      lastFrom = char.from;
      lastColor = color;
    }
  });
  return result;
}