Code Formatting
This chapter defines the surface formatting rules every source file must follow so that code reads uniformly across the codebase. It is conditional and applies to the JavaScript sources covered by the stack chapter.
Layout
Section titled “Layout”| Rule | Requirement |
|---|---|
| Indentation | You MUST indent with 4 spaces; you MUST NOT use tabs. |
| Statement terminators | You MUST NOT write trailing semicolons. |
| String quotes | You MUST use single quotes for string literals. |
Spacing
Section titled “Spacing”You MUST place a space inside the parentheses of if conditions, of destructuring payloads, and of static method calls. The opening parenthesis is followed by a space, and the closing parenthesis is preceded by one:
if( isReady === true ) { const { id, name } = payload Registry.write( { id, name } )}Array-method Chains
Section titled “Array-method Chains”When you chain array methods, you MUST break the chain so that each step starts on its own line. Each callback SHOULD use an intermediate named parameter instead of a dense single-letter one:
const names = users .filter( ( user ) => user.active === true ) .map( ( user ) => user.name )Arrow Functions
Section titled “Arrow Functions”You MUST wrap arrow-function parameters in parentheses, even for a single parameter:
// Prohibitedconst double = x => x * 2
// Requiredconst double = ( x ) => x * 2Before / After
Section titled “Before / After”// Beforeif (ready) { const {id,name}=payload; return users.filter(u=>u.active).map(u=>u.name);}
// Afterif( ready === true ) { const { id, name } = payload return users .filter( ( user ) => user.active === true ) .map( ( user ) => user.name )}