Skip to content

Commit ce4b2dd

Browse files
committed
Update formatting and add rules
1 parent 42babc9 commit ce4b2dd

File tree

1 file changed

+161
-47
lines changed

1 file changed

+161
-47
lines changed

docs/style-guides/javascript/README.md

Lines changed: 161 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,24 @@ TODO: ESLint rule
806806

807807
<!-- </rule> -->
808808

809+
<!-- <rule> -->
810+
811+
### R: No template strings
812+
813+
##### Reason
814+
815+
Immediate evaluation prevents a template being stored in a variable. Token syntax is fixed. Whitespace sensitivity causes alignment issues, especially within nested code blocks.
816+
817+
##### Notes
818+
819+
* A function which performs string concatenation is equivalently effective.
820+
821+
##### Enforcement
822+
823+
TODO: ESLint rule. Code review.
824+
825+
<!-- </rule> -->
826+
809827
<!-- </rule-set> -->
810828

811829

@@ -1092,6 +1110,45 @@ TODO: ESLint rule
10921110

10931111
<!-- <rule> -->
10941112

1113+
<!-- <rule> -->
1114+
1115+
### R: No object literal shorthand
1116+
1117+
##### Reason
1118+
1119+
Unnecessary syntactic sugar. In complex objects, shorthand notation decreases readability. Prefer making key-value pairs explicit.
1120+
1121+
##### Bad Example
1122+
1123+
``` javascript
1124+
var foo = 'beep';
1125+
var x = true;
1126+
var y = 10;
1127+
1128+
var obj = { foo, 'baz': 'boop', x, y };
1129+
```
1130+
1131+
##### Good Example
1132+
1133+
``` javascript
1134+
var foo = 'beep';
1135+
var x = true;
1136+
var y = 10;
1137+
1138+
var obj = {
1139+
'foo': foo,
1140+
'baz': 'boop',
1141+
'x': x,
1142+
'y': y
1143+
};
1144+
```
1145+
1146+
##### Enforcement
1147+
1148+
TODO: ESLint rule. Code review.
1149+
1150+
<!-- </rule> -->
1151+
10951152
<!-- <rule-set> -->
10961153

10971154

@@ -1301,11 +1358,11 @@ TODO: ESLint rule
13011358

13021359
<!-- <rule> -->
13031360

1304-
### R: Enclosed function below return statement
1361+
### R: Enclosed functions below return statement
13051362

13061363
##### Reason
13071364

1308-
Reduces noise when first attempting understand implementation flow, especially if enclosed functions are documented.
1365+
Reduces noise when first attempting to understand implementation flow, especially if enclosed functions are documented.
13091366

13101367
##### Bad Example
13111368

@@ -1374,7 +1431,7 @@ TODO: ESLint rule
13741431

13751432
##### Reason
13761433

1377-
Avoids unnecessary `function` calls introduce additional overhead and, often, functional counterparts do not save space, a frequently cited benefit.
1434+
Function calls introduce additional overhead and, often, functional counterparts do not save space, a frequently cited benefit.
13781435

13791436
##### Bad Example
13801437

@@ -1512,6 +1569,20 @@ request({
15121569
* If no errors; the `error` argument should be `null`.
15131570

15141571

1572+
##### Enforcement
1573+
1574+
Code review.
1575+
1576+
<!-- </rule> -->
1577+
1578+
<!-- <rule> -->
1579+
1580+
### R: No promises
1581+
1582+
##### Reason
1583+
1584+
Error handling in `promises` is ill-defined. These primitives originated primarily due to poor coding practices when using callbacks rather than out of existential need.
1585+
15151586
##### Enforcement
15161587

15171588
Code review.
@@ -1647,7 +1718,7 @@ function fcn() {
16471718
##### Good Example
16481719

16491720
``` javascript
1650-
// Do:
1721+
// Do...
16511722
function fcn() {
16521723
var nargs = arguments.length;
16531724
var args = new Array( nargs );
@@ -1730,62 +1801,105 @@ TODO: ESLint rule
17301801

17311802
<!-- <rule> -->
17321803

1733-
* Do assign regular expressions to variables rather than using them inline.
1804+
### R: Assign to variables
17341805

1735-
``` javascript
1736-
// Do:
1737-
var regex = /\.+/;
1806+
##### Reason
17381807

1739-
beep();
1808+
Ensures a regular expression is only created once and improves readability.
17401809

1741-
function beep( str ) {
1742-
if ( regex.test( str ) ) {
1743-
// Do something...
1744-
}
1810+
##### Bad Example
1811+
1812+
``` javascript
1813+
// Do not...
1814+
beep();
1815+
1816+
function beep( str ) {
1817+
if ( /\.+/.test( str ) ) {
1818+
// Do something...
17451819
}
1820+
}
1821+
```
17461822

1747-
// Don't:
1748-
beep();
1823+
##### Good Example
17491824

1750-
function beep( str ) {
1751-
if ( /\.+/.test( str ) ) {
1752-
// Do something...
1753-
}
1825+
``` javascript
1826+
// Do...
1827+
var regex = /\.+/;
1828+
1829+
beep();
1830+
1831+
function beep( str ) {
1832+
if ( regex.test( str ) ) {
1833+
// Do something...
17541834
}
1755-
```
1835+
}
1836+
```
1837+
1838+
##### Enforcement
17561839

1757-
* __Always__ document regular expressions.
1840+
Code review.
17581841

1759-
``` javascript
1760-
/**
1761-
* REGEX: /^\/((?:\\\/|[^\/])+)\/([imgy]*)$/
1762-
* Matches parts of a regular expression string.
1763-
*
1764-
* /^\/
1765-
* - match a string that begins with a /
1766-
* ()
1767-
* - capture
1768-
* (?:)+
1769-
* - capture, but do not remember, a group of characters which occur 1 or more times
1770-
* \\\/
1771-
* - match the literal \/
1772-
* |
1773-
* - OR
1774-
* [^\/]
1775-
* - anything which is not the literal \/
1776-
* \/
1777-
* - match the literal /
1778-
* ([imgy]*)
1779-
* - capture any characters matching `imgy` occurring 0 or more times
1780-
* $/
1781-
* - string end
1782-
*/
1783-
var re = /^\/((?:\\\/|[^\/])+)\/([imgy]*)$/;
1784-
```
1842+
<!-- </rule> -->
1843+
1844+
<!-- <rule> -->
1845+
1846+
### R: Documentation
1847+
1848+
##### Reason
1849+
1850+
Regular expressions are error prone and difficult to understand without thorough examination.
1851+
1852+
##### Good Example
1853+
1854+
``` javascript
1855+
/**
1856+
* Matches parts of a regular expression string.
1857+
*
1858+
* Regular expression: `/^\/((?:\\\/|[^\/])+)\/([imgy]*)$/`
1859+
*
1860+
* `/^\/`
1861+
* - match a string that begins with a /
1862+
* `()`
1863+
* - capture
1864+
* `(?:)+`
1865+
* - capture, but do not remember, a group of characters which occur one or more times
1866+
* `\\\/`
1867+
* - match the literal `\/`
1868+
* `|`
1869+
* - OR
1870+
* `[^\/]`
1871+
* - anything which is not the literal `\/`
1872+
* `\/`
1873+
* - match the literal `/`
1874+
* `([imgy]*)`
1875+
* - capture any characters matching `imgy` occurring zero or more times
1876+
* `$/`
1877+
* - string end
1878+
*
1879+
* @constant
1880+
* @type {RegExp}
1881+
* @default /^\/((?:\\\/|[^\/])+)\/([imgy]*)$/
1882+
*/
1883+
var RE = /^\/((?:\\\/|[^\/])+)\/([imgy]*)$/;
1884+
```
1885+
1886+
##### Enforcement
1887+
1888+
Code review.
1889+
1890+
<!-- </rule> -->
1891+
1892+
<!-- </rule-set> -->
1893+
1894+
1895+
<!-- <rule-set> -->
17851896

17861897
---
1898+
17871899
## Blocks
17881900

1901+
<!-- <rule> -->
1902+
17891903
* __Always__ use curly braces. Not using them is a common source of bugs.
17901904

17911905
``` javascript

0 commit comments

Comments
 (0)