feat: add await option for next-tick-style rule#2508
feat: add await option for next-tick-style rule#2508codiini wants to merge 5 commits intovuejs:masterfrom
Conversation
| callbackBody = | ||
| args.type === 'ArrowFunctionExpression' || | ||
| args.type === 'FunctionExpression' | ||
| ? extractCallbackBody(args, sourceCode) |
There was a problem hiding this comment.
We can't extract callback body directly, there might be variable conflicts.
export default {
async created() {
const foo = 1;
nextTick(() => {
const foo = 2;
doSomething(foo);
})
}
}
lib/rules/next-tick-style.js
Outdated
| if (callback.body.type === 'BlockStatement') { | ||
| return sourceCode | ||
| .getText(callback.body) | ||
| .slice(1, -1) // Remove curly braces |
There was a problem hiding this comment.
Maybe we can simply keep the block.
lib/rules/next-tick-style.js
Outdated
|
|
||
| callbackBody = | ||
| args.type === 'ArrowFunctionExpression' || | ||
| args.type === 'FunctionExpression' |
There was a problem hiding this comment.
| args.type === 'FunctionExpression' | |
| (args.type === 'FunctionExpression' && !args.geneator) |
lib/rules/next-tick-style.js
Outdated
| const sourceCode = context.getSourceCode() | ||
|
|
||
| // Handle callback to await conversion | ||
| if (callExpression.arguments.length > 0) { |
There was a problem hiding this comment.
| if (callExpression.arguments.length > 0) { | |
| if (callExpression.arguments.length === 1) { |
| ) | ||
| return fixer.replaceText( | ||
| callExpression.parent, | ||
| `await ${nextTickCaller}();${callbackBody};` |
There was a problem hiding this comment.
We need make sure the nextTick call is an expression statement, otherwise this will cause sytax errors.
if (foo) nextTick(...)
(() => nextTick(...))();
There was a problem hiding this comment.
@fisker How would you suggest the fix method handle these cases?
There was a problem hiding this comment.
Ignore or turn into a block.
There was a problem hiding this comment.
Isn't it related to this one: #2508 (comment) ?
I removed the .slice() method which keeps the block
There was a problem hiding this comment.
Sorry didn't see that change.
There was a problem hiding this comment.
No worries. Is everything good to go now?
There was a problem hiding this comment.
This seems not the correct fix.. It changes behavior
nextTick(() => console.log(2))
console.log(1)Logs 1 2
await nextTick(); console.log(2)
console.log(1)Logs 2 1
Maybe use suggestions instead?
lib/rules/next-tick-style.js
Outdated
| function isAwaitedFunction(callExpression) { | ||
| return ( | ||
| callExpression.parent.type === 'AwaitExpression' && | ||
| callExpression.parent.parent.type !== 'MemberExpression' |
There was a problem hiding this comment.
Why this .parent.parent check needed.
| if (callback.body.type === 'BlockStatement') { | ||
| return sourceCode.getText(callback.body).trim() | ||
| } |
| args.type === 'ArrowFunctionExpression' || | ||
| (args.type === 'FunctionExpression' && !args.generator) | ||
| ? extractCallbackBody(args, sourceCode) | ||
| : `${sourceCode.getText(args)}()` |
There was a problem hiding this comment.
This is also not safe
nextTick(foo || bar)- await nextTick(); foo || bar()
+ await nextTick(); (foo || bar)()If foo is truly, it will not be called
| return sourceCode.getText(callback.body).trim() | ||
| } | ||
|
|
||
| return sourceCode.getText(callback.body) |
There was a problem hiding this comment.
The function body can't exact directly if it has return
const foo = async () => {
nextTick(() => {
return;
})
return false
}returns false
const foo = async () => {
await nextTick(); {
return;
}
return false
}returns undefined
There was a problem hiding this comment.
It will also broken if the callback has .id or used arguments inside.
const foo = async () => {
nextTick(function foo() {
use(foo)
use(arguments)
})
}
This PR addresses #2485