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):

  1. const a = [
  2. 0,
  3. 1,
  4. 2,
  5. ];
  6. const b =
  7. [0, 1, 2];
  1. const c = [0, 1, 2];
  2. someMethod(foo, [
  3. 0, 1, 2,
  4. ], 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):

  1. const a = {
  2. a: 0,
  3. b: 1,
  4. };
  5. const b =
  6. {a: 0, b: 1};
  1. const c = {a: 0, b: 1};
  2. someMethod(foo, {
  3. a: 0, b: 1,
  4. }, 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:

  1. class Foo {
  2. constructor() {
  3. /** @type {number} */
  4. this.x = 42;
  5. }
  6. /** @return {number} */
  7. method() {
  8. return this.x;
  9. }
  10. }
  11. Foo.Empty = class {};
  1. /** @extends {Foo<string>} */
  2. foo.Bar = class extends Foo {
  3. /** @override */
  4. method() {
  5. return super.method() / 2;
  6. }
  7. };
  8. /** @interface */
  9. class Frobnicator {
  10. /** @param {string} message */
  11. frobnicate(message) {}
  12. }

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:

  1. prefix.something.reallyLongFunctionName('whatever', (a1, a2) => {
  2. // Indent the function body +2 relative to indentation depth
  3. // of the 'prefix' statement one line above.
  4. if (a1.equals(a2)) {
  5. someOtherLongFunctionName(a1);
  6. } else {
  7. andNowForSomethingCompletelyDifferent(a2.parrot);
  8. }
  9. });
  10. some.reallyLongFunctionCall(arg1, arg2, arg3)
  11. .thatsWrapped()
  12. .then((result) => {
  13. // Indent the function body +2 relative to the indentation depth
  14. // of the '.then()' call.
  15. if (result) {
  16. result.use();
  17. }
  18. });

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:

  1. switch (animal) {
  2. case Animal.BANDERSNATCH:
  3. handleBandersnatch();
  4. break;
  5. case Animal.JABBERWOCK:
  6. handleJabberwock();
  7. break;
  8. default:
  9. throw new Error('Unknown animal');
  10. }