Home / File/ parseJS.ts — astro Source File

parseJS.ts — astro Source File

Architecture documentation for parseJS.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
  2208f162_1b21_d542_51aa_98ca1eda9260["parseJS.ts"]
  18a90012_a9fe_857c_0c4e_1491c91fd354["buildMappings.ts"]
  2208f162_1b21_d542_51aa_98ca1eda9260 --> 18a90012_a9fe_857c_0c4e_1491c91fd354
  83cae376_4307_8290_1999_12a3fa77a3a2["buildMappings"]
  2208f162_1b21_d542_51aa_98ca1eda9260 --> 83cae376_4307_8290_1999_12a3fa77a3a2
  81a20ac1_6143_16be_33ec_872bb8d3a54b["types"]
  2208f162_1b21_d542_51aa_98ca1eda9260 --> 81a20ac1_6143_16be_33ec_872bb8d3a54b
  040ca79b_dadf_4383_efd2_c0b13744e9f1["language-core"]
  2208f162_1b21_d542_51aa_98ca1eda9260 --> 040ca79b_dadf_4383_efd2_c0b13744e9f1
  240c179d_8195_bda1_51ea_09ccf72ff044["muggle-string"]
  2208f162_1b21_d542_51aa_98ca1eda9260 --> 240c179d_8195_bda1_51ea_09ccf72ff044
  style 2208f162_1b21_d542_51aa_98ca1eda9260 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { TSXExtractedScript } from '@astrojs/compiler/types';
import type { CodeInformation, VirtualCode } from '@volar/language-core';
import type { Segment } from 'muggle-string';
import { toString } from 'muggle-string';
import { buildMappings } from '../buildMappings';

export function extractScriptTags(scripts: TSXExtractedScript[]): VirtualCode[] {
	const embeddedJSCodes: VirtualCode[] = [];

	const moduleScripts = scripts
		.filter((script) => script.type === 'module' || script.type === 'processed-module')
		.map(moduleScriptToVirtualCode) satisfies VirtualCode[];

	const inlineScripts = scripts
		.filter(
			(script) =>
				// TODO: Change this at some point so that unknown scripts are not included
				// We can't guarantee that they are JavaScript, so we shouldn't treat them as such, even if it might work in some cases
				// Perhaps we should make it so that the user has to specify the language of the script if it's not a known type (ex: lang="js"), not sure.
				script.type === 'event-attribute' || script.type === 'inline' || script.type === 'unknown',
		)
		.sort((a, b) => a.position.start - b.position.start);

	embeddedJSCodes.push(...moduleScripts);
	const mergedJSContext = mergeJSContexts(inlineScripts);
	if (mergedJSContext) {
		embeddedJSCodes.push(mergedJSContext);
	}

	const JSONScripts = scripts
		.filter((script) => script.type === 'json')
		.map(jsonScriptToVirtualCode) satisfies VirtualCode[];
	embeddedJSCodes.push(...JSONScripts);

	return embeddedJSCodes;
}

function moduleScriptToVirtualCode(script: TSXExtractedScript, index: number): VirtualCode {
	let extension = 'mts';
	let languageId = 'typescript';
	if (script.type === 'module') {
		extension = 'mjs';
		languageId = 'javascript';
	}

	return {
		id: `${index}.${extension}`,
		languageId,
		snapshot: {
			getText: (start, end) => script.content.substring(start, end),
			getLength: () => script.content.length,
			getChangeRange: () => undefined,
		},
		mappings: [
			{
				sourceOffsets: [script.position.start],
				generatedOffsets: [0],
				lengths: [script.content.length],
				data: {
					verification: true,
// ... (86 more lines)

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does parseJS.ts do?
parseJS.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 parseJS.ts?
parseJS.ts defines 4 function(s): extractScriptTags, jsonScriptToVirtualCode, mergeJSContexts, moduleScriptToVirtualCode.
What does parseJS.ts depend on?
parseJS.ts imports 5 module(s): buildMappings, buildMappings.ts, language-core, muggle-string, types.
Where is parseJS.ts in the architecture?
parseJS.ts is located at packages/language-tools/language-server/src/core/parseJS.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