← All posts

Automate code formatting with Transformers

Make sure your code adheres to style guides — on autopilot.

Header image
By Kaustav on 
Share on Twitter Share on LinkedIn Share on Facebook

Do you run code formatters on your local machine to make code look uniform? Or do you run them in CI builds, and manually push the changes to the repository? If you answered “yes” to either of these questions, meet Transformers.

DeepSource Transformers remove the need to have tedious manual setup to make your project’s code stick to a style guide. You set it up once, and watch as DeepSource sends pull requests to your repository, or updates commits with formatted code in an existing pull request, each time a changeset does not adhere to chosen style guides.

No more fumbling with CI scripts. No more mangled commit history in repositories. No more asking a fellow developer to fix their code style before merging.

With this release, you can enable Black and YAPF for Python, and gofmt for Go code, directly in the DeepSource configuration file.

Why we need Transformers

We all have our own preferences when it comes to how we write our code. Some of us like semicolons only when they represent a small pause; some of us like it at the end of every line. When we work on software projects with others, we must not let these differences in code style creep in.

There are code formatters like Black, YAPF, or autopep8 for Python, gofmt for Go, Prettier for JavaScript, and so on. These tools typically are part of code authoring processes — we add them to our code editors and IDEs, or pre-commit hooks, and they help keep the code we write in good shape.

However, only a handful of software teams globally enable automated code-formatting at the repository level. This needs setting up CI pipelines that run these tools on every commit or pull requests. push formatted code back to the repository on every merge, or every pull request.

DeepSource has already made actionable static code analysis easier to setup and maintain. We have also shipped Autofix which generates automated code patches based on issues DeepSource detects. We realized that we can extend what Autofix does, to code formatters. This will let developers to run their chosen code formatters on each commit, alongside analysis runs and autofixes.

This is a great way of reducing noise in issues detected. Instead of just flagging style issues in code, we can take a step ahead and fix it for you, using the tools you trust.

Enable Transformers

Enabling transformers requires you to grant DeepSource write access to your repository. Behind the scenes, transformers use the same app and authorization scopes as autofixes. If you have already given DeepSource Autofix access to create pull requests in your repository, you don’t need to install anything else.

If you haven’t, you can trigger the Autofix installation from “Generate configuration” screen (shown in video below), or you can open your repository’s dashboard in DeepSource and go to “Settings” → “Autofix” and trigger the installation.

Once installed, you can use the configuration screen’s “Transformers” section to add one of the available tools. This will add a [[transformers]] block to .deepsource.toml. Commit the updated code to your repository, or manually edit the configuration, and you are all set.

Now DeepSource will scan all new changes, and if it needed to run Transformers on your code, you will find it under “History” → “Transforms” in your repository dashboard.

What’s next

Read the release notes for Transformers beta launch.

We have added configuration references for Transformers in our docs. Check it out to see the list of available transformers and the configuration required for each.

We are working on adding support for more transformers. Till then, if you already use Black for formatting Python code, or YAPF for Python, give transformers a try. If you write Go and use gofmt for formatting Go code, you can set up that as well. We’d love to hear from you in our community forum.

About DeepSource
DeepSource helps you automatically find and fix issues in your code during code reviews, such as bug risks, anti-patterns, performance issues, and security flaws. It takes less than 5 minutes to set up with your Bitbucket, GitHub, or GitLab account. It can analyze Python, Go, and Ruby code. JavaScript is coming soon.
Sign up for DeepSource

Keep reading...