Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(website): esquery is not working at TypescriptNode #8917

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/website/src/components/ast/ASTViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function tryToApplyFilter<T>(value: T, filter?: ESQuery.Selector): T | T[] {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (window.esquery && filter) {
// @ts-expect-error - esquery requires js ast types
return window.esquery.match(value, filter);
return window.esquery.match(value, filter, {
visitorKeys: window.visitorKeys,
});
}
} catch (e: unknown) {
console.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const useSandboxServices = (

window.system = system;
window.esquery = lintUtils.esquery;
window.visitorKeys = lintUtils.visitorKeys;

const webLinter = createLinter(
system,
Expand Down
1 change: 1 addition & 0 deletions packages/website/src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ declare global {
require: WindowRequire;
esquery: typeof esquery;
system: unknown;
visitorKeys: Record<string, readonly string[] | undefined>;
}
}
25 changes: 22 additions & 3 deletions packages/website/typings/esquery.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,33 @@ declare module 'esquery' {
declare function query(ast: Node, selector: string): Node[];

declare namespace query {
interface ESQueryOptions {
nodeTypeKey?: string;
visitorKeys?: Record<string, readonly string[]>;
fallback?: (node: Node) => string[];
matchClass?: (className: string, node: Node, ancestry: Node[]) => boolean;
}
/** Parse a selector and return its AST. */
function parse(selector: string): Selector;
/** From a JS AST and a selector AST, collect all JS AST nodes that match the selector. */
function match(ast: Node, selector: Selector): Node[];
function match(
ast: Node,
selector: Selector,
options?: ESQueryOptions,
): Node[];
/** Given a `node` and its ancestors, determine if `node` is matched by `selector`. */
function matches(node: Node, selector: Selector, ancestry: Node[]): boolean;
function matches(
node: Node,
selector: Selector,
ancestry: Node[],
options?: ESQueryOptions,
): boolean;
/** Query the code AST using the selector string. */
function query(ast: Node, selector: string): Node[];
function query(
ast: Node,
selector: string,
options?: ESQueryOptions,
): Node[];

//
// Unions
Expand Down