Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch for Astro VS Code Plugin type errors #10562

Merged
merged 6 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/breezy-peaches-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes minor type issues inside the built-in components of Astro
4 changes: 2 additions & 2 deletions packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const highlighter = await getCachedHighlighter({
? Object.keys(bundledLanguages).includes(lang)
? lang
: 'plaintext'
: lang,
: (lang as any),
],
theme,
themes,
Expand All @@ -89,7 +89,7 @@ const highlighter = await getCachedHighlighter({

const html = highlighter.highlight(code, typeof lang === 'string' ? lang : lang.name, {
inline,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers, those cast look suspicious, but there's actually already a bunch of said casts in the file. I don't really know why, perhaps @bluwy has more context having worked on Shiki recently?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, not sure what happened, GitHub moved my comment.. but I'm talking about the as any casts on lang.

attributes: rest,
attributes: rest as any,
});
---

Expand Down
17 changes: 12 additions & 5 deletions packages/astro/components/Picture.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
import { getImage, type LocalImageProps, type RemoteImageProps } from 'astro:assets';
import type { GetImageResult, ImageOutputFormat } from '../dist/@types/astro';
import { isESMImportedImage } from '../dist/assets/utils/imageKind';
import { isESMImportedImage, resolveSrc } from '../dist/assets/utils/imageKind';
import { AstroError, AstroErrorData } from '../dist/core/errors/index.js';
import type { HTMLAttributes } from '../types';

Expand All @@ -27,20 +27,27 @@ if (props.alt === undefined || props.alt === null) {
throw new AstroError(AstroErrorData.ImageMissingAlt);
}

const originalSrc = await resolveSrc(props.src);
const optimizedImages: GetImageResult[] = await Promise.all(
formats.map(
async (format) =>
await getImage({ ...props, format: format, widths: props.widths, densities: props.densities })
await getImage({
...props,
src: originalSrc,
format: format,
widths: props.widths,
densities: props.densities,
})
)
);

let resultFallbackFormat = fallbackFormat ?? defaultFallbackFormat;
if (
!fallbackFormat &&
isESMImportedImage(props.src) &&
specialFormatsFallback.includes(props.src.format)
isESMImportedImage(originalSrc) &&
originalSrc.format in specialFormatsFallback
) {
resultFallbackFormat = props.src.format;
resultFallbackFormat = originalSrc.format;
}

const fallbackImage = await getImage({
Expand Down
7 changes: 2 additions & 5 deletions packages/astro/src/assets/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
SrcSetValue,
UnresolvedImageTransform,
} from './types.js';
import { isESMImportedImage, isRemoteImage } from './utils/imageKind.js';
import { isESMImportedImage, isRemoteImage, resolveSrc } from './utils/imageKind.js';
import { probe } from './utils/remoteProbe.js';

export async function getConfiguredImageService(): Promise<ImageService> {
Expand Down Expand Up @@ -56,10 +56,7 @@ export async function getImage(
// If the user inlined an import, something fairly common especially in MDX, or passed a function that returns an Image, await it for them
const resolvedOptions: ImageTransform = {
...options,
src:
typeof options.src === 'object' && 'then' in options.src
? (await options.src).default ?? (await options.src)
: options.src,
src: await resolveSrc(options.src),
};

// Infer size for remote images if inferSize is true
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/assets/utils/imageKind.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ImageMetadata } from '../types.js';
import type { ImageMetadata, UnresolvedImageTransform } from '../types.js';

export function isESMImportedImage(src: ImageMetadata | string): src is ImageMetadata {
return typeof src === 'object';
Expand All @@ -7,3 +7,7 @@ export function isESMImportedImage(src: ImageMetadata | string): src is ImageMet
export function isRemoteImage(src: ImageMetadata | string): src is string {
return typeof src === 'string';
}

export async function resolveSrc(src: UnresolvedImageTransform['src']) {
return typeof src === 'object' && 'then' in src ? (await src).default ?? (await src) : src;
}