From 0153155e24045c5b4ffae7ccf412a45672fab8b6 Mon Sep 17 00:00:00 2001
From: Mike Frysinger
/* Best: perfectly clear even without a comment. */
+/* Best: perfectly clear even without a comment. */
const units = 'μs';
/* Allowed: but unnecessary as μ is a printable character. */
@@ -99,7 +99,7 @@ const units = '\u03bcs'; // 'μs'
return '\ufeff' + content; // Prepend a byte order mark.
-/* Poor: the reader has no idea what character this is. */
+/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
@@ -146,7 +146,7 @@ that it defines concatenated to the end.
Example
-goog.module('search.urlHistory.UrlHistoryService');
+goog.module('search.urlHistory.UrlHistoryService');
3.3.1 Hierarchy
@@ -156,7 +156,7 @@ namespace.
Disallowed:
-goog.module('foo.bar'); // 'foo.bar.qux' would be fine, though
+goog.module('foo.bar'); // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
@@ -173,7 +173,7 @@ child namespaces, since they exist in the same directory.
Example:
-goog.module('my.test.helpers');
+goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
@@ -198,7 +198,7 @@ symbols.
Examples:
-const /** !Array<number> */ exportedArray = [1, 2, 3];
+const /** !Array<number> */ exportedArray = [1, 2, 3];
const /** !Array<number> */ moduleLocalArray = [4, 5, 6];
@@ -215,7 +215,7 @@ function exportedFunction() {
exports = {exportedArray, exportedFunction};
-/** @const {number} */
+/** @const {number} */
exports.CONSTANT_ONE = 1;
/** @const {string} */
@@ -225,7 +225,7 @@ exports.CONSTANT_TWO = 'Another constant';
Do not annotate the exports object as @const as it is already treated as a
constant by the compiler.
-/** @const */
+/** @const */
exports = {exportedFunction};
@@ -247,7 +247,7 @@ exports = {exportedFunction};
ES module files must use the import statement to import other ES module
files. Do not goog.require another ES module.
-import './sideeffects.js';
+import './sideeffects.js';
import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';
@@ -262,10 +262,10 @@ import {name} from './sibling.js';
The .js file extension is not optional in import paths and must always be
included.
-import '../directory/file';
+import '../directory/file';
-import '../directory/file.js';
+import '../directory/file.js';
3.4.1.2 Importing the same file multiple times
@@ -273,7 +273,7 @@ included.
Do not import the same file multiple times. This can make it hard to determine
the aggregate imports of a file.
-// Imports have the same path, but since it doesn't align it can be hard to see.
+// Imports have the same path, but since it doesn't align it can be hard to see.
import {short} from './long/path/to/a/file.js';
import {aLongNameThatBreaksAlignment} from './long/path/to/a/file.js';
@@ -287,12 +287,12 @@ import {aLongNameThatBreaksAlignment} from './long/path/to/a/file.js';
Module import names (import * as name) are lowerCamelCase names that are
derived from the imported file name.
-import * as fileOne from '../file-one.js';
+import * as fileOne from '../file-one.js';
import * as fileTwo from '../file_two.js';
import * as fileThree from '../filethree.js';
-import * as libString from './lib/string.js';
+import * as libString from './lib/string.js';
import * as math from './math/math.js';
import * as vectorMath from './vector/math.js';
@@ -302,7 +302,7 @@ import * as vectorMath from './vector/math.js';
Default import names are derived from the imported file name and follow the
rules in ??.
-import MyClass from '../my-class.js';
+import MyClass from '../my-class.js';
import myFunction from '../my_function.js';
import SOME_CONSTANT from '../someconstant.js';
@@ -318,7 +318,7 @@ the same name. Avoid aliasing imports (import {SomeThing as SomeOtherThing
Prefer fixing name collisions by using a module import (import *) or renaming
the exports themselves.
-import * as bigAnimals from './biganimals.js';
+import * as bigAnimals from './biganimals.js';
import * as domesticatedAnimals from './domesticatedanimals.js';
new bigAnimals.Cat();
@@ -328,7 +328,7 @@ new domesticatedAnimals.Cat();
If renaming a named import is needed then use components of the imported
module's file name or path in the resulting alias.
-import {Cat as BigCat} from './biganimals.js';
+import {Cat as BigCat} from './biganimals.js';
import {Cat as DomesticatedCat} from './domesticatedanimals.js';
new BigCat();
@@ -352,15 +352,15 @@ declaration, or use the export {name}; syntax.
Do not use default exports. Importing modules must give a name to these values,
which can lead to inconsistencies in naming across modules.
-// Do not use default exports:
+// Do not use default exports:
export default class Foo { ... } // BAD!
-// Use named exports:
+// Use named exports:
export class Foo { ... }
-// Alternate style named exports:
+// Alternate style named exports:
class Foo { ... }
export {Foo};
@@ -371,7 +371,7 @@ export {Foo};
Do not export container classes or objects with static methods or properties for
the sake of namespacing.
-// container.js
+// container.js
// Bad: Container is an exported class that has only static methods and fields.
export class Container {
/** @return {number} */
@@ -386,7 +386,7 @@ Container.FOO = 1;
Instead, export individual constants and functions:
-/** @return {number} */
+/** @return {number} */
export function bar() {
return 1;
}
@@ -404,7 +404,7 @@ export const /** number */ FOO = 1;
reference to an object that has mutable fields or exporting accessor functions for
mutable data.
-// Bad: both foo and mutateFoo are exported and mutated.
+// Bad: both foo and mutateFoo are exported and mutated.
export let /** number */ foo = 0;
/**
@@ -425,7 +425,7 @@ export function setMutateFoo(newMutateFoo) {
}
-// Good: Rather than export the mutable variables foo and mutateFoo directly,
+// Good: Rather than export the mutable variables foo and mutateFoo directly,
// instead make them module scoped and export a getter for foo and a wrapper for
// mutateFooFunc.
let /** number */ foo = 0;
@@ -453,7 +453,7 @@ export function setMutateFoo(mutateFoo) {
export from statements must not be line wrapped and are therefore an
exception to the 80-column limit. This applies to both export from flavors.
-export {specificName} from './other.js';
+export {specificName} from './other.js';
export * from './another.js';
@@ -463,18 +463,18 @@ export * from './another.js';
specification allows this. Note that it is possible to create cycles with both
the import and export statements.
-// a.js
+// a.js
import './b.js';
-// b.js
+// b.js
import './a.js';
// `export from` can cause circular dependencies too!
export {x} from './c.js';
-// c.js
+// c.js
import './b.js';
export let x;
@@ -490,7 +490,7 @@ export let x;
To reference the Closure goog namespace, import Closure's goog.js.
-import * as goog from '../closure/goog/goog.js';
+import * as goog from '../closure/goog/goog.js';
const name = goog.require('a.name');
@@ -508,7 +508,7 @@ used in ES modules.
require any Closure namespace symbol (i.e., symbols created by goog.provide or
goog.module) and goog.require will return the value.
-import * as goog from '../closure/goog/goog.js';
+import * as goog from '../closure/goog/goog.js';
import * as anEsModule from './anEsModule.js';
const GoogPromise = goog.require('goog.Promise');
@@ -537,7 +537,7 @@ to associate a legacy
namespace with an ES module.
goog.declareModuleId should only be used to upgrade Closure files to ES
modules in place, where named exports are used.
-import * as goog from '../closure/goog.js';
+import * as goog from '../closure/goog.js';
goog.declareModuleId('my.esm');
@@ -615,7 +615,7 @@ limit.
Example:
-// Standard alias style.
+// Standard alias style.
const MyClass = goog.require('some.package.MyClass');
const MyType = goog.requireType('some.package.MyType');
// Namespace-based alias used to disambiguate.
@@ -643,14 +643,14 @@ goog.require('my.framework.initialization');
Discouraged:
-// If necessary to disambiguate, prefer PackageClass over SomeClass as it is
+// If necessary to disambiguate, prefer PackageClass over SomeClass as it is
// closer to the format of the module name.
const SomeClass = goog.require('some.package.Class');
Disallowed:
-// Extra terms must come from the namespace.
+// Extra terms must come from the namespace.
const MyClassForBizzing = goog.require('some.package.MyClass');
// Alias must include the entire final namespace component.
const MyClass = goog.require('some.package.MyClassForBizzing');
@@ -709,7 +709,7 @@ statement. The first statement of a non-empty block must begin on its own line.
Disallowed:
-if (someVeryLongCondition())
+if (someVeryLongCondition())
doSomething();
for (let i = 0; i < foo.length; i++) bar(foo[i]);
@@ -720,7 +720,7 @@ no wrapping (and that doesn’t have an else) may be kept on a single line w
braces when it improves readability. This is the only case in which a control
structure may omit braces and newlines.
-if (shortCondition()) foo();
+if (shortCondition()) foo();
4.1.2 Nonempty blocks: K&R style
@@ -740,7 +740,7 @@ there is no line break after the brace if it is followed by else<
Example:
-class InnerClass {
+class InnerClass {
constructor() {}
/** @param {number} foo */
@@ -766,12 +766,12 @@ multiple blocks: if/else or try/cat
Example:
-function doNothing() {}
+function doNothing() {}
Disallowed:
-if (condition) {
+if (condition) {
// …
} else if (otherCondition) {} else {
// …
@@ -795,7 +795,7 @@ block. (See the example in ??).
construct.” For example, the following are all valid (not an exhaustive
list):
-const a = [
+const a = [
0,
1,
2,
@@ -806,7 +806,7 @@ const b =
-const c = [0, 1, 2];
+const c = [0, 1, 2];
someMethod(foo, [
0, 1, 2,
@@ -823,7 +823,7 @@ larger arrays.
construct.” The same examples apply as ??. For
example, the following are all valid (not an exhaustive list):
-const a = {
+const a = {
a: 0,
b: 1,
};
@@ -832,7 +832,7 @@ const b =
{a: 0, b: 1};
-const c = {a: 0, b: 1};
+const c = {a: 0, b: 1};
someMethod(foo, {
a: 0, b: 1,
@@ -849,7 +849,7 @@ are still terminated with a semicolon). Use the extends keyword, bu
Example:
-class Foo {
+class Foo {
constructor() {
/** @type {number} */
this.x = 42;
@@ -863,7 +863,7 @@ are still terminated with a semicolon). Use the extends keyword, bu
Foo.Empty = class {};
-/** @extends {Foo<string>} */
+/** @extends {Foo<string>} */
foo.Bar = class extends Foo {
/** @override */
method() {
@@ -886,7 +886,7 @@ indentation depth.
Example:
-prefix.something.reallyLongFunctionName('whatever', (a1, a2) => {
+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)) {
@@ -922,7 +922,7 @@ indentation level, as if a block had been closed.
Example:
-switch (animal) {
+switch (animal) {
case Animal.BANDERSNATCH:
handleBandersnatch();
break;
@@ -994,14 +994,14 @@ level.
Preferred:
-currentEstimate =
+currentEstimate =
calc(currentEstimate + x * currentEstimate) /
2.0;
Discouraged:
-currentEstimate = calc(currentEstimate + x *
+currentEstimate = calc(currentEstimate + x *
currentEstimate) / 2.0;
@@ -1114,7 +1114,7 @@ where it was already used.
Here is an example without alignment, followed by one with alignment. Both are
allowed, but the latter is discouraged:
-{
+{
tiny: 42, // this is great
longer: 435, // this too
};
@@ -1138,7 +1138,7 @@ information, slows down reviewers and exacerbates merge conflicts.
Prefer to put all function arguments on the same line as the function name. If doing so would exceed the 80-column limit, the arguments must be line-wrapped in a readable way. To save space, you may wrap as close to 80 as possible, or put each argument on its own line to enhance readability. Indentation should be four spaces. Aligning to the parenthesis is allowed, but discouraged. Below are the most common patterns for argument wrapping:
-// Arguments start on a new line, indented four spaces. Preferred when the
+// Arguments start on a new line, indented four spaces. Preferred when the
// arguments don't fit on the same line with the function name (or the keyword
// "function") but fit entirely on the second line. Works with very long
// function names, survives renaming without reindenting, low on space.
@@ -1190,7 +1190,7 @@ be in /* … */ or //-style. For multi-line
lines must start with * aligned with the * on the previous line, to make
comments obvious with no extra context.
-/*
+/*
* This is
* okay.
*/
@@ -1212,13 +1212,13 @@ not sufficiently convey the meaning, and refactoring the method to be clearer is
infeasible .
Their preferred format is before the value with =
:
-someFunction(obviousParam, /* shouldRender= */ true, /* name= */ 'hello');
+someFunction(obviousParam, /* shouldRender= */ true, /* name= */ 'hello');
For consistency with surrounding code you may put them after the value without
=
:
-someFunction(obviousParam, true /* shouldRender */, 'hello' /* name */);
+someFunction(obviousParam, true /* shouldRender */, 'hello' /* name */);
5 Language features
@@ -1253,7 +1253,7 @@ else inline before the variable name if no other JSDoc is present.
Example:
-const /** !Array<number> */ data = [];
+const /** !Array<number> */ data = [];
/**
* Some description.
@@ -1265,7 +1265,7 @@ const data = [];
Mixing inline and JSDoc styles is not allowed: the compiler will only process
the first JsDoc and the inline annotations will be lost.
-/** Some description. */
+/** Some description. */
const /** !Array<number> */ data = [];
@@ -1287,7 +1287,7 @@ element and the closing bracket.
Example:
-const values = [
+const values = [
'first value',
'second value',
];
@@ -1300,7 +1300,7 @@ instead.
Disallowed:
-const a1 = new Array(x1, x2, x3);
+const a1 = new Array(x1, x2, x3);
const a2 = new Array(x1, x2);
const a3 = new Array(x1);
const a4 = new Array();
@@ -1313,7 +1313,7 @@ it will be a single-element array.
Instead, write
-const a1 = [x1, x2, x3];
+const a1 = [x1, x2, x3];
const a2 = [x1, x2];
const a3 = [x1];
const a4 = [];
@@ -1334,7 +1334,7 @@ destructuring (such as when unpacking multiple values from a single array or
iterable). A final rest
element may be included (with no space between the
... and the variable name). Elements should be omitted if they are unused.
-const [a, b, c, ...rest] = generateResults();
+const [a, b, c, ...rest] = generateResults();
let [, b,, d] = someArray;
@@ -1343,13 +1343,13 @@ name is required but ignored). Always specify [] as the default val
destructured array parameter is optional, and provide default values on the left
hand side:
-/** @param {!Array<number>=} param1 */
+/** @param {!Array<number>=} param1 */
function optionalDestructuring([a = 4, b = 2] = []) { … };
Disallowed:
-function badDestructuring([a, b] = [4, 2]) { … };
+function badDestructuring([a, b] = [4, 2]) { … };
Tip: For (un)packing multiple values into a function’s parameter or return,
@@ -1365,7 +1365,7 @@ more awkward constructs with Array.prototype. There is no space af
Example:
-[...foo] // preferred over Array.prototype.slice.call(foo)
+[...foo] // preferred over Array.prototype.slice.call(foo)
[...foo, ...bar] // preferred over foo.concat(bar)
@@ -1390,7 +1390,7 @@ types in a single object literal.
Disallowed:
-{
+{
width: 42, // struct-style unquoted key
'maxWidth': 43, // dict-style quoted key
}
@@ -1402,7 +1402,7 @@ the compiler cannot rename/obfuscate the string literal.
Disallowed:
-/** @type {{width: number, maxWidth: (number|undefined)}} */
+/** @type {{width: number, maxWidth: (number|undefined)}} */
const o = {width: 42};
if (o.hasOwnProperty('maxWidth')) {
...
@@ -1411,7 +1411,7 @@ if (o.hasOwnProperty('maxWidth')) {
This is best implemented as:
-/** @type {{width: number, maxWidth: (number|undefined)}} */
+/** @type {{width: number, maxWidth: (number|undefined)}} */
const o = {width: 42};
if (o.maxWidth != null) {
...
@@ -1435,7 +1435,7 @@ function literal.
Example:
-return {
+return {
stuff: 'candy',
method() {
return this.stuff; // Returns 'candy'
@@ -1449,7 +1449,7 @@ the object literal.
Example:
-class {
+class {
getObjectLiteral() {
this.stuff = 'fruit';
return {
@@ -1466,7 +1466,7 @@ the object literal.
Example:
-const foo = 1;
+const foo = 1;
const bar = 2;
const obj = {
foo,
@@ -1492,7 +1492,7 @@ by the compiler).
Example:
-/**
+/**
* @param {string} ordinary
* @param {{num: (number|undefined), str: (string|undefined)}=} param1
* num: The number of times to do something.
@@ -1503,7 +1503,7 @@ function destructured(ordinary, {num, str = 'some default'} = {})
Disallowed:
-/** @param {{x: {num: (number|undefined), str: (string|undefined)}}} param1 */
+/** @param {{x: {num: (number|undefined), str: (string|undefined)}}} param1 */
function nestedTooDeeply({x: {num, str}}) {};
/** @param {{num: (number|undefined), str: (string|undefined)}=} param1 */
function nonShorthandProperty({num: a, str: b} = {}) {};
@@ -1523,7 +1523,7 @@ long it is (see ??).
Additional properties may not be added to an enum after it is defined. Enums
must be constant, and all enum values must be deeply immutable.
-/**
+/**
* Supported temperature scales.
* @enum {string}
*/
@@ -1563,7 +1563,7 @@ class' prototype.
Example:
-class Foo {
+class Foo {
constructor() {
/** @private @const {!Bar} */
this.bar_ = computeBar();
@@ -1608,7 +1608,7 @@ that doesn’t define the method itself.
Disallowed:
-class Base { /** @nocollapse */ static foo() {} }
+class Base { /** @nocollapse */ static foo() {} }
class Sub extends Base {}
function callFoo(cls) { cls.foo(); } // discouraged: don't call static methods dynamically
Sub.foo(); // Disallowed: don't call static methods on subclasses that don't define it themselves
@@ -1636,7 +1636,7 @@ appropriate.
goog.defineClass allows for a class-like definition similar to ES6 class
syntax:
-let C = goog.defineClass(S, {
+let C = goog.defineClass(S, {
/**
* @param {string} value
*/
@@ -1659,7 +1659,7 @@ syntax:
Alternatively, while goog.defineClass should be preferred for all new code,
more traditional syntax is also allowed.
-/**
+/**
* @constructor @extends {S}
* @param {string} value
*/
@@ -1710,7 +1710,7 @@ the get and set shorthand method keywords or Obj
Disallowed:
-class Foo {
+class Foo {
get next() { return this.nextId++; }
}
@@ -1734,7 +1734,7 @@ be declared as uninitialized members in the class constructor.
Example:
-/**
+/**
* Something that can frobnicate.
* @record
*/
@@ -1769,13 +1769,13 @@ for more on exports.
Examples:
-/** @param {string} str */
+/** @param {string} str */
exports.processString = (str) => {
// Process the string.
};
-/** @param {string} str */
+/** @param {string} str */
const processString = (str) => {
// Process the string.
};
@@ -1821,7 +1821,7 @@ simple expressions).
Examples:
-/**
+