Home / File/ utils.ts — astro Source File

utils.ts — astro Source File

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

File typescript CoreAstro CoreMiddleware 5 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  6db4d7db_edbb_c18d_7ff5_decb13f1c284["utils.ts"]
  e0c225c9_e989_934d_19e8_8e80b7a6857f["importPackage.ts"]
  6db4d7db_edbb_c18d_7ff5_decb13f1c284 --> e0c225c9_e989_934d_19e8_8e80b7a6857f
  f3e7462c_8189_532c_8f10_1698b2251467["importSvelteIntegration"]
  6db4d7db_edbb_c18d_7ff5_decb13f1c284 --> f3e7462c_8189_532c_8f10_1698b2251467
  80d479e1_14bf_c8a1_058f_a3221a7da169["importVueIntegration"]
  6db4d7db_edbb_c18d_7ff5_decb13f1c284 --> 80d479e1_14bf_c8a1_058f_a3221a7da169
  040ca79b_dadf_4383_efd2_c0b13744e9f1["language-core"]
  6db4d7db_edbb_c18d_7ff5_decb13f1c284 --> 040ca79b_dadf_4383_efd2_c0b13744e9f1
  abeb339e_cdba_0d7f_6b7f_3696c1eb86f9["vscode-uri"]
  6db4d7db_edbb_c18d_7ff5_decb13f1c284 --> abeb339e_cdba_0d7f_6b7f_3696c1eb86f9
  style 6db4d7db_edbb_c18d_7ff5_decb13f1c284 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { VirtualCode } from '@volar/language-core';
import { URI, Utils } from 'vscode-uri';
import { importSvelteIntegration, importVueIntegration } from '../importPackage';

export function framework2tsx(
	filePath: string,
	sourceCode: string,
	framework: 'vue' | 'svelte',
): VirtualCode {
	const integrationEditorEntrypoint =
		framework === 'vue' ? importVueIntegration(filePath) : importSvelteIntegration(filePath);

	if (!integrationEditorEntrypoint) {
		const EMPTY_FILE = '';
		return getVirtualCode(EMPTY_FILE);
	}

	const className = classNameFromFilename(filePath);
	const tsx = patchTSX(integrationEditorEntrypoint.toTSX(sourceCode, className), filePath);

	return getVirtualCode(tsx);

	function getVirtualCode(content: string): VirtualCode {
		return {
			id: 'tsx',
			languageId: 'typescript',
			snapshot: {
				getText: (start, end) => content.substring(start, end),
				getLength: () => content.length,
				getChangeRange: () => undefined,
			},
			mappings: [],
			embeddedCodes: [],
		};
	}
}

/**
 * Transform a string into PascalCase
 */
function toPascalCase(string: string) {
	return `${string}`
		.replace(new RegExp(/[-_]+/, 'g'), ' ')
		.replace(new RegExp(/[^\w\s]/, 'g'), '')
		.replace(
			new RegExp(/\s+(.)(\w*)/, 'g'),
			(_, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`,
		)
		.replace(new RegExp(/\w/), (s) => s.toUpperCase());
}

export function classNameFromFilename(filename: string): string {
	const url = URI.parse(filename);
	const withoutExtensions = Utils.basename(url).slice(0, -Utils.extname(url).length);

	const withoutInvalidCharacters = withoutExtensions
		.split('')
		// Although "-" is invalid, we leave it in, pascal-case-handling will throw it out later
		.filter((char) => /[\w$-]/.test(char))
		.join('');
	const firstValidCharIdx = withoutInvalidCharacters
		.split('')
		// Although _ and $ are valid first characters for classes, they are invalid first characters
		// for tag names. For a better import autocompletion experience, we therefore throw them out.
		.findIndex((char) => /[A-Za-z]/.test(char));

	const withoutLeadingInvalidCharacters = withoutInvalidCharacters.substring(firstValidCharIdx);
	const inPascalCase = toPascalCase(withoutLeadingInvalidCharacters);
	const finalName = firstValidCharIdx === -1 ? `A${inPascalCase}` : inPascalCase;

	return finalName;
}

// TODO: Patch the upstream packages with these changes
export function patchTSX(code: string, filePath: string) {
	const basename = filePath.split('/').pop()!;
	const isDynamic = basename.startsWith('[') && basename.endsWith(']');

	return code.replace(/\b(\S*)__AstroComponent_/g, (fullMatch, m1: string) => {
		// If we don't have a match here, it usually means the file has a weird name that couldn't be expressed with valid identifier characters
		if (!m1) {
			if (basename === '404') return 'FourOhFour';
			return fullMatch;
		}
		return isDynamic ? `_${m1}_` : m1[0].toUpperCase() + m1.slice(1);
	});
}

Domain

Subdomains

Frequently Asked Questions

What does utils.ts do?
utils.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in utils.ts?
utils.ts defines 4 function(s): classNameFromFilename, framework2tsx, patchTSX, toPascalCase.
What does utils.ts depend on?
utils.ts imports 5 module(s): importPackage.ts, importSvelteIntegration, importVueIntegration, language-core, vscode-uri.
Where is utils.ts in the architecture?
utils.ts is located at packages/language-tools/language-server/src/core/utils.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/language-tools/language-server/src/core).

Analyze Your Own Codebase

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

Try Supermodel Free