Skip to content

Commit

Permalink
fix: prefix type utils with ASTRO__
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Oct 4, 2023
1 parent 04f2cc3 commit f6d9524
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions internal/printer/print-to-tsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,18 @@ func renderTsx(p *printer, n *Node) {
propsIdent := props.Ident
paramsIdent := ""
if hasGetStaticPaths {
paramsIdent = "Get<InferredGetStaticPath, 'params'>"
paramsIdent = "ASTRO__Get<ASTRO__InferredGetStaticPath, 'params'>"
if propsIdent == "Record<string, any>" {
propsIdent = "Get<InferredGetStaticPath, 'props'>"
propsIdent = "ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>"
}
}

p.print(fmt.Sprintf("export default function %s%s(_props: %s%s): any {}\n", componentName, props.Statement, propsIdent, props.Generics))
if hasGetStaticPaths {
p.printf(`type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
type Flattened<T> = T extends Array<infer U> ? Flattened<U> : T;
type InferredGetStaticPath = Flattened<ArrayElement<Awaited<ReturnType<typeof getStaticPaths>>>>;
type Get<T, K> = T extends undefined ? undefined : K extends keyof T ? T[K] : never;%s`, "\n")
p.printf(`type ASTRO__ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
type ASTRO__Flattened<T> = T extends Array<infer U> ? ASTRO__Flattened<U> : T;
type ASTRO__InferredGetStaticPath = ASTRO__Flattened<ASTRO__ArrayElement<Awaited<ReturnType<typeof getStaticPaths>>>>;
type ASTRO__Get<T, K> = T extends undefined ? undefined : K extends keyof T ? T[K] : never;%s`, "\n")
}

if propsIdent != "Record<string, any>" {
Expand Down
16 changes: 8 additions & 8 deletions packages/compiler/test/tsx/props-and-getStaticPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { test } from 'uvu';
import * as assert from 'uvu/assert';

function getPrefix({
props = `Get<InferredGetStaticPath, 'props'>`,
props = `ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>`,
component = '__AstroComponent_',
params = `Get<InferredGetStaticPath, 'params'>`,
params = `ASTRO__Get<ASTRO__InferredGetStaticPath, 'params'>`,
}: {
props?: string;
component?: string;
Expand All @@ -16,14 +16,14 @@ function getPrefix({
*
* [Astro documentation](https://docs.astro.build/reference/api-reference/#astro-global)
*/
declare const Astro: Readonly<import('astro').AstroGlobal<${props}, typeof ${component}${params ? `, ${params}` : ''}>>`
declare const Astro: Readonly<import('astro').AstroGlobal<${props}, typeof ${component}${params ? `, ${params}` : ''}>>`;
}

function getSuffix() {
return `type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
type Flattened<T> = T extends Array<infer U> ? Flattened<U> : T;
type InferredGetStaticPath = Flattened<ArrayElement<Awaited<ReturnType<typeof getStaticPaths>>>>;
type Get<T, K> = T extends undefined ? undefined : K extends keyof T ? T[K] : never;`;
return `type ASTRO__ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
type ASTRO__Flattened<T> = T extends Array<infer U> ? ASTRO__Flattened<U> : T;
type ASTRO__InferredGetStaticPath = ASTRO__Flattened<ASTRO__ArrayElement<Awaited<ReturnType<typeof getStaticPaths>>>>;
type ASTRO__Get<T, K> = T extends undefined ? undefined : K extends keyof T ? T[K] : never;`;
}

test('explicit props definition', async () => {
Expand Down Expand Up @@ -69,7 +69,7 @@ export function getStaticPaths() {
"";<Fragment>
<div></div>
</Fragment>
export default function __AstroComponent_(_props: Get<InferredGetStaticPath, 'props'>): any {}
export default function __AstroComponent_(_props: ASTRO__Get<ASTRO__InferredGetStaticPath, 'props'>): any {}
${getSuffix()}
${getPrefix()}`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
Expand Down

0 comments on commit f6d9524

Please sign in to comment.