Our guidelines are predominately based on @shopify/eslint-plugin with exceptions.
See also Vue's style guide.
When saving eslint --fix should run and automatically fix most issues.
- Arrays
- Comments
- If conditions
- Indents
- Newlines
- Objects
- Order
- Passing parameters
- Semi-colons
- Variables
- Whitespace
const foo = [
'bar'
]
const bar = ['foo',
'bar'
]const foo = ['bar']
const bar = [
'foo',
'bar',
]- Single item arrays should be on a single line with no spaces inside the opening and closing tags
- Multiple item arrays should have each item on a newline with a trailing comma
- Add a newline after comments, except those starting with
eslintorwebpack - Comments should begin with a capital letter
/**
* Folder: Title
* -----------------------------------------------------------------------------
* Description.
*
*/- Use the long form comment for intro comments inside the
<script>tag in Vue files and at the start of JS files
/**
* [Comment].
* @param {String} variable - Variable description.
* @returns {String}
*/- Use the JSDoc comment before every function explaining what it does, its expected variables, and what it returns
- Wrap the variable in square brackets to denote that it's optional, e.g.
[variable] - See JSDoc documentation for details
/**
* Comment.
*/- Use the short form comment for general comments
if (condition) {
runFunctionA()
} else {
runFunctionB()
}if (condition) {
runFunctionA()
return
}
runFunctionB()- Use an early return instead of
if ... elsewhere possible to make the amount of code the browser has to run shorter - Where both parts of the condition lead to the same outcome you should continue using
if ... else, e.g.
let foo = ''
if (condition) {
foo += 'Foo'
} else {
foo += 'Bar'
}
foo += 'Baz'
return foolet foo = 'Baz'
if (condition) {
foo = 'Bar'
}
const bar = condition ? 'When the ternary would exceed 80 characters then split onto newlines' : 'Foo'const foo = condition ? 'Bar' : 'Baz'
const bar = condition
? 'When the ternary would exceed 80 characters then split onto newlines'
: 'Foo'- When assigning a variable it's preferrable to use a ternary operator, e.g.
- Split the ternary operator onto newlines when it exceeds 80 characters
const foo = 'Bar'
const bar = [
true,
false,
true,
]
bar.forEach((item) => {
if (item) return
runFunction(item)
})
if (condition) { foo = 'Baz' }const foo = 'Bar'
const bar = [
true,
false,
true,
]
bar.forEach((item) => {
if (item) {
return
}
runFunction(item)
})
if (condition) {
foo = 'Baz'
}- Newlines are free, use them to break up blocks of code
- Multi-line objects should have newlines before and after them
- Do not use single line if conditions
const foo = {
'bar': true
}
const bar = {'foo': true, 'bar-baz': false}const foo = { bar: true }
const bar = {
foo: true,
'bar-baz': false,
}- Each property in an object should be on a newline if there are multiple properties
- For single property objects there should be a space after the opening brace and before the closing brace
- Single word keys should not use an apostrophes/single quotations (
') - All properties in a multi-line object must always have a trailing comma
- As per Vue's style guide your Vue
<script>export should follow this order:namecomponentspropsemitssetupdatacomputedwatch- Lifecycle events in the order they're called
methods
- Vue SFC files should be in the following order:
<template><script><style>
- When saving your file
eslint --fixis automatically run, if there are items which need to break onto multiple lines (such as arrays) theneslintwill do this for you, however the indenting may be incorrect - Apply common sense when saving a file which
eslintthen fixes, if the indenting doesn't look right it probably isn't,eslintdoesn't lint indenting correctly at all times
<template>
<div class="example">
<!-- Code -->
</div>
</template>
<script>
export default {
name: 'Example',
}
</script>
<style lang="scss">
@import './example';
</style><template>
<div class="Example">
<!-- Code -->
</div>
</template>
<script>
export default {
name: 'Example',
}
</script>
<style lang="scss">
@import './example';
</style>- Only indent the contents of the
<template>tag <script>and<style>tags should not be indented- This gives more character length to write your JS in
array.forEach(item => {
// Code
})array.forEach((item) => {
// Code
})- Wrap the passed parameters in brackets
()
const foo = 'Bar';const foo = 'Bar'- Do not use semi-colons (
;) unless not doing so would cause an ASI error - E.g. the below needs a semi-colon on the first line otherwise when minified it would break:
const array = [];
['foo', 'bar'].forEach((value) => {
array.push(value))
}var foo = true
let bar = [
'One',
'Two',
'Three',
]
const baz = 'String'
baz += ', extra string'const foo = true
const bar = [
'One',
'Two',
'Three',
]
let baz = 'String'
baz += ', extra string'- Do not use
var, useletorconstas the situation dictates - Use
constfor variables which are never reassigned - Use
letfor variables which are reassigned
const foo='Bar'
array.forEach((item)=>{
// Code
})
for(const item of array){
// Code
}
if(condition==='foo'){
// Code
}else{
// Code
}const foo = 'Bar'
array.forEach((item) => {
// Code
})
for (const item of array) {
// Code
}
if (condition === 'foo') {
// Code
} else {
// Code
}- Whitespace is free, use it
- It makes the code more readable