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.

This talk has been presented at TypeScript Congress 2023, check out the latest edition of this Tech Conference.

FAQ

An Abstract Syntax Tree (AST) is a tree representation of the abstract syntactic structure of source code. It is used in static code analysis for various transformations such as removing specific code patterns, optimizing code, and ensuring code quality by abstracting the structure and making manipulation easier.

TypeScript's language server can enhance static code analysis by providing accurate type information and refactoring tools, which can be programmatically accessed to improve semantic analysis and code transformation tasks, ensuring type safety and efficiency in code modifications.

The code compilation process typically involves four stages: lexical analysis, syntax analysis, semantic analysis, and code generation. Each stage plays a crucial role in transforming source code into executable code.

TSMorph is a library that wraps the TypeScript compiler API to provide a simpler interface for interacting with the TypeScript compiler. It is used during abstract syntax tree analysis to programmatically access type information and perform type checks or transformations based on that information.

A Babel plugin can traverse the Abstract Syntax Tree (AST), identify console.log call expressions, and remove them based on specific conditions. This process involves checking the callee of the call expression and manipulating the tree to exclude unwanted expressions, thereby modifying the source code during the build process.

Using TypeScript in static code analysis provides the advantage of type safety, which allows for more precise code transformations and optimizations. TypeScript's type system and language server provide tools and information that can enforce type constraints and facilitate more informed and reliable code edits.

AST is used in JavaScript development for build-time optimizations like minification and transpiling, formatting code, and custom transformations such as removing unused code. It allows developers to manipulate code structure programmatically, enhancing performance and maintainability.

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.

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.

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

React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
Watch video: React's Most Useful Types
Today's Talk focuses on React's best types and JSX. It covers the types of JSX and React components, including React.fc and React.reactnode. The discussion also explores JSX intrinsic elements and react.component props, highlighting their differences and use cases. The Talk concludes with insights on using React.componentType and passing components, as well as utilizing the react.element ref type for external libraries like React-Select.
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
React and TypeScript have a strong relationship, with TypeScript offering benefits like better type checking and contract enforcement. Failing early and failing hard is important in software development to catch errors and debug effectively. TypeScript provides early detection of errors and ensures data accuracy in components and hooks. It offers superior type safety but can become complex as the codebase grows. Using union types in props can resolve errors and address dependencies. Dynamic communication and type contracts can be achieved through generics. Understanding React's built-in types and hooks like useState and useRef is crucial for leveraging their functionality.
Making Magic: Building a TypeScript-First Framework
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
Top Content
Daniel Rowe discusses building a TypeScript-first framework at TypeScript Congress and shares his involvement in various projects. Nuxt is a progressive framework built on Vue.js, aiming to reduce friction and distraction for developers. It leverages TypeScript for inference and aims to be the source of truth for projects. Nuxt provides type safety and extensibility through integration with TypeScript. Migrating to TypeScript offers long-term maintenance benefits and can uncover hidden bugs. Nuxt focuses on improving existing tools and finds inspiration in frameworks like TRPC.
Stop Writing Your Routes
Vue.js London 2023Vue.js London 2023
30 min
Stop Writing Your Routes
Designing APIs is a challenge, and it's important to consider the language used and different versions of the API. API ergonomics focus on ease of use and trade-offs. Routing is a misunderstood aspect of API design, and file-based routing can simplify it. Unplugging View Router provides typed routes and eliminates the need to pass routes when creating the router. Data loading and handling can be improved with data loaders and predictable routes. Handling protected routes and index and ID files are also discussed.
Faster TypeScript builds with --isolatedDeclarations
TypeScript Congress 2023TypeScript Congress 2023
24 min
Faster TypeScript builds with --isolatedDeclarations
Top Content
This talk discusses the performance issues in TypeScript builds and introduces a new feature called isolated declarations. By running the compiler in parallel and using isolated modules, significant performance gains can be achieved. Isolated declarations improve build speed, compatibility with other tools, and require developers to write types in code. This feature has the potential to further increase performance and may be available in TypeScript soon.
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
Alex introduces tRPC, a toolkit for making end-to-end type-safe APIs easily, with auto-completion of API endpoints and inferred data from backend to frontend. tRPC works the same way in React Native and can be adopted incrementally. The example showcases backend communication with a database using queries and validators, with types inferred to the frontend and data retrieval done using Prisma ORM.

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.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Featured 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.
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.
Building Your Own Custom Type System
React Summit 2024React Summit 2024
38 min
Building Your Own Custom Type System
Featured Workshop
Kunal Dubey
Kunal Dubey
I'll introduce the audience to a concept where they can have end-to-end type systems that helps ensure typesafety across the teams Such a system not only improves communication between teams but also helps teams collaborate effectively and ship way faster than they used to before. By having a custom type system, teams can also identify the errors and modify the API contracts on their IDE, which contributes to a better Developer Experience. The workshop would primarily leverage TS to showcase the concept and use tools like OpenAPI to generate the typesystem on the client side. 
Frictionless Development With Unified Type System
JSNation 2024JSNation 2024
113 min
Frictionless Development With Unified Type System
Featured Workshop
Ejiro Asiuwhu
Ejiro Asiuwhu
Imagine developing where frontend and backend sing in harmony, types dance in perfect sync, and errors become a distant memory. That's the magic of TypeScript Nirvana!
Join me on a journey to unveil the secrets of unified type definitions, the key to unlocking frictionless development. We'll dive into:
- Shared language, shared love: Define types once, share them everywhere. Consistency becomes your BFF, errors your worst nightmare (one you'll rarely see).- Effortless coding: Ditch the manual grind of type checking. TypeScript's got your back, freeing you to focus on building awesomeness.- Maintainability magic: With crystal-clear types guiding your code, maintaining it becomes a walk in the park. More time innovating, less time debugging.- Security fortress: TypeScript's type system shields your app from common vulnerabilities, making it a fortress against security threats.