Skip to content

Commit

Permalink
bumps deps; use ESLint 3 rule format
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme committed Jul 12, 2016
1 parent d6bd2a0 commit 083dc3e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 134 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
"test": "mocha test"
},
"devDependencies": {
"eslint": "^2.11.1",
"eslint": "^2.10 || ^3.0",
"mocha": "^2.3.4",
"standard": "^5.4.1"
"standard": "^7.1.2"
},
"peerDependencies": {
"eslint": "^2.10 || ^3.0"
},
"engines": {
"node": ">=0.10.0"
Expand Down
42 changes: 22 additions & 20 deletions rules/always-catch.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
module.exports = function (context) {
return {
ExpressionStatement: function (node) {
// hello.then()
if (node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.property.name === 'then'
) {
context.report(node, 'You should always catch() a then()')
return
}

// hello.then().then().catch()
if (node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.object.type === 'CallExpression' &&
node.expression.callee.object.callee.type === 'MemberExpression' &&
node.expression.callee.object.callee.property.name === 'then'
) {
if (node.expression.callee.property.name !== 'catch') {
module.exports = {
create: function (context) {
return {
ExpressionStatement: function (node) {
// hello.then()
if (node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.property.name === 'then'
) {
context.report(node, 'You should always catch() a then()')
return
}

// hello.then().then().catch()
if (node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.object.type === 'CallExpression' &&
node.expression.callee.object.callee.type === 'MemberExpression' &&
node.expression.callee.object.callee.property.name === 'then'
) {
if (node.expression.callee.property.name !== 'catch') {
context.report(node, 'You should always catch() a then()')
}
}
}
}
Expand Down
32 changes: 17 additions & 15 deletions rules/always-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ function isReturnOrThrowStatement (node) {
return node.type === 'ReturnStatement' || node.type === 'ThrowStatement'
}

module.exports = function (context) {
return {
MemberExpression: function (node) {
var firstArg, body, lastStatement
module.exports = {
create: function (context) {
return {
MemberExpression: function (node) {
var firstArg, body, lastStatement

if (node.property.name !== 'then' || node.parent.type !== 'CallExpression') {
return
}
if (node.property.name !== 'then' || node.parent.type !== 'CallExpression') {
return
}

firstArg = node.parent.arguments[0]
if (!firstArg || !isFunctionWithBlockStatement(firstArg)) {
return
}
firstArg = node.parent.arguments[0]
if (!firstArg || !isFunctionWithBlockStatement(firstArg)) {
return
}

body = firstArg.body.body
lastStatement = body[body.length - 1]
if (!lastStatement || !isReturnOrThrowStatement(lastStatement)) {
context.report(node, 'Each then() should return a value or throw')
body = firstArg.body.body
lastStatement = body[body.length - 1]
if (!lastStatement || !isReturnOrThrowStatement(lastStatement)) {
context.report(node, 'Each then() should return a value or throw')
}
}
}
}
Expand Down
109 changes: 57 additions & 52 deletions rules/catch-or-return.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,63 @@
module.exports = function (context) {
var options = context.options[0] || {}
var allowThen = options.allowThen
var terminationMethod = options.terminationMethod || 'catch'
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'
) || ( // 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
)
}
return {
ExpressionStatement: function (node) {
if (!isPromise(node.expression)) {
return
}
var STATIC_METHODS = [
'all',
'race',
'reject',
'resolve'
]

// somePromise.then(a, b)
if (allowThen &&
node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.property.name === 'then' &&
node.expression.arguments.length === 2
) {
return
}
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'
) || ( // 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
)
}

module.exports = {
create: function (context) {
var options = context.options[0] || {}
var allowThen = options.allowThen
var terminationMethod = options.terminationMethod || 'catch'

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

// somePromise.then(a, b)
if (allowThen &&
node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.property.name === 'then' &&
node.expression.arguments.length === 2
) {
return
}

// somePromise.catch()
if (node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.property.name === terminationMethod
) {
return
// somePromise.catch()
if (node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.property.name === terminationMethod
) {
return
}
context.report(node, 'Expected ' + terminationMethod + '() or return')
}
context.report(node, 'Expected ' + terminationMethod + '() or return')
}
}
}
70 changes: 36 additions & 34 deletions rules/no-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,45 @@

'use strict'

module.exports = function (context) {
var MESSAGE = '"{{name}}" is not defined.'

/**
* Checks for and reports reassigned constants
*
* @param {Scope} scope - an escope Scope object
* @returns {void}
* @private
*/
function isDeclared (scope, ref) {
return scope.variables.some(function (variable) {
if (variable.name !== ref.identifier.name) {
return false
}

if (!variable.defs || !variable.defs.length) {
return false
}

return true
})
}
function isDeclared (scope, ref) {
return scope.variables.some(function (variable) {
if (variable.name !== ref.identifier.name) {
return false
}

return {
'Program:exit': function () {
var scope = context.getScope()
if (!variable.defs || !variable.defs.length) {
return false
}

scope.implicit.left.forEach(function (ref) {
if (ref.identifier.name !== 'Promise') {
return
}
return true
})
}

if (!isDeclared(scope, ref)) {
context.report(ref.identifier, MESSAGE, { name: ref.identifier.name })
}
})
module.exports = {
create: function (context) {
var MESSAGE = '"{{name}}" is not defined.'

/**
* Checks for and reports reassigned constants
*
* @param {Scope} scope - an escope Scope object
* @returns {void}
* @private
*/
return {
'Program:exit': function () {
var scope = context.getScope()

scope.implicit.left.forEach(function (ref) {
if (ref.identifier.name !== 'Promise') {
return
}

if (!isDeclared(scope, ref)) {
context.report(ref.identifier, MESSAGE, { name: ref.identifier.name })
}
})
}
}
}
}
24 changes: 13 additions & 11 deletions rules/param-names.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module.exports = function (context) {
return {
NewExpression: function (node) {
if (node.callee.name === 'Promise' && node.arguments.length === 1) {
var params = node.arguments[0].params
module.exports = {
create: function (context) {
return {
NewExpression: function (node) {
if (node.callee.name === 'Promise' && node.arguments.length === 1) {
var params = node.arguments[0].params

if (!params || !params.length) { return }
if (!params || !params.length) { return }

if (params[0].name !== 'resolve') {
return context.report(node, 'Promise constructor parameters must be named resolve, reject')
}
if (params[0].name !== 'resolve') {
return context.report(node, 'Promise constructor parameters must be named resolve, reject')
}

if (params[1] && params[1].name !== 'reject') {
return context.report(node, 'Promise constructor parameters must be named resolve, reject')
if (params[1] && params[1].name !== 'reject') {
return context.report(node, 'Promise constructor parameters must be named resolve, reject')
}
}
}
}
Expand Down

0 comments on commit 083dc3e

Please sign in to comment.