Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
matrix-react-sdk
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
Matrix.org
matrix-react-sdk
Commits
04728ae0
Commit
04728ae0
authored
Jul 7, 2016
by
David Baker
Browse files
Options
Downloads
Patches
Plain Diff
PR fixes + more general notes
parent
56c73b68
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
code_style.md
+35
-3
35 additions, 3 deletions
code_style.md
with
35 additions
and
3 deletions
code_style.md
+
35
−
3
View file @
04728ae0
...
@@ -6,6 +6,19 @@ consistent with other popular JavaScript styles and consistent with the rest of
...
@@ -6,6 +6,19 @@ consistent with other popular JavaScript styles and consistent with the rest of
the Matrix codebase. For reference, the Matrix Python style guide can be found
the Matrix codebase. For reference, the Matrix Python style guide can be found
at https://github.com/matrix-org/synapse/blob/master/docs/code_style.rst
at https://github.com/matrix-org/synapse/blob/master/docs/code_style.rst
This document reflects how we would like Matrix JavaScript code to look, with
acknowledgement that a significant amount of code is written to older
standards.
Write applications in modern ECMAScript and use a transpiler where necessary to
target older platforms. When writing library code, consider carefully whether
to write in ES5 to allow all JavaScript application to use the code directly or
writing in modern ECMAScript and using a transpile step to generate the file
that applications can then include. There are significant benefits in being
able to use modern ECMAScript, although the tooling for doing so can be awkward
for library code, especially with regard to translating source maps and line
number throgh from the original code to the final application.
General Style
General Style
-------------
-------------
-
4 spaces to indent, for consistency with Matrix Python.
-
4 spaces to indent, for consistency with Matrix Python.
...
@@ -23,10 +36,12 @@ General Style
...
@@ -23,10 +36,12 @@ General Style
-
lowerCamelCase for functions and variables.
-
lowerCamelCase for functions and variables.
-
Single line ternary operators are fine.
-
Single line ternary operators are fine.
-
UPPER_CAMEL_CASE for constants
-
UPPER_CAMEL_CASE for constants
-
Single quotes for strings, for consistency with most JavaScript styles::
-
Single quotes for strings by default, for consistency with most JavaScript styles:
```
"bad" // Bad
"bad" // Bad
'good' // Good
'good' // Good
-
Use parentheses instead of '
\\
' for line continuation where ever possible
```
-
Use parentheses or
`\``
instead of '
\\
' for line continuation where ever possible
-
Open braces on the same line (consistent with Node):
-
Open braces on the same line (consistent with Node):
```
```
if (x) {
if (x) {
...
@@ -82,7 +97,7 @@ General Style
...
@@ -82,7 +97,7 @@ General Style
if (x)
if (x)
return true; // Not fine
return true; // Not fine
```
```
-
Terminate all multi-line lists with commas:
-
Terminate all multi-line lists with commas
(if using a transpiler)
:
```
```
var mascots = [
var mascots = [
"Patrick",
"Patrick",
...
@@ -103,6 +118,14 @@ General Style
...
@@ -103,6 +118,14 @@ General Style
-
Use
`null`
,
`undefined`
etc consistently with node:
-
Use
`null`
,
`undefined`
etc consistently with node:
Boolean variables and functions should always be either true or false. Don't set it to 0 unless it's supposed to be a number.
Boolean variables and functions should always be either true or false. Don't set it to 0 unless it's supposed to be a number.
When something is intentionally missing or removed, set it to null.
When something is intentionally missing or removed, set it to null.
If returning a boolean, type coerce:
```
function hasThings() {
return !!length; // bad
return new Boolean(length); // REALLY bad
return Boolean(length); // good
}
```
Don't set things to undefined. Reserve that value to mean "not yet set to anything."
Don't set things to undefined. Reserve that value to mean "not yet set to anything."
Boolean objects are verboten.
Boolean objects are verboten.
-
Use JSDoc
-
Use JSDoc
...
@@ -123,3 +146,12 @@ ECMAScript
...
@@ -123,3 +146,12 @@ ECMAScript
React
React
-----
-----
-
Use ES6 classes, although bear in mind a lot of code uses createClass.
-
Use ES6 classes, although bear in mind a lot of code uses createClass.
-
Pull out functions in props to the class, generally as specific event handlers:
```
<Foo onClick={function(ev) {doStuff();}}> // Bad
<Foo onClick={(ev) => {doStuff();}}> // Equally bad
<Foo onClick={this.doStuff}> // Better
<Foo onClick={this.onClick}> // Best
```
-
Think about whether your component really needs state: are you duplicating
information in component state that could be derived from the model?
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment