-
-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow concise ArrowFunctionExpression (named-functions-in-promises) #64
Changes from 6 commits
970cb30
57b8ceb
148c0d0
4d01fda
8cfcdc8
997edf8
3264c37
c940ab2
cbb5d33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ module.exports = { | |
}, | ||
|
||
create(context) { | ||
const options = context.options[0] || {}; | ||
const allowConciseArrow = options.allowConciseArrow || false; | ||
|
||
const message = 'Use named functions defined on objects to handle promises'; | ||
|
||
const report = function (node) { | ||
|
@@ -22,14 +25,16 @@ module.exports = { | |
CallExpression(node) { | ||
const firstArg = node.arguments[0]; | ||
|
||
if ( | ||
hasPromiseExpression(node) && | ||
( | ||
if (hasPromiseExpression(node)) { | ||
if (allowConciseArrow && isConciseArrowFunctionWithCallExpression(firstArg)) { | ||
return; | ||
} | ||
if ( | ||
utils.isFunctionExpression(firstArg) || | ||
utils.isArrowFunctionExpression(firstArg) | ||
) | ||
) { | ||
report(node); | ||
) { | ||
report(node); | ||
} | ||
} | ||
}, | ||
}; | ||
|
@@ -42,5 +47,11 @@ module.exports = { | |
utils.isIdentifier(callee.property) && | ||
promisesMethods.indexOf(callee.property.name) > -1; | ||
} | ||
|
||
function isConciseArrowFunctionWithCallExpression(node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd move this function to utilities, where other functions are, and write unit test for this specific function as well. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do! I didn't do this at first because I was a bit worried it was too specific. |
||
return utils.isArrowFunctionExpression(node) && | ||
node.expression && | ||
utils.isCallExpression(node.body); | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,30 @@ eslintTester.run('named-functions-in-promises', rule, { | |
}, { | ||
code: 'user.save().finally(_finallyDo);', | ||
parserOptions: { ecmaVersion: 6 }, | ||
}, { | ||
code: 'user.save().then(() => this._reloadUser(user));', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowConciseArrow: true, | ||
}], | ||
}, { | ||
code: 'user.save().catch(err => this._handleError(err));', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowConciseArrow: true, | ||
}], | ||
}, { | ||
code: 'user.save().finally(() => this._finallyDo());', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowConciseArrow: true, | ||
}], | ||
}, { | ||
code: 'user.save().then(() => user.reload());', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if I agree with this case. I think you should probably write There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just speaking with @bardzusny and we agreed that in this PR it's ok to allow using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense to me! |
||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowConciseArrow: true, | ||
}], | ||
}, | ||
], | ||
invalid: [ | ||
|
@@ -49,23 +73,50 @@ eslintTester.run('named-functions-in-promises', rule, { | |
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, { | ||
code: 'user.save().then(() => user.reload());', | ||
code: 'user.save().catch(() => {return error.handle();});', | ||
parserOptions: { ecmaVersion: 6 }, | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, { | ||
code: 'user.save().catch(() => {return error.handle();});', | ||
code: 'user.save().finally(() => {return finallyDo();});', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add one or three more tests like this, but with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do! |
||
parserOptions: { ecmaVersion: 6 }, | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, { | ||
code: 'user.save().finally(() => {return finallyDo();});', | ||
code: 'user.save().then(() => this._reloadUser(user));', | ||
parserOptions: { ecmaVersion: 6 }, | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, | ||
}, { | ||
code: 'user.save().catch(err => this._handleError(err));', | ||
parserOptions: { ecmaVersion: 6 }, | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, { | ||
code: 'user.save().finally(() => this._finallyDo());', | ||
parserOptions: { ecmaVersion: 6 }, | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, { | ||
code: 'user.save().then(() => user.reload());', | ||
parserOptions: { ecmaVersion: 6 }, | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, { | ||
code: 'user.save().then(user => user.name);', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowConciseArrow: true, | ||
}], | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
} | ||
], | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking about renaming this setting to
allowSimpleArrowFunction
- I think it would be more descriptive. What do you think? :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds good and would probably make more sense to users. I was trying to reuse the language from MDN regarding "concise body".