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

feat: specify Astro.self type in TSX output #843

Merged
merged 2 commits into from
Aug 14, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-panthers-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Pass the type of the current component as a type argument to the AstroGlobal in order to type Astro.self
2 changes: 1 addition & 1 deletion internal/printer/print-to-tsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func renderTsx(p *printer, n *Node) {
*
* [Astro documentation](https://docs.astro.build/reference/api-reference/#astro-global)
*/
declare const Astro: Readonly<import('astro').AstroGlobal<%s>>`, props.Ident)
declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s>>`, props.Ident, componentName)
}
return
}
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler/test/tsx/props-and-staticPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { convertToTSX } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

const PREFIX = `/**
const PREFIX = (component: string = '__AstroComponent_') => `/**
* Astro global available in all contexts in .astro files
*
* [Astro documentation](https://docs.astro.build/reference/api-reference/#astro-global)
*/
declare const Astro: Readonly<import('astro').AstroGlobal<Props>>`;
declare const Astro: Readonly<import('astro').AstroGlobal<Props, typeof ${component}>>`;

test('no props', async () => {
const input = `---
Expand All @@ -29,7 +29,7 @@ export function getStaticProps() {
<div></div>
</Fragment>
export default function __AstroComponent_(_props: Props): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand Down
24 changes: 12 additions & 12 deletions packages/compiler/test/tsx/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { convertToTSX } from '@astrojs/compiler';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

const PREFIX = `/**
const PREFIX = (component: string = '__AstroComponent_') => `/**
* Astro global available in all contexts in .astro files
*
* [Astro documentation](https://docs.astro.build/reference/api-reference/#astro-global)
*/
declare const Astro: Readonly<import('astro').AstroGlobal<Props>>`;
declare const Astro: Readonly<import('astro').AstroGlobal<Props, typeof ${component}>>`;

test('no props', async () => {
const input = `<div></div>`;
Expand Down Expand Up @@ -48,7 +48,7 @@ interface Props {}

</Fragment>
export default function __AstroComponent_(_props: Props): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand All @@ -69,7 +69,7 @@ import { Props } from './somewhere';

</Fragment>
export default function __AstroComponent_(_props: Props): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand All @@ -90,7 +90,7 @@ import { MyComponent as Props } from './somewhere';

</Fragment>
export default function __AstroComponent_(_props: Props): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand All @@ -111,7 +111,7 @@ import type { Props } from './somewhere';

</Fragment>
export default function __AstroComponent_(_props: Props): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand All @@ -132,7 +132,7 @@ type Props = {}

</Fragment>
export default function Test__AstroComponent_(_props: Props): any {}
${PREFIX}`;
${PREFIX('Test__AstroComponent_')}`;
const { code } = await convertToTSX(input, { filename: '/Users/nmoo/test.astro', sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand All @@ -153,7 +153,7 @@ interface Props<T> {}

</Fragment>
export default function __AstroComponent_<T>(_props: Props<T>): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand All @@ -174,7 +174,7 @@ interface Props<T extends Other<{ [key: string]: any }>> {}

</Fragment>
export default function __AstroComponent_<T extends Other<{ [key: string]: any }>>(_props: Props<T>): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand All @@ -195,7 +195,7 @@ interface Props<T extends { [key: string]: any }, P extends string ? { [key: str

</Fragment>
export default function __AstroComponent_<T extends { [key: string]: any }, P extends string ? { [key: string]: any }: never>(_props: Props<T, P>): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand All @@ -216,7 +216,7 @@ interface Props<T extends Something<false> ? A : B, P extends string ? { [key: s

</Fragment>
export default function __AstroComponent_<T extends Something<false> ? A : B, P extends string ? { [key: string]: any }: never>(_props: Props<T, P>): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand All @@ -241,7 +241,7 @@ interface Props<Tag extends keyof JSX.IntrinsicElements> extends HTMLAttributes<

</Fragment>
export default function __AstroComponent_<Tag extends keyof JSX.IntrinsicElements>(_props: Props<Tag>): any {}
${PREFIX}`;
${PREFIX()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, `expected code to match snapshot`);
});
Expand Down