Skip to content

Commit f96e872

Browse files
committed
Correct checks for syntax error
1 parent a37148f commit f96e872

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import './index.css';
44
import { queryCode } from './engine';
55
import QueryEditor from './QueryEditor';
66
import Code, { HighlightedInterval, HighlightedIntervals } from './Code';
7+
import { isSyntaxError } from './tsquery-util';
78

89
const REG_EXPS: Record<string, RegExp> = {
910
AllLineBreaks: /\n/g,
@@ -43,7 +44,7 @@ export default function App() {
4344
const highlightedIntervals = nodes.map((node) => mapNodeToHighlightInterval(node));
4445
setHighlightedIntervals(highlightedIntervals);
4546
} catch (error) {
46-
if (error instanceof SyntaxError) {
47+
if (isSyntaxError(error)) {
4748
return;
4849
}
4950
throw error;

src/QueryEditor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Editor, { EditorProps } from '@monaco-editor/react';
33
import type * as Monaco from 'monaco-editor';
44
import { tsquery } from '@phenomnomnominal/tsquery';
55
import { sanitizeQuery } from './engine';
6+
import { isSyntaxError } from './tsquery-util';
67

78
const MONACO_MODEL_MARKER_OWNER = 'tsquery';
89

@@ -73,7 +74,7 @@ function getSyntaxError(query: string): Error | null {
7374
tsquery.parse(query);
7475
return null;
7576
} catch (error) {
76-
if (error instanceof SyntaxError) {
77+
if (isSyntaxError(error)) {
7778
return error;
7879
}
7980
throw error;

src/tsquery-util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type SyntaxError = Error & { name: 'SyntaxError' };
2+
3+
export function isSyntaxError(error: unknown): error is SyntaxError {
4+
// can't rely on instanceof SyntaxError as errors from the esquery parser are not instances of the global SyntaxError
5+
return error instanceof Error && error.name === 'SyntaxError';
6+
}

0 commit comments

Comments
 (0)