@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1@title Javascript Coding Standards
2@group standards
3
4This document describes Javascript coding standards for Phorge and Javelin.
5
6= Overview =
7
8This document outlines technical and style guidelines which are followed in
9Phorge and Javelin. Contributors should also follow these guidelines. Many
10of these guidelines are automatically enforced by lint.
11
12These guidelines are essentially identical to the Facebook guidelines, since I
13basically copy-pasted them. If you are already familiar with the Facebook
14guidelines, you can probably get away with skimming this document.
15
16
17= Spaces, Linebreaks and Indentation =
18
19 - Use two spaces for indentation. Don't use literal tab characters.
20 - Use Unix linebreaks ("\n"), not MSDOS ("\r\n") or OS9 ("\r").
21 - Put a space after control keywords like `if` and `for`.
22 - Put a space after commas in argument lists.
23 - Put space around operators like `=`, `<`, etc.
24 - Don't put spaces after function names.
25 - Parentheses should hug their contents.
26 - Generally, prefer to wrap code at 80 columns.
27
28= Case and Capitalization =
29
30The Javascript language unambiguously dictates casing/naming rules; follow those
31rules.
32
33 - Name variables using `lowercase_with_underscores`.
34 - Name classes using `UpperCamelCase`.
35 - Name methods and properties using `lowerCamelCase`.
36 - Name global functions using `lowerCamelCase`. Avoid defining global
37 functions.
38 - Name constants using `UPPERCASE`.
39 - Write `true`, `false`, and `null` in lowercase.
40 - "Internal" methods and properties should be prefixed with an underscore.
41 For more information about what "internal" means, see
42 **Leading Underscores**, below.
43
44= Comments =
45
46 - Strongly prefer `//` comments for making comments inside the bodies of
47 functions and methods (this lets someone easily comment out a block of code
48 while debugging later).
49
50= Javascript Language =
51
52 - Use `[]` and `{}`, not `new Array` and `new Object`.
53 - When creating an object literal, do not quote keys unless required.
54
55= Examples =
56
57**if/else:**
58
59 lang=js
60 if (x > 3) {
61 // ...
62 } else if (x === null) {
63 // ...
64 } else {
65 // ...
66 }
67
68You should always put braces around the body of an if clause, even if it is only
69one line. Note that operators like `>` and `===` are also surrounded by
70spaces.
71
72**for (iteration):**
73
74 lang=js
75 for (var ii = 0; ii < 10; ii++) {
76 // ...
77 }
78
79Prefer ii, jj, kk, etc., as iterators, since they're easier to pick out
80visually and react better to "Find Next..." in editors.
81
82**for (enumeration):**
83
84 lang=js
85 for (var k in obj) {
86 // ...
87 }
88
89Make sure you use enumeration only on Objects, not on Arrays. For more details,
90see @{article:Javascript Object and Array}.
91
92**switch:**
93
94 lang=js
95 switch (x) {
96 case 1:
97 // ...
98 break;
99 case 2:
100 if (flag) {
101 break;
102 }
103 break;
104 default:
105 // ...
106 break;
107 }
108
109`break` statements should be indented to block level. If you don't push them
110in, you end up with an inconsistent rule for conditional `break` statements,
111as in the `2` case.
112
113If you insist on having a "fall through" case that does not end with `break`,
114make it clear in a comment that you wrote this intentionally. For instance:
115
116 lang=js
117 switch (x) {
118 case 1:
119 // ...
120 // Fall through...
121 case 2:
122 //...
123 break;
124 }
125
126= Leading Underscores =
127
128By convention, methods names which start with a leading underscore are
129considered "internal", which (roughly) means "private". The critical difference
130is that this is treated as a signal to Javascript processing scripts that a
131symbol is safe to rename since it is not referenced outside the current file.
132
133The upshot here is:
134
135 - name internal methods which shouldn't be called outside of a file's scope
136 with a leading underscore; and
137 - **never** call an internal method from another file.
138
139If you treat them as though they were "private", you won't run into problems.