@tsrx/hono 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +73 -0
- package/package.json +60 -0
- package/src/compiler.js +129 -0
- package/src/dom/error-boundary.js +1 -0
- package/src/dom.js +9 -0
- package/src/error-boundary.js +1 -0
- package/src/index.js +7 -0
- package/src/platform.js +431 -0
- package/src/ref.js +1 -0
- package/src/runtime/iterable.js +1 -0
- package/types/dom/error-boundary.d.ts +1 -0
- package/types/dom.d.ts +12 -0
- package/types/dynamic.d.ts +31 -0
- package/types/error-boundary.d.ts +1 -0
- package/types/index.d.ts +12 -0
- package/types/ref.d.ts +1 -0
- package/types/runtime/iterable.d.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dominic Gannaway
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# @tsrx/hono
|
|
2
|
+
|
|
3
|
+
Compile TSRX to Hono JSX for server rendering or the browser DOM renderer.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add hono @tsrx/hono
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Choose a compiler mode
|
|
10
|
+
|
|
11
|
+
| Mode | Compiler | JSX import source |
|
|
12
|
+
| ------ | ---------------- | ----------------- |
|
|
13
|
+
| Server | `@tsrx/hono` | `hono/jsx` |
|
|
14
|
+
| DOM | `@tsrx/hono/dom` | `hono/jsx/dom` |
|
|
15
|
+
|
|
16
|
+
Both modes support TSRX template blocks, conditional and list rendering, switch
|
|
17
|
+
statements, dynamic tags, sibling-scoped styles, themes, and error boundaries.
|
|
18
|
+
Hono handles rendering, hooks, context, and Suspense through its own runtime APIs.
|
|
19
|
+
Authored `class` attributes are preserved.
|
|
20
|
+
|
|
21
|
+
Server components may be async and use top-level `await`. DOM components must be
|
|
22
|
+
synchronous; use Hono's `use(promise)` with a pending boundary for asynchronous
|
|
23
|
+
work. `@catch (error)` is supported, but Hono does not provide the reset callback
|
|
24
|
+
used by some other targets.
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { use } from 'hono/jsx';
|
|
28
|
+
|
|
29
|
+
const profilePromise = Promise.resolve({ name: 'Ada' });
|
|
30
|
+
|
|
31
|
+
function Profile() @{
|
|
32
|
+
const profile = use(profilePromise);
|
|
33
|
+
<p>Hello, {profile.name}!</p>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function App() @{
|
|
37
|
+
@try {
|
|
38
|
+
<Profile />
|
|
39
|
+
} @pending {
|
|
40
|
+
<p>Loading profile...</p>
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The DOM validator diagnoses explicit async component references that it can
|
|
46
|
+
resolve statically within the module. It does not infer arbitrary Promise returns,
|
|
47
|
+
inter-module values, or object members that may have been overwritten by unknown
|
|
48
|
+
spreads or computed properties.
|
|
49
|
+
|
|
50
|
+
## Build integrations
|
|
51
|
+
|
|
52
|
+
Use [@tsrx/vite-plugin-hono](../vite-plugin-hono/README.md) or
|
|
53
|
+
[@tsrx/bun-plugin-hono](../bun-plugin-hono/README.md) with an explicit
|
|
54
|
+
`mode: 'server'` or `mode: 'dom'`. Separate server and browser build graphs rather
|
|
55
|
+
than selecting modes per file. The Hono mode is independent of `tsrx.platform`.
|
|
56
|
+
|
|
57
|
+
For editor diagnostics in DOM projects, set
|
|
58
|
+
`"tsrx": { "compiler": "@tsrx/hono/dom" }` in `tsconfig.json`.
|
|
59
|
+
|
|
60
|
+
## Compiler API
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
import { compile } from '@tsrx/hono/dom';
|
|
64
|
+
|
|
65
|
+
const { code, css, map } = compile(source, 'App.tsrx');
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The compiler returns TSX plus extracted CSS and a source map. A subsequent JSX
|
|
69
|
+
transform must use the import source matching the selected mode. The build plugins
|
|
70
|
+
perform that transform and integrate extracted CSS.
|
|
71
|
+
|
|
72
|
+
Try both modes and the supported language examples in the
|
|
73
|
+
[TSRX playground](https://tsrx.dev/playground).
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsrx/hono",
|
|
3
|
+
"description": "Hono JSX compiler target for TSRX",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Dominic Gannaway",
|
|
6
|
+
"version": "0.1.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/tsrx-org/tsrx.git",
|
|
14
|
+
"directory": "packages/tsrx-hono"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./types/index.d.ts",
|
|
19
|
+
"default": "./src/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./dom": {
|
|
22
|
+
"types": "./types/dom.d.ts",
|
|
23
|
+
"default": "./src/dom.js"
|
|
24
|
+
},
|
|
25
|
+
"./dynamic": {
|
|
26
|
+
"types": "./types/dynamic.d.ts"
|
|
27
|
+
},
|
|
28
|
+
"./error-boundary": {
|
|
29
|
+
"types": "./types/error-boundary.d.ts",
|
|
30
|
+
"default": "./src/error-boundary.js"
|
|
31
|
+
},
|
|
32
|
+
"./dom/error-boundary": {
|
|
33
|
+
"types": "./types/dom/error-boundary.d.ts",
|
|
34
|
+
"default": "./src/dom/error-boundary.js"
|
|
35
|
+
},
|
|
36
|
+
"./ref": {
|
|
37
|
+
"types": "./types/ref.d.ts",
|
|
38
|
+
"default": "./src/ref.js"
|
|
39
|
+
},
|
|
40
|
+
"./runtime/iterable": {
|
|
41
|
+
"types": "./types/runtime/iterable.d.ts",
|
|
42
|
+
"default": "./src/runtime/iterable.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@tsrx/core": "0.2.2"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"hono": "^4.13.7"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/estree": "^1.0.8",
|
|
53
|
+
"@types/estree-jsx": "^1.0.5",
|
|
54
|
+
"typescript": "^5.9.3"
|
|
55
|
+
},
|
|
56
|
+
"files": [
|
|
57
|
+
"src",
|
|
58
|
+
"types"
|
|
59
|
+
]
|
|
60
|
+
}
|
package/src/compiler.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/** @import * as AST from 'estree' */
|
|
2
|
+
/** @import { BaseCompileOptions, CompileError, CompileResult, JsxTransformOptions, JsxTransformResult, ParseOptions, TSRXAnalysisResult, VolarMappingsResult } from '@tsrx/core/types' */
|
|
3
|
+
/** @import { NonEmptyString } from '@tsrx/core/types/helpers' */
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
analyzeTsrx,
|
|
7
|
+
createVolarMappingsResult,
|
|
8
|
+
dedupeMappings,
|
|
9
|
+
hasPlatformNamespace,
|
|
10
|
+
parseModule,
|
|
11
|
+
specializePlatform,
|
|
12
|
+
withPlatformTypes,
|
|
13
|
+
} from '@tsrx/core';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create the public compiler facade shared by the Hono server and DOM targets.
|
|
17
|
+
*
|
|
18
|
+
* @param {(ast: AST.Program, source: string, filename?: string, options?: JsxTransformOptions) => JsxTransformResult} transform
|
|
19
|
+
* @param {{ validate?: (ast: AST.Program, filename: NonEmptyString<string>, context: { source: string, errors?: CompileError[], comments: AST.CommentWithLocation[], collect: boolean, analysis: TSRXAnalysisResult }) => void }} [settings]
|
|
20
|
+
*/
|
|
21
|
+
export function createCompiler(transform, settings = {}) {
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} source
|
|
24
|
+
* @param {NonEmptyString<string>} filename
|
|
25
|
+
* @param {ParseOptions} [options]
|
|
26
|
+
* @returns {AST.Program}
|
|
27
|
+
*/
|
|
28
|
+
function parse(source, filename, options) {
|
|
29
|
+
return parseModule(source, filename, options);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {string} source
|
|
34
|
+
* @param {NonEmptyString<string>} filename
|
|
35
|
+
* @param {BaseCompileOptions} [options]
|
|
36
|
+
* @returns {CompileResult}
|
|
37
|
+
*/
|
|
38
|
+
function compile(source, filename, options) {
|
|
39
|
+
const errors = /** @type {CompileError[]} */ ([]);
|
|
40
|
+
const comments = /** @type {AST.CommentWithLocation[]} */ ([]);
|
|
41
|
+
const collect = !!(options?.collect || options?.loose);
|
|
42
|
+
let ast = parseModule(
|
|
43
|
+
source,
|
|
44
|
+
filename,
|
|
45
|
+
collect ? { collect: true, loose: !!options?.loose, errors, comments } : undefined,
|
|
46
|
+
);
|
|
47
|
+
ast = specializePlatform(
|
|
48
|
+
ast,
|
|
49
|
+
options?.platform,
|
|
50
|
+
filename,
|
|
51
|
+
collect ? { errors, comments } : undefined,
|
|
52
|
+
);
|
|
53
|
+
const analysis = analyzeTsrx(
|
|
54
|
+
ast,
|
|
55
|
+
filename,
|
|
56
|
+
collect ? { collect: true, loose: !!options?.loose, errors, comments } : undefined,
|
|
57
|
+
);
|
|
58
|
+
settings.validate?.(ast, filename, {
|
|
59
|
+
source,
|
|
60
|
+
errors: collect ? errors : undefined,
|
|
61
|
+
comments,
|
|
62
|
+
collect,
|
|
63
|
+
analysis,
|
|
64
|
+
});
|
|
65
|
+
const { ast: _ast, ...result } = transform(
|
|
66
|
+
ast,
|
|
67
|
+
source,
|
|
68
|
+
filename,
|
|
69
|
+
collect ? { ...options, collect: true, loose: !!options?.loose, errors, comments } : options,
|
|
70
|
+
);
|
|
71
|
+
return { ...result, errors };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {string} source
|
|
76
|
+
* @param {NonEmptyString<string>} filename
|
|
77
|
+
* @param {ParseOptions & BaseCompileOptions} [options]
|
|
78
|
+
* @returns {VolarMappingsResult}
|
|
79
|
+
*/
|
|
80
|
+
function compile_to_volar_mappings(source, filename, options) {
|
|
81
|
+
const errors = /** @type {CompileError[]} */ ([]);
|
|
82
|
+
const comments = /** @type {AST.CommentWithLocation[]} */ ([]);
|
|
83
|
+
let ast = parseModule(source, filename, {
|
|
84
|
+
...options,
|
|
85
|
+
collect: true,
|
|
86
|
+
loose: !!options?.loose,
|
|
87
|
+
preserveParens: true,
|
|
88
|
+
keywordTokens: true,
|
|
89
|
+
errors,
|
|
90
|
+
comments,
|
|
91
|
+
});
|
|
92
|
+
const uses_platform_flags = hasPlatformNamespace(ast);
|
|
93
|
+
ast = specializePlatform(ast, options?.platform, filename, { errors, comments });
|
|
94
|
+
const analysis = analyzeTsrx(ast, filename, {
|
|
95
|
+
collect: true,
|
|
96
|
+
loose: !!options?.loose,
|
|
97
|
+
typeOnly: true,
|
|
98
|
+
errors,
|
|
99
|
+
comments,
|
|
100
|
+
});
|
|
101
|
+
settings.validate?.(ast, filename, { source, errors, comments, collect: true, analysis });
|
|
102
|
+
const transformed = transform(ast, source, filename, {
|
|
103
|
+
...options,
|
|
104
|
+
collect: true,
|
|
105
|
+
loose: !!options?.loose,
|
|
106
|
+
typeOnly: true,
|
|
107
|
+
moduleScopedHookComponents: false,
|
|
108
|
+
errors,
|
|
109
|
+
comments,
|
|
110
|
+
});
|
|
111
|
+
const result = createVolarMappingsResult({
|
|
112
|
+
ast: transformed.ast,
|
|
113
|
+
ast_from_source: ast,
|
|
114
|
+
source,
|
|
115
|
+
generated_code: transformed.code,
|
|
116
|
+
source_map: transformed.map,
|
|
117
|
+
errors,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const deduped = {
|
|
121
|
+
...result,
|
|
122
|
+
mappings: dedupeMappings(result.mappings),
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
return uses_platform_flags ? withPlatformTypes(deduped, options?.platform) : deduped;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return { parse, compile, compile_to_volar_mappings };
|
|
129
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ErrorBoundary as TsrxErrorBoundary } from 'hono/jsx/dom';
|
package/src/dom.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createCompiler } from './compiler.js';
|
|
2
|
+
import { hono_dom_transform, validate_hono_dom_components } from './platform.js';
|
|
3
|
+
|
|
4
|
+
const compiler = createCompiler(hono_dom_transform, {
|
|
5
|
+
validate: validate_hono_dom_components,
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export const { parse, compile, compile_to_volar_mappings } = compiler;
|
|
9
|
+
export { isRefProp } from './ref.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ErrorBoundary as TsrxErrorBoundary } from 'hono/jsx';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createCompiler } from './compiler.js';
|
|
2
|
+
import { hono_server_transform } from './platform.js';
|
|
3
|
+
|
|
4
|
+
const compiler = createCompiler(hono_server_transform);
|
|
5
|
+
|
|
6
|
+
export const { parse, compile, compile_to_volar_mappings } = compiler;
|
|
7
|
+
export { isRefProp } from './ref.js';
|
package/src/platform.js
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
/** @import * as AST from 'estree' */
|
|
2
|
+
/** @import * as ESTreeJSX from 'estree-jsx' */
|
|
3
|
+
/** @import { JsxPlatform, JsxTransformContext, TSRXAnalysisResult } from '@tsrx/core/types' */
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
builders as b,
|
|
7
|
+
createJsxTransform,
|
|
8
|
+
error,
|
|
9
|
+
findFirstTopLevelAwait,
|
|
10
|
+
findFirstTopLevelAwaitInTsrxFunctionBody,
|
|
11
|
+
is_component_like_element as isComponentLikeElement,
|
|
12
|
+
isFunctionNode,
|
|
13
|
+
} from '@tsrx/core';
|
|
14
|
+
|
|
15
|
+
const HONO_SERVER_SOURCE = 'hono/jsx';
|
|
16
|
+
const HONO_DOM_SOURCE = 'hono/jsx/dom';
|
|
17
|
+
const AST_METADATA_KEYS = new Set(['loc', 'start', 'end', 'metadata']);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The Hono JSX runtimes accept authored `class` attributes and expose their
|
|
21
|
+
* own Fragment/Suspense/ErrorBoundary components. The compiler-only Dynamic
|
|
22
|
+
* import is needed for type-only output; production output aliases a dynamic
|
|
23
|
+
* tag to a local component binding and therefore needs no runtime Dynamic
|
|
24
|
+
* component.
|
|
25
|
+
*
|
|
26
|
+
* @param {'server' | 'dom'} mode
|
|
27
|
+
* @returns {JsxPlatform}
|
|
28
|
+
*/
|
|
29
|
+
function create_hono_platform(mode) {
|
|
30
|
+
const is_dom = mode === 'dom';
|
|
31
|
+
const jsx_source = is_dom ? HONO_DOM_SOURCE : HONO_SERVER_SOURCE;
|
|
32
|
+
/** @type {NonNullable<JsxPlatform['hooks']>} */
|
|
33
|
+
const hooks = {
|
|
34
|
+
createErrorBoundary(try_content, _raw_try_content, fallback_fn, ctx, node) {
|
|
35
|
+
return create_hono_error_boundary(try_content, fallback_fn, ctx, node);
|
|
36
|
+
},
|
|
37
|
+
...(is_dom
|
|
38
|
+
? {
|
|
39
|
+
// Hono DOM keys hook state by the runtime component function. A
|
|
40
|
+
// helper recreated inside its parent would lose state on updates.
|
|
41
|
+
moduleScopedHookComponents: true,
|
|
42
|
+
// Hono DOM JSX nodes carry mutable reconciliation and hook state.
|
|
43
|
+
// Reusing any module-scoped node across mounts is unsafe, not only
|
|
44
|
+
// reusing composite nodes.
|
|
45
|
+
canHoistStaticNode() {
|
|
46
|
+
return false;
|
|
47
|
+
},
|
|
48
|
+
validateComponentAwait(await_node, _component, ctx) {
|
|
49
|
+
error(
|
|
50
|
+
'Hono JSX DOM does not support top-level `await` in components. Use `use(promise)` with `<Suspense>` instead.',
|
|
51
|
+
ctx.filename,
|
|
52
|
+
await_node,
|
|
53
|
+
ctx.errors,
|
|
54
|
+
ctx.comments,
|
|
55
|
+
);
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
: {}),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
name: is_dom ? 'Hono JSX DOM' : 'Hono JSX',
|
|
63
|
+
imports: {
|
|
64
|
+
fragment: jsx_source,
|
|
65
|
+
suspense: jsx_source,
|
|
66
|
+
dynamic: '@tsrx/hono/dynamic',
|
|
67
|
+
dynamicFactory: {},
|
|
68
|
+
errorBoundary: is_dom ? '@tsrx/hono/dom/error-boundary' : '@tsrx/hono/error-boundary',
|
|
69
|
+
mergeRefs: '@tsrx/hono/ref',
|
|
70
|
+
refProp: '@tsrx/hono/ref',
|
|
71
|
+
forOfIterableHelper: '@tsrx/hono/runtime/iterable',
|
|
72
|
+
},
|
|
73
|
+
directRuntimeImports: {
|
|
74
|
+
// Hono-specific adapters intentionally remain on @tsrx/hono/* because
|
|
75
|
+
// this target does not publish a separate standalone runtime package.
|
|
76
|
+
mergeRefs: '@tsrx/core/runtime/ref',
|
|
77
|
+
refProp: '@tsrx/core/runtime/ref',
|
|
78
|
+
forOfIterableHelper: '@tsrx/core/runtime/iterable',
|
|
79
|
+
},
|
|
80
|
+
jsx: {
|
|
81
|
+
rewriteClassAttr: false,
|
|
82
|
+
multiRefStrategy: 'merge-refs',
|
|
83
|
+
},
|
|
84
|
+
validation: {
|
|
85
|
+
// Server JSX supports async function components. The DOM renderer is
|
|
86
|
+
// synchronous; use Hono's `use(promise)` + Suspense there instead.
|
|
87
|
+
requireUseServerForAwait: is_dom,
|
|
88
|
+
},
|
|
89
|
+
hooks,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Hono names the callback prop `fallbackRender` and invokes it with only the
|
|
95
|
+
* error. TSRX's default React-shaped output uses `fallback={(error, reset) =>
|
|
96
|
+
* ...}`, which would be rendered as a function value by Hono. Adapt the
|
|
97
|
+
* boundary explicitly and reject the unsupported reset contract.
|
|
98
|
+
*
|
|
99
|
+
* @param {ESTreeJSX.JSXRenderNode} try_content
|
|
100
|
+
* @param {AST.ArrowFunctionExpression} fallback_fn
|
|
101
|
+
* @param {JsxTransformContext} ctx
|
|
102
|
+
* @param {AST.TryStatement} node
|
|
103
|
+
* @returns {ESTreeJSX.JSXRenderNode}
|
|
104
|
+
*/
|
|
105
|
+
function create_hono_error_boundary(try_content, fallback_fn, ctx, node) {
|
|
106
|
+
const reset_param = node.handler?.resetParam;
|
|
107
|
+
if (reset_param) {
|
|
108
|
+
error(
|
|
109
|
+
'Hono JSX ErrorBoundary does not provide a reset callback. Use `@catch (error)` without a reset parameter.',
|
|
110
|
+
ctx.filename,
|
|
111
|
+
reset_param,
|
|
112
|
+
ctx.errors,
|
|
113
|
+
ctx.comments,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// The core transform keeps a second reset parameter for React-shaped
|
|
118
|
+
// boundaries. Hono invokes `fallbackRender` with the error only, so remove
|
|
119
|
+
// the unused parameter from generated output and preserve Hono's public type.
|
|
120
|
+
const hono_fallback_fn = /** @type {AST.ArrowFunctionExpression} */ ({
|
|
121
|
+
...fallback_fn,
|
|
122
|
+
params: fallback_fn.params.slice(0, 1),
|
|
123
|
+
});
|
|
124
|
+
const name = b.jsx_id('TsrxErrorBoundary');
|
|
125
|
+
const fallback_render = b.jsx_attribute(
|
|
126
|
+
b.jsx_id('fallbackRender'),
|
|
127
|
+
b.jsx_expression_container(hono_fallback_fn),
|
|
128
|
+
);
|
|
129
|
+
return b.jsx_element_fresh(
|
|
130
|
+
b.jsx_opening_element(name, [fallback_render], false),
|
|
131
|
+
b.jsx_closing_element(b.jsx_id('TsrxErrorBoundary')),
|
|
132
|
+
[try_content],
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export const hono_server_transform = createJsxTransform(create_hono_platform('server'));
|
|
137
|
+
export const hono_dom_transform = createJsxTransform(create_hono_platform('dom'));
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Hono DOM renders components synchronously. This validator deliberately
|
|
141
|
+
* diagnoses only explicit `async` component functions that can be resolved
|
|
142
|
+
* from a component position in the same module. Promise return types require
|
|
143
|
+
* type information or inter-module analysis and are outside this pass.
|
|
144
|
+
*
|
|
145
|
+
* @param {AST.Program} ast
|
|
146
|
+
* @param {string} filename
|
|
147
|
+
* @param {{ source: string, errors?: import('@tsrx/core/types').CompileError[], comments: AST.CommentWithLocation[], collect: boolean, analysis: TSRXAnalysisResult }} context
|
|
148
|
+
*/
|
|
149
|
+
export function validate_hono_dom_components(ast, filename, context) {
|
|
150
|
+
if (context.source && !/\basync\b/.test(context.source)) return;
|
|
151
|
+
|
|
152
|
+
const component = find_hono_dom_async_component(
|
|
153
|
+
ast,
|
|
154
|
+
context.analysis,
|
|
155
|
+
context.collect ? (node) => find_hono_component_await(node) === null : undefined,
|
|
156
|
+
);
|
|
157
|
+
if (!component) return;
|
|
158
|
+
|
|
159
|
+
error(
|
|
160
|
+
'Hono JSX DOM does not support async components. Use use(promise) with <Suspense> instead.',
|
|
161
|
+
filename,
|
|
162
|
+
component,
|
|
163
|
+
context.errors,
|
|
164
|
+
context.comments,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Use the same await boundary as the core transform for both ordinary
|
|
170
|
+
* functions and TSRX statement containers.
|
|
171
|
+
*
|
|
172
|
+
* @param {AST.Function} node
|
|
173
|
+
* @returns {AST.Node | null}
|
|
174
|
+
*/
|
|
175
|
+
function find_hono_component_await(node) {
|
|
176
|
+
if (node.body?.type === 'JSXCodeBlock') {
|
|
177
|
+
return findFirstTopLevelAwaitInTsrxFunctionBody([
|
|
178
|
+
...(node.body.body || []),
|
|
179
|
+
...(node.body.render ? [node.body.render] : []),
|
|
180
|
+
]);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return findFirstTopLevelAwait(node.body, false);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Resolve only static same-module component references. Mutable bindings are
|
|
188
|
+
* intentionally ignored: resolving their current value would require source
|
|
189
|
+
* order and control-flow analysis. This keeps the validator conservative and
|
|
190
|
+
* prevents a later assignment from changing the meaning of an earlier JSX use.
|
|
191
|
+
*
|
|
192
|
+
* @param {AST.Program} ast
|
|
193
|
+
* @param {TSRXAnalysisResult} analysis
|
|
194
|
+
* @param {(node: AST.Function) => boolean} [is_custom_diagnostic_candidate]
|
|
195
|
+
* @returns {AST.Function | null}
|
|
196
|
+
*/
|
|
197
|
+
function find_hono_dom_async_component(ast, analysis, is_custom_diagnostic_candidate) {
|
|
198
|
+
/** @type {Map<AST.Node, import('@tsrx/core/types').ScopeInterface | null>} */
|
|
199
|
+
const scopes = new Map();
|
|
200
|
+
/** @type {Array<{ node: AST.Node, scope: import('@tsrx/core/types').ScopeInterface | null }>} */
|
|
201
|
+
const jsx_references = [];
|
|
202
|
+
/** @type {Map<import('@tsrx/core/types').Binding, string[]>} */
|
|
203
|
+
const destructured_paths = new Map();
|
|
204
|
+
|
|
205
|
+
/** @param {AST.Node | null | undefined} node @param {import('@tsrx/core/types').ScopeInterface | null} scope */
|
|
206
|
+
function walk(node, scope) {
|
|
207
|
+
if (!node || typeof node !== 'object') return;
|
|
208
|
+
scope = analysis.scopes.get(node) ?? scope;
|
|
209
|
+
scopes.set(node, scope);
|
|
210
|
+
|
|
211
|
+
if (node.type === 'JSXElement') {
|
|
212
|
+
const name = node.openingElement.name;
|
|
213
|
+
const reference =
|
|
214
|
+
name.type === 'JSXExpressionContainer' && name.expression.type !== 'JSXEmptyExpression'
|
|
215
|
+
? name.expression
|
|
216
|
+
: name;
|
|
217
|
+
if (name.type !== 'JSXIdentifier' || isComponentLikeElement(node)) {
|
|
218
|
+
jsx_references.push({ node: reference, scope });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (node.type === 'VariableDeclarator' && node.init) {
|
|
222
|
+
collect_hono_destructured_paths(node.id, scope, destructured_paths);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
for_each_hono_child(node, (child) => walk(child, scope));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* @param {AST.Node | null | undefined} node
|
|
230
|
+
* @param {string[]} property_path
|
|
231
|
+
* @param {Set<AST.Node>} seen
|
|
232
|
+
* @returns {AST.Function | null}
|
|
233
|
+
*/
|
|
234
|
+
function resolve_async_component(node, property_path, seen) {
|
|
235
|
+
if (!node || seen.has(node)) return null;
|
|
236
|
+
const next_seen = new Set(seen).add(node);
|
|
237
|
+
const expression = unwrap_hono_transparent_expression(node);
|
|
238
|
+
if (expression !== node) return resolve_async_component(expression, property_path, next_seen);
|
|
239
|
+
|
|
240
|
+
if (isFunctionNode(node)) {
|
|
241
|
+
if (property_path.length || !node.async) return null;
|
|
242
|
+
if (is_custom_diagnostic_candidate && !is_custom_diagnostic_candidate(node)) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
return node;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (node.type === 'ObjectExpression' && property_path.length) {
|
|
249
|
+
// Later properties replace earlier ones. A spread or dynamic key can
|
|
250
|
+
// replace any earlier member, so stop unless a later static definition
|
|
251
|
+
// has already established the effective value.
|
|
252
|
+
for (let index = node.properties.length - 1; index >= 0; index--) {
|
|
253
|
+
const property = node.properties[index];
|
|
254
|
+
if (property.type !== 'Property') return null;
|
|
255
|
+
if (
|
|
256
|
+
property.computed &&
|
|
257
|
+
(property.key.type !== 'Literal' || typeof property.key.value !== 'string')
|
|
258
|
+
) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
if (get_hono_static_name(property.key, true) !== property_path[0]) continue;
|
|
262
|
+
// Accessors also replace data properties, but resolving their return
|
|
263
|
+
// values would require execution rather than static reference lookup.
|
|
264
|
+
return property.kind === 'init'
|
|
265
|
+
? resolve_async_component(property.value, property_path.slice(1), next_seen)
|
|
266
|
+
: null;
|
|
267
|
+
}
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const reference = resolve_hono_reference(node, scopes.get(node) ?? analysis.scope);
|
|
272
|
+
if (!reference || reference.binding.updated || !reference.binding.initial) return null;
|
|
273
|
+
|
|
274
|
+
// Combine destructuring and member segments in root-to-leaf order.
|
|
275
|
+
return resolve_async_component(
|
|
276
|
+
reference.binding.initial,
|
|
277
|
+
[...(destructured_paths.get(reference.binding) ?? []), ...reference.path, ...property_path],
|
|
278
|
+
next_seen,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
walk(ast, analysis.scope);
|
|
283
|
+
for (const reference of jsx_references) {
|
|
284
|
+
const found = resolve_async_component(reference.node, [], new Set());
|
|
285
|
+
if (found) return found;
|
|
286
|
+
}
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* @param {AST.Node} node
|
|
292
|
+
* @param {(child: AST.Node) => void} visit
|
|
293
|
+
*/
|
|
294
|
+
function for_each_hono_child(node, visit) {
|
|
295
|
+
for (const key of Object.keys(node)) {
|
|
296
|
+
if (AST_METADATA_KEYS.has(key)) continue;
|
|
297
|
+
const value = /** @type {unknown} */ (/** @type {Record<string, unknown>} */ (node)[key]);
|
|
298
|
+
for (const child of Array.isArray(value) ? value : [value]) {
|
|
299
|
+
if (child && typeof child === 'object' && 'type' in child) {
|
|
300
|
+
visit(/** @type {AST.Node} */ (child));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* @param {AST.Node | null | undefined} node
|
|
308
|
+
* @returns {AST.Node | null}
|
|
309
|
+
*/
|
|
310
|
+
function unwrap_hono_transparent_expression(node) {
|
|
311
|
+
let expression = node ?? null;
|
|
312
|
+
while (expression) {
|
|
313
|
+
const child = /** @type {{ expression?: AST.Node }} */ (expression).expression;
|
|
314
|
+
if (!child || !is_hono_transparent_expression_wrapper(expression, child)) return expression;
|
|
315
|
+
expression = child;
|
|
316
|
+
}
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Hono's local resolver only needs wrappers that preserve an expression value.
|
|
322
|
+
* Keep this boundary local because the core predicate is intentionally not a
|
|
323
|
+
* public target API.
|
|
324
|
+
*
|
|
325
|
+
* @param {AST.Node} parent
|
|
326
|
+
* @param {AST.Node} child
|
|
327
|
+
* @returns {boolean}
|
|
328
|
+
*/
|
|
329
|
+
function is_hono_transparent_expression_wrapper(parent, child) {
|
|
330
|
+
return (
|
|
331
|
+
(parent.type === 'ParenthesizedExpression' ||
|
|
332
|
+
parent.type === 'TSAsExpression' ||
|
|
333
|
+
parent.type === 'TSSatisfiesExpression' ||
|
|
334
|
+
parent.type === 'TSNonNullExpression' ||
|
|
335
|
+
parent.type === 'TSInstantiationExpression' ||
|
|
336
|
+
parent.type === 'TSTypeAssertion' ||
|
|
337
|
+
parent.type === 'ChainExpression') &&
|
|
338
|
+
/** @type {{ expression?: AST.Node }} */ (parent).expression === child
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Resolve an identifier or static member without evaluating computed
|
|
344
|
+
* expressions. `obj['App']` is statically known; `obj[key]` is not.
|
|
345
|
+
*
|
|
346
|
+
* @param {AST.Node | null | undefined} node
|
|
347
|
+
* @param {import('@tsrx/core/types').ScopeInterface | null} scope
|
|
348
|
+
* @returns {{ binding: import('@tsrx/core/types').Binding, path: string[] } | null}
|
|
349
|
+
*/
|
|
350
|
+
function resolve_hono_reference(node, scope) {
|
|
351
|
+
if (!node || !scope) return null;
|
|
352
|
+
const expression = unwrap_hono_transparent_expression(node);
|
|
353
|
+
if (!expression) return null;
|
|
354
|
+
if (expression.type === 'Identifier' || expression.type === 'JSXIdentifier') {
|
|
355
|
+
const binding = scope.get(expression.name);
|
|
356
|
+
return binding ? { binding, path: [] } : null;
|
|
357
|
+
}
|
|
358
|
+
if (expression.type === 'MemberExpression' || expression.type === 'JSXMemberExpression') {
|
|
359
|
+
const base = resolve_hono_reference(expression.object, scope);
|
|
360
|
+
const property_name =
|
|
361
|
+
expression.type === 'MemberExpression'
|
|
362
|
+
? get_hono_member_name(expression)
|
|
363
|
+
: get_hono_static_name(expression.property);
|
|
364
|
+
return base && property_name !== null
|
|
365
|
+
? { binding: base.binding, path: [...base.path, property_name] }
|
|
366
|
+
: null;
|
|
367
|
+
}
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* @param {AST.MemberExpression} node
|
|
373
|
+
* @returns {string | null}
|
|
374
|
+
*/
|
|
375
|
+
function get_hono_member_name(node) {
|
|
376
|
+
if (node.computed) {
|
|
377
|
+
return node.property.type === 'Literal' && typeof node.property.value === 'string'
|
|
378
|
+
? node.property.value
|
|
379
|
+
: null;
|
|
380
|
+
}
|
|
381
|
+
return get_hono_static_name(node.property);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* @param {AST.Node | null | undefined} node
|
|
386
|
+
* @param {import('@tsrx/core/types').ScopeInterface | null} scope
|
|
387
|
+
* @param {Map<import('@tsrx/core/types').Binding, string[]>} paths
|
|
388
|
+
* @param {string[]} [path]
|
|
389
|
+
*/
|
|
390
|
+
function collect_hono_destructured_paths(node, scope, paths, path = []) {
|
|
391
|
+
if (!node || !scope) return;
|
|
392
|
+
const expression = unwrap_hono_transparent_expression(node);
|
|
393
|
+
if (!expression) return;
|
|
394
|
+
if (expression.type === 'Identifier') {
|
|
395
|
+
if (path.length) {
|
|
396
|
+
const binding = scope.get(expression.name);
|
|
397
|
+
if (binding) paths.set(binding, path);
|
|
398
|
+
}
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if (expression.type === 'AssignmentPattern') {
|
|
402
|
+
collect_hono_destructured_paths(expression.left, scope, paths, path);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
if (expression.type !== 'ObjectPattern') return;
|
|
406
|
+
for (const property of expression.properties) {
|
|
407
|
+
if (property.type === 'RestElement') continue;
|
|
408
|
+
if (
|
|
409
|
+
property.computed &&
|
|
410
|
+
(property.key.type !== 'Literal' || typeof property.key.value !== 'string')
|
|
411
|
+
) {
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
const property_name = get_hono_static_name(property.key, true);
|
|
415
|
+
if (property_name !== null) {
|
|
416
|
+
collect_hono_destructured_paths(property.value, scope, paths, [...path, property_name]);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* @param {AST.Node | null | undefined} node
|
|
423
|
+
* @param {boolean} [allow_literal]
|
|
424
|
+
* @returns {string | null}
|
|
425
|
+
*/
|
|
426
|
+
function get_hono_static_name(node, allow_literal = false) {
|
|
427
|
+
if (node?.type === 'Identifier' || node?.type === 'JSXIdentifier') return node.name;
|
|
428
|
+
if (allow_literal && node?.type === 'Literal' && typeof node.value === 'string')
|
|
429
|
+
return node.value;
|
|
430
|
+
return null;
|
|
431
|
+
}
|
package/src/ref.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tsrx/core/runtime/ref';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tsrx/core/runtime/iterable';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ErrorBoundary as TsrxErrorBoundary } from 'hono/jsx/dom';
|
package/types/dom.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Program } from 'estree';
|
|
2
|
+
import type { BaseCompileOptions, CompileFn, ParseOptions, VolarCompileFn } from '@tsrx/core/types';
|
|
3
|
+
|
|
4
|
+
export type { Platform, RuntimeImportMode, TsrxPlatformFlags } from '@tsrx/core/types';
|
|
5
|
+
|
|
6
|
+
export function parse(source: string, filename?: string, options?: ParseOptions): Program;
|
|
7
|
+
|
|
8
|
+
export type { DynamicElementType, DynamicProps } from './dynamic.js';
|
|
9
|
+
export { isRefProp } from './ref.js';
|
|
10
|
+
|
|
11
|
+
export const compile: CompileFn;
|
|
12
|
+
export const compile_to_volar_mappings: VolarCompileFn<ParseOptions & BaseCompileOptions>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { FC, JSX, JSXNode } from 'hono/jsx';
|
|
2
|
+
|
|
3
|
+
type DynamicIntrinsicElements = JSX.IntrinsicElements;
|
|
4
|
+
type DynamicIntrinsicElement = Extract<keyof DynamicIntrinsicElements, string>;
|
|
5
|
+
export type DynamicElementType = DynamicIntrinsicElement | FC<any> | (string & {});
|
|
6
|
+
type DynamicTarget<T> = Exclude<T, null | undefined | false>;
|
|
7
|
+
type DynamicComponentProps<T> = [T] extends [never]
|
|
8
|
+
? Record<string, unknown>
|
|
9
|
+
: T extends FC<infer P>
|
|
10
|
+
? Omit<P, 'is'>
|
|
11
|
+
: T extends DynamicIntrinsicElement
|
|
12
|
+
? DynamicIntrinsicElements[T]
|
|
13
|
+
: Record<string, unknown>;
|
|
14
|
+
|
|
15
|
+
export type DynamicProps<T extends DynamicElementType> = DynamicComponentProps<
|
|
16
|
+
DynamicTarget<NoInfer<T>>
|
|
17
|
+
> & {
|
|
18
|
+
is: T | null | undefined | false;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** Type-only helper used by TSRX's Volar output for dynamic tags. */
|
|
22
|
+
export declare function Dynamic<T extends DynamicIntrinsicElement>(
|
|
23
|
+
props: DynamicIntrinsicElements[T] & {
|
|
24
|
+
is: T | null | undefined | false;
|
|
25
|
+
},
|
|
26
|
+
): JSXNode;
|
|
27
|
+
export declare function Dynamic<T extends DynamicElementType>(
|
|
28
|
+
props: DynamicComponentProps<DynamicTarget<NoInfer<T>>> & {
|
|
29
|
+
is: T | null | undefined | false;
|
|
30
|
+
},
|
|
31
|
+
): JSXNode;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ErrorBoundary as TsrxErrorBoundary } from 'hono/jsx';
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Program } from 'estree';
|
|
2
|
+
import type { BaseCompileOptions, CompileFn, ParseOptions, VolarCompileFn } from '@tsrx/core/types';
|
|
3
|
+
|
|
4
|
+
export type { Platform, RuntimeImportMode, TsrxPlatformFlags } from '@tsrx/core/types';
|
|
5
|
+
|
|
6
|
+
export function parse(source: string, filename?: string, options?: ParseOptions): Program;
|
|
7
|
+
|
|
8
|
+
export type { DynamicElementType, DynamicProps } from './dynamic.js';
|
|
9
|
+
export { isRefProp } from './ref.js';
|
|
10
|
+
|
|
11
|
+
export const compile: CompileFn;
|
|
12
|
+
export const compile_to_volar_mappings: VolarCompileFn<ParseOptions & BaseCompileOptions>;
|
package/types/ref.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tsrx/core/runtime/ref';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tsrx/core/runtime/iterable';
|