Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"@codemirror/lang-css": "^6.3.1",
"@codemirror/lang-html": "^6.4.9",
"@codemirror/lang-javascript": "^6.2.2",
"@codemirror/lang-json": "^6.0.1",
"@codemirror/lang-markdown": "^6.2.5",
Expand All @@ -43,6 +44,7 @@
"@eslint/js": "^9.9.0",
"@eslint/json": "^0.12.0",
"@eslint/markdown": "^6.4.0",
"@html-eslint/eslint-plugin": "^0.40.3",
"@lezer/highlight": "^1.2.1",
"@radix-ui/react-accordion": "^1.2.0",
"@radix-ui/react-dialog": "^1.1.1",
Expand Down
7 changes: 7 additions & 0 deletions public/languages/html.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions src/components/ast/html-ast-tree-item.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
import { TreeEntry } from "../tree-entry";
import type { FC } from "react";
import { cn } from "@/lib/utils";

type ASTNode = {
readonly type: string;
readonly [key: string]: unknown;
};

export type HtmlAstTreeItemProperties = {
readonly index: number;
readonly data: ASTNode;
readonly esqueryMatchedNodes: ASTNode[];
};

export const HtmlAstTreeItem: FC<HtmlAstTreeItemProperties> = ({
data,
index,
esqueryMatchedNodes,
}) => {
const isEsqueryMatchedNode = esqueryMatchedNodes.includes(data);

return (
<AccordionItem
value={`${index}-${data.type}`}
className={cn(
"border border-card rounded-lg overflow-hidden",
isEsqueryMatchedNode && "border-primary border-4",
)}
>
<AccordionTrigger className="text-sm bg-card px-4 py-3 capitalize">
{data.type}
</AccordionTrigger>
<AccordionContent className="p-4 border-t">
<div className="space-y-1">
{Object.entries(data).map(item => (
<TreeEntry
key={item[0]}
data={item}
esqueryMatchedNodes={esqueryMatchedNodes}
/>
))}
</div>
</AccordionContent>
</AccordionItem>
);
};
44 changes: 44 additions & 0 deletions src/components/ast/html-ast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Accordion } from "@/components/ui/accordion";
import { Editor } from "@/components/editor";
import { useAST } from "@/hooks/use-ast";
import { useExplorer } from "@/hooks/use-explorer";
import {
HtmlAstTreeItem,
type HtmlAstTreeItemProperties,
} from "./html-ast-tree-item";
import type { FC } from "react";
import { parseError } from "@/lib/parse-error";
import { ErrorState } from "../error-boundary";

export const HtmlAst: FC = () => {
const result = useAST();
const { viewModes } = useExplorer();
const { astView } = viewModes;

if (!result.ok) {
const message = parseError(result.errors[0]);
return <ErrorState message={message} />;
}

const ast = JSON.stringify(result.ast, null, 2);

if (astView === "tree") {
return (
<Accordion
type="multiple"
className="px-8 pb-4 font-mono space-y-3 min-w-max"
defaultValue={["0-Program"]}
>
<HtmlAstTreeItem
data={result.ast as HtmlAstTreeItemProperties["data"]}
index={0}
esqueryMatchedNodes={
result.esqueryMatchedNodes as HtmlAstTreeItemProperties["esqueryMatchedNodes"]
}
/>
</Accordion>
);
}

return <Editor readOnly value={ast} />;
};
3 changes: 3 additions & 0 deletions src/components/ast/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { JavascriptAst } from "./javascript-ast";
import { JsonAst } from "./json-ast";
import { CssAst } from "./css-ast";
import { MarkdownAst } from "./markdown-ast";
import { HtmlAst } from "./html-ast";
import type { FC } from "react";

export const Ast: FC = () => {
Expand All @@ -17,6 +18,8 @@ export const Ast: FC = () => {
return <JsonAst />;
case "css":
return <CssAst />;
case "html":
return <HtmlAst />;
default:
return <JavascriptAst />;
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { json } from "@codemirror/lang-json";
import { javascript } from "@codemirror/lang-javascript";
import { markdown } from "@codemirror/lang-markdown";
import { css } from "@codemirror/lang-css";
import { html } from "@codemirror/lang-html";
import { EditorView } from "@codemirror/view";
import { EditorState } from "@codemirror/state";
import clsx from "clsx";
Expand All @@ -27,6 +28,7 @@ const languageExtensions: Record<string, (isJSX?: boolean) => LanguageSupport> =
json: () => json(),
markdown: () => markdown(),
css: () => css(),
html: () => html(),
};

type EditorProperties = {
Expand Down
6 changes: 6 additions & 0 deletions src/components/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ const JavaScriptPanel = () => {
);
};

const HTMLPanel: React.FC = () => {
return <></>;
};

const Panel = ({ language }: { language: string }) => {
switch (language) {
case "json":
Expand All @@ -188,6 +192,8 @@ const Panel = ({ language }: { language: string }) => {
return <MarkdownPanel />;
case "css":
return <CssPanel />;
case "html":
return <HTMLPanel />;
default:
return <JavaScriptPanel />;
}
Expand Down
12 changes: 12 additions & 0 deletions src/hooks/use-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Node as EstreeNode } from "estree";
import css from "@eslint/css";
import json from "@eslint/json";
import markdown from "@eslint/markdown";
import html from "@html-eslint/eslint-plugin";
import esquery from "esquery";
import { useExplorer } from "@/hooks/use-explorer";
import { assertIsUnreachable } from "@/lib/utils";
Expand Down Expand Up @@ -86,6 +87,17 @@ export function useAST() {
break;
}

case "html": {
const language = html.languages.html;
astParseResult = language.parse({
body: code.html,
path: "",
physicalPath: "",
bom: false,
});
break;
}

default: {
assertIsUnreachable(language);
}
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/use-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "../lib/const";
export type SourceType = Exclude<Options["sourceType"], undefined>;
export type Version = Exclude<Options["ecmaVersion"], undefined>;
export type Language = "javascript" | "json" | "markdown" | "css";
export type Language = "javascript" | "json" | "markdown" | "css" | "html";
export type JsonMode = "json" | "jsonc" | "json5";
export type MarkdownMode = "commonmark" | "gfm";
export type MarkdownFrontmatter = "off" | "yaml" | "toml";
Expand All @@ -28,6 +28,7 @@ export type Code = {
json: string;
markdown: string;
css: string;
html: string;
};
export type JsOptions = {
parser: string;
Expand Down
30 changes: 30 additions & 0 deletions src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export const languages = [
label: "CSS",
icon: "/languages/css.svg",
},
{
value: "html",
label: "HTML",
icon: "/languages/html.svg",
},
];

export const parsers = [
Expand Down Expand Up @@ -376,11 +381,36 @@ p {
margin: 1em 0;
}`.trim();

export const defaultHtmlCode = `
<!DOCTYPE html>
<!--
Type or paste some HTML here to learn more about
the static analysis that ESLint can do for you.

The tabs are:

- AST - The Abstract Syntax Tree of the code, which can
be useful to understand the structure of the code. You
can view this structure as JSON or in a tree format.
-->

<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML</title>
</head>
<body>
<p>Text</p>
</body>
</html>
`.trim();

export const defaultCode: Code = {
javascript: defaultJsCode,
json: defaultJsonCode,
markdown: defaultMarkdownCode,
css: defaultCssCode,
html: defaultHtmlCode,
};

export const defaultJsOptions: JsOptions = {
Expand Down
21 changes: 21 additions & 0 deletions src/lib/convert-nodes-to-ranges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export function convertNodesToRanges(
node.loc.start.offset,
node.loc.end.offset,
] satisfies HighlightedRange;
} else if (isNodeWithRange(node)) {
return [
node.range[0],
node.range[1],
] satisfies HighlightedRange;
}
})
.filter(range => range !== undefined);
Expand All @@ -42,6 +47,10 @@ type PositionElem = {
offset: number;
};

type NodeWithRange = {
range: [number, number];
};

function isNodeWithStartEnd(node: unknown): node is NodeWithStartEnd {
return (
typeof node === "object" &&
Expand Down Expand Up @@ -92,3 +101,15 @@ function isNodeWithLoc(node: unknown): node is NodeWithLoc {
typeof node.loc.end.offset === "number"
);
}

function isNodeWithRange(node: unknown): node is NodeWithRange {
return (
typeof node === "object" &&
node !== null &&
"range" in node &&
Array.isArray(node.range) &&
node.range.length === 2 &&
typeof node.range[0] === "number" &&
typeof node.range[1] === "number"
);
}
3 changes: 3 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default defineConfig({
"@": path.resolve(__dirname, "src"),
},
},
optimizeDeps: {
include: ["@html-eslint/eslint-plugin"],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

html-eslint is written in commonjs. (https://vite.dev/config/dep-optimization-options)

},
build: {
outDir: "build",
},
Expand Down