Skip to content

Commit 59ed619

Browse files
committed
Fix types
1 parent f895032 commit 59ed619

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

src/__tests__/rehype-static-to-dynamic.test.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
import dedent from 'dedent';
22
import assert from 'node:assert';
33
import { 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+
79101
describe('rehype-static-to-dynamic', () => {
80102
test('basic screen transformation', async () => {
81103
const input = dedent /* javascript */ `

src/plugins/rehype-static-to-dynamic.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as recast from 'recast';
88
import * as babelParser from 'recast/parsers/babel-ts.js';
99
import { visit } from 'unist-util-visit';
1010
import { fileURLToPath } from 'url';
11-
import type { Element, Root } from 'hast';
11+
import type { Element, Root, Text } from 'hast';
1212
import type { Parent } from 'unist';
1313

1414
const __filename = fileURLToPath(import.meta.url);
@@ -22,6 +22,16 @@ type TrackedComment = {
2222
type: t.Comment['type'];
2323
};
2424

25+
export type RehypeStaticToDynamicRoot = Root;
26+
27+
export type RehypeStaticToDynamicElement = Element;
28+
29+
export type RehypeStaticToDynamicText = Text;
30+
31+
export type RehypeStaticToDynamicTreeChild = Root['children'][number];
32+
33+
export type RehypeStaticToDynamicElementChild = Element['children'][number];
34+
2535
type CommentWithMarkers = t.Comment & {
2636
leading?: boolean;
2737
trailing?: boolean;

0 commit comments

Comments
 (0)