Skip to content

Commit

Permalink
Patch for Astro VS Code Plugin type errors (#10562)
Browse files Browse the repository at this point in the history
* Update Code.astro

Adding 'as any' to suppress Astro VS Code extension complaining

* Changes to Picture.astro to stop Astro VS Code extension complaining

* Couple more changes for supressing type errors

* fix(types): Fixes type errors in component properly

* chore: changeset

---------

Co-authored-by: Princesseuh <3019731+Princesseuh@users.noreply.github.com>
  • Loading branch information
apetta and Princesseuh authored Mar 26, 2024
1 parent b5a8040 commit 348c1ca
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
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,
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;
}

0 comments on commit 348c1ca

Please sign in to comment.