Javascript Style Guide

From Proxmox VE
Revision as of 11:51, 23 October 2020 by Thomas Lamprecht (talk | contribs) (initial import, derived of the perl one)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Proxmox Javascript Style Guide

Various of our files have inconsistent styles due to historical growth as well as because of the mixed styles we got from various contributors.

Please try to follow this guide if you're working on code for Proxmox projects to avoid adding to the style mess.

Here's a summary of our style (which is somewhat unusual at least when it comes to the mixed indentation).

Indentation

We indent by 4 spaces, assume tabs to be 8 spaces and convert all groups of 8 spaces into tabs at the beginning of a line. Here's an example, with tabs represented via '>........'

function foo(arg1, arg2, arg3) {
    if (arg1) {
>.......console.log(arg2);
>.......if (arg3) {
>.......    if (arg3 !== arg2) {
>.......>.......throw "Exceptions should avoided for dynamic stuff";
>.......    }
>.......}
    }
}

Like all editors for some reason I'll never understand we do not distinguish between indentation and alignment, so if you split up an expression over multiple lines we still use the same "all 8 spaces are 1 tab" pattern.

The vim configuration would be :set ts=8 sts=4 sw=4 noet

Breaking long lines and strings

The preferred limit on the length of a single line is 80 columns.

Statements longer than 80 columns should be broken into sensible chunks, unless exceeding 80 columns significantly increases readability and does not hide information. The maximal line length tolerated for those exceptions is 100 columns.

The vim configuration would be :set cc=81 or, to show both soft and hard limit: :set cc=81,101

Linting

Instead of describing various rules and examples to follow the used coding and formatting style, it's quicker to just use eslint.

We provide an eslint wrapper with our desired rule set backed in as default. You can install it from the 'devel' package repository.

apt install pve-eslint

Check specific files:

eslint src/Utils.js

Check all .js files from this and child directories:

eslint .

A great deal of errors or warnings can be automatically fixed:

eslint --fix .