Enhanced AST Static Analysis with Typescript Language Server

Rate this content

Most of the ecosystem tools, like bundlers or transpilers, are based on AST. And Typescript provides one of the best Developer Experiences to work with the code base.

This talk is about the experience of how beneficial it can be to use Type Hints and Typescript Language Server during the AST and static code analysis, based on the example of building a compile-time css-in-js library.

12 min
21 Sep, 2023


Sign in or register to post your comment.

AI Generated Video Summary

Today's Talk discusses enhancing static code analysis using TypeScript language server and abstract syntax tree (AST). TypeScript can help with static analysis by providing types based on function signatures. By integrating TSMorph into a Babel plugin, we can check types for specific nodes in the abstract syntax tree. Enhancements to static analysis include checking console.log arguments and removing unnecessary expressions. TypeScript's type information can be used to compile CSS and extract it into a separate stylesheet, enabling better compilation and build time performance.

1. Enhancing Static Code Analysis with AST

Short description:

Today we'll discuss enhancing static code analysis using TypeScript language server and abstract syntax tree (AST). We can use AST to make code transformations, such as removing specific function calls. By implementing a Babel plugin, we can automatically remove all console.log calls from the code. AST and static code analysis enable build-time optimizations, minification, formatting, and transpiling to support older browsers.

Hi everyone, I'm Artur, tech lead at Apps Platform team at London-based company called Zoho. Today I'd like to talk about enhancing static code analysis using TypeScript language server. But first, let's discuss quickly what abstract syntax tree is and how can we use it for static analysis.

Imagine we want to implement some automatic code transformation during compilation to remove all CONSOLE.LOC calls from the output. In theory, we could just use a regular expression to find all CONSOLE.LOC in the code. But remember, solve the problem with a regular expression and now you have one more problem. And actually, it'll be quite tricky to write such a regular expression to handle all the different cases. Instead, we can make code transformations using abstract syntax tree.

Let's take a look at code compilation process. Compilation usually contains 4 stages – lexical analysis, syntax analysis, semantic analysis and code generation. Today we'll focus on semantic analysis with abstract syntax tree. And we'll implement our code transformation using AST. Most of the tools of our ecosystem are using AST for analysis and code changes.

Let's take a look at our code again. We're going to use a tool called AST explorer. And on the right side you can see how our code is represented by abstract syntax tree. The function call, which is console.log in our case, is represented by call expression node in the tree, which contains other nodes like callee or arguments. This call expression is a part of the block statement body, let's just remove it. Once we remove it, block statement wouldn't contain any other expressions in the body. So in the end, generating a code will get just an empty Hello World function, in this particular case. Going back to diagram, so what we did, we just transformed our abstract syntax tree, removing call expression node, and then we generated code from the new AST. Of course, we don't want to make these transformations manually, so let's implement a simple Babel plugin to remove all console.log calls. In AST Explore, you can select Built-in Babel API to implement a Babel plugin and you can see the four parts on the screen, like source code, AST, plugin implementation and output code. Original source code contains three console.log call expressions, which are represented by abstract syntax tree. You can see these three expression statements as part of the block statement body. Let's expand each of them and you can find one of the call expressions with callee and argument nodes. Now moving to the plugin implementation, the implementation is quite straightforward. All we need to do is traverse over call expressions, check if callee is a member expression and if that's a console.log, remove the whole expression. In the end, we'll get an empty function, hello world, without console.log again because they were automatically removed. You can use AST and static code analysis to implement build-time optimizations, minification, formatting, transpiling, for example, from the new language features to the old one to support older browsers, for example, and many more.

2. TypeScript's Role in Static Analysis

Short description:

TypeScript can help with static analysis by providing types based on function signatures. It offers a language server that allows tools to connect and access type information. TSMorph, a library wrapping the TypeScript compiler API, simplifies interaction. We can integrate TSMorph into a Babel plugin to check types for specific nodes in the abstract syntax tree. By enhancing the original plugin with TypeScript support, we can remove console.log arguments that are not numbers.

But how can TypeScript help with static analysis? Let's imagine now that we need to remove console.log calls as before, but keep logs of arguments of number type. So, for example, we want to keep 1, 2, 3 or some constant if that's a number, but we want to remove Hello World strings or Now strings with in-place values like 1, 2, 3, that's pretty straightforward because you can get this value just from the syntax. But it gets quite complicated with function calls or external variables like some constant here. We have to have some kind of type system in place or implement type inference ourselves and TypeScript can provide such types.

In this example, TypeScript can automatically info the correct type based on the function signatures. And to make TypeScript not only check types with the command-line tool but work with different code editors, TypeScript provides a language server which is a separate process like a backend and tools can connect to this server to get the information about types or use code refactoring tools provided by TypeScript. And the beauty of the language server is that it can be used not only for code editing or type checking. Following the language server protocol we can interact with it programmatically and benefit from TypeScript knowledge about the project and types at any level including we can use it during our semantic analysis.

TSMorph is an amazing library which wraps TypeScript compiler API and provides a simple interface to interact with. There is an example how you can configure TSMorph in your program. Now we can use it during abstract tree analysis. Let's see how we can integrate TSMorph into the actual plugin. So there is quite a lot of code here, but let's focus on the most important parts. So we want to have a getTypeAtPost function which accepts file name, code, start and end of the position we are interested in and then we can create a virtual source file based on the file name and code, and read the type for this provided position using TSMorph. The idea is the same as, for example, in IDE. If we need to see the type in our editor, we select an expression we are interested in and editor shows types for this specific cursor position. In this case, instead of cursor, we just have programmable interface but the idea is mostly the same. And now let's integrate TSMorph with our Babel plugin. We need to initialize the defined previously TypedClick class for TS processor. Then we can define a helper called GetTSType with file name, source code, and path to the node in the abstract syntax tree as arguments. Each path in the abstract syntax tree contains nodes with information where the node is positioned from start to end. This way we can check the type for the specific node. And in the end, we just need to return the type representation provided by TSMorph. Having these simple helpers defined, let's see how our original Babel plugin can be enhanced with TypeScript support. To remind, this was the original plugin implementation that's integrated with TypeScript's language server. First of all, again, let's initialize our TSProcessor instance and define GetTSType helper. Next, we can get code and file name from the Babel plugin state. At this state, we already did all the checks, like we check that this is console.log expression, so we just need to go over console.log arguments and check that each argument... We need to get the type for each argument and check that if that argument type is number or not. If that's not a number, we can just remove this argument completely, and if it is number, we can just leave it.

3. Enhancing Static Analysis and Compilation

Short description:

We can enhance static code analysis by checking the arguments of console.log calls and removing unnecessary expressions. TypeScript provides type information that can be used to compile CSS and extract it into a separate stylesheet. The atomic representation of styles allows us to generate classes based on different property values. Tidy.dev offers a functional interface for CSS generation during build time. Use TSMorph for programmable interaction with the TypeScript compiler. Pay attention to type safety in your codebase for better compilation and build time performance.

And then, we just need to check that there are any arguments left or not. If there are any, then we can keep this expression because it means that there are number arguments for this console.log call. If not, we can remove the whole expression by path.remove.

So yeah, this example is quite unrealistic. I don't think there is a real use case for console.log and types of console.log parameters. But there is a bit more practical example I've been working on with Compile Time CSS and JS library.

I've been working on library called Tidy, and the idea is that it can provide a simple functional interface with CSS function, which accepts properties like inline styles. But the idea is that instead of generating these styles in the runtime, we can compile them during the build time and extract them into the separate style sheet.

In this case, that's pretty straightforward because blue value and yellow value are static. So we can get them just from the syntax tree. But what if our code is a bit more complicated? And for example, our styles depend on the runtime. In this case, color and font-weight depend on the variant property and weight property, which are not known during the build time.

But with TypeScript, we can get the actual type of each of the properties. Like, for example, for color, we can see that it can be either violet or purple. And for font-weight, we can see that that's a union of lighter, normal, and bold values. Which makes it possible for us to go over these values and pregenerate classes for each of the value.

Thanks to the atomic representation of styles, we can compose these items in the end, just as class name composition. And all we need in the runtime is just to identify the right hash for the class name for this specific value. There is no styles generation. And again, we can benefit from styles compilation during the build time and extract it into the separate stylesheet.

If that sounds interesting to you, please check the tidy.dev website and tidy repository. It's still in a quite experimental state, but any feedback or ideas will be very much appreciated.

And in the end, just some notes. Use TSMorph for programmable interface to interact with TypeScript compiler. And you can get TypeScript type hints in Babel or any other AST traverser just from the position in the code, which is quite useful. And TypeScript integration might slow down static analysis because that's quite a lot of overhead, and it might be a good idea to apply some performance optimizations, for example, isolated into the separate worker.

There are dozens of different language server protocols. It's not just about TypeScript. So you can build a lot of different integrations as well.

And in the end, last but not least, types are not 100% trusted and they can't be because there are types like any or there are commands like TSignore. So the more attention you pay to the type safety of your codebase, the more you can benefit from it, especially during the compilation and build time. There are some useful links.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Vue.js London 2023Vue.js London 2023
30 min
Stop Writing Your Routes
The more you keep working on an application, the more complicated its routing becomes, and the easier it is to make a mistake. ""Was the route named users or was it user?"", ""Did it have an id param or was it userId?"". If only TypeScript could tell you what are the possible names and params. If only you didn't have to write a single route anymore and let a plugin do it for you. In this talk we will go through what it took to bring automatically typed routes for Vue Router.
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
I'll dive into the internals of Nuxt to describe how we've built a TypeScript-first framework that is deeply integrated with the user's IDE and type checking setup to offer end-to-end full-stack type safety, hints for layouts, middleware and more, typed runtime configuration options and even typed routing. Plus, I'll highlight what I'm most excited about doing in the days to come and how TypeScript makes that possible not just for us but for any library author.
React Advanced Conference 2021React Advanced Conference 2021
6 min
Full-stack & typesafe React (+Native) apps with tRPC.io
Why are we devs so obsessed with decoupling things that are coupled nature? tRPC is a library that replaces the need for GraphQL or REST for internal APIs. When using it, you simply write backend functions whose input and output shapes are instantly inferred in your frontend without any code generation; making writing API schemas a thing of the past. It's lightweight, not tied to React, HTTP-cacheable, and can be incrementally adopted. In this talk, I'll give a glimpse of the DX you can get from tRPC and how (and why) to get started.
TypeScript Congress 2022TypeScript Congress 2022
10 min
How to properly handle URL slug changes in Next.js
If you're using a headless CMS for storing content, you also work with URL slugs, the last parts of any URL. The problem is, content editors are able to freely change the slugs which can cause 404 errors, lost page ranks, broken links, and in the end confused visitors on your site. In this talk, I will present a solution for keeping a history of URL slugs in the CMS and explain how to implement a proper redirect mechanism (using TypeScript!) for dynamically generated pages on a Next.js website.
Add to the talk notes: https://github.com/ondrabus/kontent-boilerplate-next-js-ts-congress-2022 
React Summit 2023React Summit 2023
19 min
7 TypeScript Patterns You Should Be Using
In this talk, we will be going over a number of common useful and best-practice-proven TypeScript patterns to use in React 18. This includes how to correctly type component properties, children and return types, using React's built-in types, typing contexts, and the usual enum rant (but constructively).

Workshops on related topic

React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Featured WorkshopFree
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.
The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.
React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.

React Advanced Conference 2022React Advanced Conference 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Featured Workshop
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.
In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
TypeScript Congress 2022TypeScript Congress 2022
116 min
Advanced TypeScript types for fun and reliability
If you're looking to get the most out of TypeScript, this workshop is for you! In this interactive workshop, we will explore the use of advanced types to improve the safety and predictability of your TypeScript code. You will learn when to use types like unknown or never. We will explore the use of type predicates, guards and exhaustive checking to make your TypeScript code more reliable both at compile and run-time. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Are you familiar with the basics of TypeScript and want to dive deeper? Then please join me with your laptop in this advanced and interactive workshop to learn all these topics and more.
You can find the slides, with links, here:
And the repository we will be using is here:
TypeScript Congress 2023TypeScript Congress 2023
131 min
Practice TypeScript Techniques Building React Server Components App
In this hands-on workshop, Maurice will personally guide you through a series of exercises designed to empower you with a deep understanding of React Server Components and the power of TypeScript. Discover how to optimize your applications, improve performance, and unlock new possibilities.
During the workshop, you will:
- Maximize code maintainability and scalability with advanced TypeScript practices
- Unleash the performance benefits of React Server Components, surpassing traditional approaches
- Turbocharge your TypeScript with the power of Mapped Types
- Make your TypeScript types more secure with Opaque Types
- Explore the power of Template Literal Types when using Mapped Types
Maurice will virtually be by your side, offering comprehensive guidance and answering your questions as you navigate each exercise. By the end of the workshop, you'll have mastered React Server Components, armed with a newfound arsenal of TypeScript knowledge to supercharge your React applications.
Don't miss this opportunity to elevate your React expertise to new heights. Join our workshop and unlock the potential of React Server Components with TypeScript. Your apps will thank you.
Node Congress 2021Node Congress 2021
245 min
Building Serverless Applications on AWS with TypeScript
This workshop teaches you the basics of serverless application development with TypeScript. We'll start with a simple Lambda function, set up the project and the infrastructure-as-a-code (AWS CDK), and learn how to organize, test, and debug a more complex serverless application.
Table of contents:
        - How to set up a serverless project with TypeScript and CDK
        - How to write a testable Lambda function with hexagonal architecture
        - How to connect a function to a DynamoDB table
        - How to create a serverless API
        - How to debug and test a serverless function
        - How to organize and grow a serverless application
Materials referred to in the workshop:
DynamoDB blog Alex DeBrie: https://www.dynamodbguide.com/
Excellent book for the DynamoDB: 

TypeScript Congress 2022TypeScript Congress 2022
118 min
Crash Course into TypeScript for content from headless CMS
In this workshop, I’ll first show you how to create a new project in a headless CMS, fill it with data, and use the content in your project. Then, we’ll spend the rest of time in code, we will:
- Generate strongly typed models and structure for the fetched content.
- Use the content in components
- Resolve content from rich text fields into React components
- Touch on deployment pipelines and possibilities for discovering content-related issues before hitting production