Files
mattermost/doc/developer/Style-Guide.md

171 lines
3.6 KiB
Markdown
Raw Normal View History

2015-07-28 12:10:14 -04:00
# Mattermost Style Guide
1. [Go](#go)
2015-07-28 12:10:14 -04:00
2. [Javascript](#javascript)
2015-07-28 12:14:40 -04:00
3. [React-JSX](#react-jsx)
2015-07-28 12:10:14 -04:00
## Go
All go code must follow the golang official [Style Guide](https://golang.org/doc/effective_go.html)
In addition all code must be run though the official go formatter tool [gofmt](https://golang.org/cmd/gofmt/)
2015-07-28 12:10:14 -04:00
## Javascript
2015-09-15 10:55:15 -04:00
Part of the build process is running ESLint. ESLint is the final authority on all style issues. PRs will not be accepted unless there are no errors running ESLint. The ESLint configuration file can be found in: [web/react/.eslintrc](/web/react/.eslintrc)
2015-07-28 12:10:14 -04:00
Instructions on how to use ESLint with your favourite editor can be found here: [http://eslint.org/docs/user-guide/integrations](http://eslint.org/docs/user-guide/integrations)
2015-09-15 10:55:15 -04:00
You can run eslint using the makefile by using `make check`
The following is a subset of what ESLint checks for. ESLint is always the authority.
2015-07-28 12:10:14 -04:00
### Whitespace
2015-08-27 11:24:52 -07:00
- Indentation is four spaces.
- Use a space before the leading brace.
2015-07-28 12:10:14 -04:00
- Use one space between the comma and the next argument in a bracketed list. No other space.
- Use whitespace to make code more readable.
- Do not use more than one newline to separate code blocks.
- Do not use a newline as the first line of a function
```javascript
// Correct
function myFunction(parm1, parm2) {
stuff...;
morestuff;
}
// Incorrect
function myFunction ( parm1, parm2 ){
stuff...;
morestuff;
}
```
### Semicolons
2015-08-27 14:26:24 -04:00
- You must use them always.
2015-07-28 12:10:14 -04:00
```javascript
// Correct
2015-09-15 10:55:15 -04:00
let x = 1;
2015-07-28 12:10:14 -04:00
// Incorrect
2015-09-15 10:55:15 -04:00
let x = 1
2015-07-28 12:10:14 -04:00
```
### Variables
- Declarations must always use var, let or const.
- Prefer let or const over var.
- camelCase for all variable names.
2015-07-28 12:10:14 -04:00
```javascript
// Correct
let myVariable = 4;
2015-07-28 12:10:14 -04:00
// OK
var myVariable = 4;
2015-07-28 12:10:14 -04:00
// Incorrect
myVariable = 4;
var my_variable = 4;
2015-07-28 12:10:14 -04:00
```
### Blocks
- Braces must be used on all blocks.
- Braces must start on the same line as the statement starting the block.
2015-07-28 12:10:14 -04:00
- Else and else if must be on the same line as the if block closing brace.
```javascript
// Correct
2015-08-27 11:26:38 -07:00
if (something) {
2015-07-28 12:10:14 -04:00
stuff...;
} else if (otherthing) {
stuff...;
}
// Incorrect
2015-08-27 11:26:38 -07:00
if (something)
2015-07-28 12:10:14 -04:00
{
stuff...;
}
else
{
stuff...;
}
// Incorrect
2015-08-27 11:26:38 -07:00
if (something) stuff...;
if (something)
2015-07-28 12:10:14 -04:00
stuff...;
```
### Strings
2015-09-15 10:55:15 -04:00
- Use of template strings is preferred instead of concatenation.
2015-07-28 12:10:14 -04:00
```javascript
// Correct
function getStr(stuff) {
return "This is the ${stuff} string";
}
// Incorrect
function wrongGetStr(stuff) {
return "This is the " + stuff + " string";
}
```
2015-07-28 12:14:40 -04:00
## React-JSX
2015-09-15 10:55:15 -04:00
Part of the build process is running ESLint. ESLint is the final authority on all style issues. PRs will not be accepted unless there are no errors running ESLint. The ESLint configuration file can be found in: [web/react/.eslintrc](/web/react/.eslintrc)
2015-07-28 12:14:40 -04:00
Instructions on how to use ESLint with your favourite editor can be found here: [http://eslint.org/docs/user-guide/integrations](http://eslint.org/docs/user-guide/integrations)
2015-07-28 12:10:14 -04:00
2015-09-15 10:55:15 -04:00
You can run eslint using the makefile by using `make check`
The following is a subset of what ESLint checks for. ESLint is always the authority.
2015-07-28 12:10:14 -04:00
### General
- Include only one React component per file.
- Use class \<name\> extends React.Component over React.createClass unless you need mixins
2015-07-28 12:10:14 -04:00
- Filenames should be the component name.
### Alignment
- Follow alignment styles shown below:
```xml
// Correct
<Tag
propertyOne="1"
propertyTwo="2"
>
<Child />
</Tag>
// Correct
<Tag propertyOne="1" />
```
### Naming
2015-07-28 12:10:14 -04:00
- Property names use camelCase.
- React component names use CapitalCamelCase.
- Do not use an underscore for internal methods in a react component.
2015-07-28 12:10:14 -04:00
```xml
// Correct
<ReactComponent propertyOne="value" />
```