Skip to content

Formatting .js with clang format

Joel Martinez edited this page Dec 18, 2015 · 1 revision

Formatting .js with clang-format

Overview

Closure Library auto-formats its codebase with a tool called clang-format. If submitting a Pull Request to Closure Library, you may be asked to format your code with it.

Download/install clang-format

  • Download appropriate Clang archive from the "Pre-Built Binaries" list here.
  • Extract the downloaded tar.
  • In the extracted archive, the files you will use are:
    • bin/clang-format: Core clang-format binary.
    • share/clang/clang-format-diff.py: Utility script to run clang-format on a diff.

Using clang-format on Pull Requests

The python script share/clang/clang-format-diff.py parses the output of a unified diff and reformats all contained lines with clang-format.

This command will format the diffed lines of your current branch compared to HEAD.

git diff -U0 HEAD^ | clang-format-diff.py -i -p1 -style=Google

Formatting an entire file

Here is an example of using clang-format directly.

This command prints the formatted output of the files to STDOUT:

clang-format -style=Google foo.js bar.js

If you're happy with your changes, add the -i flag to tell clang-format to do an inline edit of the files (this will change your files):

clang-format -i -style=Google foo.js bar.js

See the full clang-format documentation for more info.