-
-
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 8 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 |
---|---|---|
|
@@ -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: [{ | ||
allowSimpleArrowFunction: true, | ||
}], | ||
}, { | ||
code: 'user.save().catch(err => this._handleError(err));', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowSimpleArrowFunction: true, | ||
}], | ||
}, { | ||
code: 'user.save().finally(() => this._finallyDo());', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowSimpleArrowFunction: true, | ||
}], | ||
}, { | ||
code: 'user.save().then(() => user.reload());', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowSimpleArrowFunction: true, | ||
}], | ||
}, | ||
], | ||
invalid: [ | ||
|
@@ -49,23 +73,77 @@ 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().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().then(() => {return user.reload();});', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowSimpleArrowFunction: true, | ||
}], | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, { | ||
code: 'user.save().catch(() => {return error.handle();});', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowSimpleArrowFunction: true, | ||
}], | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, { | ||
code: 'user.save().finally(() => {return finallyDo();});', | ||
parserOptions: { ecmaVersion: 6 }, | ||
options: [{ | ||
allowSimpleArrowFunction: true, | ||
}], | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
}, | ||
}, { | ||
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: [{ | ||
allowSimpleArrowFunction: true, | ||
}], | ||
errors: [{ | ||
message: 'Use named functions defined on objects to handle promises', | ||
}], | ||
} | ||
], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,14 @@ describe('isArrowFunctionExpression', () => { | |
}); | ||
}); | ||
|
||
describe('isConciseArrowFunctionExpressionWithCall', () => { | ||
const node = parse('test = () => foo()').right; | ||
|
||
it('should check if node is concise arrow function expression with call expression body', () => { | ||
assert.ok(utils.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 cannot help but mention that test case for 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. In this case it might have a sense indeed. Just to be sure that arrow function with body is not filling requirements of this function. Could you please address that as well @sudowork ? :) 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. Done @bardzusny @michalsnik 😄 . Added two test cases:
|
||
}); | ||
|
||
describe('isNewExpression', () => { | ||
const node = parse('new Date()'); | ||
|
||
|
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 not sure if I agree with this case. I think you should probably write
user => this._reloadUser(user)
anyway, to be able to test it nicely. What do you think @bardzusny? I see it was like this before but then it was updated..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 was just speaking with @bardzusny and we agreed that in this PR it's ok to allow using
xxx.method()
to keep it consistent with current regular function expressions that are allowed. We do however think that it might be worth considering in next PR to add additional optional setting called for examplerequireThisBinding
. This setting in practice would force you to usethis.method.bind(this)
or() => this.method()
. I'll create separate issue with this proposition though.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 makes sense to me!