From 74220b7245a4d1c19472d2a9fc76a832295e0fd2 Mon Sep 17 00:00:00 2001 From: Gerard Ryan Date: Sat, 12 Nov 2016 18:53:35 +0000 Subject: [PATCH] Use eslint to enforce good const/let/var usage 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: https://github.com/nodejs/node/issues/8637#issuecomment-248656241 More info on this rule: http://eslint.org/docs/rules/block-scoped-var * Remove the recommendation to check for this manually. --- GUIDELINES.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/GUIDELINES.md b/GUIDELINES.md index 214b291..32d0bf9 100644 --- a/GUIDELINES.md +++ b/GUIDELINES.md @@ -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. @@ -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.