Home / File/ diagnostics.ts — astro Source File

diagnostics.ts — astro Source File

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

File typescript CoreAstro RoutingSystem 1 imports 6 functions

Entity Profile

Dependency Diagram

graph LR
  eb2f7d36_9d97_e74c_0d2e_fc25c2af688f["diagnostics.ts"]
  6857b6b2_4d48_bfb0_0a0e_8e2e52fabb56["language-server"]
  eb2f7d36_9d97_e74c_0d2e_fc25c2af688f --> 6857b6b2_4d48_bfb0_0a0e_8e2e52fabb56
  style eb2f7d36_9d97_e74c_0d2e_fc25c2af688f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { Diagnostic } from '@volar/language-server';

// List of codes:
// https://github.com/Microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json
export enum DiagnosticCodes {
	IS_NOT_A_MODULE = 2306, // '{0}' is not a module.
	CANNOT_FIND_MODULE = 2307, // Cannot find module '{0}' or its corresponding type declarations.
	DUPLICATED_JSX_ATTRIBUTES = 17001, // JSX elements cannot have multiple attributes with the same name.
	CANT_RETURN_OUTSIDE_FUNC = 1108, // A 'return' statement can only be used within a function body.
	ISOLATED_MODULE_COMPILE_ERR = 1208, // '{0}' cannot be compiled under '--isolatedModules' because it is considered a global script file.
	TYPE_NOT_ASSIGNABLE = 2322, // Type '{0}' is not assignable to type '{1}'.
	JSX_NO_CLOSING_TAG = 17008, // JSX element '{0}' has no corresponding closing tag.
	JSX_ELEMENT_NO_CALL = 2604, // JSX element type '{0}' does not have any construct or call signatures.
}

export function enhancedProvideSemanticDiagnostics(
	originalDiagnostics: Diagnostic[],
	tsxLineCount?: number,
) {
	const diagnostics = originalDiagnostics
		.filter(
			(diagnostic) =>
				(tsxLineCount ? diagnostic.range.start.line <= tsxLineCount : true) &&
				isNoCantReturnOutsideFunction(diagnostic) &&
				isNoIsolatedModuleError(diagnostic) &&
				isNoJsxCannotHaveMultipleAttrsError(diagnostic),
		)
		.map((diag) =>
			tsxLineCount ? generalEnhancements(astroEnhancements(diag)) : generalEnhancements(diag),
		);

	return diagnostics;
}

// General enhancements that apply to all files
function generalEnhancements(diagnostic: Diagnostic) {
	if (
		diagnostic.code === DiagnosticCodes.CANNOT_FIND_MODULE &&
		diagnostic.message.includes('astro:content')
	) {
		diagnostic.message +=
			"\n\nIf you're using content collections, make sure to run `astro dev`, `astro build` or `astro sync` to first generate the types so you can import from them. If you already ran one of those commands, restarting the language server might be necessary in order for the change to take effect.";

		return diagnostic;
	}

	return diagnostic;
}

/**
 * Astro-specific enhancements. For instance, when the user tries to import a component from a framework that is not installed
 * or a difference with JSX needing a different error message
 */
function astroEnhancements(diagnostic: Diagnostic): Diagnostic {
	// When the language integrations are not installed, the content of the imported snapshot is empty
	// As such, it triggers the "is not a module error", which we can enhance with a more helpful message for the related framework
	if (diagnostic.code === DiagnosticCodes.IS_NOT_A_MODULE) {
		if (diagnostic.message.includes('.svelte')) {
			diagnostic.message +=
				'\n\nIs the `@astrojs/svelte` package installed? You can add it to your project by running the following command: `astro add svelte`. If already installed, restarting the language server might be necessary in order for the change to take effect.';
// ... (72 more lines)

Domain

Subdomains

Dependencies

  • language-server

Frequently Asked Questions

What does diagnostics.ts do?
diagnostics.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in diagnostics.ts?
diagnostics.ts defines 6 function(s): astroEnhancements, enhancedProvideSemanticDiagnostics, generalEnhancements, isNoCantReturnOutsideFunction, isNoIsolatedModuleError, isNoJsxCannotHaveMultipleAttrsError.
What does diagnostics.ts depend on?
diagnostics.ts imports 1 module(s): language-server.
Where is diagnostics.ts in the architecture?
diagnostics.ts is located at packages/language-tools/language-server/src/plugins/typescript/diagnostics.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/language-tools/language-server/src/plugins/typescript).

Analyze Your Own Codebase

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

Try Supermodel Free