11import dedent from 'dedent' ;
22import assert from 'node:assert' ;
33import { describe , test } from 'node:test' ;
4- import rehypeStaticToDynamic from '../plugins/rehype-static-to-dynamic.ts' ;
4+ import rehypeStaticToDynamic , {
5+ type RehypeStaticToDynamicElement as Element ,
6+ type RehypeStaticToDynamicElementChild as ElementChild ,
7+ type RehypeStaticToDynamicRoot as Root ,
8+ type RehypeStaticToDynamicText as Text ,
9+ type RehypeStaticToDynamicTreeChild as TreeChild ,
10+ } from '../plugins/rehype-static-to-dynamic.ts' ;
511
612/**
713 * Helper function to create a test tree structure
814 */
9- function createTestTree ( code ) {
10- return {
15+ function createTestTree ( code : string ) : Root {
16+ const tree : Root = {
1117 type : 'root' ,
1218 children : [
1319 {
1420 type : 'element' ,
1521 tagName : 'pre' ,
22+ properties : { } ,
1623 children : [
1724 {
1825 type : 'element' ,
1926 tagName : 'code' ,
27+ properties : { } ,
2028 data : { meta : 'static2dynamic' } ,
2129 children : [
2230 {
@@ -29,23 +37,25 @@ function createTestTree(code) {
2937 } ,
3038 ] ,
3139 } ;
40+
41+ return tree ;
3242}
3343
3444/**
3545 * Helper function to extract the transformed code from the tree
3646 */
37- function extractTransformedCode ( tree ) {
47+ function extractTransformedCode ( tree : Root ) : string {
3848 // After transformation, the tree should have TabItem elements
3949 const tabsElement = tree . children [ 0 ] ;
4050
41- if ( ! tabsElement || tabsElement . tagName !== 'Tabs' ) {
51+ if ( ! isElement ( tabsElement ) || tabsElement . tagName !== 'Tabs' ) {
4252 throw new Error ( 'Expected Tabs element not found' ) ;
4353 }
4454
4555 // Find the "Dynamic" tab
4656 const dynamicTab = tabsElement . children . find (
47- ( child ) =>
48- child . type === 'element' &&
57+ ( child ) : child is Element =>
58+ isElement ( child ) &&
4959 child . tagName === 'TabItem' &&
5060 child . properties ?. value === 'dynamic'
5161 ) ;
@@ -56,26 +66,38 @@ function extractTransformedCode(tree) {
5666
5767 // Extract the code from the dynamic tab
5868 const preElement = dynamicTab . children . find (
59- ( child ) => child . type === 'element' && child . tagName === 'pre'
69+ ( child ) : child is Element =>
70+ isElement ( child ) && child . tagName === 'pre'
6071 ) ;
6172
6273 if ( ! preElement ) {
6374 throw new Error ( 'Pre element not found in dynamic tab' ) ;
6475 }
6576
6677 const codeElement = preElement . children . find (
67- ( child ) => child . type === 'element' && child . tagName === 'code'
78+ ( child ) : child is Element =>
79+ isElement ( child ) && child . tagName === 'code'
6880 ) ;
6981
7082 if ( ! codeElement ) {
7183 throw new Error ( 'Code element not found' ) ;
7284 }
7385
74- const textNode = codeElement . children . find ( ( child ) => child . type === 'text' ) ;
86+ const textNode = codeElement . children . find ( isTextNode ) ;
7587
7688 return textNode ?. value || '' ;
7789}
7890
91+ function isElement (
92+ node : TreeChild | ElementChild | undefined
93+ ) : node is Element {
94+ return Boolean ( node && node . type === 'element' ) ;
95+ }
96+
97+ function isTextNode ( node : ElementChild ) : node is Text {
98+ return node . type === 'text' ;
99+ }
100+
79101describe ( 'rehype-static-to-dynamic' , ( ) => {
80102 test ( 'basic screen transformation' , async ( ) => {
81103 const input = dedent /* javascript */ `
0 commit comments