13.1. EdX JavaScript Style Guide

This section describes the requirements and conventions used to contribute JavaScript programming to the edX platform. The majority of edX JavaScript code is written as ES5, but is starting to move to ES2015.

13.1.1. Code Style

EdX JavaScript style generally follows the Airbnb JavaScript Style Guide, with a few custom rules that can be seen in the eslint-config-edx repository. EdX recommends using ESLint for JavaScript linting, and provides configurations for both ES2015 and ES5.

13.1.2. Testing

All JavaScript code should be tested using Jasmine. In addition, there are a number of Jasmine-based helper classes provided by the edX UI Toolkit.

JavaScript tests are run with Karma, along with karma-coverage to provide code coverage reporting.

For more information about testing JavaScript, see the description of testing for the edx-platform repository.

13.1.3. Documentation

All JavaScript code should be documented using JSDoc. For detailed information about using JSDoc, see the JSDOC site.

As a tool, JSDoc takes JavaScript code with special /** */ comments and produces HTML documentation for it. An example follows.

/** @namespace */
var util = {
    /**
     * Repeat <tt>str</tt> several times.
     *
     * @param {string} str The string to repeat.
     * @param {number} [times=1] How many times to repeat the string.
     * @returns {string}
     */
    repeat: function(str, times) {
        if (times === undefined || times < 1) {
            times = 1;
        }
        return new Array(times+1).join(str);
    }
};