Enhanced AST Static Analysis with Typescript Language Server

Rate this content
Bookmark

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.

Artur Kenzhaev
Artur Kenzhaev
12 min
21 Sep, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

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.

Available in Español

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

TypeScript and React: Secrets of a Happy Marriage
React Advanced Conference 2022React Advanced Conference 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
TypeScript and React are inseparable. What's the secret to their successful union? Quite a lot of surprisingly strange code. Learn why useRef always feels weird, how to wrangle generics in custom hooks, and how union types can transform your components.
React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
We don't think of React as shipping its own types. But React's types are a core part of the framework - overseen by the React team, and co-ordinated with React's major releases.In this live coding talk, we'll look at all the types you've been missing out on. How do you get the props type from a component? How do you know what ref a component takes? Should you use React.FC? And what's the deal with JSX.Element?You'll walk away with a bunch of exciting ideas to take to your React applications, and hopefully a new appreciation for the wonders of React and TypeScript working together.
Stop Writing Your Routes
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.
Making Magic: Building a TypeScript-First Framework
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.
Full-stack & typesafe React (+Native) apps with tRPC.io
React Advanced Conference 2021React Advanced Conference 2021
6 min
Full-stack & typesafe React (+Native) apps with tRPC.io
Top Content
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.
Faster TypeScript builds with --isolatedDeclarations
TypeScript Congress 2023TypeScript Congress 2023
24 min
Faster TypeScript builds with --isolatedDeclarations
Top Content
Type-checking a TypeScript codebase can be slow, especially for monorepos containing lots of projects that each need to use the type checker to generate type declaration files. In this talk, we introduce — for the very first time — a new TypeScript feature we are working on called “Isolated Declarations” that allows DTS files to be generated without using the type checker at all! This opens the door to faster declaration generation in TypeScript itself, as well as in external tools written in other languages such as ESBuild and swc. You'll see how to use this new option, and maybe (just maybe) you’ll be convinced about the benefits of explicit return types! Most importantly, we will show how Isolated Declarations enables parallel builds to spread work across your CPU cores to significantly improve the build speed of your TypeScript projects.

Workshops on related topic

React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
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.
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced Conference 2022React Advanced Conference 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
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.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
Practice TypeScript Techniques Building React Server Components App
TypeScript Congress 2023TypeScript Congress 2023
131 min
Practice TypeScript Techniques Building React Server Components App
Workshop
Maurice de Beijer
Maurice de Beijer
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.
Advanced TypeScript types for fun and reliability
TypeScript Congress 2022TypeScript Congress 2022
116 min
Advanced TypeScript types for fun and reliability
Workshop
Maurice de Beijer
Maurice de Beijer
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: http://theproblemsolver.nl/docs/ts-advanced-workshop.pdf
And the repository we will be using is here: https://github.com/mauricedb/ts-advanced
Mastering Node.js Test Runner
TestJS Summit 2023TestJS Summit 2023
78 min
Mastering Node.js Test Runner
Workshop
Marco Ippolito
Marco Ippolito
Node.js test runner is modern, fast, and doesn't require additional libraries, but understanding and using it well can be tricky. You will learn how to use Node.js test runner to its full potential. We'll show you how it compares to other tools, how to set it up, and how to run your tests effectively. During the workshop, we'll do exercises to help you get comfortable with filtering, using native assertions, running tests in parallel, using CLI, and more. We'll also talk about working with TypeScript, making custom reports, and code coverage.