Adopting AirBnb JS Style Guide and ES6 Primitives

Release DateApr 3, 2017
Change Type
Platforms Affected

Keeping with our tradition of Continuous Improvement, we are proud to announce a major revamp of NodeJS SDKs!

  • Our code now conforms with Airbnb’s JavaScript Style Guide.
  • We are using ES6 classes, along with other ES6 primitives.
  • You can run npm run lint to lint your code immediately.
  • We generate CI/CD files for Travis, CircleCi, Appveyor and Jenkins to run NodeJS tests.

Please generate your SDK again if you want the improved ES6 based code.

Detailed Changes

Here is a summary of code related changes you’ll see:

  • Classes are now written using ES6 class syntax.
  • All code and comments now follow a max line length of 100 characters.
  • var declarations have been replaced by block scoped const and let.
  • All files now work in strict mode.
  • Unnecessary imports in files have been removed.
  • Double quotes have been replaced by single quotes throughout the sdk.
  • JSDoc annotations in code have improved.
  • String concatenation has been replaced by template literals.
  • Object properties are strictly being accessed using dot notation whenever possible.
  • Tabs have been replaced by spaces.
  • Indent of 4 spaces is used.
  • Line endings are now Unix style (LF) line-endings.
  • Arrow functions are now being used.
  • Ensured === is used instead of == for equality checks.
  • All requires have been moved to the top of the file.
  • Object literal shorthand syntax is now being used.
  • Removed all trailing spaces.
  • Infix operators are now spaced according to AirBnb guide.
  • Removed all code that manipulated object prototype (due to ES6 class inheritance).

Breaking Changes

We no longer generate getters and setters for properties in model and exception/error classes. Properties should directly be accessed using the property name.

// Access properties simply by dot notation
let user = new User();
user.name = 'Foo';
console.log(user.name);

// Getters and setters like these are no longer supported
// This code will no longer run
let user = new User();
user.setName('Foo');
console.log(user.getName());

These changes were made keeping in line with the current practices for JavaScript APIs.

Running Linter Tests

To check for Style Guide compliance, you can run the ESLint tool.

First ensure all the project dependencies are installed:

npm install

Then run the ESLint tool using:

npm run lint

Our current ESLint configuration looks like this:

{
    "eslintConfig": {
        "extends": "airbnb",
        "env": {
          "commonjs": true,
          "node": true,
          "mocha": true
        },
        "rules": {
          "indent": ["error", 4],
          "no-underscore-dangle": 0,
          "strict": 0,
          "prefer-rest-params": 0
        }
    }
}

Supported NodeJS Runtimes

The generated SDK should be compatible with NodeJS version 4 and above.

We specifically avoided some ES6 features in order to keep compatibility with version 4: see node.green.

Related Links


Have questions? Submit a request.