Skip to content

Commit

Permalink
chore: update code formatting tools (eslint-community#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu committed Feb 10, 2018
1 parent 7b30978 commit 834c69d
Show file tree
Hide file tree
Showing 32 changed files with 805 additions and 307 deletions.
11 changes: 6 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
"parserOptions": {
"ecmaVersion": 6
},
"plugins": [
"eslint-plugin"
],
"plugins": ["eslint-plugin", "prettier"],
"extends": [
"plugin:eslint-plugin/recommended"
"standard",
"plugin:eslint-plugin/recommended",
"plugin:prettier/recommended"
],
"rules": {
"eslint-plugin/require-meta-docs-url": [
"error",
{
"pattern": "https://github.com/xjamundx/eslint-plugin-promise#{{name}}"
}
]
],
"prettier/prettier": "error"
}
}
30 changes: 26 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,40 @@
"author": "jden <jason@denizac.org>",
"repository": "git@github.com:xjamundx/eslint-plugin-promise.git",
"scripts": {
"pretest": "standard",
"precommit": "lint-staged",
"test": "mocha test",
"lint": "eslint index.js rules test"
"lint": "eslint index.js rules test --ignore-pattern '**/*.json'"
},
"devDependencies": {
"doctoc": "^1.3.0",
"eslint": "^4.17.0",
"eslint-config-prettier": "^2.9.0",
"eslint-config-standard": "^11.0.0-beta.0",
"eslint-plugin-eslint-plugin": "^1.4.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^6.0.0",
"eslint-plugin-prettier": "^2.6.0",
"eslint-plugin-promise": "./",
"eslint-plugin-standard": "^3.0.1",
"husky": "^0.14.3",
"lint-staged": "^6.1.0",
"mocha": "^5.0.0",
"standard": "^7.1.2"
"prettier": "^1.10.2"
},
"engines": {
"node": ">=4"
},
"license": "ISC"
"license": "ISC",
"lint-staged": {
"concurrent": false,
"linters": {
"README.md": ["doctoc --maxlevel 3 --notitle", "git add"],
"*.js": ["prettier --write", "eslint --fix", "git add"],
"*.json": ["prettier --write", "git add"]
}
},
"prettier": {
"semi": false,
"singleQuote": true
}
}
44 changes: 21 additions & 23 deletions rules/always-return.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

function isFunctionWithBlockStatement (node) {
function isFunctionWithBlockStatement(node) {
if (node.type === 'FunctionExpression') {
return true
}
Expand All @@ -10,31 +10,29 @@ function isFunctionWithBlockStatement (node) {
return false
}

function isThenCallExpression (node) {
function isThenCallExpression(node) {
return (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.property.name === 'then'
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.property.name === 'then'
)
}

function isFirstArgument (node) {
function isFirstArgument(node) {
return (
node.parent &&
node.parent.arguments &&
node.parent.arguments[0] === node
node.parent && node.parent.arguments && node.parent.arguments[0] === node
)
}

function isInlineThenFunctionExpression (node) {
function isInlineThenFunctionExpression(node) {
return (
isFunctionWithBlockStatement(node) &&
isThenCallExpression(node.parent) &&
isFirstArgument(node)
isFunctionWithBlockStatement(node) &&
isThenCallExpression(node.parent) &&
isFirstArgument(node)
)
}

function hasParentReturnStatement (node) {
function hasParentReturnStatement(node) {
if (node && node.parent && node.parent.type) {
// if the parent is a then, and we haven't returned anything, fail
if (isThenCallExpression(node.parent)) {
Expand All @@ -50,7 +48,7 @@ function hasParentReturnStatement (node) {
return false
}

function peek (arr) {
function peek(arr) {
return arr[arr.length - 1]
}

Expand All @@ -60,7 +58,7 @@ module.exports = {
url: 'https://github.com/xjamundx/eslint-plugin-promise#always-return'
}
},
create: function (context) {
create: function(context) {
// funcInfoStack is a stack representing the stack of currently executing
// functions
// funcInfoStack[i].branchIDStack is a stack representing the currently
Expand Down Expand Up @@ -92,7 +90,7 @@ module.exports = {
// loc: <loc> } } } ]
var funcInfoStack = []

function markCurrentBranchAsGood () {
function markCurrentBranchAsGood() {
var funcInfo = peek(funcInfoStack)
var currentBranchID = peek(funcInfo.branchIDStack)
if (funcInfo.branchInfoMap[currentBranchID]) {
Expand All @@ -105,32 +103,32 @@ module.exports = {
ReturnStatement: markCurrentBranchAsGood,
ThrowStatement: markCurrentBranchAsGood,

onCodePathSegmentStart: function (segment, node) {
onCodePathSegmentStart: function(segment, node) {
var funcInfo = peek(funcInfoStack)
funcInfo.branchIDStack.push(segment.id)
funcInfo.branchInfoMap[segment.id] = {good: false, node: node}
funcInfo.branchInfoMap[segment.id] = { good: false, node: node }
},

onCodePathSegmentEnd: function (segment, node) {
onCodePathSegmentEnd: function(segment, node) {
var funcInfo = peek(funcInfoStack)
funcInfo.branchIDStack.pop()
},

onCodePathStart: function (path, node) {
onCodePathStart: function(path, node) {
funcInfoStack.push({
branchIDStack: [],
branchInfoMap: {}
})
},

onCodePathEnd: function (path, node) {
onCodePathEnd: function(path, node) {
var funcInfo = funcInfoStack.pop()

if (!isInlineThenFunctionExpression(node)) {
return
}

path.finalSegments.forEach((segment) => {
path.finalSegments.forEach(segment => {
var id = segment.id
var branch = funcInfo.branchInfoMap[id]
if (!branch.good) {
Expand Down
4 changes: 2 additions & 2 deletions rules/avoid-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module.exports = {
url: 'https://github.com/xjamundx/eslint-plugin-promise#avoid-new'
}
},
create: function (context) {
create: function(context) {
return {
NewExpression: function (node) {
NewExpression: function(node) {
if (node.callee.name === 'Promise') {
context.report({ node, message: 'Avoid creating new promises.' })
}
Expand Down
18 changes: 12 additions & 6 deletions rules/catch-or-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
url: 'https://github.com/xjamundx/eslint-plugin-promise#catch-or-return'
}
},
create: function (context) {
create: function(context) {
var options = context.options[0] || {}
var allowThen = options.allowThen
var terminationMethod = options.terminationMethod || 'catch'
Expand All @@ -24,13 +24,14 @@ module.exports = {
}

return {
ExpressionStatement: function (node) {
ExpressionStatement: function(node) {
if (!isPromise(node.expression)) {
return
}

// somePromise.then(a, b)
if (allowThen &&
if (
allowThen &&
node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.property.name === 'then' &&
Expand All @@ -40,23 +41,28 @@ module.exports = {
}

// somePromise.catch()
if (node.expression.type === 'CallExpression' &&
if (
node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
terminationMethod.indexOf(node.expression.callee.property.name) !== -1
) {
return
}

// somePromise['catch']()
if (node.expression.type === 'CallExpression' &&
if (
node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.property.type === 'Literal' &&
node.expression.callee.property.value === 'catch'
) {
return
}

context.report({ node, message: 'Expected ' + terminationMethod + '() or return' })
context.report({
node,
message: 'Expected ' + terminationMethod + '() or return'
})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rules/lib/has-promise-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

'use strict'

function hasPromiseCallback (node) {
function hasPromiseCallback(node) {
if (node.type !== 'CallExpression') return
if (node.callee.type !== 'MemberExpression') return
var propertyName = node.callee.property.name
Expand Down
2 changes: 1 addition & 1 deletion rules/lib/is-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var isNamedCallback = require('./is-named-callback')

function isCallingBack (node, exceptions) {
function isCallingBack(node, exceptions) {
var isCallExpression = node.type === 'CallExpression'
var callee = node.callee || {}
var nameIsCallback = isNamedCallback(callee.name, exceptions)
Expand Down
5 changes: 3 additions & 2 deletions rules/lib/is-inside-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

var isInsidePromise = require('./is-inside-promise')

function isInsideCallback (node) {
var isCallExpression = node.type === 'FunctionExpression' ||
function isInsideCallback(node) {
var isCallExpression =
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression' ||
node.type === 'FunctionDeclaration' // this may be controversial

Expand Down
9 changes: 5 additions & 4 deletions rules/lib/is-inside-promise.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict'

function isInsidePromise (node) {
var isFunctionExpression = node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression'
function isInsidePromise(node) {
var isFunctionExpression =
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression'
var parent = node.parent || {}
var callee = parent.callee || {}
var name = callee.property && callee.property.name || ''
var name = (callee.property && callee.property.name) || ''
var parentIsPromise = name === 'then' || name === 'catch'
var isInCB = isFunctionExpression && parentIsPromise
return isInCB
Expand Down
8 changes: 5 additions & 3 deletions rules/lib/is-named-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

var callbacks = ['done', 'cb', 'callback', 'next']

module.exports = function isNamedCallback (potentialCallbackName, exceptions) {
module.exports = function isNamedCallback(potentialCallbackName, exceptions) {
for (var i = 0; i < exceptions.length; i++) {
callbacks = callbacks.filter(function (item) { return item !== exceptions[i] })
callbacks = callbacks.filter(function(item) {
return item !== exceptions[i]
})
}
return callbacks.some(function (trueCallbackName) {
return callbacks.some(function(trueCallbackName) {
return potentialCallbackName === trueCallbackName
})
}
54 changes: 25 additions & 29 deletions rules/lib/is-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,32 @@
*/
'use strict'

var STATIC_METHODS = [
'all',
'race',
'reject',
'resolve'
]
var STATIC_METHODS = ['all', 'race', 'reject', 'resolve']

function isPromise (expression) {
return ( // hello.then()
expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'then'
) || ( // hello.catch()
expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'catch'
) || ( // hello.finally()
expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'finally'
) || ( // somePromise.ANYTHING()
expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
isPromise(expression.callee.object)
) || ( // Promise.STATIC_METHOD()
expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.object.type === 'Identifier' &&
expression.callee.object.name === 'Promise' &&
STATIC_METHODS.indexOf(expression.callee.property.name) !== -1
function isPromise(expression) {
return (
// hello.then()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'then') ||
// hello.catch()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'catch') ||
// hello.finally()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'finally') ||
// somePromise.ANYTHING()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
isPromise(expression.callee.object)) ||
// Promise.STATIC_METHOD()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.object.type === 'Identifier' &&
expression.callee.object.name === 'Promise' &&
STATIC_METHODS.indexOf(expression.callee.property.name) !== -1)
)
}

Expand Down

0 comments on commit 834c69d

Please sign in to comment.