2019-11-29 13:06:24 -05:00
---
2020-10-30 14:08:56 -04:00
stage: none
group: unassigned
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2019-11-29 13:06:24 -05:00
disqus_identifier: 'https://docs.gitlab.com/ee/development/fe_guide/style_guide_js.html'
---
# JavaScript style guide
We use [Airbnb's JavaScript Style Guide ](https://github.com/airbnb/javascript ) and it's accompanying
linter to manage most of our JavaScript style guidelines.
In addition to the style guidelines set by Airbnb, we also have a few specific rules
listed below.
2020-12-07 22:09:37 -05:00
NOTE:
2019-11-29 13:06:24 -05:00
You can run eslint locally by running `yarn eslint`
## Avoid forEach
Avoid forEach when mutating data. Use `map` , `reduce` or `filter` instead of `forEach`
2020-12-14 13:09:48 -05:00
when mutating data. This minimizes mutations in functions,
2019-11-29 13:06:24 -05:00
which aligns with [Airbnb's style guide ](https://github.com/airbnb/javascript#testing--for-real ).
```javascript
// bad
users.forEach((user, index) => {
user.id = index;
});
// good
const usersWithId = users.map((user, index) => {
return Object.assign({}, user, { id: index });
});
```
## Limit number of parameters
If your function or method has more than 3 parameters, use an object as a parameter
instead.
```javascript
// bad
function a(p1, p2, p3) {
// ...
};
// good
function a(p) {
// ...
};
```
## Avoid classes to handle DOM events
If the only purpose of the class is to bind a DOM event and handle the callback, prefer
using a function.
```javascript
// bad
class myClass {
constructor(config) {
this.config = config;
}
init() {
document.addEventListener('click', () => {});
}
}
// good
const myFunction = () => {
document.addEventListener('click', () => {
// handle callback here
});
}
```
## Pass element container to constructor
When your class manipulates the DOM, receive the element container as a parameter.
This is more maintainable and performant.
```javascript
// bad
class a {
constructor() {
document.querySelector('.b');
}
}
// good
class a {
constructor(options) {
options.container.querySelector('.b');
}
}
```
## Use ParseInt
Use `ParseInt` when converting a numeric string into a number.
```javascript
// bad
Number('10')
// good
parseInt('10', 10);
```
## CSS Selectors - Use `js-` prefix
If a CSS class is only being used in JavaScript as a reference to the element, prefix
the class name with `js-` .
```html
// bad
< button class = "add-user" > < / button >
// good
< button class = "js-add-user" > < / button >
```
## ES Module Syntax
Use ES module syntax to import modules:
```javascript
// bad
const SomeClass = require('some_class');
// good
import SomeClass from 'some_class';
// bad
module.exports = SomeClass;
// good
export default SomeClass;
```
2020-11-03 19:09:12 -05:00
We still use `require` in `scripts/` and `config/` files.
2019-11-29 13:06:24 -05:00
## Absolute vs relative paths for modules
Use relative paths if the module you are importing is less than two levels up.
```javascript
// bad
import GitLabStyleGuide from '~/guides/GitLabStyleGuide';
// good
import GitLabStyleGuide from '../GitLabStyleGuide';
```
If the module you are importing is two or more levels up, use an absolute path instead:
```javascript
// bad
import GitLabStyleGuide from '../../../guides/GitLabStyleGuide';
// good
import GitLabStyleGuide from '~/GitLabStyleGuide';
```
Additionally, **do not add to global namespace** .
## Do not use `DOMContentLoaded` in non-page modules
Imported modules should act the same each time they are loaded. `DOMContentLoaded`
events are only allowed on modules loaded in the `/pages/*` directory because those
are loaded dynamically with webpack.
## Avoid XSS
Do not use `innerHTML` , `append()` or `html()` to set content. It opens up too many
vulnerabilities.
2020-02-28 10:09:13 -05:00
## Avoid single-line conditional statements
Indentation is important when scanning code as it gives a quick indication of the existence of branches, loops, and return points.
This can help to quickly understand the control flow.
```javascript
// bad
if (isThingNull) return '';
2020-05-04 08:09:46 -04:00
if (isThingNull)
return '';
2020-02-28 10:09:13 -05:00
// good
if (isThingNull) {
return '';
}
```
2019-11-29 13:06:24 -05:00
## ESLint
2020-04-22 14:09:52 -04:00
ESLint behavior can be found in our [tooling guide ](../tooling.md ).
2019-11-29 13:06:24 -05:00
## IIFEs
Avoid using IIFEs (Immediately-Invoked Function Expressions). Although
we have a lot of examples of files which wrap their contents in IIFEs,
this is no longer necessary after the transition from Sprockets to webpack.
Do not use them anymore and feel free to remove them when refactoring legacy code.
2019-12-02 16:06:51 -05:00
## Global namespace
2019-11-29 13:06:24 -05:00
Avoid adding to the global namespace.
```javascript
// bad
window.MyClass = class { /* ... */ };
// good
export default class MyClass { /* ... */ }
```
2019-12-02 16:06:51 -05:00
## Side effects
### Top-level side effects
2019-11-29 13:06:24 -05:00
Top-level side effects are forbidden in any script which contains `export` :
```javascript
// bad
export default class MyClass { /* ... */ }
document.addEventListener("DOMContentLoaded", function(event) {
new MyClass();
}
```
2019-12-02 16:06:51 -05:00
### Avoid side effects in constructors
2019-11-29 13:06:24 -05:00
2019-12-02 16:06:51 -05:00
Avoid making asynchronous calls, API requests or DOM manipulations in the `constructor` .
2020-12-14 13:09:48 -05:00
Move them into separate functions instead. This makes tests easier to write and
2019-12-02 16:06:51 -05:00
avoids violating the [Single Responsibility Principle ](https://en.wikipedia.org/wiki/Single_responsibility_principle ).
2019-11-29 13:06:24 -05:00
```javascript
2019-12-02 16:06:51 -05:00
// bad
class myClass {
constructor(config) {
this.config = config;
axios.get(this.config.endpoint)
2019-11-29 13:06:24 -05:00
}
}
2019-12-02 16:06:51 -05:00
// good
class myClass {
constructor(config) {
this.config = config;
2019-11-29 13:06:24 -05:00
}
2019-12-02 16:06:51 -05:00
makeRequest() {
axios.get(this.config.endpoint)
2019-11-29 13:06:24 -05:00
}
}
2019-12-02 16:06:51 -05:00
const instance = new myClass();
instance.makeRequest();
2019-11-29 13:06:24 -05:00
```
## Pure Functions and Data Mutation
Strive to write many small pure functions and minimize where mutations occur
```javascript
// bad
const values = {foo: 1};
function impureFunction(items) {
const bar = 1;
items.foo = items.a * bar + 2;
return items.a;
}
const c = impureFunction(values);
// good
var values = {foo: 1};
function pureFunction (foo) {
var bar = 1;
foo = foo * bar + 2;
return foo;
}
var c = pureFunction(values.foo);
```