4.2 Block indentation: +2 spaces
Each time a new block or block-like construct is opened, the indent increases bytwo spaces. When the block ends, the indent returns to the previous indentlevel. The indent level applies to both code and comments throughout theblock. (See the example in ??).
4.2.1 Array literals: optionally block-like
Any array literal may optionally be formatted as if it were a “block-likeconstruct.” For example, the following are all valid (not an exhaustivelist):
const a = [
0,
1,
2,
];
const b =
[0, 1, 2];
const c = [0, 1, 2];
someMethod(foo, [
0, 1, 2,
], bar);
Other combinations are allowed, particularly when emphasizing semantic groupingsbetween elements, but should not be used only to reduce the vertical size oflarger arrays.
4.2.2 Object literals: optionally block-like
Any object literal may optionally be formatted as if it were a “block-likeconstruct.” The same examples apply as ??. Forexample, the following are all valid (not an exhaustive list):
const a = {
a: 0,
b: 1,
};
const b =
{a: 0, b: 1};
const c = {a: 0, b: 1};
someMethod(foo, {
a: 0, b: 1,
}, bar);
4.2.3 Class literals
Class literals (whether declarations or expressions) are indented as blocks. Donot add semicolons after methods, or after the closing brace of a classdeclaration (statements—such as assignments—that contain class _expressions_are still terminated with a semicolon). Use the extends
keyword, but not the@extends
JSDoc annotation unless the class extends a templatized type.
Example:
class Foo {
constructor() {
/** @type {number} */
this.x = 42;
}
/** @return {number} */
method() {
return this.x;
}
}
Foo.Empty = class {};
/** @extends {Foo<string>} */
foo.Bar = class extends Foo {
/** @override */
method() {
return super.method() / 2;
}
};
/** @interface */
class Frobnicator {
/** @param {string} message */
frobnicate(message) {}
}
4.2.4 Function expressions
When declaring an anonymous function in the list of arguments for a functioncall, the body of the function is indented two spaces more than the precedingindentation depth.
Example:
prefix.something.reallyLongFunctionName('whatever', (a1, a2) => {
// Indent the function body +2 relative to indentation depth
// of the 'prefix' statement one line above.
if (a1.equals(a2)) {
someOtherLongFunctionName(a1);
} else {
andNowForSomethingCompletelyDifferent(a2.parrot);
}
});
some.reallyLongFunctionCall(arg1, arg2, arg3)
.thatsWrapped()
.then((result) => {
// Indent the function body +2 relative to the indentation depth
// of the '.then()' call.
if (result) {
result.use();
}
});
4.2.5 Switch statements
As with any other block, the contents of a switch block are indented +2.
After a switch label, a newline appears, and the indentation level is increased+2, exactly as if a block were being opened. An explicit block may be used ifrequired by lexical scoping. The following switch label returns to the previousindentation level, as if a block had been closed.
A blank line is optional between a break
and the following case.
Example:
switch (animal) {
case Animal.BANDERSNATCH:
handleBandersnatch();
break;
case Animal.JABBERWOCK:
handleJabberwock();
break;
default:
throw new Error('Unknown animal');
}