| Defined | src/docs/contributing/javascript_coding_standards.diviner:1 |
|---|---|
| Group | Contributing |
This document describes Javascript coding standards for Phabricator and Javelin.
This document outlines technical and style guidelines which are followed in Phabricator and Javelin. Contributors should also follow these guidelines. Many of these guidelines are automatically enforced by lint.
These guidelines are essentially identical to the Facebook guidelines, since I basically copy-pasted them. If you are already familiar with the Facebook guidelines, you can probably get away with skimming this document.
The Javascript language unambiguously dictates casing/naming rules; follow those rules.
if/else:
if (x > 3) { // ... } else if (x === null) { // ... } else { // ... }
You should always put braces around the body of an if clause, even if it is only one line. Note that operators like > and === are also surrounded by spaces.
for (iteration):
for (var ii = 0; ii < 10; ii++) { // ... }
Prefer ii, jj, kk, etc., as iterators, since they're easier to pick out visually and react better to "Find Next..." in editors.
for (enumeration):
for (var k in obj) { // ... }
Make sure you use enumeration only on Objects, not on Arrays. For more details, see Javascript Object and Array.
switch:
switch (x) { case 1: // ... break; case 2: if (flag) { break; } break; default: // ... break; }
break statements should be indented to block level. If you don't push them in, you end up with an inconsistent rule for conditional break statements, as in the 2 case.
If you insist on having a "fall through" case that does not end with break, make it clear in a comment that you wrote this intentionally. For instance:
switch (x) { case 1: // ... // Fall through... case 2: //... break; }
By convention, methods names which start with a leading underscore are considered "internal", which (roughly) means "private". The critical difference is that this is treated as a signal to Javascript processing scripts that a symbol is safe to rename since it is not referenced outside the current file.
The upshot here is:
If you treat them as though they were "private", you won't run into problems.