Skip to content

perf: parse code once and share the AST via context - #594

Open
xbinaryx wants to merge 2 commits into
eslint:mainfrom
xbinaryx:share-ast-parse
Open

xbinaryx wants to merge 2 commits into
eslint:mainfrom
xbinaryx:share-ast-parse

Conversation

@xbinaryx

@xbinaryx xbinaryx commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Prerequisites checklist

AI acknowledgment

  • I did not use AI to generate this PR.
  • (If the above is not checked) I have reviewed the AI-generated content before submitting.

What is the purpose of this pull request?

Every component that called useAST() parsed the code on its own. Up to three are mounted at once (App, EsquerySelectorInput, and the AST or Scope tab), so each code edit could trigger three parses. Even typing in the ESQuery selector re-parsed the code in each of them.

The Scope tab had a separate problem. The React Compiler silently skipped the Scope component because of a conditional inside a try/catch. With nothing memoized, eslintScope.analyze ran on every render of the tab, including on every selector keystroke.

What changes did you make? (Give an overview)

  • Added an ASTProvider that parses the code once and shares the result through context. useAST() now reads from that context.
  • Memoized the parse and the ESQuery match separately, so typing in the selector reruns only the match.
  • Moved the try/catch in Scope into an analyzeScope helper and memoized the analysis on the AST and the JS options.
  • Exported the ExplorerState type for the parse input, and updated docs/adding-languages.md to point to the new file.

Related Issues

#64

Is there anything you'd like reviewers to focus on?

Summary by CodeRabbit

  • New Features
    • Added a unified code editor and analysis experience. The editor displays code for the selected language, highlights matching syntax when parsing succeeds, and preserves code entered for other languages.
    • Scope analysis now displays clear error states when parsing or analysis fails.
  • Documentation
    • Updated language contribution instructions to reflect the current parsing workflow.

@netlify

netlify Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for eslint-code-explorer ready!

Name Link
🔨 Latest commit 9762495
🔍 Latest deploy log https://app.netlify.com/projects/eslint-code-explorer/deploys/6ade3d85a99d20b6b4b20f18
😎 Deploy Preview https://deploy-preview-594--eslint-code-explorer.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@eslintbot eslintbot added this to Triage Oct 17, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in Triage Oct 17, 2026
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Central YAML; Path: .coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 083db3bc-1025-4606-837d-d17bb8db0d21
Commits

Reviewing files that changed from the base of the PR and between 8456248 and 9762495.

Files selected for processing: 7
  • docs/adding-languages.md
  • src/app.tsx
  • src/components/ast-provider.tsx
  • src/components/ast/index.tsx
  • src/components/code-editor.tsx
  • src/components/esquery-selector-input.tsx
  • src/components/scope/index.tsx

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

AST parsing and selector matching now run through a React provider. The editor and analysis panels consume AST results through that provider. Scope analysis is memoized and reports analysis errors separately from AST errors.

Changes: AST and Scope Analysis

Layer / File(s) Summary
Provide and consume AST results
src/components/ast-provider.tsx, docs/adding-languages.md
ASTProvider memoizes parsing and selector matching and exposes results through useAST. Language instructions now direct contributors to add parsing cases in parseCode.
Connect the editor to AST results
src/components/code-editor.tsx, src/app.tsx, src/components/ast/index.tsx, src/components/esquery-selector-input.tsx, src/hooks/use-explorer.ts
App wraps the panels with ASTProvider and uses CodeEditor. CodeEditor displays and updates the active language’s code and passes matched ranges to Editor. AST consumers import useAST from the provider. ExplorerState is exported.
Memoize and report scope analysis
src/components/scope/index.tsx
Scope memoizes analysis by AST result and JavaScript options. Scope-analysis errors and AST errors each render ErrorState.

Priority: Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant useExplorer
  participant ASTProvider
  participant parseCode
  participant addEsqueryMatchedNodes
  participant useAST
  participant CodeEditor
  participant Editor
  useExplorer->>ASTProvider: Provide parsing inputs and selector
  ASTProvider->>parseCode: Parse language, code, and options
  parseCode-->>ASTProvider: Return parse result
  ASTProvider->>addEsqueryMatchedNodes: Match nodes using result and selector
  CodeEditor->>useAST: Read AST result from context
  useAST-->>CodeEditor: Return AST result
  CodeEditor->>Editor: Pass active code and matched ranges
Loading

Suggested reviewers: dmartens

Merge Risk: Minimal · up to 97624

No actionable merge-blocking issue was identified; the change is mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 9 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: parsing code once and sharing the AST through context to improve performance.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 9 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@DMartens DMartens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the PR.
I am assuming this is PR lays the groundwork for the mentioned the issue?

Comment thread src/index.tsx Resolved
Comment thread src/hooks/use-ast.tsx Resolved
Comment thread src/components/ast-provider.tsx Resolved
],
);
const astResult = useMemo(
() => addEsqueryMatchedNodes(astParseResult, selector),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't we move addEsqueryMatchedNodes move into its own context provider / memoized hook?
The data is required for the input and the editor but not the AST view.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The AST tree highlights matched nodes too.

children: React.ReactNode;
};

const ASTProviderContext = createContext<ASTResult | null>(null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am unsure why we should convert this hook to a context.
Can you please explain the reasoning for this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Each useAST() call parsed the code on its own, so up to three components re-parsed on every edit and selector keystroke. The context parses once and shares the result.

@DMartens DMartens moved this from Needs Triage to Implementing in Triage Oct 23, 2026
@xbinaryx

Copy link
Copy Markdown
Contributor Author

I am assuming this is PR lays the groundwork for the mentioned the issue?

Yes.

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions Bot added the Stale label Nov 4, 2026
@Pixel998 Pixel998 added accepted There is consensus among the team that this change meets the criteria for inclusion and removed Stale labels Nov 7, 2026
@Pixel998

Copy link
Copy Markdown
Contributor

There's a merge conflict now. @xbinaryx, could you please take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accepted There is consensus among the team that this change meets the criteria for inclusion chore

Projects

Status: Implementing

Development

Successfully merging this pull request may close these issues.

3 participants