Home / Function/ addAstroTypes() — astro Function Reference

addAstroTypes() — astro Function Reference

Architecture documentation for the addAstroTypes() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  d72a5db8_63e3_e182_73db_b064b7f43f71["addAstroTypes()"]
  3d0a8903_cb4c_d139_320b_9571e3568661["index.ts"]
  d72a5db8_63e3_e182_73db_b064b7f43f71 -->|defined in| 3d0a8903_cb4c_d139_320b_9571e3568661
  style d72a5db8_63e3_e182_73db_b064b7f43f71 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/language-tools/language-server/src/core/index.ts lines 24–92

export function addAstroTypes(
	astroInstall: PackageInfo | undefined,
	ts: typeof import('typescript'),
	host: ts.LanguageServiceHost,
) {
	if (decoratedHosts.has(host)) {
		return;
	}
	decoratedHosts.add(host);

	const getScriptFileNames = host.getScriptFileNames.bind(host);
	const getCompilationSettings = host.getCompilationSettings.bind(host);

	host.getScriptFileNames = () => {
		const languageServerTypesDirectory = getLanguageServerTypesDir(ts);
		const fileNames = getScriptFileNames();
		const addedFileNames = [];

		if (astroInstall) {
			addedFileNames.push(
				...['./env.d.ts', './astro-jsx.d.ts'].map((filePath) =>
					ts.sys.resolvePath(path.resolve(astroInstall.directory, filePath)),
				),
			);

			// If Astro version is < 4.0.8, add jsx-runtime-augment.d.ts to the files to fake `JSX` being available from "astro/jsx-runtime".
			// TODO: Remove this once a majority of users are on Astro 4.0.8+, erika - 2023-12-28
			if (
				astroInstall.version.major < 4 ||
				(astroInstall.version.major === 4 &&
					astroInstall.version.minor === 0 &&
					astroInstall.version.patch < 8)
			) {
				addedFileNames.push(
					...['./jsx-runtime-augment.d.ts'].map((filePath) =>
						ts.sys.resolvePath(path.resolve(languageServerTypesDirectory, filePath)),
					),
				);
			}
		} else {
			// If we don't have an Astro installation, add the fallback types from the language server.
			// See the README in packages/language-server/types for more information.
			addedFileNames.push(
				...['./env.d.ts', './astro-jsx.d.ts', './jsx-runtime-fallback.d.ts'].map((f) =>
					ts.sys.resolvePath(path.resolve(languageServerTypesDirectory, f)),
				),
			);
		}

		return [...fileNames, ...addedFileNames];
	};
	host.getCompilationSettings = () => {
		const baseCompilationSettings = getCompilationSettings();
		return {
			...baseCompilationSettings,
			module: ts.ModuleKind.ESNext ?? 99,
			target: ts.ScriptTarget.ESNext ?? 99,
			jsx: ts.JsxEmit.Preserve ?? 1,
			resolveJsonModule: true,
			allowJs: true, // Needed for inline scripts, which are virtual .js files
			isolatedModules: true,
			moduleResolution:
				baseCompilationSettings.moduleResolution === ts.ModuleResolutionKind.Classic ||
				!baseCompilationSettings.moduleResolution
					? ts.ModuleResolutionKind.Node10
					: baseCompilationSettings.moduleResolution,
		};
	};
}

Domain

Subdomains

Frequently Asked Questions

What does addAstroTypes() do?
addAstroTypes() is a function in the astro codebase, defined in packages/language-tools/language-server/src/core/index.ts.
Where is addAstroTypes() defined?
addAstroTypes() is defined in packages/language-tools/language-server/src/core/index.ts at line 24.

Analyze Your Own Codebase

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

Try Supermodel Free