Skip to content

Commit

Permalink
eslint: update eslint config & fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Dec 3, 2016
1 parent c37bbbc commit b158454
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 414 deletions.
48 changes: 22 additions & 26 deletions .eslintrc.yml
@@ -1,13 +1,6 @@
# use babel
# parser: babel-eslint

# root
root: true

# globals
# globals:
# Promise: true

# code env
env:
node: true
Expand All @@ -17,27 +10,30 @@ env:
# rules
rules:
# basic
semi: [2, always] # ;
indent: [2, 2]
quotes: [2, single] # '' | ""
eqeqeq: 2 # ===
camelcase: 2
radix: 2 # parseInt('100', 10)
yoda: 2 # 'blue' === sky
no-var: 2 # ES6 let & const
no-const-assign: 2
strict: [2, global] # 'use strict'

# 大括号
curly: [2, multi-line] # 需要大括号
brace-style: 2 # 左大括号同一行
semi: [error, never]
quotes: [error, single, { allowTemplateLiterals: true }]
eqeqeq: error
camelcase: error
radix: error
yoda: error
no-var: error
no-const-assign: error
strict: [error, global]
comma-dangle: [error, only-multiline]

# indent
indent: [error, 2, { SwitchCase: 1 }]

# brace
curly: [error, multi-line]
brace-style: error

# disable default
no-unused-vars: 0 # 已定义, 未使用
no-console: 0 # console

# space
# 有 jsbeautify 保持风格
no-unused-vars: off
no-console: off
no-unreachable: off
require-yield: off
no-constant-condition: off

parserOptions:
ecmaVersion: 6
Expand Down
58 changes: 29 additions & 29 deletions demo/inspect/app.js
@@ -1,10 +1,10 @@
'use strict';
const app = module.exports = new(require('koa'))();
const Router = require('../');
const co = require('co');
const router = new Router();
const request = require('supertest');
app.use(router);
'use strict'
const app = module.exports = new(require('koa'))()
const Router = require('../')
const co = require('co')
const router = new Router()
const request = require('supertest')
app.use(router)

/**
* 在某一path上use middleware
Expand All @@ -14,59 +14,59 @@ router.use('/public', function(ctx, next) {
originalPath: ctx.originalPath,
basePath: ctx.basePath,
path: ctx.path
};
});
}
})

/**
* simple route
*/
router.get('/hello', function(ctx, next) {
ctx.body = 'world';
});
ctx.body = 'world'
})

/**
* nested router test
*/
const fn = co.wrap(function*(ctx, next) {
console.log('basePath: %s, path: %s', ctx.basePath, ctx.path);
yield next();
});
console.log('basePath: %s, path: %s', ctx.basePath, ctx.path)
yield next()
})

const routerA = new Router();
const routerB = new Router();
const routerC = new Router();
const routerA = new Router()
const routerB = new Router()
const routerC = new Router()

router.use('/a', fn, routerA);
routerA.use('/b', fn, routerB);
routerB.use('/c', fn, routerC);
router.use('/a', fn, routerA)
routerA.use('/b', fn, routerB)
routerB.use('/c', fn, routerC)
routerC.get('/', function(ctx, next) {
console.log('calling ');
console.log('calling ')
ctx.body = {
path: ctx.path,
basePath: ctx.basePath,
originalPath: ctx.originalPath
};
});
}
})

/**
* param
*/
router.get('/user/:id/detail', function(ctx, next) {
ctx.body = {
id: ctx.params.id
};
});
}
})

/**
* nested param
*/
const routerUser = new Router({
mergeParams: true
});
router.use('/user/:id', routerUser);
})
router.use('/user/:id', routerUser)
routerUser.get('/:field', function(ctx, next) {
ctx.body = {
id: ctx.params.id,
field: ctx.params.field
};
});
}
})
28 changes: 14 additions & 14 deletions demo/inspect/inspect.js
@@ -1,25 +1,25 @@
'use strict';
const app = require('./app');
const server = app.listen();
const request = require('supertest');
'use strict'
const app = require('./app')
const server = app.listen()
const request = require('supertest')

/**
* middleware
*/
request(server)
.get('/public/js/app.js')
.end(function(err, res) {
console.log(res.body);
});
console.log(res.body)
})

/**
* route
*/
request(server)
.get('/hello')
.end(function(err, res) {
console.log(res.text);
});
console.log(res.text)
})

/**
* nested router
Expand All @@ -28,23 +28,23 @@ request(server)
.get('/a/b/c')
.end(function(err, res) {
// console.log(err);
console.log(res.body);
});
console.log(res.body)
})

/**
* simple params
*/
request(server)
.get('/user/magicdawn/detail')
.end(function(err, res) {
console.log(res.body);
});
console.log(res.body)
})

/**
* nested params
*/
request(server)
.get('/user/magicdawn/age')
.end(function(err, res) {
console.log(res.body);
});
console.log(res.body)
})
16 changes: 8 additions & 8 deletions demo/koa-static/app.js
@@ -1,15 +1,15 @@
'use strict';
'use strict'

const app = new(require('koa'));
const router = new(require('../../'));
app.use(router);
const app = new(require('koa'))
const router = new(require('../../'))
app.use(router)

const _static = require('koa-static');
router.use('/public', _static(__dirname));
const _static = require('koa-static')
router.use('/public', _static(__dirname))

app.listen(3000, () => {
console.log('koa server listening at http://localhost:3000');
});
console.log('koa server listening at http://localhost:3000')
})

/**
* then try 'http://localhost:3000/public/app.js'
Expand Down
52 changes: 26 additions & 26 deletions lib/layer.js
@@ -1,71 +1,71 @@
'use strict';
'use strict'

/**
* module dependencies
*/

const pathToRegexp = require('path-to-regexp');
const debug = require('debug')('impress:layer');
const pathToRegexp = require('path-to-regexp')
const debug = require('debug')('impress:layer')

module.exports = class Layer {
constructor(path, options, fn) {
// setup regex
this.regexp = pathToRegexp(path, this.keys = [], options);
this.regexp = pathToRegexp(path, this.keys = [], options)

// fn*(next)
this.fn = fn;
this.name = fn.name || '<anonymous>';
this.fn = fn
this.name = fn.name || '<anonymous>'

// will present after match
this.path = '';
this.params = {};
this.path = ''
this.params = {}

if (path === '/' && options.end === false) {
this.regexp.fastSlash = true;
this.regexp.fastSlash = true
}
}

match(path) {
if (!path) {
return false;
return false
}

if (this.regexp.fastSlash) {
return true;
return true
}

const m = this.regexp.exec(path);
const m = this.regexp.exec(path)
if (!m) {
return false;
return false
}

// m = [full,group1...groupN]
this.path = m[0];
this.params = {};
let n = 0; // store the anaymous params
this.path = m[0]
this.params = {}
let n = 0 // store the anaymous params

for (let i = 0, len = this.keys.length; i < len; i++) {
const key = this.keys[i].name || n++;
const val = m[i + 1];
this.params[key] = decodeParam(val);
const key = this.keys[i].name || n++
const val = m[i + 1]
this.params[key] = decodeParam(val)
}

return true;
return true
}
};
}

function decodeParam(val) {
/* istanbul ignore next */
if (typeof val !== 'string') {
return val;
return val
}

/* istanbul ignore next */
try {
return decodeURIComponent(val);
return decodeURIComponent(val)
} catch (e) {
const err = new TypeError('Failed to decode param "' + val + '"');
err.status = 400;
throw err;
const err = new TypeError('Failed to decode param "' + val + '"')
err.status = 400
throw err
}
}

0 comments on commit b158454

Please sign in to comment.