See the relevant style guides for our guidelines and for information on linting:
We defer to Airbnb on most style-related conventions and enforce them with eslint.
See our current .eslintrc for specific rules and patterns.
Never disable eslint rules unless you have a good reason.
You may see a lot of legacy files with /* eslint-disable some-rule, some-other-rule */
at the top, but legacy files are a special case. Any time you develop a new feature or
refactor an existing one, you should abide by the eslint rules.
Never Ever EVER disable eslint globally for a file
// bad
/* eslint-disable */
// better
/* eslint-disable some-rule, some-other-rule */
// best
// nothing :)
// bad
/* eslint-disable no-new */
import Foo from 'foo';
new Foo();
// better
import Foo from 'foo';
// eslint-disable-next-line no-new
new Foo();
There are few rules that we need to disable due to technical debt. Which are:
When they are needed always place ESlint directive comment blocks on the first line of a script, followed by any global declarations, then a blank newline prior to any imports or code.
// bad
/* global Foo */
/* eslint-disable no-new */
import Bar from './bar';
// good
/* eslint-disable no-new */
/* global Foo */
import Bar from './bar';
Never disable the no-undef
rule. Declare globals with /* global Foo */
instead.
When declaring multiple globals, always use one /* global [name] */
line per variable.
// bad
/* globals Flash, Cookies, jQuery */
// good
/* global Flash */
/* global Cookies */
/* global jQuery */
// bad
fn(p1, p2, p3, p4) {}
// good
fn(options) {}
// bad
require('foo');
// good
import Foo from 'foo';
// bad
module.exports = Foo;
// good
export default Foo;
~
In app/assets/javascripts:
// bad
import Foo from '~/foo'
// good
import Foo from '../foo';
In spec/javascripts:
// bad
import Foo from '../../app/assets/javascripts/foo'
// good
import Foo from '~/foo';
Avoid using IIFE. Although we have a lot of examples of files which wrap their contents in IIFEs (immediately-invoked function expressions), 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.
Avoid adding to the global namespace.
// bad
window.MyClass = class { /* ... */ };
// good
export default class MyClass { /* ... */ }
// bad
export default class MyClass { /* ... */ }
document.addEventListener("DOMContentLoaded", function(event) {
new MyClass();
}
// 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);
Avoid constructors with side-effects
Prefer .map
, .reduce
or .filter
over .forEach
A forEach will cause side effects, it will be mutating the array being iterated. Prefer using .map
,
.reduce
or .filter
const users = [ { name: 'Foo' }, { name: 'Bar' } ];
// bad
users.forEach((user, index) => {
user.id = index;
});
// good
const usersWithId = users.map((user, index) => {
return Object.assign({}, user, { id: index });
});
parseInt()
is preferable over Number()
or +
// bad
+'10' // 10
// good
Number('10') // 10
// better
parseInt('10', 10);
js-
// bad
<button class="add-user">
Add User
</button>
// good
<button class="js-add-user">
Add User
</button>
// bad
class {
init() {
new Component({})
}
}
// good
document.addEventListener('DOMContentLoaded', () => new Vue({
el: '#element',
components: {
componentName
},
render: createElement => createElement('component-name'),
}));
// bad
class Store {
constructor() {
if (!this.prototype.singleton) {
// do something
}
}
}
// good
class Store {
constructor() {
// do something
}
}
.vue
extension for Vue components. // good
import cardBoard from 'cardBoard'
components: {
cardBoard:
};
// bad
<component class="btn">
// good
<component css-class="btn">
// bad
<component myProp="prop" />
// good
<component my-prop="prop" />
// bad
<component v-if="bar"
param="baz" />
<button class="btn">Click me</button>
// good
<component
v-if="bar"
param="baz"
/>
<button class="btn">
Click me
</button>
// if props fit in one line then keep it on the same line
<component bar="bar" />
"
inside templates and single quotes '
for all other JS. // bad
template: `
<button :class='style'>Button</button>
`
// good
template: `
<button :class="style">Button</button>
`
// bad
props: ['foo']
// good
props: {
foo: {
type: String,
required: false,
default: 'bar'
}
}
// bad
props: {
foo: {
type: String,
}
}
// good
props: {
foo: {
type: String,
required: false,
default: 'bar'
}
}
// bad
props: {
foo: {
type: String,
required: false,
}
}
// good
props: {
foo: {
type: String,
required: false,
default: 'bar'
}
}
// good
props: {
foo: {
type: String,
required: true
}
}
data
method should always be a function // bad
data: {
foo: 'foo'
}
// good
data() {
return {
foo: 'foo'
};
}
@
is preferable over v-on
// bad
<component v-on:click="eventHandler"/>
// good
<component @click="eventHandler"/>
:
is preferable over v-bind
// bad
<component v-bind:class="btn"/>
// good
<component :class="btn"/>
// bad
<component></component>
// good
<component />
name
props
mixins
data
components
computedProps
methods
beforeCreate
created
beforeMount
mounted
beforeUpdate
updated
activated
deactivated
beforeDestroy
destroyed
has-tooltip
class name for Vue components // bad
<span
class="has-tooltip"
title="Some tooltip text">
Text
</span>
// good
<span
v-tooltip
title="Some tooltip text">
Text
</span>
Tooltips: When using a tooltip, include the tooltip directive, ./app/assets/javascripts/vue_shared/directives/tooltip.js
Don't change data-original-title
.
// bad
<span data-original-title="tooltip text">Foo</span>
// good
<span title="tooltip text">Foo</span>
$('span').tooltip('fixTitle');
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )