Skip to content

Commit

Permalink
增加promise检查
Browse files Browse the repository at this point in the history
  • Loading branch information
leizongmin committed Oct 11, 2016
1 parent a715aee commit 9bdfae1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
## 安装

```bash
$ npm install eslint-config-lei --save-dev
$ npm install eslint-config-lei eslint-plugin-promise --save-dev
```

说明:由于本配置使用了`promise`插件,因此需要同时安装`eslint-plugin-promise`模块。


## 使用方法

### 配置文件
Expand Down Expand Up @@ -57,7 +60,7 @@ $ eslint dir/**.js --fix

## 配置文件

+ `lei` - 默认的配置,基于Node.js
+ `lei` - 默认的配置,基于Node.js/ES6
+ `lei/mocha` - mocha测试环境
+ `lei/wechat` - 微信小程序环境

Expand Down
29 changes: 29 additions & 0 deletions examples/test_promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

function sleep(ms) {
return new Promise((r, x) => {
if (isNaN(ms)) return x(new Error(`invalid param: is not a number`));
if (!(ms > 0)) return x(new Error(`invalid param: must greater than 0`));
setTimeout(r, ms);
});
}

console.log('hello');
sleep(500).then(() => {
console.log('world');
});

function sleep2(ms) {
return new Promise((resolve, reject) => {
if (isNaN(ms)) return reject(new Error(`invalid param: is not a number`));
if (!(ms > 0)) return reject(new Error(`invalid param: must greater than 0`));
setTimeout(resolve, ms);
});
}

console.log('hello2');
sleep2(500).then(() => {
console.log('world2');
}).catch(err => {
console.log(err);
});
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
sourceType: 'module',
allowImportExportEverywhere: false,
},
plugins: [ 'eslint-plugin-promise' ],
rules: {

// ---------------------------- 强制的风格 -----------------------------------
Expand Down Expand Up @@ -230,5 +231,9 @@ module.exports = {
// 建议使用 const
'prefer-const': 'warn',

// ------------------------ Pomise 强制的风格 --------------------------------
'promise/catch-or-return': 'error',
'promise/param-names': 'error',

},
};
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"version": "0.0.9",
"description": "my own eslint config",
"main": "index.js",
"files": [
"index.js",
"mocha.js",
"wechat.js"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint *.js --fix",
Expand All @@ -21,5 +26,9 @@
"bugs": {
"url": "https://github.com/leizongmin/eslint-config-lei/issues"
},
"homepage": "https://github.com/leizongmin/eslint-config-lei#readme"
"homepage": "https://github.com/leizongmin/eslint-config-lei#readme",
"peerDependencies": {
"eslint": "^3.0",
"eslint-plugin-promise": "^3.0.0"
}
}

0 comments on commit 9bdfae1

Please sign in to comment.