Transformers: Automatically format all incoming code

Transformers: Beta Release

We are happy to announce that we are releasing a new feature – Transformers today.

With Transformers, you can automatically format code with the available code formatters, and if the code doesn’t already conform to the style, changes are committed back.

Right now, the following tools are supported:

Transformer Release Shortcode
Black Beta black
Gofmt Beta gofmt
YAPF Beta yapf

Here’s how it works: for all repositories which have at least one transformer enabled in config:

  • If a Pull/Merge Request is raised with the default branch as base, a
    transforming commit is added to that branch.
  • If a commit is created on the default branch, DeepSource raises a Pull/Merge
    Request with transforming changes.

FAQ

How do I enable Transformers?

Add an entry in transformers table in .deepsource.toml with the name of the transformer and enabled = true.

e.g. to add black:

[[transformers]]
    name = "black"
    enabled = true

If you don’t like to type TOML by hand, go to Settings → “Generate Configuration” and select the ones you’d like to add from the list.

How to specify additional configuration to formatters?

If you have a configuration file for the formatter/linter (pyproject.toml for black, .style.yapf for yapf, etc.), we already respect it. However, the beta version doesn’t support supplying meta-configuration options via .deepsource.toml (yet!).

Our project doesn’t use any code formatter right now, but we want to start using one. What do we do?

When transformers are first introduced in .deepsource.toml (in default branch), the entire code base is transformed and a PR/MR is raised with all the changes. Once that is merged, the entire code base conforms to the style.

Then onward, for each new commit/PR/MR, all incoming code would automatically be formatted.

If I’m using Transformers, do I need to add the code formatter as a dependency to my project?

It’s entirely up to you. If you want to encourage your developers to use it locally, and push only then – yes, add it as a dev-dependency by all means. But, as far as DeepSource is concerned, you can leave your dependencies as is.

What about other tools?

Currently, we’re planning to add support for isort, autopep8, prettier, rubocop, rufo and many others… very soon, stay tuned!

Can I use Transformers without turning on Analysis?

It’s required to have at least a single DeepSource analyzer enabled to be able to use Transformers.

P.S. The release is still in Beta, and some things might break.

2 Likes

Hey,
The auto-commit bot automatically adds commit in the branch, can we stop that?