Home / File/ compile_diagnostic.js — svelte Source File

compile_diagnostic.js — svelte Source File

Architecture documentation for compile_diagnostic.js, a javascript file in the svelte codebase. 1 imports, 3 dependents.

File javascript Compiler Migrator 1 imports 3 dependents 2 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  266f0f82_b1c9_08bb_80b9_55a66e7d3dba["compile_diagnostic.js"]
  62f818c8_e890_17ed_5ec1_92f953d4a7a6["state.js"]
  266f0f82_b1c9_08bb_80b9_55a66e7d3dba --> 62f818c8_e890_17ed_5ec1_92f953d4a7a6
  495501a4_a342_6a4d_ac11_e3e2fee8b218["errors.js"]
  495501a4_a342_6a4d_ac11_e3e2fee8b218 --> 266f0f82_b1c9_08bb_80b9_55a66e7d3dba
  d1a9b9b5_cc04_9500_c39a_435c606b0766["index.d.ts"]
  d1a9b9b5_cc04_9500_c39a_435c606b0766 --> 266f0f82_b1c9_08bb_80b9_55a66e7d3dba
  56a689f9_11c0_cc76_bd60_41bb6dc96475["warnings.js"]
  56a689f9_11c0_cc76_bd60_41bb6dc96475 --> 266f0f82_b1c9_08bb_80b9_55a66e7d3dba
  style 266f0f82_b1c9_08bb_80b9_55a66e7d3dba fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { Location } from 'locate-character' */
import * as state from '../state.js';

const regex_tabs = /^\t+/;

/**
 * @param {string} str
 */
function tabs_to_spaces(str) {
	return str.replace(regex_tabs, (match) => match.split('\t').join('  '));
}

/**
 * @param {string} source
 * @param {number} line
 * @param {number} column
 */
function get_code_frame(source, line, column) {
	const lines = source.split('\n');
	const frame_start = Math.max(0, line - 2);
	const frame_end = Math.min(line + 3, lines.length);
	const digits = String(frame_end + 1).length;
	return lines
		.slice(frame_start, frame_end)
		.map((str, i) => {
			const is_error_line = frame_start + i === line;
			const line_num = String(i + frame_start + 1).padStart(digits, ' ');
			if (is_error_line) {
				const indicator =
					' '.repeat(digits + 2 + tabs_to_spaces(str.slice(0, column)).length) + '^';
				return `${line_num}: ${tabs_to_spaces(str)}\n${indicator}`;
			}
			return `${line_num}: ${tabs_to_spaces(str)}`;
		})
		.join('\n');
}

/**
 * @typedef {{
 * 	code: string;
 * 	message: string;
 *  stack?: string;
 * 	filename?: string;
 * 	start?: Location;
 * 	end?: Location;
 * 	position?: [number, number];
 * 	frame?: string;
 * }} ICompileDiagnostic
 */

/** @implements {ICompileDiagnostic} */
export class CompileDiagnostic {
	name = 'CompileDiagnostic';

	/**
	 * @param {string} code
	 * @param {string} message
	 * @param {[number, number] | undefined} position
	 */
	constructor(code, message, position) {
		this.code = code;
		this.message = message;

		if (state.filename !== state.UNKNOWN_FILENAME) {
			this.filename = state.filename;
		}

		if (position) {
			this.position = position;
			this.start = state.locator(position[0]);
			this.end = state.locator(position[1]);
			if (this.start && this.end) {
				this.frame = get_code_frame(state.source, this.start.line - 1, this.end.column);
			}
		}
	}

	toString() {
		let out = `${this.code}: ${this.message}`;

		if (this.filename) {
			out += `\n${this.filename}`;

			if (this.start) {
				out += `:${this.start.line}:${this.start.column}`;
			}
		}

		if (this.frame) {
			out += `\n${this.frame}`;
		}

		return out;
	}

	toJSON() {
		return {
			code: this.code,
			message: this.message,
			filename: this.filename,
			start: this.start,
			end: this.end,
			position: this.position,
			frame: this.frame
		};
	}
}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does compile_diagnostic.js do?
compile_diagnostic.js is a source file in the svelte codebase, written in javascript. It belongs to the Compiler domain, Migrator subdomain.
What functions are defined in compile_diagnostic.js?
compile_diagnostic.js defines 2 function(s): get_code_frame, tabs_to_spaces.
What does compile_diagnostic.js depend on?
compile_diagnostic.js imports 1 module(s): state.js.
What files import compile_diagnostic.js?
compile_diagnostic.js is imported by 3 file(s): errors.js, index.d.ts, warnings.js.
Where is compile_diagnostic.js in the architecture?
compile_diagnostic.js is located at packages/svelte/src/compiler/utils/compile_diagnostic.js (domain: Compiler, subdomain: Migrator, directory: packages/svelte/src/compiler/utils).

Analyze Your Own Codebase

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

Try Supermodel Free