<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Herrington Darkholme on Medium]]></title>
        <description><![CDATA[Stories by Herrington Darkholme on Medium]]></description>
        <link>https://medium.com/@hchan_nvim?source=rss-1cebc1e355be------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*xfzPvW5O_A3kiBJC</url>
            <title>Stories by Herrington Darkholme on Medium</title>
            <link>https://medium.com/@hchan_nvim?source=rss-1cebc1e355be------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 09:13:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hchan_nvim/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[ast-grep’s Journey to AI Generated Rules]]></title>
            <link>https://medium.com/@hchan_nvim/ast-greps-journey-to-ai-generated-rules-80db3c4a7e26?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/80db3c4a7e26</guid>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Sun, 22 Jun 2025 20:05:20 GMT</pubDate>
            <atom:updated>2025-06-22T20:31:03.272Z</atom:updated>
            <content:encoded><![CDATA[<p>ast-grep is a command-line tool that empowers developers to find and replace code with precision. It operates directly on the syntax tree (AST), the true structure of your code. While powerful, writing ast-grep rules is a hurdle that requires grokking the tool.</p><p>This project has always been about designed and built as a tool for human beings, not for AI hype. But ast-grep been <a href="https://ast-grep.github.io/blog/more-llm-support.html">exploring how to use Al</a> to improve the human experience. This is the story of my journey into using Al to generate YAML rules.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fsummer-heart-0930.chufeiyun1688.workers.dev%3A443%2Fhttps%2Fwww.youtube.com%2Fembed%2F2hah-9N5YQ8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fsummer-heart-0930.chufeiyun1688.workers.dev%3A443%2Fhttps%2Fwww.youtube.com%2Fshorts%2F2hah-9N5YQ8%3Ffeature%3Dshare&amp;image=https%3A%2F%2Fsummer-heart-0930.chufeiyun1688.workers.dev%3A443%2Fhttps%2Fi.ytimg.com%2Fvi%2F2hah-9N5YQ8%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a36abac4185b9cea83e8c0a18c2f065/href">https://medium.com/media/9a36abac4185b9cea83e8c0a18c2f065/href</a></iframe><h3>Why ast-grep Rules are Hard for AI</h3><p>Generating ast-grep rules is not an easy task,especially for AI. First, ast-grep is a relatively new tool, so Large Language Models have not been extensively trained on its specific syntax, leading to hallucinations or a complete lack of understanding. While <a href="https://www.prompthub.us/blog/in-context-learning-guide">in-context learning</a> can help an LLM grasp the basic syntax, it often fails to address the problem of <a href="https://arxiv.org/abs/2505.24187v1">compounding errors</a>.</p><p>This is particularly true for ast-grep, where rules are frequently composed of smaller, <a href="https://ast-grep.github.io/guide/rule-config/atomic-rule.html">atomic rules</a>. Human developers often need to debug these smaller rules through <a href="https://ast-grep.github.io/advanced/faq.html#my-pattern-does-not-work-why">trial and error</a>, and pay extra attention to how they are orchestrated into a <a href="https://ast-grep.github.io/advanced/faq.html#my-rule-does-not-work-why">working composition</a>. For an LLM, a small mistake in one part of the rule can snowball into a completely incorrect rule. Quoting from the paper <a href="https://arxiv.org/abs/2305.18654">Faith and Fate</a>:</p><blockquote>These tasks require breaking problems down into sub-steps and synthesizing these steps into a precise answer.</blockquote><p>This explains why a simple LLM ask won’t work for ast-grep. However, recent advancements in “thinking” and “agentic” models, which can correct their mistakes, may <a href="https://arxiv.org/abs/2501.15602v2">reduce the probability of such errors</a>. This makes it a good time to give AI-powered ast-grep rule generation another try.</p><h3>The First Foray a Year Ago</h3><p>My initial attempt, about a year ago, was a lesson in humility. I tried to build an agent using <a href="https://dspy.ai/">DSPy</a> with a rigid, fixed pipeline: generate use cases, create code examples, describe the rule in natural language, translate that description to an ast-grep rule, and finally, verify it.</p><p>The results were, to put it mildly, not good.</p><p>The models I was using are mostly free-tier APIs from <a href="https://ollama.com/">Ollama</a> and <a href="https://aistudio.google.com">aistudio</a>(thank <a href="https://finance.yahoo.com/quote/GOOG/">$GOOG</a>). They struggled to adhere to a consistent output format. They often failed to generate correct code examples, let alone valid ast-grep rules. The limited context windows meant I couldn’t even provide sufficient instructions to guide them properly. It felt like I was missing two crucial pieces: a way for the agent to refine a rule based on search result feedback, and a method to dynamically break down complex requests into smaller, manageable rules. The agent architecture could solve these, but the underlying models could not.</p><h3>One Year After: Better Models, Better Agents</h3><p>Fast forward to today. The landscape has changed dramatically. Modern LLMs boast long context windows and a much-improved ability to follow complex instructions. Armed with these new capabilities and inspired by <a href="https://www.anthropic.com/engineering/building-effective-agents">Anthropic’s new guide</a>, I tried again.</p><p>This time, I discovered that a complex agent framework wasn’t necessary. AI programming tools like <a href="https://www.cursor.com/">Cursor</a> have sufficient agent frameworks for tool developers. The secret sauce was something far simpler: prompt engineering. And by “ <a href="https://www.promptingguide.ai/">prompt engineering</a>”, I don’t mean any framework or trick. It’s just writing a clear, human-readable manual for both human and Al.</p><p>Before the prompt engineering, however, I saw some interesting failure modes across different models that somehow reflects different LLM vendors’ training setup. All of them did a decent job of interpreting the user’s intent and creating relevant code examples, but their approaches to rule generation were wildly different.</p><ul><li><strong>OpenAI O3</strong> hallucinated with wild abandon. It invented syntax that looked more like <a href="https://codeql.github.com/">CodeQL</a> or <a href="https://github.com/facebook/jscodeshift">jscodeshift</a>, completely ignoring the ast-grep documentation available online. It couldn’t recover from tool errors and would quickly give up on using the tools I gave it. It felt like OpenAI’s pretraining dataset glanced at my documentation, decided it knew better, and threw it in the bin. (Oops, like how OpenAI treated my resume)</li><li><strong>Gemini</strong> was a bit more grounded. Its hallucinations were at least in the right ballpark, borrowing syntax from a <a href="https://ast-grep.github.io/advanced/tool-comparison.html#semgrep">related but more established</a> tool, <a href="https://semgrep.dev/">Semgrep</a>. (Thanks for the flattery, again, $GOOG). It also showed a decent ability to recover from errors but had a stubborn streak, preferring to invent its own ast-grep cli commands rather than using the <a href="https://modelcontextprotocol.io">MCP</a> tools I <a href="https://github.com/ast-grep/ast-grep-mcp">provided</a>.</li><li><strong>Claude 4</strong> was the most promising out of the box. It correctly identified ast-grep and produced syntactically valid rules. Looks like Anthropic’s training data is indexing ast-grep’s doc! Hoooray! However, it struggled with subtle semantic details that would make a rule functionally correct. To its credit, it tried very hard, retrying the tools I gave it over and over with different inputs, demonstrating a dogged persistence the others lacked.</li></ul><h3>Prompting AI Agents like Teaching a Human</h3><p>After iterating on the prompt, I found that all three models could perform remarkably well. The key was to treat the prompt not as a magic incantation, but as a straightforward instruction manual.</p><p>The core of my final prompt gives the Al a simple, five-step plan:</p><ol><li>First, clearly understand the user’s request.</li><li>Next, write a simple code snippet that the user wants to find.</li><li>Then, write an ast-grep rule that precisely matches that code snippet.</li><li><a href="https://github.com/ast-grep/ast-grep-mcp/blob/b69eb5391bd93d46ef3dec07de814c3c39675c8f/main.py#L33-L57">Test the rule</a> against the example to ensure it works as expected.</li><li>Finally, <a href="https://github.com/ast-grep/ast-grep-mcp/blob/b69eb5391bd93d46ef3dec07de814c3c39675c8f/main.py#L72-L82">search</a> the codebase with the verified rule.</li></ol><p>This clear, step-by-step process turned erratic geniuses into more reliable assistants. By breaking the problem down and providing a verification loop, I gave the models the structure they needed to succeed. You can see the full prompt in the <a href="https://github.com/ast-grep/ast-grep-mcp/blob/main/ast-grep.mdc">sg-mcp GitHub repository</a>.</p><p>The end result is quite impressive, see the <a href="https://youtube.com/shorts/2hah-9N5YQ8?si=bzl6PF2tuFbBwXpL">demo video</a> below. No speed up, but with music and cats. (It is oddly satisfactory to watch AI generating the rules while I myself is nodding like the kitty in the video)</p><p>This journey has reinforced a core belief: Al’s true power isn’t about replacing the developer, but about building better tools for them. It also demonstrates that the best way to teach an AI, with the progress of more capable Large Language Models, is not through complex frameworks or rigid pipelines, but through clear, human-readable instructions.</p><p>By teaching an Al to write rules, I hope it makes ast-grep more accessible, more powerful, and easier to use. Happy grepping!</p><p><em>Originally published at </em><a href="https://ast-grep.github.io/blog/ast-grep-agent.html"><em>https://ast-grep.github.io</em></a><em> on June 22, 2025.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80db3c4a7e26" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ast-grep Rockets to 8000 Stars!]]></title>
            <link>https://medium.com/@hchan_nvim/ast-grep-rockets-to-8000-stars-d06fed5abe02?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/d06fed5abe02</guid>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Tue, 04 Mar 2025 04:46:58 GMT</pubDate>
            <atom:updated>2025-03-04T04:53:08.574Z</atom:updated>
            <content:encoded><![CDATA[<p>We are absolutely bursting with excitement to announce that ast-grep has soared past <strong>8,000 stars</strong> on GitHub! Every star represents a developer who sees the potential in ast-grep, and we’re deeply grateful for your support.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ziTEwetXAvw9UaUM.jpeg" /></figure><p>ast-grep’s mission to make code searching, linting, and rewriting more accessible and powerful has truly resonated with the community. This blog post is your guide to all the fantastic updates, encompassing both the core ast-grep CLI tool and our ever-improving website. Buckle up, let’s explore what’s new!</p><h3>Expanding the Language Universe: YAML, PHP, and More!</h3><p>ast-grep is rapidly becoming a truly polyglot code analysis powerhouse! We’ve significantly expanded our language support to empower you to work with even more of your codebase:</p><p><strong>YAML Support Arrives!</strong> YAML is the backbone of configuration for countless projects. Now, ast-grep CLI officially speaks <a href="https://ast-grep.github.io/catalog/yaml/">YAML</a>, allowing you to leverage the same powerful rule system to lint, search, and even rewrite your YAML configuration files. Imagine using ast-grep rules to enforce best practices in your Kubernetes manifests or streamline your CI/CD pipelines! And yes, you can even write ast-grep rules <em>using YAML</em> itself!</p><p><strong>Enhanced PHP Analysis:</strong> We’ve introduced a dedicated PHP language parser ( php-only-language) for the CLI. This means more accurate and reliable analysis for your PHP code, helping you catch tricky bugs and enforce code quality standards with greater confidence.</p><p><strong>Dynamic Languages in APIs:</strong> Python and JavaScript API users, rejoice! You can now tap into dynamic language support within <a href="https://github.com/ast-grep/ast-grep/blob/main/crates/pyo3/tests/test_register_lang.py">PyO3</a> and <a href="https://github.com/ast-grep/ast-grep/blob/main/crates/napi/__test__/custom.spec.ts">napi</a>. This unlocks exciting possibilities for extending ast-grep’s reach and integrating it into even more diverse and dynamic environments.</p><p><strong>Embedded Language in HTML:</strong> We’ve refined support for registering <a href="https://ast-grep.github.io/advanced/language-injection.html">embedded languages</a> in the CLI, giving you even more flexibility when dealing with complex code structures like searching JavaScript/CSS in HTML.</p><h3>More Powerful Rules &amp; Patterns</h3><p>We’ve been laser-focused on making the ast-grep’s rule system an even more powerful and precise tool for code manipulation:</p><p><strong>CSS inspired </strong><strong>nthChild Matcher:</strong> <a href="https://ast-grep.github.io/guide/rule-config/atomic-rule.html#nthchild">nthChild</a> is a rule to find nodes based on their positions in the parent node&#39;s children list. It is heavily inspired by CSS&#39;s nth-child pseudo-class and helps you target specific nodes in a more granular way.</p><p><strong>Pinpoint Precision with </strong><strong>range Matchers:</strong> Need to refine your rules to target a very specific section of code, even down to the character? ast-grep now supports <a href="https://ast-grep.github.io/guide/rule-config/atomic-rule.html#range">range</a> matchers! You can define rules that activate only within a particular line <em>and</em> character column range. This is useful for interacting with external tools like compilers.</p><p><strong>Pattern with </strong><strong>--selector and </strong><strong>--strictness in </strong><strong>sg run:</strong> Need to fine tune your search pattern? The --selector and --strictness flag in gives you fine-grained control over pattern matching.</p><p><strong>Simplified Suppression with </strong><strong>ast-grep-ignore:</strong> <a href="https://ast-grep.github.io/guide/project/severity.html">Suppressing rules</a> just got simpler! You can now use the ast-grep-ignore comment directly on the same line as the code you want to exclude. Less clutter, more control.</p><p><strong>More Robust Partial Pattern Snippet</strong> The ERROR node in patterns can now match <em>anything</em>. This makes partial pattern snippet even more robust.</p><h3>Sharpening the Code Search CLI</h3><p><strong>Glob Path Matching &amp; Symbolic Link Traversal Unleashed:</strong> CLI users can now leverage the power of <a href="https://ast-grep.github.io/reference/cli/run.html#globs-globs">glob patterns</a> to specify file paths and effortlessly traverse <a href="https://ast-grep.github.io/reference/cli/run.html#follow">symbolic links</a>. Navigating and analyzing your projects is now more intuitive than ever.</p><p><strong>Rule Entity Inspection &amp; Overwrite: Deeper Insights, More Control:</strong> Gain insights by --inspect CLI flag with <a href="https://ast-grep.github.io/reference/cli/scan.html#inspect-granularity">semi-structured tracing output</a>. This feature empowers advanced users with deeper debugging and customization capabilities.</p><p><strong>Contextual Code Scanning with Before/After Flags:</strong> Enhance your CLI scan results with surrounding code context using the new context, before, and after flags. Understand the bigger picture around your matches at a glance.</p><p><strong>Know Your Impact: Fixed Rules Count:</strong> The CLI now prints a count of fixed rules, giving you immediate feedback on the scope of your code modifications.</p><p><strong>Debugging Supercharged:</strong> We’ve significantly improved debugging with prettified pattern output, Debug AST/CST visualization, and colorized output via the <a href="https://ast-grep.github.io/reference/cli/run.html#debug-query-format">--debug-query</a> flag. Troubleshooting and refining your rules is now a much smoother and more visual experience.</p><h3>Enhanced Tooling and API Experience</h3><p>We’re committed to providing a seamless developer experience across all of ast-grep’s interfaces:</p><p><strong>Typed </strong><strong>SgNode and </strong><strong>SgRoot in NAPI:</strong> For our NAPI users, we&#39;ve introduced <a href="https://ast-grep.github.io/blog/typed-napi.html">typed </a><a href="https://ast-grep.github.io/blog/typed-napi.html">SgNode and </a><a href="https://ast-grep.github.io/blog/typed-napi.html">SgRoot</a>, significantly improving type safety and code clarity when working with the API. This enhancement is <a href="https://github.com/ast-grep/ast-grep/pull/1661">initiated</a> by <a href="https://github.com/mohebifar">mohebifar</a> from <a href="https://codemod.com/">Codemod</a>.</p><p><strong>Rule Config in </strong><strong>SgNode Match Methods:</strong> Flexibility at your fingertips! Rule configurations can now be <a href="https://github.com/ast-grep/ast-grep/pull/1730">passed directly</a> to SgNode match methods like matches, has, inside, follows, and precedes. Configure your rules dynamically within your code. This feature is also contributed by <a href="https://codemod.com/">mohebifar</a>.</p><p><strong>New </strong><strong>fieldChildren:</strong> The new fieldChildren method in NAPI and PyO3 provides easier access to named children nodes, simplifying AST traversal and manipulation in your API integrations.</p><p><strong>Powerful Code Modification in PyO3/NAPI:</strong> Unlock advanced code modification features with Fix Related Features and Modify Edit Range in <a href="https://ast-grep.github.io/guide/api-usage/py-api.html#fix-code">PyO3</a>/ <a href="https://ast-grep.github.io/guide/api-usage/js-api.html#fix-code">NAPI</a>. Refactoring and code transformation just got even more powerful from within your Python and JavaScript code.</p><p><strong>Smaller, Faster NAPI Binaries:</strong> We’ve reduced the NAPI binary size, resulting in smaller downloads and faster installations — get up and running with ast-grep even quicker!</p><p><strong>Robust Python Integration:</strong> Typings for PyO3 and strictness improvements in PyO3/YAML enhance the overall robustness and reliability of our Python integration.</p><h3>Website: Documentation &amp; Interactive Exploration</h3><p>The ast-grep website isn’t just a static page; it’s your interactive command center for learning, exploring, and mastering ast-grep! We’ve poured significant effort into expanding and refining the website to be your ultimate resource:</p><p><strong>Documentation Deep Dive:</strong> We’ve massively expanded and clarified the documentation, with deeper dives into crucial topics, clearer explanations of pattern objects, a comprehensive FAQ, and enhanced API documentation. Whether you’re a beginner or an expert, you’ll find valuable resources to level up your ast-grep skills.</p><p><strong>Revamped Blog Section:</strong> Dive into in-depth articles and latest news in the <a href="https://ast-grep.github.io/blog.html">brand-new blog section</a>. Stay up-to-date with the latest ast-grep insights and learn from real-world examples.</p><p><strong>Improved Sections &amp; Navigation:</strong> Finding what you need is now easier than ever with a reorganized and polished section and improved overall website navigation.</p><p><strong>Website Stability &amp; Polish:</strong> We’ve squashed styling issues, resolved mobile responsiveness problems, fixed typing errors, and eliminated broken links to ensure a smooth and reliable browsing experience across all devices.</p><h3>Interactive Example Catalog: Learn by Doing!</h3><p>The <a href="https://ast-grep.github.io/catalog">example catalog</a> has received a major upgrade, transforming it into an interactive learning environment:</p><p><strong>Interactive Rule Exploration:</strong> Dive deep into rules with interactive features like Rule Display &amp; Extraction, MetaVar Panel, Matched Labeling, Pattern Debugger, Selector Explorer, and Pattern Configuration &amp; Icons. Dissect rules, understand their components, and visualize how they work — all in your browser!</p><p><strong>Effortless Rule Discovery:</strong> Finding the right rule is now a breeze with new filters for language and sorting options.</p><p><strong>Enhanced Usability:</strong> Small but mighty additions like Empty Filter and Rule Counting further enhance the catalog’s ease of use.</p><p>See <a href="https://www.youtube.com/watch?v=oNbOoBhVL8o">the youtube video</a> for a live demo.</p><h3>Playground Power-Ups: Your Online Rule Lab</h3><p>The online playground at <a href="https://ast-grep.github.io/">https://ast-grep.github.io/</a> is now an even more powerful lab for experimenting and refining your rules:</p><p><strong>Parser Version Visibility:</strong> Small popups now display the tree-sitter version used in the playground, giving you valuable context for your rule testing.</p><p><strong>Reset &amp; Counter Enhancements:</strong> Thanks to <a href="https://github.com/zhangmo8">@zhangmo8</a>, we’ve added a Reset Button and a match counter to further streamline your playground workflow.</p><p><strong>CSS Support in Playground:</strong> The online playground now speaks CSS! Test your ast-grep rules directly on CSS code snippets.</p><h3>Performance Unleashed</h3><p>We’re obsessed with speed and efficiency! Here are the performance enhancements we’ve delivered in the CLI:</p><p><strong>Leaner Binaries, Faster Performance:</strong> Optimized printer implementation has resulted in significant binary size reduction and improved overall performance.</p><p><strong>Intelligent File Scanning:</strong> ast-grep now only scans rule-sensitive files, dramatically improving performance for large projects. Less scanning, faster results!</p><p><strong>cargo binstall for Instant Installs:</strong> Faster installation is now a reality with cargo binstall support. Get pre-built binaries and get analyzing your code in record time.</p><p><strong>Configurable Threads: Fine-Tune for Your Machine:</strong> Fine-tune performance by configuring the number of threads ast-grep uses. Optimize ast-grep for your specific hardware and project needs.</p><p>… and of course, numerous bug fixes under the hood to ensure a smoother, more reliable experience!</p><h3>🎉 Thank You — From the Bottom of Our Hearts! 🎉</h3><p>Reaching over 8000 stars is an absolutely fantastic milestone, and it’s all thanks to <em>you</em>, our incredible community! We are deeply grateful for your unwavering support, invaluable feedback, detailed bug reports, inspiring feature requests, and generous code contributions. You are the fuel that powers ast-grep’s rocket!</p><p><strong>Get Started &amp; Get Involved Today!</strong></p><ul><li><strong>Explore the Enhanced Website:</strong> Dive into the wealth of resources at <a href="https://ast-grep.github.io/">https://ast-grep.github.io/</a></li><li><strong>Star us on GitHub!</strong> Show your support and help us reach the next milestone: <a href="https://github.com/ast-grep/ast-grep">Star the GitHub Repo</a></li><li><strong>Try out the New Features &amp; Give Feedback:</strong> Join the conversation on <a href="https://discord.com/invite/4YZjf6htSQ">Discord</a> and tell us what you think!</li><li><strong>Contribute Rules to the Example Catalog:</strong> Share your expertise and help others by <a href="https://github.com/ast-grep/ast-grep.github.io/tree/main/website/catalog">contributing rules</a>.</li><li><strong>Report Bugs &amp; Feature Requests:</strong> Help us make ast-grep even better by reporting issues and suggesting new features on <a href="https://github.com/ast-grep/ast-grep/issues">GitHub Issues</a></li></ul><p>We’re incredibly excited to continue this journey with you! Let’s keep pushing the boundaries of code searching, linting, and rewriting, making it more powerful and accessible for everyone! 🚀 ✨</p><p><em>Originally published at </em><a href="https://ast-grep.github.io/blog/stars-8000.html"><em>https://ast-grep.github.io</em></a><em> on March 4, 2025.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d06fed5abe02" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Design Space for Code Search Query]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@hchan_nvim/design-space-for-code-search-query-0bd0b7a36a4b?source=rss-1cebc1e355be------2"><img src="https://cdn-images-1.medium.com/max/1850/0*FJG0com64U_WGbVY.png" width="1850"></a></p><p class="medium-feed-snippet">Code search is a critical tool for modern software development. It enables developers to quickly locate, understand, and reuse existing&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@hchan_nvim/design-space-for-code-search-query-0bd0b7a36a4b?source=rss-1cebc1e355be------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@hchan_nvim/design-space-for-code-search-query-0bd0b7a36a4b?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/0bd0b7a36a4b</guid>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Thu, 26 Dec 2024 00:02:42 GMT</pubDate>
            <atom:updated>2024-12-26T02:03:11.540Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[ast-grep’s Journey to Type Safety in Node API]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@hchan_nvim/ast-greps-journey-to-type-safety-in-node-api-3a5fbfc110ba?source=rss-1cebc1e355be------2"><img src="https://cdn-images-1.medium.com/max/1766/0*TOt7t4mpGaVBCw3T.jpeg" width="1766"></a></p><p class="medium-feed-snippet">Recipe to Craft Balanced Types: Design, Define, Refine, and Confine</p><p class="medium-feed-link"><a href="https://medium.com/@hchan_nvim/ast-greps-journey-to-type-safety-in-node-api-3a5fbfc110ba?source=rss-1cebc1e355be------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@hchan_nvim/ast-greps-journey-to-type-safety-in-node-api-3a5fbfc110ba?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a5fbfc110ba</guid>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Sun, 22 Dec 2024 16:32:08 GMT</pubDate>
            <atom:updated>2024-12-22T16:48:51.452Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Search Multi-language Documents in ast-grep​]]></title>
            <link>https://medium.com/@hchan_nvim/search-multi-language-documents-in-ast-grep-e2310091fa53?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2310091fa53</guid>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Wed, 24 Jul 2024 03:40:21 GMT</pubDate>
            <atom:updated>2024-07-24T03:40:21.394Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction<a href="https://ast-grep.github.io/advanced/language-injection.html#introduction">​</a></h3><p>ast-grep works well searching files of one single language, but it is hard to extract a sub language embedded inside a document.</p><p>However, in modern development, it’s common to encounter multi-language documents. These are source files containing code written in multiple different languages. Notable examples include:</p><ul><li>HTML files: These can contain JavaScript inside &lt;script&gt; tags and CSS inside &lt;style&gt; tags.</li><li>JavaScript files: These often contain regular expression, CSS style and query languages like graphql.</li><li>Ruby files: These can contain snippets of code inside heredoc literals, where the heredoc delimiter often indicates the language.</li></ul><p>These multi-language documents can be modeled in terms of a parent syntax tree with one or more injected syntax trees residing inside certain nodes of the parent tree.</p><p>ast-grep now supports a feature to handle language injection, allowing you to search for code written in one language within documents of another language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*ywMIHKUPKvUwPMkF.png" /></figure><p>This concept and terminology come from <a href="https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection">tree-sitter’s language injection</a>, which implies you can inject another language into a language document. (BTW, <a href="https://github.com/nvim-treesitter/nvim-treesitter?tab=readme-ov-file#adding-queries">neovim</a> also embraces this terminology.)</p><h3>Example: Search JS/CSS in the CLI<a href="https://ast-grep.github.io/advanced/language-injection.html#example-search-js-css-in-the-cli">​</a></h3><p>Let’s start with a simple example of searching for JavaScript and CSS within HTML files using ast-grep’s command-line interface (CLI). ast-grep has builtin support to search JavaScript and CSS inside HTML files.</p><h3>Using sg run: find patterns of CSS in an HTML file<a href="https://ast-grep.github.io/advanced/language-injection.html#using-sg-run-find-patterns-of-css-in-an-html-file">​</a></h3><p>Suppose we have an HTML file like below:</p><pre>&lt;style&gt;<br>  h1 { color: red; }<br>&lt;/style&gt;<br>&lt;h1&gt;<br>  Hello World!<br>&lt;/h1&gt;<br>&lt;script&gt;<br>  alert(&#39;hello world!&#39;)<br>&lt;/script&gt;</pre><p>Running this ast-grep command will extract the matching CSS style code out of the HTML file!</p><pre>sg run -p &#39;color: $COLOR&#39;</pre><p>ast-grep outputs this beautiful CLI report.</p><pre>./test.html<br>2│  h1 { color: red; }</pre><p>ast-grep works well even if just providing the pattern without specifying the pattern language!</p><h3>Using sg scan: find JavaScript in HTML with rule files<a href="https://ast-grep.github.io/advanced/language-injection.html#using-sg-scan-find-javascript-in-html-with-rule-files">​</a></h3><p>You can also use ast-grep’s <a href="https://ast-grep.github.io/guide/rule-config.html">rule file</a> to search injected languages.</p><p>For example, we can warn the use of alert in JavaScript, even if it is inside the HTML file.</p><pre>id: no-alert<br>language: JavaScript<br>severity: warning<br>rule:<br>  pattern: alert($MSG)<br>message: Prefer use appropriate custom UI instead of obtrusive alert call.</pre><p>The rule above will detect usage of alert in JavaScript. Running the rule via sg scan.</p><pre>sg scan --rule no-alert.yml</pre><p>The command leverages built-in behaviors in ast-grep to handle language injection seamlessly. It will produce the following warning message for the HTML file above.</p><pre>warning[no-alert]: Prefer use appropriate custom UI instead of obtrusive alert call.<br>  ┌─ ./test.html:8:3<br>  │<br>8 │   alert(&#39;hello world!&#39;)<br>  │   ^^^^^^^^^^^^^^^^^^^^^</pre><h3>How language injections work?<a href="https://ast-grep.github.io/advanced/language-injection.html#how-language-injections-work">​</a></h3><p>ast-grep employs a multi-step process to handle language injections effectively. Here’s a detailed breakdown of the workflow:</p><ol><li>File Discovery: The CLI first discovers files on the disk via the venerable <a href="https://crates.io/crates/ignore">ignore</a> crate, the same library under <a href="https://github.com/BurntSushi/ripgrep">ripgrep</a>’s hood.</li><li>Language Inference: ast-grep infers the language of each discovered file based on file extensions.</li><li>Injection Extraction: For documents that contain code written in multiple languages (e.g., HTML with embedded JS), ast-grep extracts the injected language sub-regions. At the moment, ast-grep handles HTML/JS/CSS natively.</li><li>Code Matching: ast-grep matches the specified patterns or rules against these regions. Pattern code will be interpreted according to the injected language (e.g. JS/CSS), instead of the parent document language (e.g. HTML).</li></ol><h3>Customize Language Injection: styled-components in JavaScript<a href="https://ast-grep.github.io/advanced/language-injection.html#customize-language-injection-styled-components-in-javascript">​</a></h3><p>You can customize language injection via the sgconfig.yml <a href="https://ast-grep.github.io/reference/sgconfig.html">configuration file</a>. This allows you to specify how ast-grep handles multi-language documents based on your specific needs, without modifying ast-grep&#39;s built-in behaviors.</p><p>Let’s see an example of searching CSS code in JavaScript. <a href="https://styled-components.com/">styled-components</a> is a library for styling React applications using <a href="https://bootcamp.uxdesign.cc/css-in-js-libraries-for-styling-react-components-a-comprehensive-comparison-56600605a5a1">CSS-in-JS</a>. It allows you to write CSS directly within your JavaScript via <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">tagged template literals</a>, creating styled elements as React components.</p><p>The example will configure ast-grep to detect styled-components’ CSS.</p><h3>Injection Configuration<a href="https://ast-grep.github.io/advanced/language-injection.html#injection-configuration">​</a></h3><p>You can add the languageInjections section in the project configuration file sgconfig.yml.</p><pre>languageInjections:<br>- hostLanguage: js<br>  rule:<br>    pattern: styled.$TAG`$CONTENT`<br>  injected: css</pre><p>Let’s break the configuration down.</p><ol><li>hostLanguage: Specifies the main language of the document. In this example, it is set to js (JavaScript).</li><li>rule: Defines the ast-grep rule to identify the injected language region within the host language.</li></ol><ul><li>pattern: The pattern matches styled components syntax where styled is followed by a tag (e.g., button, div) and a template literal containing CSS.</li><li>the rule should have a meta variable $CONTENT to specify the subregion of injected language. In this case, it is the content inside the template string.</li></ul><ol><li>injected: Specifies the injected language within the identified regions. In this case, it is css.</li></ol><h3>Example Match<a href="https://ast-grep.github.io/advanced/language-injection.html#example-match">​</a></h3><p>Consider a JSX file using styled components:</p><pre>import styled from &#39;styled-components&#39;;<br>const Button = styled.button`<br>  background: red;<br>  color: white;<br>  padding: 10px 20px;<br>  border-radius: 3px;<br>`<br>exporrt default function App() {<br>  return &lt;Button&gt;Click Me&lt;/Button&gt;<br>}</pre><p>With the above languageInjections configuration, ast-grep will:</p><ol><li>Identify the styled.button block as a CSS region.</li><li>Extract the CSS code inside the template literal.</li><li>Apply any CSS-specific pattern searches within this extracted region.</li></ol><p>You can search the CSS inside JavaScript in the project configuration folder using this command:</p><pre>sg -p &#39;background: $COLOR&#39; -C 2</pre><p>It will produce the match result:</p><pre>./styled.js<br>2│<br>3│const Button = styled.button`<br>4│  background: red;<br>5│  color: white;<br>6│  padding: 10px 20px;</pre><h3>Using Custom Language with Injection<a href="https://ast-grep.github.io/advanced/language-injection.html#using-custom-language-with-injection">​</a></h3><p>Finally, let’s look at an example of searching for GraphQL within JavaScript files. This demonstrates ast-grep’s flexibility in handling custom language injections.</p><h3>Define graphql custom language in sgconfig.yml.<a href="https://ast-grep.github.io/advanced/language-injection.html#define-graphql-custom-language-in-sgconfig-yml">​</a></h3><p>First, we need to register graphql as a custom language in ast-grep. See <a href="https://ast-grep.github.io/advanced/custom-language.html">custom language reference</a> for more details.</p><pre>customLanguages:<br>  graphql:<br>    libraryPath: graphql.so # the graphql tree-sitter parser dynamic library<br>    extensions: [graphql]   # graphql file extension<br>    expandoChar: $          # see reference above for explanation</pre><h3>Define graphql injection in sgconfig.yml.<a href="https://ast-grep.github.io/advanced/language-injection.html#define-graphql-injection-in-sgconfig-yml">​</a></h3><p>Next, we need to customize what region should be parsed as graphql string in JavaScript. This is similar to styled-components example above.</p><pre>languageInjections:<br>- hostLanguage: js<br>  rule:<br>    pattern: graphql`$CONTENT`<br>  injected: graphql</pre><h3>Search GraphQL in JavaScript<a href="https://ast-grep.github.io/advanced/language-injection.html#search-graphql-in-javascript">​</a></h3><p>Suppose we have this JavaScript file from <a href="https://relay.dev/">Relay</a>, a GraphQL client framework.</p><pre>import React from &quot;react&quot;<br>import { graphql } from &quot;react-relay&quot;</pre><pre>const artistsQuery = graphql`<br>  query ArtistQuery($artistID: String!) {<br>    artist(id: $artistID) {<br>      name<br>      ...ArtistDescription_artist<br>    }<br>  }<br>`</pre><p>We can search the GraphQL fragment via this --inline-rules scan.</p><pre>sg scan --inline-rules=&quot;{id: test, language: graphql, rule: {kind: fragment_spread}}&quot;</pre><p>Output</p><pre>help[test]:<br>  ┌─ ./relay.js:8:7<br>  │<br>8 │       ...ArtistDescription_artist<br>  │       ^^^^^^^^^^^^^^^^^^^^^^^^^^^</pre><h3>More Possibility to be Unlocked…<a href="https://ast-grep.github.io/advanced/language-injection.html#more-possibility-to-be-unlocked">​</a></h3><p>By following these steps, you can effectively use ast-grep to search and analyze code across multiple languages within the same document, enhancing your ability to manage and understand complex codebases.</p><p>This feature extends to various frameworks like <a href="https://vuejs.org/">Vue</a> and <a href="https://svelte.dev/">Svelte</a>, enables searching for <a href="https://x.com/peer_rich/status/1717609270475194466">SQL in React server actions</a>, and supports new patterns like <a href="https://x.com/hd_nvim/status/1815300932793663658">Vue-Vine</a>.</p><p>Hope you enjoy the feature! Happy ast-grepping!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2310091fa53" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How ast-grep Works: A bird’s-eye view]]></title>
            <link>https://medium.com/@hchan_nvim/how-ast-grep-works-a-birds-eye-view-d406e8fa92ec?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/d406e8fa92ec</guid>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Fri, 07 Jun 2024 02:12:17 GMT</pubDate>
            <atom:updated>2024-06-07T16:49:11.862Z</atom:updated>
            <content:encoded><![CDATA[<p>In the world of software development, efficiently searching, rewriting, linting, and analyzing code is essential for maintaining high-quality projects.</p><p>This is where <strong>ast-grep</strong> comes into play. Designed as a powerful structural search tool, ast-grep simplifies these tasks by leveraging the Abstract Syntax Tree (AST) representation of code. Let’s break down how ast-grep works with the help of a diagram.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zGKC8UedO2heeE0J.png" /></figure><h3>The Workflow of ast-grep</h3><p>Generally speaking, ast-grep takes user <em>queries of various input</em> formats, <em>parses the code into an AST</em> using TreeSitter, and performs <em>search, rewrite, lint, and analysis</em>, utilizing the full power of CPU cores.</p><h3>Query via Various Formats</h3><p>ast-grep can accept queries in multiple formats, making it flexible and user-friendly. Here are some common query formats:</p><ul><li><strong>Pattern Query</strong>: Users can define <a href="https://ast-grep.github.io/guide/pattern-syntax.html">specific patterns</a> to search for within their codebase.</li><li><strong>YAML Rule</strong>: Structured rules written in <a href="https://ast-grep.github.io/guide/rule-config.html">YAML</a> format to guide the search and analysis process.</li><li><strong>API Code</strong>: Direct <a href="https://ast-grep.github.io/guide/api-usage.html">API calls</a> for more programmatic control over the searching and rewriting tasks.</li></ul><h3>ast-grep’s Core</h3><p>ast-grep’s core functionality is divided into two main components: parsing and matching.</p><h4>1. Parsing with Tree-Sitter</h4><p>The core of ast-grep’s functionality relies on <strong>Tree-Sitter Parsers</strong>. <a href="https://tree-sitter.github.io/">TreeSitter</a> is a powerful parsing library that converts source code into an Abstract Syntax Tree (AST). This tree structure represents the syntactic structure of the code, making it easier to analyze and manipulate.</p><h4>2. Tree Matching</h4><p>Once the code is parsed into an AST, the ast-grep core takes over and finds the matching AST nodes based on the input queries. Written in <strong>Rust</strong>, ast-grep ensures efficient performance by utilizing full CPU cores. This means it can handle large codebases and perform complex searches and transformations quickly.</p><h3>Usage Scenarios</h3><p>ast-grep can be helpful for these scenarios.</p><ul><li><strong>Search</strong>: Find specific patterns or constructs within the code.</li><li><strong>Rewrite</strong>: Automatically refactor or transform code based on predefined rules or patterns.</li><li><strong>Lint</strong>: Identify and report potential issues or code smells.</li><li><strong>Analyze</strong>: Perform in-depth code analysis to gather insights and metrics.</li></ul><h3>Benefits of Using ast-grep</h3><ul><li><strong>Multi-Core Processing</strong>: ast-grep can handle multiple files in parallel by taking full advantage of multi-core processors. Typically ast-grep performs tasks faster than many other tools, making it suitable for large projects.</li><li><strong>Versatility</strong>: Whether you need to search for a specific code pattern, rewrite sections of code, lint for potential issues, or perform detailed analysis, ast-grep has you covered.</li></ul><h3>Example in the Real World</h3><ul><li><strong>Pattern + Search</strong>: <a href="https://coderabbit.ai/">CodeRabbit</a> uses ast-grep patterns to search code repo for code review knowledge. This example is collected from ast-grep’s own <a href="https://github.com/ast-grep/ast-grep/pull/780#discussion_r1425817237">dogfooding</a>.</li><li><strong>API + Rewrite</strong>: <a href="https://github.com/vue-macros/vue-macros-cli">@vue-macros/cli</a> is a CLI for rewriting at Vue Macros powered by ast-grep.</li><li><strong>YAML + Lint</strong>: <a href="https://github.com/vercel/turbo/pull/8275">Vercel turbo</a> is using ast-grep to lint their Rust code with <a href="https://github.com/vercel/turbo/blob/main/.config/ast-grep/rules/no-context.yml">custom rules</a>.</li></ul><h3>Conclusion</h3><p>ast-grep is a versatile and efficient tool for modern software development needs. By parsing code into an Abstract Syntax Tree and leveraging the power of Rust, it provides robust capabilities for searching, rewriting, linting, and analyzing code. With multiple input formats and the ability to utilize full CPU cores, ast-grep is designed to handle the demands of today’s complex codebases.</p><p>Whether you are maintaining a small project or a large enterprise codebase, ast-grep can help streamline your development workflow.</p><p><em>Originally published at </em><a href="https://ast-grep.github.io/advanced/how-ast-grep-works.html"><em>https://ast-grep.github.io</em></a><em> on June 7, 2024.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d406e8fa92ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ast-grep got 6000 stars!]]></title>
            <link>https://medium.com/@hchan_nvim/ast-grep-got-6000-stars-9c3077825672?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c3077825672</guid>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Sun, 19 May 2024 04:17:05 GMT</pubDate>
            <atom:updated>2024-05-19T04:25:39.254Z</atom:updated>
            <content:encoded><![CDATA[<p>We are thrilled to announce that <a href="https://ast-grep.github.io/">ast-grep</a>, the powerful code search tool, has reached a stellar milestone of 6000 stars on GitHub! This is a testament to the community’s trust in our tool and the continuous improvements we’ve made. Let’s dive into the latest features and enhancements that make ast-grep the go-to tool for developers worldwide.</p><h3>Feature Enhancements</h3><ul><li><strong>Rewriters Addition</strong>: We’ve added support for rewriters <a href="https://github.com/ast-grep/ast-grep/pull/855">#855</a>, enabling complex code transformations and refactoring with ease. The new feature unlocks a novel functional programming like code rewrite scheme: <a href="https://ast-grep.github.io/advanced/find-n-patch.html">find and patch</a>. Check out our previous <a href="https://dev.to/herrington_darkholme/find-patch-a-novel-functional-programming-like-code-rewrite-scheme-3964">blog post</a> for more details.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*c-kBE3LpFv1OP673.png" /></figure><ul><li><strong>Error/Warning Suppression Support</strong>: The new feature <a href="https://github.com/ast-grep/ast-grep/pull/446">#446</a> allows users to suppress specific errors or warnings via the <a href="https://ast-grep.github.io/guide/project/lint-rule.html#suppress-linting-error">code comment</a> ast-grep-ignore. ast-grep also <a href="https://github.com/ast-grep/ast-grep/issues/1019">respects suppression comments</a> in Language Server Protocol (LSP), making it easier to manage warnings and errors in your codebase.</li><li><strong>Enhanced Rule Constraints</strong>: The ast-grep rule constraints previously only accepted pattern, kind and regex.<br> Now it accepts a full rule<a href="https://github.com/ast-grep/ast-grep/pull/855">#855</a>, providing more flexibility than ever before.</li></ul><h3>VSCode extension</h3><p>The <a href="https://marketplace.visualstudio.com/items?itemName=ast-grep.ast-grep-vscode">ast-grep VSCode extension</a> is an official <a href="https://ast-grep.github.io/guide/tools/editors.html">VSCode integration</a> for this CLI tool. It unleashes the power of structural search and replace (SSR) directly into your editor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*85zg3roDCacZFXwX" /></figure><h3>Notable Features</h3><ul><li><strong>Search</strong>: Find code patterns with syntax tree.</li><li><strong>Replace</strong>: Refactor code with pattern.</li><li><strong>Diagnose</strong>: Identify issues via ast-grep rule.</li></ul><p><a href="https://www.youtube.com/watch?v=1ZM4RfIvWKc">ast-grep VSCode, a structural search and replace extension (youtube.com)</a></p><h3>Performance Boost</h3><ul><li><strong>Parallel Thread Output Fix</strong>: A significant fix <a href="https://github.com/ast-grep/ast-grep/commit/be230ca">#be230ca</a> ensures parallel thread outputs are now guaranteed, boosting overall performance.</li></ul><h3>Architectural Evolution</h3><ul><li><strong>Tree-Sitter Version Bump</strong>: We’ve upgraded to the latest tree-sitter version, enhancing parsing accuracy and speed. In future releases, we plan to leverage tree-sitter’s <a href="https://zed.dev/blog/language-extensions-part-1">new Web Assembly grammar</a> to support even more languages.</li><li><strong>Scan and Diff Merge</strong>: The <a href="https://github.com/ast-grep/ast-grep/commit/c78299d2902662cd98bda44f3faf3fbc88439078">refactor</a> combines CombinedScan::scan and CombinedScan::diff for a more streamlined process.</li><li><strong>Input Stream Optimization</strong>: Now, ast-grep avoids unnecessary input stream usage when updating all rules <a href="https://github.com/ast-grep/ast-grep/pull/943">#943</a>, making it possible to use sg scan --update-all.</li></ul><h3>Usability Improvements</h3><ul><li><strong>Error Messaging for Rule File Parsing</strong>: The VSCode extension now provides clearer error messages <a href="https://github.com/ast-grep/ast-grep/pull/968">#968</a> when rule file parsing fails, making troubleshooting a breeze.</li><li><strong>Better Pattern Parsing</strong>: Improved expando character replacement <a href="https://github.com/ast-grep/ast-grep/pull/883">#883</a> to make pattern more permissible.</li><li><strong>More Permissive Patterns</strong>: Patterns have become more permissive <a href="https://github.com/ast-grep/ast-grep/pull/1087">#1087</a> that allows matching $METAVAR with different syntax kind.</li></ul><h3>Enhanced Error Reporting</h3><p>We’ve introduced a suite of features to improve error reporting, making it easier to debug and refine your code:</p><ul><li>Report undefined meta-variables, errors in fixes, unused rewriters, and undefined utility rules.</li><li>Add field ID errors for relational rules and optimize test updates to avoid erroneous reports.</li><li>Shift from reporting file counts to error counts for a more meaningful insight into code quality.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/0*ZXvD76-ISz5cBHoX.png" /></figure><h3>Language Support Expansion</h3><ul><li><strong>Haskell Support</strong>: Haskell enthusiasts rejoice! ast-grep now supports Haskell via tree-sitter-haskell <a href="https://github.com/ast-grep/ast-grep/pull/1128">#1128</a>, broadening our language coverage.</li></ul><h3>NAPI Advancements</h3><ul><li><strong>NAPI Linux x64 musl Support</strong>: Our latest feat in NAPI <a href="https://github.com/ast-grep/ast-grep/commit/c4d7902">#c4d7902</a> adds support for Linux x64 musl, ensuring wider compatibility and performance.</li></ul><h3>Thanks</h3><p>As ast-grep continues to grow, we remain committed to providing a tool that not only meets but exceeds the expectations of our diverse user base.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*BpDVs1f8eVwikNlC.png" /></figure><p>We thank each and every one of you, espeically ast-grep’s sponsors, for your support, contributions, and feedback that have shaped ast-grep into what it is today. Here’s to many more milestones ahead!</p><p><em>Originally published at </em><a href="https://dev.to/herrington_darkholme/ast-grep-got-6000-stars-3eo2"><em>https://dev.to</em></a><em> on May 19, 2024.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c3077825672" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migrate to React 19 with ast-grep]]></title>
            <link>https://medium.com/@hchan_nvim/migrate-to-react-19-with-ast-grep-9faa06b65c72?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/9faa06b65c72</guid>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Mon, 29 Apr 2024 04:00:27 GMT</pubDate>
            <atom:updated>2024-04-29T04:08:04.488Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*w4RAWCtNbhmy9B9E" /><figcaption>Photo by <a href="https://unsplash.com/@sigmund?utm_source=medium&amp;utm_medium=referral">Rivage</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The release of React version 19 has been accompanied with a set of new features and enhancements.</p><p>However, upgrading to this new version requires certain modifications in the source code. This process can be quite taxing and repetitive, particularly for large codebases.</p><p>This article illustrates the usage of <a href="https://ast-grep.github.io/">ast-grep</a>, a tool designed <a href="https://github.com/ast-grep/ast-grep">to locate and substitute patterns</a> in your codebase, towards easing your migration to React 19.</p><p>We will focus on three major codemods:</p><ul><li>Use &lt;Context&gt; as a provider</li><li>Remove implicit ref callback return</li><li>Use ref as props and remove forwardRef</li></ul><h3>Prerequisite: Setting up ast-grep</h3><p>Initially, you need to set up ast-grep. This can be carried out via npm:</p><pre>npm install -g @ast-grep/cli</pre><p>Once installed, the proper set up of ast-grep can be confirmed by running the command below:</p><pre>ast-grep --version</pre><h3>Use &lt;Context&gt; as a provider</h3><p>Let’s kick off with the simplest modification: use as a provider.</p><p>React 19 uses &lt;Context&gt; as a provider instead of &lt;Context.Provider&gt;:</p><pre>function App() {<br>  const [theme, setTheme] = useState(&#39;light&#39;);<br>  // ...<br>  return (<br>-    &lt;UseTheme.Provider value={theme}&gt;<br>+    &lt;UseTheme value={theme}&gt;<br>      &lt;Page /&gt;<br>-    &lt;/UseTheme&gt;<br>+    &lt;/UseTheme&gt;<br><br>  );<br>}</pre><p>With ast-grep, the pattern $CONTEXT.Provider can be found and replaced with $CONTEXT.<br> However, the pattern$CONTEXT.Provider could appear in multiple locations, requiring us to specifically look for it within a JSX opening and closing element.</p><p>We can utilize the <a href="https://ast-grep.github.io/playground.html">ast-grep playground</a> to find the correct names for JSX opening elements and JSX closing elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*10lX7g2pdmuwimXB.png" /></figure><p>Afterwards, using and , we can pinpoint that the pattern should exist within a JSX opening element and a JSX closing element.</p><pre>id: use-context-as-provider<br>language: javascript<br>rule:<br>  pattern: $CONTEXT.Provider<br>  inside:<br>    any:<br>    - kind: jsx_opening_element<br>    - kind: jsx_closing_element<br>fix: $CONTEXT</pre><p><a href="https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImphdmFzY3JpcHQiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIkJCRLRUVQIiwiY29uZmlnIjoicnVsZTpcbiAgcGF0dGVybjogJFRIRU1FLlByb3ZpZGVyXG4gIGluc2lkZTpcbiAgICBhbnk6XG4gICAgLSBraW5kOiBqc3hfb3BlbmluZ19lbGVtZW50XG4gICAgLSBraW5kOiBqc3hfY2xvc2luZ19lbGVtZW50XG5maXg6ICRUSEVNRVxuIiwic291cmNlIjoiZnVuY3Rpb24gQXBwKCkge1xuICBjb25zdCBbdGhlbWUsIHNldFRoZW1lXSA9IHVzZVN0YXRlKCdsaWdodCcpO1xuICAvLyAuLi5cbiAgcmV0dXJuIChcbiAgICA8VXNlVGhlbWUuUHJvdmlkZXIgdmFsdWU9e3RoZW1lfT5cbiAgICAgIDxQYWdlIC8+XG4gICAgPC9Vc2VUaGVtZS5Qcm92aWRlcj5cbiAgKTtcbn1cbiJ9">Example playground link</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*utFU7WDjCf3Fhqam.png" /></figure><p>You can <a href="https://ast-grep.github.io/reference/cli/scan.html#r-rule-rule-file">launch the rule from a YAML file</a> using the command below:</p><pre>ast-grep scan -r use-context-as-provider.yml</pre><h3>Remove implicit-ref-callback-return</h3><p>Our next example is to remove implicit ref&#39;s return.</p><p>React 19 now supports returning a cleanup function from ref callbacks.</p><p>Due to the introduction of ref cleanup functions, returning anything else from a ref callback will now be rejected by TypeScript. The fix is usually to stop using implicit returns, for example:</p><pre>- &lt;div ref={current =&gt; (instance = current)} /&gt; <br>+ &lt;div ref={current =&gt; {instance = current}} /&gt;</pre><p>How can we find this pattern? We should find the pattern &lt;div ref={$A =&gt; $B}/&gt; where $B is not a statement block.</p><p>First, ast-grep can use <a href="https://ast-grep.github.io/guide/rule-config/atomic-rule.html#pattern">pattern object</a> to find jsx_attribute, which is the AST kind for the attribute in a JSX element.</p><p>You can use ast-grep’s playground to find out the kind name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*zX-6A62rQ6mEGZ1i.png" /></figure><p>Next, we need to find the pattern &lt;div ref={$A =&gt; $B}/&gt;. The pattern means that we are looking for a JSX element with a ref attribute that has a callback function.</p><pre>pattern:<br>  context: &lt;div ref={$A =&gt; $B}/&gt;<br>  selector: jsx_attribute</pre><p>Finally, we need to check if $B is not a statement block. We can use the <a href="https://ast-grep.github.io/guide/project/lint-rule.html#constraints">constraints</a> field to specify this condition.</p><pre>constraints:<br>  B:<br>    not: {kind: statement_block}</pre><p>Combining all these pieces, we get the following rule:</p><pre>id: remove-implicit-ref-callback-return<br>language: javascript<br>rule:<br>  pattern:<br>    context: &lt;div ref={$A =&gt; $B}/&gt;<br>    selector: jsx_attribute<br>constraints:<br>  B:<br>    not: {kind: statement_block}<br>fix: $A =&gt; {$B}</pre><p><a href="https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImphdmFzY3JpcHQiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIkJCRLRUVQIiwiY29uZmlnIjoicnVsZTpcbiAgcGF0dGVybjpcbiAgICBjb250ZXh0OiA8ZGl2IHJlZj17JEEgPT4gJEJ9Lz5cbiAgICBzZWxlY3RvcjoganN4X2F0dHJpYnV0ZVxuY29uc3RyYWludHM6XG4gIEI6XG4gICAgbm90OiB7a2luZDogc3RhdGVtZW50X2Jsb2NrfVxuZml4OiAkQSA9PiB7JEJ9Iiwic291cmNlIjoiZnVuY3Rpb24gTXlEaXYoKSB7XG4gIGNvbnN0IHJlZiA9IHVzZVJlZihudWxsKVxuICByZXR1cm4gPGRpdiByZWY9e2N1cnJlbnQgPT4gKHJlZi5jdXJyZW50ID0gY3VycmVudCl9IC8+XG59XG4ifQ==">Example playground link</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*dLPCsI9AzFlaIb-3.png" /></figure><h3>Remove forwardRef</h3><p>Let’s explore our most complex change: Remove forwardRef. In our example, we will only focus on the simplest case where no arrow function nor TypeScript is involved.</p><p>The example code is as follows:</p><pre>const MyInput = forwardRef(function MyInput(props, ref) {<br>  return &lt;input {...props} ref={ref} /&gt;;<br>});</pre><p>Let’s first start simple. We can find the pattern forwardRef($FUNC).<br> However, the$FUNC does not capture the arguments of the function. We need to capture the arguments of the function and rewrite them in the fix.</p><p>This pattern rule captures the function arguments.</p><pre>rule:<br>  pattern: forwardRef(function $M($PROPS, $REF) { $$$BODY })</pre><p>Next, we need to rewrite the arguments of the function. The easiest way is to rewrite $PROPS as an object destructuring and to add ref into the object destructuring.</p><pre>rule:<br>  pattern: forwardRef(function $M($PROPS, $REF) { $$$BODY })<br>fix: |-<br>  function $M({ref: $REF, ...$PROPS}) {<br>    $$$BODY<br>  }</pre><p><a href="https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImphdmFzY3JpcHQiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIkJCRLRUVQIiwiY29uZmlnIjoicnVsZTpcbiAgcGF0dGVybjogZm9yd2FyZFJlZihmdW5jdGlvbiAkTSgkUFJPUFMsICRSRUYpIHsgJCQkQk9EWSB9KVxuZml4OiB8LVxuICBmdW5jdGlvbiAkTSh7cmVmOiAkUkVGLCAuLi4kUFJPUFN9KSB7XG4gICAgJCQkQk9EWVxuICB9XG4iLCJzb3VyY2UiOiJjb25zdCBNeUlucHV0ID0gZm9yd2FyZFJlZihmdW5jdGlvbiBNeUlucHV0KHByb3BzLCByZWYpIHtcbiAgcmV0dXJuIDxpbnB1dCB7Li4ucHJvcHN9IHJlZj17cmVmfSAvPjtcbn0pO1xuIn0=">Example playground link</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*UPlScbBWb-j5c_eU.png" /></figure><h3>Handle more complex cases</h3><p>The above rule handles single identifier arguments gracefully. But it does not work for more complex cases like object destructuring, which is popular in React components.</p><pre>const MyInput = forwardRef(function MyInput({value}, ref) {<br>  return &lt;input value={value} ref={ref} /&gt;;<br>});</pre><p>We can use <a href="https://ast-grep.github.io/advanced/find-n-patch.html">rewriters</a> to handle more complex cases. The basic idea is that we can break down the rewriting into two scenarios: object destructuring and identifiers.</p><p>The object rewriter captures the object destructuring pattern and extract the inner content.</p><pre>id: object<br>rule:<br>pattern:<br>  context: ({ $$$ARGS }) =&gt; {}<br>  selector: object_pattern<br>fix: $$$ARGS</pre><p>For example, the rewriter above will capture the {value} inside function MyInput({value}, ref) and extract value as $$$ARGS.</p><p>The identifier rewriter captures the identifier pattern and spreads it.</p><pre>id: identifier<br>rule: { pattern: $P }<br>fix: ...$P</pre><p>For example, the rewriter above will capture props and spread it as ...props.</p><p>Finally, we can use the rewriters field to register the two rules above.</p><pre>rewriters:<br>- id: object<br>  rule:<br>    pattern:<br>      context: ({ $$$ARGS }) =&gt; {}<br>      selector: object_pattern<br>  fix: $$$ARGS<br>- id: identifier<br>  rule: { pattern: $P }<br>  fix: ...$P</pre><p>And we then can use them in the transform field to rewrite the arguments and use them in the fix.</p><pre>transform:<br>  NEW_ARG:<br>    rewrite:<br>      rewriters: [object, identifier]<br>      source: $PROPS<br>fix: |-<br>  function $M({ref: $REF, $NEW_ARG}) {<br>    $$$BODY<br>  }</pre><p>Putting all these together, we get the final rule:</p><pre>id: remove-forward-ref<br>language: javascript<br>rule:<br>  pattern: forwardRef(function $M($PROPS, $REF) { $$$BODY })<br>rewriters:<br>- id: object<br>  rule:<br>    pattern:<br>      context: ({ $$$ARGS }) =&gt; {}<br>      selector: object_pattern<br>  fix: $$$ARGS<br>- id: identifier<br>  rule: { pattern: $P }<br>  fix: ...$P<br>transform:<br>  NEW_ARG:<br>    rewrite:<br>      rewriters: [object, identifier]<br>      source: $PROPS<br>fix: |-<br>  function $M({ref: $REF, $NEW_ARG}) {<br>    $$$BODY<br>  }</pre><p><a href="https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImphdmFzY3JpcHQiLCJxdWVyeSI6IiRUSEVNRS5Qcm92aWRlciIsInJld3JpdGUiOiJsb2dnZXIubG9nKCRNQVRDSCkiLCJjb25maWciOiJydWxlOlxuICBwYXR0ZXJuOiBmb3J3YXJkUmVmKGZ1bmN0aW9uICRNKCRQUk9QUywgJFJFRikgeyAkJCRCT0RZIH0pXG5yZXdyaXRlcnM6XG4tIGlkOiBvYmplY3RcbiAgcnVsZTpcbiAgICBwYXR0ZXJuOlxuICAgICAgY29udGV4dDogKHsgJCQkQVJHUyB9KSA9PiB7fVxuICAgICAgc2VsZWN0b3I6IG9iamVjdF9wYXR0ZXJuXG4gIGZpeDogJCQkQVJHU1xuLSBpZDogaWRlbnRpZmllclxuICBydWxlOiB7IHBhdHRlcm46ICRQIH1cbiAgZml4OiAuLi4kUCBcbnRyYW5zZm9ybTpcbiAgTkVXX0FSRzpcbiAgICByZXdyaXRlOlxuICAgICAgcmV3cml0ZXJzOiBbb2JqZWN0LCBpZGVudGlmaWVyXVxuICAgICAgc291cmNlOiAkUFJPUFNcbmZpeDogfC1cbiAgZnVuY3Rpb24gJE0oe3JlZjogJFJFRiwgJE5FV19BUkd9KSB7IFxuICAgICQkJEJPRFlcbiAgfVxuIiwic291cmNlIjoiY29uc3QgTXlJbnB1dCA9IGZvcndhcmRSZWYoZnVuY3Rpb24gTXlJbnB1dChwcm9wcywgcmVmKSB7XG4gIHJldHVybiA8aW5wdXQgey4uLnByb3BzfSByZWY9e3JlZn0gLz47XG59KTtcblxuY29uc3QgTXlJbnB1dCA9IGZvcndhcmRSZWYoZnVuY3Rpb24gTXlJbnB1dCh7dmFsdWV9LCByZWYpIHtcbiAgcmV0dXJuIDxpbnB1dCB2YWx1ZT17dmFsdWV9IHJlZj17cmVmfSAvPjtcbn0pOyJ9">Example playground link</a></p><h3>Conclusion</h3><p>Codemod is a powerful paradigm to automate code changes. In this article, we have shown how to use ast-grep to migrate to React 19.</p><p>We have covered three common changes: use as a provider, remove implicit-ref-callback-return, and remove forwardRef.</p><p>The examples here are for educational purposes and you are encouraged to use <a href="https://codemod.com/">codemod.com</a> to automate these changes in your codebase. codemod.com has curated rules that take care of these changes and more subtle edge cases.</p><p>You can adapt the example and explore ast-grep’s power to automate more code changes.</p><p>ast-grep is also available on <a href="https://docs.codemod.com/introduction">codemod.com</a>.</p><p>Happy coding!</p><p><em>Originally published at </em><a href="https://dev.to/herrington_darkholme/migrate-to-react-19-with-ast-grep-28op"><em>https://dev.to</em></a><em> on April 29, 2024.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9faa06b65c72" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Explore useEffect Alternatives: Beyond the Well-Trodden Paths]]></title>
            <link>https://medium.com/@hchan_nvim/explore-useeffect-alternatives-beyond-the-well-trodden-paths-574e09ccfb43?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/574e09ccfb43</guid>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Wed, 27 Mar 2024 23:25:14 GMT</pubDate>
            <atom:updated>2024-03-27T23:29:22.685Z</atom:updated>
            <content:encoded><![CDATA[<p>React hooks have transformed the way we design and structure our React components — they’ve encouraged more functional and less class-based patterns. The useEffect hook is one of the most common hooks we use, and for good reason. It allows us to handle side effects — like data fetching, subscriptions, or manual DOM manipulations — in our functional components.</p><p>However, there may be cases where alternatives could suit better. Today, we’ll explore both the well-known and lesser-known alternatives to useEffect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*shg-vm5xl3AS_vMy" /><figcaption>Photo by <a href="https://unsplash.com/@steve_j?utm_source=medium&amp;utm_medium=referral">Steve Johnson</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Well-Known Alternatives</h3><h4>1. Computed State: useMemo</h4><p>useMemo is a hook that returns a memorized value. This is typically used to optimize performance in scenarios where a computationally intensive value does not need to be recalculated unless its dependencies change. An instance where useMemo could be a great alternative is when we have complex computed state.</p><pre>const computationIntensiveValue = useMemo(() =&gt; performHeavyComputation(a, b), [a, b]);</pre><h4>2. Event Handling: useCallback</h4><p>For event handling scenarios, useCallback could be a better alternative. It&#39;s a hook that returns a memoized version of the callback that only changes if one of the dependencies has changed.</p><pre>const memoizedCallback = useCallback(() =&gt; {<br>    eventHandler(a, b);<br>}, [a, b]);</pre><h4>3. State Management: useContext</h4><p>useContext allows us to access the value of a context without wrapping a component in a &lt;Context.Consumer&gt; or using a class component. It&#39;s a perfect choice when it comes to lifting state up.</p><pre>const contextValue = useContext(MyContext);</pre><h4>4. Data Fetching: useSWR</h4><p>For data fetching, useSWR is a superb choice over useEffect. useSWR is a reusable state management tool for data fetching, which includes built-in handling for caching, retries, deduplication, and updates.</p><pre>const { data, error } = useSWR(&#39;/api/data&#39;, fetcher)</pre><h3>Lesser-Known Alternatives</h3><h4>5. Resetting a component: use new key</h4><p>Resetting a component sometimes might be useful to return the component to its initial state. Assigning a new key prop to a component makes React perceive the latter as a completely new component, thereby remounting it.</p><pre>&lt;MyComponent key={uniqueId} /&gt;</pre><h4>6. Resetting some state: setState in render</h4><p>Instead of using useEffect, setting state directly in the render method is a viable option for some cases. This might seem counter-intuitive, but can be effective when it needs to reset specific state values during rendering.</p><pre>if (needToReset) {<br>  setState(initialState);<br>}</pre><h4>7. External data source: useSyncExternalStore</h4><p>The useSyncExternalStore hook provides a way to read from and subscribe to an external (mutable) source. This can be ideal when dealing with external data sources.</p><pre>const state = useSyncExternalStore(store.subscribe, store.getState);</pre><h3>Conclusion</h3><p>Using useEffect for everything might seem like a go-to solution, but there&#39;s a plethora of hooks and alternatives readily available for particular problems. It&#39;s important to understand these to get the best out of React. Use the most suitable tool for your issue and make your React applications shine!</p><p>Remember, the beauty of Hooks lies in the orchestration of simplicity and functionality, making React a more potent and friendly framework. Stay curious, explore more, and keep coding innovative solutions!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=574e09ccfb43" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ast-grep VSCode: A Powerful Tool for Structural Search and Replace]]></title>
            <link>https://medium.com/@hchan_nvim/ast-grep-vscode-a-powerful-tool-for-structural-search-and-replace-d8e9bf1426a0?source=rss-1cebc1e355be------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8e9bf1426a0</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[vscode-extension]]></category>
            <category><![CDATA[vscode]]></category>
            <dc:creator><![CDATA[Herrington Darkholme]]></dc:creator>
            <pubDate>Sun, 03 Mar 2024 02:11:14 GMT</pubDate>
            <atom:updated>2024-03-03T03:12:36.968Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi, I’m <a href="https://twitter.com/hd_nvim">Herrington</a>, the author of <a href="https://ast-grep.github.io/">ast-grep</a>.</p><p>If you’ve ever searched code with regular expressions, you may have struggled with matching multiple lines, nested structures, or ignoring comments.</p><p>Let me introduce <a href="https://marketplace.visualstudio.com/items?itemName=ast-grep.ast-grep-vscode">ast-grep VSCode</a>, a new extension leveraging the power of structural search and replace (SSR) to help you perform more precise and efficient search and replace.</p><h3>The Limitations of Textual Search and Replace</h3><p>Suppose you want to refactor your JavaScript code to replace the <a href="https://lodash.com/">lodash</a> _.filter function with the native Array.prototype.filter <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter">method</a>. A simple text search and replace might look like this:</p><pre>// Find <br>_.filter\((\.+), (.+)\) <br>// Replace <br>$1.filter($2)</pre><p>This might work for some cases, but it has several limitations:</p><ul><li>It can only match single-line expressions. If your code spans multiple lines, you’ll miss some matches or get incorrect replacements.</li><li>It can’t handle nested structures like parentheses, brackets, or braces. If your code has complex expressions, you’ll get incorrect matches or replacements.</li><li>It can’t ignore comments or other irrelevant parts of the code. If your code has comments that contain the search pattern, you’ll get unwanted matches or replacements.</li><li>It requires you to use match groups or named capture groups to preserve the arguments of the function. This can be tedious and error-prone, especially with many arguments or nested functions.</li></ul><h3>Structural Search and Replace Comes to the Rescue</h3><p>Can we make our search algorithm smarter, understand our code better, and be easier to use? Yes! Structural search and replace (SSR) comes to the rescue!</p><p><a href="https://www.jetbrains.com/help/idea/structural-search-and-replace.html">Structural search and replace</a> (SSR) is a technique that allows you to find and modify code patterns based on their syntax and semantics, not just their text. Instead of treating code as plain text, SSR treats code as a collection of nodes with types, properties, and relationships.</p><p><a href="https://ast-grep.github.io/">ast-grep</a> is a command-line tool that implements SSR. The query for the lodash example above is quite simple.</p><pre>// Search pattern:<br>_.filter($ARR, $FUNC)<br>// Rewrite:<br>$ARR.filter($FUNC)</pre><p>The pattern illustrates the basic usage of ast-grep. $ARR and $FUNC are <a href="https://ast-grep.github.io/guide/pattern-syntax.html#meta-variable">meta-variables</a>. Meta-variables are like the dot . in regular expressions, except they match AST nodes instead of characters. You can also use captured meta-variables in the rewrite pattern.</p><p>Other than the meta-variables, I hope the pattern and rewrite are self-explanatory.</p><p>This query has several advantages over the text search and replace:</p><ul><li>It can match expressions across multiple lines, as long as they are syntactically valid.</li><li>It can handle nested structures like parentheses, brackets, or braces, as they are part of the AST.</li><li>It can ignore comments or other irrelevant parts of the code, as they are not part of the AST.</li><li>It does not require match groups or named capture groups, as the meta-variables are accessed by their names in the search pattern.</li></ul><h3>Unique Features of ast-grep</h3><p>ast-grep uses the tree-sitter library, a fast and robust parser for many programming languages. ast-grep allows you to write queries using a simple <a href="https://ast-grep.github.io/guide/pattern-syntax.html#meta-variable">pattern syntax</a> that resembles code and apply them to files or directories of code. Some of the unique features of ast-grep are:</p><ul><li>It supports many languages, including JavaScript, TypeScript, Python, Ruby, Java, C#, Go, Rust, and more. You can also add support for new languages by <a href="https://ast-grep.github.io/advanced/custom-language.html">registering tree-sitter grammars</a> in the configuration.</li><li>It is <a href="https://github.com/ast-grep/ast-grep">written in Rust</a>, which makes it very fast and memory-efficient. It can process large codebases in a matter of seconds.</li><li>It has a <a href="https://ast-grep.github.io/reference/cli.html">rich set of options</a> and flags, such as dry run, interactive mode, color output, and more. You can customize your SSR experience to suit your needs and preferences.</li></ul><p>But, ast-grep until now only has a command-line interface. Can we have its power right near our hands instead of switching between terminals and editors?</p><h3>ast-grep VSCode: Bridging the CLI and the Editor</h3><p>ast-grep VSCode is a new extension that integrates ast-grep with Visual Studio Code, one of the most popular code editors. With ast-grep VSCode, you can use SSR within your editor, without leaving your workflow.</p><h3>Structural Search, Replace, and More</h3><p>Some features of ast-grep VSCode include:</p><ul><li>It provides a user interface for writing and executing SSR queries, and you can see the results of your queries in a sidebar, with previews and diffs.</li><li>It also supports linting and code fix. You can set up an ast-grep project and write custom rules tailored to your needs.</li><li>It feels like a native VSCode feature, with seamless integration and consistent design.</li></ul><p>Search Pattern</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*aBzk1rOgTsrrJZnC" /></figure><p>Search In Folder</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*dFRamgvQcnZtibZg" /></figure><p>Replace Preview</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*6krJkdOlu8H_zQA4" /></figure><p>Commit Replace</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*rVvqeAVi4hAKYlUw" /></figure><p>Code Linting</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*nbe25727BZe7R-yG" /></figure><h3>Comparing with Other SSR Tools</h3><p>There are other SSR tools and extensions available. However, I believe ast-grep VSCode is one of the best SSR extensions for VSCode because:</p><ul><li>It has good performance, backed by a multi-threaded CLI written in a native language.</li><li>It supports multiple languages, leveraging the tree-sitter grammars widely used and maintained by the community.</li><li>It has a user-friendly and intuitive interface that feels like a VSCode built-in feature. See it in <a href="https://www.youtube.com/watch?v=1ZM4RfIvWKc">action</a>.</li></ul><p>Finally, I want to highlight some React techniques used in ast-grep VSCode that make it fast and responsive:</p><ul><li><a href="https://github.com/ast-grep/ast-grep-vscode/blob/789d27325fb9ce6a0bb969caefdc718942f6d2b3/src/webview/hooks/useSearch.tsx#L150-L159">useSyncExternalStore</a> was used to manage streaming results from ast-grep CLI. This <a href="https://react.dev/reference/react/useSyncExternalStore">hook</a> allows components to subscribe to an external mutable source of data and update the rendering accordingly. This way, the extension can show the results as soon as they are available.</li><li><a href="https://github.com/ast-grep/ast-grep-vscode/blob/main/src/webview/SearchSidebar/index.tsx#L13-L16">useDeferredValue</a> deferred rendering the result list. This <a href="https://react.dev/reference/react/useDeferredValue">hook</a> can avoid blocking user input and improve the perceived performance of the UI by delaying the update of a derived state until the next concurrent render.</li><li>The extension carefully used plenty of memo and CSS tricks to reduce the JavaScript workload.</li></ul><h3>Conclusion</h3><p>I hope this article helps you understand the benefits of SSR and the features of ast-grep VSCode. If you are interested in trying it out, you can install it from the <a href="https://marketplace.visualstudio.com/items?itemName=ast-grep.ast-grep-vscode">VSCode Marketplace</a>. ast-grep VSCode is still in development and has a lot of room for improvement. I would love to hear your feedback and suggestions, so feel free to open an issue or a pull request on <a href="https://github.com/ast-grep/ast-grep-vscode">GitHub</a>. I also want to thank <a href="https://github.com/SoonIter">SoonIter</a>, <a href="https://github.com/StevenLove">Steven Love</a>, <a href="https://github.com/SigureMo">Nyakku Shigure</a>, and <a href="https://github.com/cshuaimin/ssr.nvim">ssr.nvim</a> for their contribution and inspiration!</p><p>Happy grepping!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8e9bf1426a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>