Skip to content

Commit a1aec3d

Browse files
committedMar 21, 2016
Merge pull request #686 from robinpokorny/lint-js-in-markdown
Correct code style of JavaScript in Markdown
2 parents 39f058e + 340dd4f commit a1aec3d

File tree

7 files changed

+56
-62
lines changed

7 files changed

+56
-62
lines changed
 

‎.eslintrc ‎.eslintrc.yml

File renamed without changes.

‎Readme.md

+14-20
Original file line numberDiff line numberDiff line change
@@ -50,63 +50,57 @@ Koa is an middleware framework, it can take 3 different kind function as middlew
5050
Here we write an logger middleware with different function.
5151

5252
### Common function
53-
```js
5453

54+
```js
5555
// Middleware normally take two parameters (ctx, next), ctx is the context for one request,
5656
// next is an function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion.
5757

5858
app.use((ctx, next) => {
59-
const start = new Date;
59+
const start = new Date();
6060
return next().then(() => {
61-
const ms = new Date - start;
61+
const ms = new Date() - start;
6262
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
6363
});
6464
});
65-
6665
```
6766

6867
### ___async___ functions (Babel required)
6968

7069
```js
71-
7270
app.use(async (ctx, next) => {
73-
const start = new Date;
71+
const start = new Date();
7472
await next();
75-
const ms = new Date - start;
73+
const ms = new Date() - start;
7674
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
7775
});
78-
7976
```
8077

8178
### GeneratorFunction
8279

8380
To use generator functions, you must use a wrapper such as [co](https://github.com/tj/co) that is no longer supplied with Koa.
8481

8582
```js
86-
87-
app.use(co.wrap(function *(ctx, next){
88-
const start = new Date;
83+
app.use(co.wrap(function *(ctx, next) {
84+
const start = new Date();
8985
yield next();
90-
const ms = new Date - start;
86+
const ms = new Date() - start;
9187
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
9288
}));
93-
9489
```
9590

9691
### Old signature middleware (v1.x)
9792

9893
If you want to use old signature or be compatible with old middleware, you must use [koa-convert](https://github.com/gyson/koa-convert) to convert legacy generator middleware to promise middleware.
9994

10095
```js
101-
const convert = require('koa-convert')
96+
const convert = require('koa-convert');
10297

103-
app.use(convert(function *(next){
104-
const start = new Date;
98+
app.use(convert(function *(next) {
99+
const start = new Date();
105100
yield next;
106-
const ms = new Date - start;
101+
const ms = new Date() - start;
107102
console.log(`${this.method} ${this.url} - ${ms}ms`);
108103
}));
109-
110104
```
111105

112106

@@ -122,8 +116,8 @@ $ npm install babel-preset-stage-3 --save
122116

123117
```js
124118
// set babel in entry file
125-
require("babel-core/register")({
126-
presets: ['es2015-node5', 'stage-3']
119+
require('babel-core/register')({
120+
presets: ['es2015-node5', 'stage-3']
127121
});
128122
```
129123

‎docs/api/context.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
snippet:
1313

1414
```js
15-
app.use(function *(){
15+
app.use(function * () {
1616
this; // is the Context
1717
this.request; // is a koa Request
1818
this.response; // is a koa Response

‎docs/api/index.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,25 @@ const app = new Koa();
8080

8181
// x-response-time
8282

83-
app.use(async function (ctx, next){
84-
const start = new Date;
83+
app.use(async function (ctx, next) {
84+
const start = new Date();
8585
await next();
86-
const ms = new Date - start;
86+
const ms = new Date() - start;
8787
ctx.set('X-Response-Time', `${ms}ms`);
8888
});
8989

9090
// logger
9191

92-
app.use(async function (ctx, next){
93-
const start = new Date;
92+
app.use(async function (ctx, next) {
93+
const start = new Date();
9494
await next();
95-
const ms = new Date - start;
95+
const ms = new Date() - start;
9696
console.log(`${ctx.method} ${ctx.url} - ${ms}`);
9797
});
9898

9999
// response
100100

101-
app.use((ctx) => {
101+
app.use(ctx => {
102102
ctx.body = 'Hello World';
103103
});
104104

@@ -198,17 +198,17 @@ app.context.db = db();
198198
To perform custom error-handling logic such as centralized logging you can add an "error" event listener:
199199

200200
```js
201-
app.on('error', function(err){
202-
log.error('server error', err);
203-
});
201+
app.on('error', err =>
202+
log.error('server error', err)
203+
);
204204
```
205205

206206
If an error is in the req/res cycle and it is _not_ possible to respond to the client, the `Context` instance is also passed:
207207

208208
```js
209-
app.on('error', function(err, ctx){
210-
log.error('server error', err, ctx);
211-
});
209+
app.on('error', (err, ctx) =>
210+
log.error('server error', err, ctx)
211+
);
212212
```
213213

214214
When an error occurs _and_ it is still possible to respond to the client, aka no data has been written to the socket, Koa will respond

‎docs/api/request.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ this.request.href
9696
Get request `Content-Type` void of parameters such as "charset".
9797

9898
```js
99-
const ct = this.request.type;
99+
const ct = this.request.type
100100
// => "image/png"
101101
```
102102

@@ -130,7 +130,7 @@ this.request.charset
130130
setter does _not_ support nested objects.
131131

132132
```js
133-
this.query = { next: '/login' };
133+
this.query = { next: '/login' }
134134
```
135135

136136
### request.fresh

‎docs/api/response.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ If `response.status` has not been set, Koa will automatically set the status to
146146
Here's an example of stream error handling without automatically destroying the stream:
147147

148148
```js
149-
const PassThrough = require('stream').PassThrough
149+
const PassThrough = require('stream').PassThrough;
150150

151151
app.use(function * (next) {
152-
this.body = someHTTPStream.on('error', this.onerror).pipe(PassThrough())
153-
})
152+
this.body = someHTTPStream.on('error', this.onerror).pipe(PassThrough());
153+
});
154154
```
155155

156156
#### Object
@@ -233,12 +233,12 @@ this.type = 'png';
233233
```js
234234
const minify = require('html-minifier');
235235

236-
app.use(function *minifyHTML(next){
236+
app.use(function * minifyHTML(next) {
237237
yield next;
238238

239239
if (!this.response.is('html')) return;
240240

241-
const body = this.body;
241+
let body = this.body;
242242
if (!body || body.pipe) return;
243243

244244
if (Buffer.isBuffer(body)) body = body.toString();

‎docs/guide.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
```js
1515
async function responseTime(ctx, next) {
16-
const start = new Date;
16+
const start = new Date();
1717
await next();
18-
const ms = new Date - start;
18+
const ms = new Date() - start;
1919
ctx.set('X-Response-Time', `${ms}ms`);
2020
}
2121

@@ -52,7 +52,7 @@ to this behaviour.
5252
For example this would be fine:
5353

5454
```js
55-
app.use(async function response(ctx, next){
55+
app.use(async function response(ctx, next) {
5656
if ('/' != this.url) return;
5757
ctx.body = 'Hello World';
5858
});
@@ -79,15 +79,15 @@ app.use(async function response(ctx, next){
7979
function logger(format) {
8080
format = format || ':method ":url"';
8181

82-
return async function (ctx, next){
82+
return async function (ctx, next) {
8383
const str = format
8484
.replace(':method', ctx.method)
8585
.replace(':url', ctx.url);
8686

8787
console.log(str);
8888

8989
await next();
90-
}
90+
};
9191
}
9292

9393
app.use(logger());
@@ -100,9 +100,9 @@ app.use(logger(':method :url'));
100100

101101
```js
102102
function logger(format) {
103-
return async function logger(ctx, next){
103+
return async function logger(ctx, next) {
104104

105-
}
105+
};
106106
}
107107
```
108108

@@ -115,7 +115,7 @@ const compose = require('koa-compose');
115115

116116
async function random(ctx, next) {
117117
if ('/random' == this.path) {
118-
ctx.body = Math.floor(Math.random()*10);
118+
ctx.body = Math.floor(Math.random() * 10);
119119
} else {
120120
await next();
121121
}
@@ -137,7 +137,7 @@ async function pi(ctx, next) {
137137
}
138138
}
139139

140-
const all = compose([random, backwards, pi])
140+
const all = compose([random, backwards, pi]);
141141

142142
app.use(all);
143143
```
@@ -152,20 +152,20 @@ app.use(all);
152152
downstream "three" middleware a chance to manipulate the response.
153153

154154
```js
155-
app.use(async function (ctx, next){
155+
app.use(async function (ctx, next) {
156156
console.log('>> one');
157157
await next();
158-
console.log('<< one');
158+
console.log('<< one');
159159
});
160160

161-
app.use(async function (ctx, next){
161+
app.use(async function (ctx, next) {
162162
console.log('>> two');
163163
ctx.body = 'two';
164164
await next();
165165
console.log('<< two');
166166
});
167167

168-
app.use(async function (ctx, next){
168+
app.use(async function (ctx, next) {
169169
console.log('>> three');
170170
await next();
171171
console.log('<< three');
@@ -176,19 +176,19 @@ app.use(async function (ctx, next){
176176
with "two", however the third (and any other downstream middleware) will be ignored:
177177

178178
```js
179-
app.use(async function (ctx, next){
179+
app.use(async function (ctx, next) {
180180
console.log('>> one');
181181
await next();
182-
console.log('<< one');
182+
console.log('<< one');
183183
});
184184

185-
app.use(async function (ctx, next){
185+
app.use(async function (ctx, next) {
186186
console.log('>> two');
187187
ctx.body = 'two';
188188
console.log('<< two');
189189
});
190190

191-
app.use(async function (ctx, next){
191+
app.use(async function (ctx, next) {
192192
console.log('>> three');
193193
await next();
194194
console.log('<< three');
@@ -208,7 +208,7 @@ app.use(async function (ctx, next){
208208
```js
209209
const fs = require('fs-promise');
210210

211-
app.use(async function (ctx, next){
211+
app.use(async function (ctx, next) {
212212
const paths = await fs.readdir('docs');
213213
const files = await Promise.all(paths.map(path => fs.readFile(`docs/${path}`, 'utf8')));
214214

@@ -241,15 +241,15 @@ $ DEBUG=koa* node --harmony examples/simple
241241

242242
```js
243243
const path = require('path');
244-
const static = require('koa-static');
244+
const serve = require('koa-static');
245245

246-
const publicFiles = static(path.join(__dirname, 'public'));
246+
const publicFiles = serve(path.join(__dirname, 'public'));
247247
publicFiles._name = 'static /public';
248248

249249
app.use(publicFiles);
250250
```
251251

252-
Now, instead of just seeing "static" when debugging, you will see:
252+
Now, instead of just seeing "serve" when debugging, you will see:
253253

254254
```
255255
koa:application use static /public +0ms

0 commit comments

Comments
 (0)
Please sign in to comment.