Start Building Your Own JavaScript Tools

Rate this content
Bookmark

Your first JavaScript tool might not be the next Babel or ESLint, but it can be built on them! Let's demystify the secret art of JavaScript tools, how they work, and how to build our own. We'll discover the opportunities in our everyday work to apply these techniques, writing our own ESLint rules to prevent mistakes and code transforms to make breaking changes easy to apply. We’ll walk through the fundamentals of working with an abstract syntax tree, and develop our understanding through a live-code. You will be amazed at what you can build, and together we’ll explore how to get started.

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

FAQ

Developers can start by learning about ASTs and using tools like ESLint for custom linting rules or JS CodeShift for code mods. Exploring community resources, documentation, and experimenting with modifying existing tools or creating new ones based on project needs are good initial steps.

To create your own ESLint rules, you can start by using tools like astexplorer.net to understand the abstract syntax tree (AST) of the code. Then, define conditions and node types you care about in a rule file, and use ESLint's API to report and enforce the rules in your codebase.

For ESLint, the official documentation at eslint.org is highly recommended. For code mods, the CodeShift Community website provides examples and best practices. Additionally, astsareawesome.com offers links and resources to further explore tool creation in JavaScript.

Code mods are beneficial for making repetitive or large-scale changes to a codebase efficiently, reducing the potential for errors and saving developers time. They are particularly useful for upgrading to new versions of libraries that include breaking changes, allowing for seamless transitions.

ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs. It is useful for automating code review feedback, sharing best practices, and preventing common coding mistakes by enforcing rules and styles.

A code mod is a script used to transform source code according to specific rules or API changes, typically aiding in large-scale refactoring or upgrades. Unlike ESLint rules that primarily identify and report issues, code mods automate the process of making changes to the code syntax itself.

Will Klein
Will Klein
22 min
05 Jun, 2023

Comments

Sign in or register to post your comment.
Video Summary and Transcription
[♪ music ♪ by The Illuminati plays)] I see a common thread across any project I work on. Different developers are making the same mistake and we have preferred ways of doing things. Preventing mistakes and sharing best practices are great reasons to look at tools like linters and in particular ESLint. Let's write our first rule together. We're just scratching the surface of building our own tools, which can have a massive impact on improving the developer experience.

1. Introduction to Building Custom Tools with ESLint

Short description:

[♪ music ♪ by The Illuminati plays)] I see a common thread across any project I work on. Different developers are making the same mistake and we have preferred ways of doing things. Preventing mistakes and sharing best practices are great reasons to look at tools like linters and in particular ESLint. Let's write our first rule together. I'm going to share a little about what I work on and a problem I like to solve where I think would be really helpful. Right now, I work on a design system called Canvas and we provide a component library for UI developers to use. We invest a lot into making our components accessible and useful across a wide range of use cases. Let's dive in.

[♪ music ♪ by The Illuminati plays)] [♪ music ♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ by The Illuminati plays♪ show you how. What if we could get started with building our own tools? Where would we begin? What if the tools were specific to our project? Uniquely helpful and tailored to our needs? What if we could learn how to do this in the next 20 minutes learning what we need to know and how to take our first steps? In the next 20 minutes, let's write our first printing rule, write our first code mod, look at our first AST and build our understanding of them, and learn the fundamentals of how our tools work. Let's dive in.

I see a common thread across any project I work on. Different developers are making the same mistake and we have preferred ways of doing things while it's hard to communicate that to everyone else on our project or using our library. Preventing mistakes and sharing best practices are great reasons to look at tools like linters and in particular ESLint. Not only can we use ESLint's existing rules to handle many problems, but it has a rich ecosystem of plugins specific to different areas and we can write our own rules and plugins, which I'll show you how. ESLint rules are great for problems or mistakes that keep coming up, creating a better feedback loop for developers as they're writing their code, automating code review feedback, sharing best practices, and preventing anti-patterns in our code bases. Rules can be for any project, maybe just for your app, helping developers on your immediate team or others in a shared code base. If you maintain a library, they can help anyone using your library. Anyone can write these rules, not just project maintainers or expert developers.

All right, let's write our first rule together. I'm going to share a little about what I work on and a problem I like to solve where I think would be really helpful. Right now, I work on a design system called Canvas and we provide a component library for UI developers to use. We invest a lot into making our components accessible and useful across a wide range of use cases. As part of that effort, we maintain three different packages for our components, main, preview, and labs. We usually build new components in labs or preview, and as we prove out the API, refine design and make sure accessibility is perfected, we'll promote things from labs and preview up to main. This presents a somewhat unique challenge though. We have some old components, usually in main, that are going to be replaced by newer ones in labs or preview. When a developer is choosing between the two versions though, which way do they go? Sometimes, we really want to tell developers to use the newer one in labs or preview. We already plan to promote it to main in an upcoming release. The component is ready to use and has some benefits we don't want our users to miss out on. How do we communicate this? We do what we can with our docs and sending out announcements, but the best way is to tell them as they write their code. I'm pulling up an online tool that I use to test out new rules. Usually I'll develop with a test suite writing, Lint rules is a really good use case for test driven development. This tool is great to explore the problem, get a first draft, and often that's enough. This is astexplorer.net. There are a few things going on here, but I'll explain how each part works. On the left, there are two editors placed for the code we're working on on the top left and editor for writing an AS Lint rule down below and other tools as well. On the right, we have an AST viewer up above and the output window for debugging below that.

2. Understanding AST and Sample Code

Short description:

In the header, we can configure the parser and transform type. The AST viewer is amazing and we're going to explore it. An AST is just a tree that stores the syntax of our code. We can tell ESLint what we care about by specifying node types. Looking at this gives us everything we need to detect patterns. Let's look at some sample code that includes imports for our components. We have a not preferred situation with a segmented control component. When we selected the transform for ESLint V8, we got some sample code with a create function that returns an object literal.

In the header, we can also configure the parser and transform type which I've already set to Babel AS Lint parser and AS Lint v8. Besides giving us a really useful development environment, this AST viewer in the top right is amazing and we're going to explore it in a bit.

AST is an acronym for abstract syntaxtry. If you didn't study computer science at university though, the terminology for an AST might seem intimidating. I promise you though, it's just a tree and if you've worked with a DOM for a webpage, you know how a tree works. An AST just has the syntax for our code stored in it. We can explore this tree in the AST Explorer. It's also worth mentioning that the tree is basically a JavaScript object. There's nothing really new here. Like an HTML tag at the top of a webpage, that being the top node of the tree in the DOM, the top of the AST is the program node. This program node has child nodes such as the ones under body. Here we've got multiple import declarations and these import declarations have children of their own and so on. There's another type of property on a node that's very important, the type. This is how we're going to tell ESLint what we care about. We're going to tell it this for this node type. We want to hear about it and ESLint is going to handle walking the tree and any time it node that we care about it's going to pass it into a function that we can control and define. There are also some properties to describe each node, like the source type being module on the program. There are other values that we can look at that can be really helpful depending on the node we're working with. Looking at this gives us everything we need to detect patterns which is what an ESLint rule is doing.

Let's look at some sample code that includes imports for our components. On the top left we've got a couple different examples, a few actually. First there's the not preferred situation where we have a segmented control component that is coming from our main package CanvasKit React here. That is not preferred because we have a newer one in CanvasKit Preview React. This is our preview package. We want to direct developers to using that over our main package at some point. When we selected the transform for ESLint V8, we got some sample code in the bottom left. This is awesome because it's going to give us a place to kick off. I've cleaned it up a little bit down to its bare essentials. What we have is a create function. This create function returns an object literal.

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

Vite: Rethinking Frontend Tooling
JSNation Live 2021JSNation Live 2021
31 min
Vite: Rethinking Frontend Tooling
Top Content
Vite is a next-generation build tool that leverages native ES modules for improved performance. It eliminates the need for bundling and improves hot module replacement. Vite provides an opinionated default configuration while still allowing advanced customization through plugins. It is framework agnostic and can be used for React and other applications. Vite is being adopted by Next.js and Create React App, and integration with Nuxt 3 offers significant speed improvements.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced 2023React Advanced 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
Watch video: React Compiler - Understanding Idiomatic React (React Forget)
Joe Savona
Mofei Zhang
2 authors
The Talk discusses React Forget, a compiler built at Meta that aims to optimize client-side React development. It explores the use of memoization to improve performance and the vision of Forget to automatically determine dependencies at build time. Forget is named with an F-word pun and has the potential to optimize server builds and enable dead code elimination. The team plans to make Forget open-source and is focused on ensuring its quality before release.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
Turbopack. Why? How? When? and the Vision...
React Day Berlin 2022React Day Berlin 2022
32 min
Turbopack. Why? How? When? and the Vision...
The Talk discusses TurboPack, a successor to Webpack, aiming to create a framework-independent, flexible, and extensible tool for the open-source community. It addresses performance challenges by integrating SWC into Next.js. The challenges with Next.js and Webpack include orchestration issues, backward compatibility constraints, and cache invalidation problems. TurboEngine and TurboPack provide constant performance in incremental builds, leveraging Rust's predictable performance and parallelism. The Talk also covers topics like dependency tracking, task graphs, cache invalidation, lazy asset graphs, and the integration of TurboPack with Next.js. The future plans involve reconfiguring Webpack and TurboEngine, moving computations to the cloud, providing insights into builds, and facilitating migration and integration with JavaScript projects.
How Bun Makes Building React Apps Simpler & Faster
React Day Berlin 2022React Day Berlin 2022
9 min
How Bun Makes Building React Apps Simpler & Faster
BUN is a modern all-in-one JavaScript runtime environment that achieves new levels of performance. It includes BUN dev, a fast front-end dev server, BUN install, a speedy package manager, and BUN run, a fast package runner. BUN supports JSX, has optimized React server-side rendering, and offers hot module reloading on the server. The priorities for BUN include stability, node compatibility, documentation improvement, missing features in BUN install, AST plugin API, native Windows support, Bundler and Minifier optimization, and easier deployment to production. BUN's AST plugin API allows for bundle-time JavaScript execution and embedding code, potentially inspiring new frameworks.
The Core of Turbopack Explained (Live Coding)
JSNation 2023JSNation 2023
29 min
The Core of Turbopack Explained (Live Coding)
Tobias Koppers introduces TurboPack and TurboEngine, addressing the limitations of Webpack. He demonstrates live coding to showcase the optimization of cache validation and build efficiency. The talk covers adding logging and memorization, optimizing execution and tracking dependencies, implementing invalidation and watcher, and storing and deleting invalidators. It also discusses incremental compilation, integration with other monorepo tools, error display, and the possibility of a plugin system for Toolpag. Lastly, the comparison with Bunn's Builder is mentioned.

Workshops on related topic

Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
React Day Berlin 2022React Day Berlin 2022
86 min
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
Top Content
WorkshopFree
Hussien Khayoon
Kahvi Patel
2 authors
Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?
In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.
Solve 100% Of Your Errors: How to Root Cause Issues Faster With Session Replay
JSNation 2023JSNation 2023
44 min
Solve 100% Of Your Errors: How to Root Cause Issues Faster With Session Replay
WorkshopFree
Ryan Albrecht
Ryan Albrecht
You know that annoying bug? The one that doesn’t show up locally? And no matter how many times you try to recreate the environment you can’t reproduce it? You’ve gone through the breadcrumbs, read through the stack trace, and are now playing detective to piece together support tickets to make sure it’s real.
Join Sentry developer Ryan Albrecht in this talk to learn how developers can use Session Replay - a tool that provides video-like reproductions of user interactions - to identify, reproduce, and resolve errors and performance issues faster (without rolling your head on your keyboard).
Build React-like apps for internal tooling 10x faster with Retool
JSNation Live 2021JSNation Live 2021
86 min
Build React-like apps for internal tooling 10x faster with Retool
Workshop
Chris Smith
Chris Smith
Most businesses have to build custom software and bespoke interfaces to their data in order to power internal processes like user trial extensions, refunds, inventory management, user administration, etc. These applications have unique requirements and often, solving the problem quickly is more important than appearance. Retool makes it easy for js developers to rapidly build React-like apps for internal tools using prebuilt API and database interfaces as well as reusable UI components. In this workshop, we’ll walk through how some of the fastest growing businesses are doing internal tooling and build out some simple apps to explain how Retool works off of your existing JavaScript and ReactJS knowledge to enable rapid tool building.
Prerequisites:A free Retool.com trial accountSome minimal JavaScript and SQL/NoSQL database experience
Retool useful link: https://docs.retool.com/docs