Home / File/ printer.ts — astro Source File

printer.ts — astro Source File

Architecture documentation for printer.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 2 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  7d34d289_ea0e_ec1c_780b_4c297e3a4f60["printer.ts"]
  dd6187d6_53c4_ce90_a1d1_3a0b5e7e7d3f["../../core/errors/errors.js"]
  7d34d289_ea0e_ec1c_780b_4c297e3a4f60 --> dd6187d6_53c4_ce90_a1d1_3a0b5e7e7d3f
  91e2aae7_9271_3bbb_4d0e_63fd50e547d0["./utils.js"]
  7d34d289_ea0e_ec1c_780b_4c297e3a4f60 --> 91e2aae7_9271_3bbb_4d0e_63fd50e547d0
  style 7d34d289_ea0e_ec1c_780b_4c297e3a4f60 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { ErrorLocation } from './errors.js';
import { normalizeLF } from './utils.js';

/** Generate a code frame from string and an error location */
export function codeFrame(src: string, loc: ErrorLocation): string {
	if (!loc || loc.line === undefined || loc.column === undefined) {
		return '';
	}
	const lines = normalizeLF(src)
		.split('\n')
		.map((ln) => ln.replace(/\t/g, '  '));
	// grab 2 lines before, and 3 lines after focused line
	const visibleLines = [];
	for (let n = -2; n <= 2; n++) {
		if (lines[loc.line + n]) visibleLines.push(loc.line + n);
	}
	// figure out gutter width
	let gutterWidth = 0;
	for (const lineNo of visibleLines) {
		let w = `> ${lineNo}`;
		if (w.length > gutterWidth) gutterWidth = w.length;
	}
	// print lines
	let output = '';
	for (const lineNo of visibleLines) {
		const isFocusedLine = lineNo === loc.line - 1;
		output += isFocusedLine ? '> ' : '  ';
		output += `${lineNo + 1} | ${lines[lineNo]}\n`;
		if (isFocusedLine)
			output += `${Array.from({ length: gutterWidth }).join(' ')}  | ${Array.from({
				length: loc.column,
			}).join(' ')}^\n`;
	}
	return output;
}

Domain

Subdomains

Functions

Dependencies

  • ../../core/errors/errors.js
  • ./utils.js

Frequently Asked Questions

What does printer.ts do?
printer.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in printer.ts?
printer.ts defines 1 function(s): codeFrame.
What does printer.ts depend on?
printer.ts imports 2 module(s): ../../core/errors/errors.js, ./utils.js.
Where is printer.ts in the architecture?
printer.ts is located at packages/astro/src/core/errors/printer.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core/errors).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free