Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 8296fb8

Browse files
author
Daniel Brook-Roberge
authored
Merge pull request #127 from mobify/global-and-environment-es6
Add a section on global declarations in ES6
2 parents 6f63f86 + 19da11e commit 8296fb8

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

es6/README.md

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,39 @@ Included is a default ESLint configuration, `mobify-es6.yml` written for ESLint
55
- `eslint-plugin-import`
66
- `eslint-import-resolver-webpack`
77

8-
The lint configuration is the definitive source for rules; this document explains the most notable ones. `eslint <source-files> --fix` will fix most formatting issues, such as spacing.
8+
The lint configuration is the definitive source for rules; this document explains the most notable ones. `eslint <source-files> --fix` will fix most formatting issues, such as spacing.
99

1010
If React/JSX is in use, use the React default configuration `mobify-es6-react.yml`, which also requires the `eslint-plugin-react` module.
1111

12+
## Global Names and Environments
13+
14+
By default, the lint configuration only assumes the standard set of
15+
global names in the browser environment are defined. If writing a
16+
script that runs in a different environment, such as a worker or Node,
17+
add an `eslint-env` directive at the top of the file, e.g.:
18+
```javascript
19+
/* eslint-env node */
20+
```
21+
22+
If you have defined or are using a global that is not associated with
23+
any environment, you can define it for the linter in one of two
24+
ways. If the global is only used in a single file or single place,
25+
define it in that file by inserting a `global` directive at the top of
26+
the file:
27+
```javascript
28+
/* global myGlobal anotherGlobal Mobify */
29+
```
30+
31+
If, instead, it is used throughout your project, add it to the
32+
`.eslintrc.yml` file for that project, with
33+
```yaml
34+
globals:
35+
myGlobal: true
36+
anotherGlobal: false
37+
```
38+
The boolean value indicates whether the global is to be treated as
39+
read-only (`false`) or read-write (`true`).
40+
1241
## Overriding Lint Rules
1342

1443
Some of the lint rules disallow uncommon but valid behaviour that is easily confused with/typoed from much more common behaviour. If you need to use the disallowed behaviour on purpose, use an explicit lint override in the source.
@@ -35,21 +64,21 @@ if (x > y)
3564
return x - y
3665
else
3766
return y - x
38-
39-
for (let item of cartItems)
67+
68+
for (let item of cartItems)
4069
console.log(item)
41-
70+
4271
//good!
4372
if (x > y) {
4473
return x - y
4574
} else {
4675
return y - x
4776
}
48-
77+
4978
for (let item of cartItems) {
5079
console.log(item)
5180
}
52-
81+
5382
```
5483

5584
## Avoid 'Yoda' conditions
@@ -91,12 +120,12 @@ return {
91120
It is clearer to use dot notation instead of bracket notation when it is available (i.e. when the property name is a constant, identifier-legal string).
92121

93122
```javascript
94-
// Bad
123+
// Bad
95124
console.log(myObj['prop'])
96125
97126
// Good
98127
console.log(myObj.prop)
99-
```
128+
```
100129

101130
## Use ES6 `import` rather than `require`
102131

@@ -138,7 +167,7 @@ export const func3 = () => { console.log(3) }
138167

139168
## Don't use semicolons
140169

141-
With ES6 (and the current lint rules!) we have finally arrived at an environment where automatic semicolon insertion won't cause problems. So don't use them.
170+
With ES6 (and the current lint rules!) we have finally arrived at an environment where automatic semicolon insertion won't cause problems. So don't use them.
142171

143172
## Prefer template strings to string concatenation
144173

@@ -159,9 +188,9 @@ Ternary expressions can be helpful, but can also lead to unclear code. If multip
159188

160189
```javascript
161190
// Bad
162-
let name = (item
163-
? item.name
164-
: (defaultItem
191+
let name = (item
192+
? item.name
193+
: (defaultItem
165194
? defaultItem.name
166195
: defaultName
167196
)
@@ -222,7 +251,7 @@ return fetch('http://mysite.com/api')
222251
.then(function(response) {
223252
return response.json()
224253
})
225-
254+
226255
// Good
227256
return fetch('http://mysite.com/api')
228257
.then((response) => {
@@ -232,7 +261,7 @@ return fetch('http://mysite.com/api')
232261
// Also good
233262
return fetch('http://mysite.com/api')
234263
.then((response) => response.json())
235-
264+
236265
// Still fine
237266
element.addEventListener('click', function(event) {
238267
console.log(this.id)
@@ -270,7 +299,7 @@ This is more readable and prevents unexpected results when commenting out lines.
270299
let x = 5,
271300
y = 10,
272301
z = 100
273-
302+
274303
// Good
275304
let x = 5
276305
let y = 10
@@ -354,11 +383,11 @@ class MyComponent extends React.Component {
354383
componentDidMount() {
355384
this.props.sendAnalytics()
356385
}
357-
386+
358387
render() {
359388
return <div>{this.props.content}</div>
360389
}
361-
}
390+
}
362391
```
363392
364393
## Use plain functions for stateless components
@@ -370,7 +399,7 @@ React now allows stateless components to be implemented as pure functions rather
370399
class MyComponent extends React.Component {
371400
render() {
372401
return (
373-
<ul>
402+
<ul>
374403
{this.props.items.map((item) => <li>{item.content}</li>}
375404
</ul>
376405
)
@@ -390,7 +419,7 @@ const MyComponent = ({items}) => {
390419
391420
# JSX
392421
393-
## Self-close component elements if possible
422+
## Self-close component elements if possible
394423
395424
```javascript
396425
// Bad
@@ -400,4 +429,3 @@ const MyComponent = ({items}) => {
400429
// Good
401430
<MyComponent prop1="test" prop2={10+2} />
402431
```
403-

0 commit comments

Comments
 (0)