Skip to content

Commit 29d7617

Browse files
authored
refactor: use types from @eslint/core (#223)
1 parent 764d0f6 commit 29d7617

File tree

6 files changed

+46
-43
lines changed

6 files changed

+46
-43
lines changed

package-lock.json

Lines changed: 29 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"zustand": "^4.5.7"
7373
},
7474
"devDependencies": {
75+
"@eslint/core": "^0.17.0",
7576
"@types/eslint-scope": "^8.3.2",
7677
"@types/espree": "^10.1.0",
7778
"@types/esquery": "^1.5.4",

src/components/editor.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import {
1313
ESLintPlaygroundTheme,
1414
ESLintPlaygroundHighlightStyle,
1515
} from "@/utils/codemirror-themes";
16-
import {
17-
highlightedRangesExtension,
18-
type HighlightedRange,
19-
} from "@/utils/highlighted-ranges";
16+
import { highlightedRangesExtension } from "@/utils/highlighted-ranges";
17+
import type { SourceRange } from "@eslint/core";
2018

2119
const languageExtensions: Record<
2220
Language,
@@ -32,7 +30,7 @@ const languageExtensions: Record<
3230
type EditorProperties = {
3331
readOnly?: boolean;
3432
value?: string;
35-
highlightedRanges?: HighlightedRange[];
33+
highlightedRanges?: SourceRange[];
3634
onChange?: (value: string) => void;
3735
};
3836

src/hooks/use-ast.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as espree from "espree";
22
import esquery from "esquery";
33
import type { Node as EstreeNode } from "estree";
4+
import type { ParseResult, FileError } from "@eslint/core";
45
import css from "@eslint/css";
56
import json from "@eslint/json";
67
import markdown from "@eslint/markdown";
@@ -21,9 +22,7 @@ export function useAST() {
2122
esquerySelector,
2223
} = useExplorer();
2324

24-
let astParseResult:
25-
| { ok: true; ast: unknown }
26-
| { ok: false; errors: Array<unknown> };
25+
let astParseResult: ParseResult<unknown>;
2726

2827
switch (language) {
2928
case "javascript": {
@@ -40,7 +39,7 @@ export function useAST() {
4039
astParseResult = { ast, ok: true };
4140
} catch (err) {
4241
// 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)
43-
astParseResult = { ok: false, errors: [err] };
42+
astParseResult = { ok: false, errors: [err as FileError] };
4443
}
4544
break;
4645
}

src/lib/convert-nodes-to-ranges.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
import type { HighlightedRange } from "@/utils/highlighted-ranges";
1+
import type { SourceRange } from "@eslint/core";
22

33
export function convertNodesToRanges(
44
esqueryMatchedNodes: unknown[],
5-
): HighlightedRange[] {
6-
const highlightedRanges: HighlightedRange[] = esqueryMatchedNodes
5+
): SourceRange[] {
6+
const highlightedRanges: SourceRange[] = esqueryMatchedNodes
77
.map(node => {
88
if (isNodeWithStartEnd(node)) {
9-
return [node.start, node.end] satisfies HighlightedRange;
9+
return [node.start, node.end] satisfies SourceRange;
1010
}
1111
if (isNodeWithPosition(node)) {
1212
return [
1313
node.position.start.offset,
1414
node.position.end.offset,
15-
] satisfies HighlightedRange;
15+
] satisfies SourceRange;
1616
} else if (isNodeWithLoc(node)) {
1717
return [
1818
node.loc.start.offset,
1919
node.loc.end.offset,
20-
] satisfies HighlightedRange;
20+
] satisfies SourceRange;
2121
} else if (isNodeWithRange(node)) {
22-
return [
23-
node.range[0],
24-
node.range[1],
25-
] satisfies HighlightedRange;
22+
return [node.range[0], node.range[1]] satisfies SourceRange;
2623
}
2724
})
2825
.filter(range => range !== undefined);
@@ -48,7 +45,7 @@ type PositionElem = {
4845
};
4946

5047
type NodeWithRange = {
51-
range: [number, number];
48+
range: SourceRange;
5249
};
5350

5451
function isNodeWithStartEnd(node: unknown): node is NodeWithStartEnd {

src/utils/highlighted-ranges.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Decoration, ViewPlugin } from "@codemirror/view";
2+
import type { SourceRange } from "@eslint/core";
23

34
const highlightRangeDecoration = Decoration.mark({
45
class: "bg-editorHighlightedRangeColor",
56
});
6-
export type HighlightedRange = [rangeFrom: number, rangeTo: number];
77

8-
export const highlightedRangesExtension = (ranges: HighlightedRange[]) =>
8+
export const highlightedRangesExtension = (ranges: SourceRange[]) =>
99
ViewPlugin.define(() => ({}), {
1010
decorations: () =>
1111
Decoration.set(

0 commit comments

Comments
 (0)