Outline
Code formatting tools have been popular lately, with Prettier being the leader in the clubhouse. The idea that everyone can write their code how they like and then the file will be formatted according to the team's specifications has removed some unnecessary overhead from the development lifecycle. Prettier works great for JavaScript and TypeScript projects, but Deno also provides a formatter that you can use on your project. To format your file, use the fmt
command, followed by the path to one or more files:
$ deno fmt my-file.ts
/path/to/my-file.ts
Checked 1 file
The format command also provides a flag, --check
, that tells you if a given file has been formatted. If the file is formatted, nothing happens other than a message saying that the file was checked. If the file wasn't formatted, then an error is shown, with the offending lines being output as they are and as they should be.
Lastly, there are times when you have a reason for your code being written in a specific manner. If you have a two dimensional array, for example, you might want it to output in an easy-to-read way that may be altered by the formatter. If you have a section of your code that you don't want to be formatted, you can prevent it with an ignore statement like this:
// deno-fmt-ignore
export const twoDimensionalArray = [
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
];
The block of code that follows the comment, // deno-fmt-ignore
, will not be formatted. Everything else, before and after the comment, will be formatted.