Skip to content

Commit c68993c

Browse files
committed
fix: add rule documentation
Resolves #61
1 parent 0095432 commit c68993c

17 files changed

+184
-82
lines changed

Diff for: .eslintrc.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"env": {
3+
"node": true
4+
},
5+
"parserOptions": {
6+
"ecmaVersion": 6
7+
},
8+
"plugins": [
9+
"eslint-plugin"
10+
],
11+
"rules": {
12+
"eslint-plugin/require-meta-docs-url": [
13+
"error",
14+
{
15+
"pattern": "https://github.com/xjamundx/eslint-plugin-promise#{{name}}"
16+
}
17+
]
18+
}
19+
}

Diff for: .npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
test
44
.npmignore
55
.travis.yml
6+
.eslintrc.json

Diff for: .travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ node_js:
33
- 4
44
- 6
55
- 8
6+
script:
7+
- npm run test
8+
- npm run lint

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
"repository": "[email protected]:xjamundx/eslint-plugin-promise.git",
1414
"scripts": {
1515
"pretest": "standard",
16-
"test": "mocha test"
16+
"test": "mocha test",
17+
"lint": "eslint index.js rules test"
1718
},
1819
"devDependencies": {
1920
"eslint": "^4.17.0",
21+
"eslint-plugin-eslint-plugin": "^1.4.0",
2022
"mocha": "^5.0.0",
2123
"standard": "^7.1.2"
2224
},

Diff for: rules/always-return.js

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ function peek (arr) {
5555
}
5656

5757
module.exports = {
58+
meta: {
59+
docs: {
60+
url: 'https://github.com/xjamundx/eslint-plugin-promise#always-return'
61+
}
62+
},
5863
create: function (context) {
5964
// funcInfoStack is a stack representing the stack of currently executing
6065
// functions

Diff for: rules/avoid-new.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55

66
'use strict'
77

8-
module.exports = function (context) {
9-
return {
10-
NewExpression: function (node) {
11-
if (node.callee.name === 'Promise') {
12-
context.report(node, 'Avoid creating new promises.')
8+
module.exports = {
9+
meta: {
10+
docs: {
11+
url: 'https://github.com/xjamundx/eslint-plugin-promise#avoid-new'
12+
}
13+
},
14+
create: function (context) {
15+
return {
16+
NewExpression: function (node) {
17+
if (node.callee.name === 'Promise') {
18+
context.report(node, 'Avoid creating new promises.')
19+
}
1320
}
1421
}
1522
}

Diff for: rules/catch-or-return.js

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
var isPromise = require('./lib/is-promise')
1010

1111
module.exports = {
12+
meta: {
13+
docs: {
14+
url: 'https://github.com/xjamundx/eslint-plugin-promise#catch-or-return'
15+
}
16+
},
1217
create: function (context) {
1318
var options = context.options[0] || {}
1419
var allowThen = options.allowThen

Diff for: rules/lib/.eslintrc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"eslint-plugin/require-meta-docs-url": "off"
4+
}
5+
}

Diff for: rules/no-callback-in-promise.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,31 @@ var hasPromiseCallback = require('./lib/has-promise-callback')
99
var isInsidePromise = require('./lib/is-inside-promise')
1010
var isCallback = require('./lib/is-callback')
1111

12-
module.exports = function (context) {
13-
return {
14-
CallExpression: function (node) {
15-
var options = context.options[0] || {}
16-
var exceptions = options.exceptions || []
17-
if (!isCallback(node, exceptions)) {
18-
// in general we send you packing if you're not a callback
19-
// but we also need to watch out for whatever.then(cb)
20-
if (hasPromiseCallback(node)) {
21-
var name = node.arguments && node.arguments[0] && node.arguments[0].name
22-
if (name === 'callback' || name === 'cb' || name === 'next' || name === 'done') {
23-
context.report(node.arguments[0], 'Avoid calling back inside of a promise.')
12+
module.exports = {
13+
meta: {
14+
docs: {
15+
url: 'https://github.com/xjamundx/eslint-plugin-promise#no-callback-in-promise'
16+
}
17+
},
18+
create: function (context) {
19+
return {
20+
CallExpression: function (node) {
21+
var options = context.options[0] || {}
22+
var exceptions = options.exceptions || []
23+
if (!isCallback(node, exceptions)) {
24+
// in general we send you packing if you're not a callback
25+
// but we also need to watch out for whatever.then(cb)
26+
if (hasPromiseCallback(node)) {
27+
var name = node.arguments && node.arguments[0] && node.arguments[0].name
28+
if (name === 'callback' || name === 'cb' || name === 'next' || name === 'done') {
29+
context.report(node.arguments[0], 'Avoid calling back inside of a promise.')
30+
}
2431
}
32+
return
33+
}
34+
if (context.getAncestors().some(isInsidePromise)) {
35+
context.report(node, 'Avoid calling back inside of a promise.')
2536
}
26-
return
27-
}
28-
if (context.getAncestors().some(isInsidePromise)) {
29-
context.report(node, 'Avoid calling back inside of a promise.')
3037
}
3138
}
3239
}

Diff for: rules/no-native.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ function isDeclared (scope, ref) {
1818
}
1919

2020
module.exports = {
21+
meta: {
22+
docs: {
23+
url: 'https://github.com/xjamundx/eslint-plugin-promise#no-native'
24+
}
25+
},
2126
create: function (context) {
2227
var MESSAGE = '"{{name}}" is not defined.'
2328

Diff for: rules/no-nesting.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@
88
var hasPromiseCallback = require('./lib/has-promise-callback')
99
var isInsidePromise = require('./lib/is-inside-promise')
1010

11-
module.exports = function (context) {
12-
return {
13-
CallExpression: function (node) {
14-
if (!hasPromiseCallback(node)) return
15-
if (context.getAncestors().some(isInsidePromise)) {
16-
context.report(node, 'Avoid nesting promises.')
11+
module.exports = {
12+
meta: {
13+
docs: {
14+
url: 'https://github.com/xjamundx/eslint-plugin-promise#no-nesting'
15+
}
16+
},
17+
create: function (context) {
18+
return {
19+
CallExpression: function (node) {
20+
if (!hasPromiseCallback(node)) return
21+
if (context.getAncestors().some(isInsidePromise)) {
22+
context.report(node, 'Avoid nesting promises.')
23+
}
1724
}
1825
}
1926
}

Diff for: rules/no-promise-in-callback.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,27 @@
88
var isPromise = require('./lib/is-promise')
99
var isInsideCallback = require('./lib/is-inside-callback')
1010

11-
module.exports = function (context) {
12-
return {
13-
CallExpression: function (node) {
14-
if (!isPromise(node)) return
11+
module.exports = {
12+
meta: {
13+
docs: {
14+
url: 'https://github.com/xjamundx/eslint-plugin-promise#no-promise-in-callback'
15+
}
16+
},
17+
create: function (context) {
18+
return {
19+
CallExpression: function (node) {
20+
if (!isPromise(node)) return
1521

16-
// if i'm returning the promise, it's probably not really a callback
17-
// function, and I should be okay....
18-
if (node.parent.type === 'ReturnStatement') return
22+
// if i'm returning the promise, it's probably not really a callback
23+
// function, and I should be okay....
24+
if (node.parent.type === 'ReturnStatement') return
1925

20-
// what about if the parent is an ArrowFunctionExpression
21-
// would that imply an implicit return?
26+
// what about if the parent is an ArrowFunctionExpression
27+
// would that imply an implicit return?
2228

23-
if (context.getAncestors().some(isInsideCallback)) {
24-
context.report(node.callee, 'Avoid using promises inside of callbacks.')
29+
if (context.getAncestors().some(isInsideCallback)) {
30+
context.report(node.callee, 'Avoid using promises inside of callbacks.')
31+
}
2532
}
2633
}
2734
}

Diff for: rules/no-return-in-finally.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
var isPromise = require('./lib/is-promise')
44

55
module.exports = {
6+
meta: {
7+
docs: {
8+
url: 'https://github.com/xjamundx/eslint-plugin-promise#no-return-in-finally'
9+
}
10+
},
611
create: function (context) {
712
return {
813
CallExpression: function (node) {

Diff for: rules/no-return-wrap.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ function isInPromise (context) {
1818
}
1919

2020
module.exports = {
21+
meta: {
22+
docs: {
23+
url: 'https://github.com/xjamundx/eslint-plugin-promise#no-return-wrap'
24+
}
25+
},
2126
create: function (context) {
2227
var options = context.options[0] || {}
2328
var allowReject = options.allowReject

Diff for: rules/param-names.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
'use strict'
22

33
module.exports = {
4+
meta: {
5+
docs: {
6+
url: 'https://github.com/xjamundx/eslint-plugin-promise#param-names'
7+
}
8+
},
49
create: function (context) {
510
return {
611
NewExpression: function (node) {

Diff for: rules/prefer-await-to-callbacks.js

+38-31
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,48 @@
77

88
var errorMessage = 'Avoid callbacks. Prefer Async/Await.'
99

10-
module.exports = function (context) {
11-
function checkLastParamsForCallback (node) {
12-
var len = node.params.length - 1
13-
var lastParam = node.params[len]
14-
if (lastParam && (lastParam.name === 'callback' || lastParam.name === 'cb')) {
15-
context.report(lastParam, errorMessage)
10+
module.exports = {
11+
meta: {
12+
docs: {
13+
url: 'https://github.com/xjamundx/eslint-plugin-promise#prefer-await-to-callbacks'
1614
}
17-
}
18-
function isInsideYieldOrAwait () {
19-
return context.getAncestors().some(function (parent) {
20-
return parent.type === 'AwaitExpression' || parent.type === 'YieldExpression'
21-
})
22-
}
23-
return {
24-
CallExpression: function (node) {
25-
// callbacks aren't allowed
26-
if (node.callee.name === 'cb' || node.callee.name === 'callback') {
27-
context.report(node, errorMessage)
28-
return
15+
},
16+
create: function (context) {
17+
function checkLastParamsForCallback (node) {
18+
var len = node.params.length - 1
19+
var lastParam = node.params[len]
20+
if (lastParam && (lastParam.name === 'callback' || lastParam.name === 'cb')) {
21+
context.report(lastParam, errorMessage)
2922
}
23+
}
24+
function isInsideYieldOrAwait () {
25+
return context.getAncestors().some(function (parent) {
26+
return parent.type === 'AwaitExpression' || parent.type === 'YieldExpression'
27+
})
28+
}
29+
return {
30+
CallExpression: function (node) {
31+
// callbacks aren't allowed
32+
if (node.callee.name === 'cb' || node.callee.name === 'callback') {
33+
context.report(node, errorMessage)
34+
return
35+
}
3036

31-
// thennables aren't allowed either
32-
var args = node.arguments
33-
var num = args.length - 1
34-
var arg = num > -1 && node.arguments && node.arguments[num]
35-
if (arg && arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
36-
if (arg.params && arg.params[0] && arg.params[0].name === 'err') {
37-
if (!isInsideYieldOrAwait()) {
38-
context.report(arg, errorMessage)
37+
// thennables aren't allowed either
38+
var args = node.arguments
39+
var num = args.length - 1
40+
var arg = num > -1 && node.arguments && node.arguments[num]
41+
if (arg && arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
42+
if (arg.params && arg.params[0] && arg.params[0].name === 'err') {
43+
if (!isInsideYieldOrAwait()) {
44+
context.report(arg, errorMessage)
45+
}
3946
}
4047
}
41-
}
42-
},
43-
FunctionDeclaration: checkLastParamsForCallback,
44-
FunctionExpression: checkLastParamsForCallback,
45-
ArrowFunctionExpression: checkLastParamsForCallback
48+
},
49+
FunctionDeclaration: checkLastParamsForCallback,
50+
FunctionExpression: checkLastParamsForCallback,
51+
ArrowFunctionExpression: checkLastParamsForCallback
52+
}
4653
}
4754
}

Diff for: rules/prefer-await-to-then.js

+19-12
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@
55

66
'use strict'
77

8-
module.exports = function (context) {
9-
return {
10-
MemberExpression: function (node) {
11-
// you can then() if you are inside of a yield or await
12-
if (context.getAncestors().some(function (parent) {
13-
return parent.type === 'AwaitExpression' || parent.type === 'YieldExpression'
14-
})) {
15-
return
16-
}
8+
module.exports = {
9+
meta: {
10+
docs: {
11+
url: 'https://github.com/xjamundx/eslint-plugin-promise#prefer-await-to-then'
12+
}
13+
},
14+
create: function (context) {
15+
return {
16+
MemberExpression: function (node) {
17+
// you can then() if you are inside of a yield or await
18+
if (context.getAncestors().some(function (parent) {
19+
return parent.type === 'AwaitExpression' || parent.type === 'YieldExpression'
20+
})) {
21+
return
22+
}
1723

18-
// if you're a then expression then you're probably a promise
19-
if (node.property && node.property.name === 'then') {
20-
context.report(node.property, 'Prefer await to then().')
24+
// if you're a then expression then you're probably a promise
25+
if (node.property && node.property.name === 'then') {
26+
context.report(node.property, 'Prefer await to then().')
27+
}
2128
}
2229
}
2330
}

0 commit comments

Comments
 (0)