Skip to content

Commit

Permalink
Use eslint to enforce good const/let/var usage
Browse files Browse the repository at this point in the history
Motivation:
Currently the guidelines put this into the "linting won't catch this"
section, because we've just been using plain semistandard.

Modification:
* Add prefer-const rule, which will force usage of const if an
  identifier is never reassigned. More info:
  http://eslint.org/docs/rules/prefer-const

* Add no-use-before-define rule, which will prevent ReferenceErrors
  being thrown due to "temporal dead zone" issues with const/let. More
  info: http://eslint.org/docs/rules/no-use-before-define

* Add block-scoped-var rule, which will force var to act like let. The
  current guidelines recommend against using var, and instead using
  let for mutable variables. Unfortunately, let currently doesn't
  perform as well as const or var:

  nodejs/node#8637 (comment)

  More info on this rule:
  http://eslint.org/docs/rules/block-scoped-var

* Remove the recommendation to check for this manually.
  • Loading branch information
grdryn committed Nov 12, 2016
1 parent 376d213 commit 74220b7
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ plugins for `eslint` in our builds.
First, add an `.eslintrc.json` file to your project and put this in there.

{
"extends": "semistandard"
"extends": "semistandard",

"rules": {
"prefer-const": "error",
"block-scoped-var": "error",
"no-use-before-define": ["error", "nofunc"],
}
}

Then install `eslint` and the `semistandard` and dependent plugins.
Expand Down Expand Up @@ -154,12 +160,9 @@ At this point, we eschew any Node.js version below 4.x. This means, that usage o
features is possible. And there are some good features to take advatage of. Here are
some of our recommendations.

* Use `let` and `const` instead of `var`. There is rarely ever a valid case for using
`var` in modern Javascript code. If the variable is immutable (which we believe most
should be), use `const`. If the variable may be written, use `let`.
* Use promises for asynchronous code execution, where appropriate. (Consider using
bucharest-gold/fidelity).
* Use the new ES6 `class` keyword to create classess, and avoid direct manipulation
* Use the new ES6 `class` keyword to create classes, and avoid direct manipulation
of prototypes.
* Use `Symbol`s to keep private data private. There are a few ways to achieve data privacy
in Javascript. We've found that using a `Symbol` as a key is the most performant.
Expand Down

0 comments on commit 74220b7

Please sign in to comment.