Supercharged Code Refactoring via Abstract Syntax Trees

Rate this content
Bookmark

When refactoring large code bases, Find and Replace even with Regexes, can only get you so far. When that fails don’t fallback to painful manual updates instead reach for Abstract Syntax Trees (AST). 

In this session we introduce AST’s and show how they can be used to reason about / generate code. You will leave with a greater understanding of how you can automatically update code and new insights into how code linters work under the hood. 

This talk has been presented at React Summit 2023, check out the latest edition of this React Conference.

Watch video on a separate page

FAQ

Before joining AG Grid, the speaker found ASTs intimidating and complex. However, after gaining practical experience at AG Grid, they realized the potential and power of using ASTs for tasks such as maintaining code examples and implementing generic typing across products.

The speaker focused on maintaining thousands of React examples and introducing generic typing across the AG Grid product to enhance features like auto-completion and type checking.

Tools like TypeScript and ts-morph are used alongside ASTs. TypeScript aids in maintaining type integrity across transformations, while ts-morph simplifies the process of modifying TypeScript ASTs by providing higher-level abstractions for manipulating the code structure.

Despite their power, ASTs can be daunting due to their complexity and the detailed nature of their structure, which often requires a deep understanding of syntax trees. Additionally, finding comprehensive documentation and examples can be challenging.

Yes, platforms like AST Explorer and resources like Codeshift Community offer interactive environments and community-driven examples to learn about ASTs and their applications in various programming tasks.

Abstract Syntax Trees (ASTs) are hierarchical representations of the structure of a program, capturing syntax and semantics in a tree-like structure. ASTs are crucial for tasks like code analysis, transformation, and generation, enabling powerful operations such as automatically updating code or creating code linters.

At AG Grid, ASTs are employed to automate the conversion of TypeScript examples into React examples, manage generic typing across interfaces, and ensure the code documentation is accurate and efficient without manual errors.

Stephen Cooper
Stephen Cooper
29 min
02 Jun, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk explores the power of Abstract Syntax Trees (ASTs) in software development. It covers the use of ASTs in maintaining React examples, automating dependency identification, and introducing generic typing. The Talk also discusses using ASTs to reason about and generate code, as well as their application in building ESLint plugins and code mods. Additionally, it highlights resources for exploring ASTs, testing AST scripts, and building Babel plugins.

1. Introduction to ASTs

Short description:

I'm going to talk to you about ASTs and the power they provide. Let's get started.

I should have an intro like that all the time. That would be really good. So, yeah, as you've heard there, I'm going to talk to you about ASTs, because these are a topic which, before I joined AG Grid, were always a little bit scary, always a little bit like, oh, I don't know if I know how to use those. But once I got into it, I got your hands into it, got the hand dirty, you realize there is so much power using them. And that's what I want to convey to you today and maybe get you started on your journey with ASTs. So let's get started.

2. AG Grid Mission and Key Tasks

Short description:

The mission for us at AG Grid is to build the best JavaScript data grid. We have two key tasks: maintaining React examples and introducing generic typing. We want to provide code that aligns with your needs and ensure seamless integration with TypeScript.

So the mission for us at AG Grid is to build the best JavaScript data grid. And so there's a number of tasks that we need to do to make that happen. And when I joined the company two years ago, there were these two key tasks that needed to be done.

We needed to maintain thousands of React examples so that when you come to our docs, you can see, I want this feature, I want it in React. I'm still using classes because my company hasn't upgraded yet. Or I'm using hooks or I'm using hooks with TypeScript. So we want to give you the code exactly the way that you want to consume it.

And then the second one is we love TypeScript. So we want to introduce generic typing across our product. So if you give us an interface for your row data, we want that to flow through all of our interfaces so you get lovely auto-completion and type checking. So the question is, how am I going to do this?

QnA