-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
ASTViewerESTree.tsx
32 lines (27 loc) · 982 Bytes
/
ASTViewerESTree.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import React, { useEffect, useState } from 'react';
import ASTViewer from './ast/ASTViewer';
import type { ASTViewerBaseProps, ASTViewerModelMap } from './ast/types';
import type { TSESTree } from '@typescript-eslint/utils';
import { serialize } from './ast/serializer/serializer';
import { createESTreeSerializer } from './ast/serializer/serializerESTree';
export interface ASTESTreeViewerProps extends ASTViewerBaseProps {
readonly value: TSESTree.BaseNode | string;
}
export default function ASTViewerESTree({
value,
position,
onSelectNode,
}: ASTESTreeViewerProps): JSX.Element {
const [model, setModel] = useState<string | ASTViewerModelMap>('');
useEffect(() => {
if (typeof value === 'string') {
setModel(value);
} else {
const astSerializer = createESTreeSerializer();
setModel(serialize(value, astSerializer));
}
}, [value]);
return (
<ASTViewer value={model} position={position} onSelectNode={onSelectNode} />
);
}