1
- import * as espree from "espree" ;
2
1
import { Accordion } from "@/components/ui/accordion" ;
3
2
import { Editor } from "@/components/editor" ;
4
3
import { useExplorer } from "@/hooks/use-explorer" ;
5
- import { JavascriptAstTreeItem } from "./javascript-ast-tree-item" ;
4
+ import { useAST } from "@/hooks/use-ast" ;
5
+ import {
6
+ JavascriptAstTreeItem ,
7
+ type JavascriptAstTreeItemProperties ,
8
+ } from "./javascript-ast-tree-item" ;
6
9
import type { FC } from "react" ;
7
10
import { parseError } from "@/lib/parse-error" ;
8
11
import { ErrorState } from "../error-boundary" ;
9
12
10
13
export const JavascriptAst : FC = ( ) => {
14
+ const result = useAST ( ) ;
11
15
const explorer = useExplorer ( ) ;
12
16
const { viewModes } = explorer ;
13
17
const { astView } = viewModes ;
14
- let ast = "" ;
15
- let tree : ReturnType < typeof espree . parse > | null = null ;
16
18
17
- try {
18
- tree = espree . parse ( explorer . code . javascript , {
19
- ecmaVersion : explorer . jsOptions . esVersion ,
20
- sourceType : explorer . jsOptions . sourceType ,
21
- ecmaFeatures : {
22
- jsx : explorer . jsOptions . isJSX ,
23
- } ,
24
- } ) ;
25
-
26
- ast = JSON . stringify ( tree , null , 2 ) ;
27
- } catch ( error ) {
28
- const message = parseError ( error ) ;
19
+ if ( ! result . ok ) {
20
+ const message = parseError ( result . errors [ 0 ] ) ;
29
21
return < ErrorState message = { message } /> ;
30
22
}
23
+
24
+ const ast = JSON . stringify ( result . ast , null , 2 ) ;
25
+
31
26
if ( astView === "tree" ) {
32
- if ( tree === null ) {
27
+ if ( result . ast === null ) {
33
28
return null ;
34
29
}
35
30
@@ -39,7 +34,13 @@ export const JavascriptAst: FC = () => {
39
34
className = "px-8 font-mono space-y-3"
40
35
defaultValue = { [ "0-Program" ] }
41
36
>
42
- < JavascriptAstTreeItem data = { tree } index = { 0 } />
37
+ < JavascriptAstTreeItem
38
+ data = { result . ast as JavascriptAstTreeItemProperties [ "data" ] }
39
+ index = { 0 }
40
+ esqueryMatchedNodes = {
41
+ result . esqueryMatchedNodes as JavascriptAstTreeItemProperties [ "esqueryMatchedNodes" ]
42
+ }
43
+ />
43
44
</ Accordion >
44
45
) ;
45
46
}
0 commit comments