-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dadf787
commit 9483a16
Showing
9 changed files
with
56 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import esquery from "esquery"; | ||
import { Node } from "acorn"; | ||
import type { Node as EstreeNode } from "estree"; | ||
import { useExplorer } from "@/hooks/use-explorer"; | ||
import { parseJavascriptAST } from "@/lib/parse-javascript-ast"; | ||
import type { HighlightedRange } from "@/utils/highlighted-ranges"; | ||
|
||
export function useHighlightedRanges() { | ||
const { language, code, jsOptions, esquerySelector } = useExplorer(); | ||
|
||
let highlightedRanges: HighlightedRange[] = []; | ||
if (language === "javascript" && jsOptions.esquerySelectorEnabled) { | ||
try { | ||
const tree = parseJavascriptAST({ | ||
code: code.javascript, | ||
jsOptions: jsOptions, | ||
}); | ||
/** | ||
* "esquery" uses type "Node" of "@types/estree", "espree" returns "Node" of "acorn" | ||
* but they are compatible | ||
* Therefore, cast between "Node" of "acorn" and "Node" of "@types/estree" | ||
*/ | ||
const esqueryMatchedNodes = esquery.match( | ||
tree as EstreeNode, | ||
esquery.parse(esquerySelector), | ||
) as Node[]; | ||
highlightedRanges = esqueryMatchedNodes.map(node => [ | ||
node.start, | ||
node.end, | ||
]); | ||
} catch { | ||
// error occured e.g. because the JS code cannot be parsed into an AST, or the esquery selector is no valid selector --> just ignore (no highlighted ranges) | ||
} | ||
} | ||
|
||
return highlightedRanges; | ||
} |
6 changes: 3 additions & 3 deletions
6
src/utils/highlight-ranges.tsx → src/utils/highlighted-ranges.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters