Skip to content
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
4 changes: 3 additions & 1 deletion packages/next-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
"test:unit": "vitest run --coverage",
"test:unit:watch": "vitest watch",
"test:size": "size-limit",
"test:types": "tsgo --noEmit",
"test:ts": "tsgo --noEmit",
"test:dist-types": "run emitDeclarations [ node ../../scripts/check-dist-app-routes.js dist/link.d.ts ]",
"test:types": "run -p test:ts test:dist-types",
"test": "run -p lint test:unit test:types"
},
"peerDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/preact-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
"test:unit": "vitest run --coverage",
"test:unit:watch": "vitest watch",
"test:size": "size-limit",
"test:types": "tsgo --noEmit",
"test:ts": "tsgo --noEmit",
"test:dist-types": "run emitDeclarations [ node ../../scripts/check-dist-app-routes.js dist/link.d.ts ]",
"test:types": "run -p test:ts test:dist-types",
"test": "run -p lint test:unit test:types"
},
"peerDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/preact-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
listenLinks
} from '@nano_kit/router'
import type {
LinkComponent,
LinkSettingsHook,
LinkProps,
LinkSettings
Expand All @@ -31,7 +32,7 @@ export type * from './link.types.js'
function createLinkComponent<R extends Routes>(
useSettings: () => LinkSettings,
usePaths: () => Paths<R>
) {
): LinkComponent<R> {
return function Link<K extends keyof R & string>(props: LinkProps<R, K>) {
const {
onClick: onClickProp,
Expand Down Expand Up @@ -150,7 +151,7 @@ export function LinkSettings$(): LinkSettings {
* Link component for navigation.
* Should be used inside injection context with navigation and paths provided.
*/
export const Link = /* @__PURE__ */ createLinkComponent<AppRoutes>(
export const Link: LinkComponent<AppRoutes> = /* @__PURE__ */ createLinkComponent<AppRoutes>(
() => useInject(LinkSettings$),
() => usePaths()
)
5 changes: 5 additions & 0 deletions packages/preact-router/src/link.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
AnchorHTMLAttributes,
ComponentChildren,
TargetedMouseEvent
} from 'preact'
import type {
Expand Down Expand Up @@ -54,6 +55,10 @@ export type LinkProps<R extends Routes, K extends keyof R & string> = AnchorHTML
href?: string
})

export type LinkComponent<R extends Routes> = <K extends keyof R & string>(
props: LinkProps<R, K>
) => ComponentChildren

export type LinkSettingsHook<S extends LinkSettings = LinkSettings> = (
props: Partial<LinkProps<Routes, string>>,
settings: S
Expand Down
4 changes: 3 additions & 1 deletion packages/react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
"test:unit": "vitest run --coverage",
"test:unit:watch": "vitest watch",
"test:size": "size-limit",
"test:types": "tsgo --noEmit",
"test:ts": "tsgo --noEmit",
"test:dist-types": "run emitDeclarations [ node ../../scripts/check-dist-app-routes.js dist/link.d.ts ]",
"test:types": "run -p test:ts test:dist-types",
"test": "run -p lint test:unit test:types"
},
"peerDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
listenLinks
} from '@nano_kit/router'
import type {
LinkComponent,
LinkSettingsHook,
LinkProps,
LinkSettings
Expand All @@ -34,7 +35,7 @@ export type * from './link.types.js'
function createLinkComponent<R extends Routes>(
useSettings: () => LinkSettings,
usePaths: () => Paths<R>
) {
): LinkComponent<R> {
return function Link<K extends keyof R & string>(props: LinkProps<R, K>) {
const {
onClick: onClickProp,
Expand Down Expand Up @@ -153,7 +154,7 @@ export function LinkSettings$(): LinkSettings {
* Link component for navigation.
* Should be used inside injection context with navigation and paths provided.
*/
export const Link = /* @__PURE__ */ createLinkComponent<AppRoutes>(
export const Link: LinkComponent<AppRoutes> = /* @__PURE__ */ createLinkComponent<AppRoutes>(
() => useInject(LinkSettings$),
() => usePaths()
)
7 changes: 6 additions & 1 deletion packages/react-router/src/link.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
AnchorHTMLAttributes,
MouseEvent
MouseEvent,
ReactNode
} from 'react'
import type {
Routes,
Expand Down Expand Up @@ -54,6 +55,10 @@ export type LinkProps<R extends Routes, K extends keyof R & string> = AnchorHTML
href?: string
})

export type LinkComponent<R extends Routes> = <K extends keyof R & string>(
props: LinkProps<R, K>
) => ReactNode

export type LinkSettingsHook<S extends LinkSettings = LinkSettings> = (
props: Partial<LinkProps<Routes, string>>,
settings: S
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"test:unit:watch": "vitest watch",
"test:ts": "tsgo --noEmit",
"test:svelte": "svelte-check --tsconfig ./tsconfig.json",
"test:types": "run -p test:ts test:svelte",
"test:dist-types": "run build:dist [ node ../../scripts/check-dist-app-routes.js dist/link.d.ts ]",
"test:types": "run -p test:ts test:svelte test:dist-types",
"test": "run -p lint test:unit test:types"
},
"peerDependencies": {
Expand Down
10 changes: 6 additions & 4 deletions packages/svelte-router/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { inject as storeInject } from '@nano_kit/store'
import { getInject } from '@nano_kit/svelte'
import {
type AppRoutes,
type Navigation,
type Paths,
type Routes,
Expand All @@ -19,23 +20,24 @@ import {
onLinkClick
} from '@nano_kit/router'
import type {
LinkComponent,
LinkProps,
LinkSettings,
LinkSettingsHook
} from './link.types.js'
import LinkComponent from './LinkComponent.svelte'
import LinkView from './LinkComponent.svelte'

export type * from './link.types.js'

/* @__NO_SIDE_EFFECTS__ */
function createLinkComponent<R extends Routes>(
useSettings: () => LinkSettings,
usePaths: () => Paths<R>
) {
): LinkComponent<R> {
return <K extends keyof R & string>(
internals: ComponentInternals,
props: LinkProps<R, K>
) => LinkComponent(internals, Object.assign(props, {
) => LinkView(internals, Object.assign(props, {
// Suppress conflict with AppRoutes
paths: usePaths() as unknown as Paths<Routes>,
settings: useSettings()
Expand Down Expand Up @@ -113,7 +115,7 @@ export function LinkSettings$(): LinkSettings {
* Link component for navigation.
* Should be used inside injection context with navigation and paths provided.
*/
export const Link = /* @__PURE__ */ createLinkComponent(
export const Link: LinkComponent<AppRoutes> = /* @__PURE__ */ createLinkComponent<AppRoutes>(
() => getInject(LinkSettings$),
() => getInject(Paths$)
)
10 changes: 9 additions & 1 deletion packages/svelte-router/src/link.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Snippet } from 'svelte'
import type {
ComponentInternals,
Snippet
} from 'svelte'
import type {
HTMLAnchorAttributes,
MouseEventHandler
Expand Down Expand Up @@ -60,6 +63,11 @@ export type LinkProps<R extends Routes, K extends keyof R & string> = AnchorAttr
href?: string
})

export type LinkComponent<R extends Routes> = <K extends keyof R & string>(
internals: ComponentInternals,
props: LinkProps<R, K>
) => object

export type LinkSettingsHook<S extends LinkSettings = LinkSettings> = (
props: Readable<Partial<LinkProps<Routes, string>>>,
settings: S
Expand Down
18 changes: 18 additions & 0 deletions scripts/check-dist-app-routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'

const [file] = process.argv.slice(2)

if (!file) {
throw new Error('Expected a declaration file path.')
}

const path = resolve(process.cwd(), file)
const content = readFileSync(path, 'utf8')

if (
!content.includes('LinkComponent<AppRoutes>')
&& !content.includes('LinkProps<AppRoutes')
) {
throw new Error(`${file} does not preserve AppRoutes in public Link types.`)
}
11 changes: 11 additions & 0 deletions scripts/oxlint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from '@trigen/oxlint'
import rootConfig from '../oxlint.config.ts'

export default defineConfig({
extends: [
rootConfig
],
env: {
node: true
}
})