Skip to content
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

feat: add await option for next-tick-style rule #2508

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

codiini
Copy link

@codiini codiini commented Jul 11, 2024

This PR addresses #2485

callbackBody =
args.type === 'ArrowFunctionExpression' ||
args.type === 'FunctionExpression'
? extractCallbackBody(args, sourceCode)
Copy link
Contributor

@fisker fisker Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
    })
   }
}

if (callback.body.type === 'BlockStatement') {
return sourceCode
.getText(callback.body)
.slice(1, -1) // Remove curly braces
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can simply keep the block.


callbackBody =
args.type === 'ArrowFunctionExpression' ||
args.type === 'FunctionExpression'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
args.type === 'FunctionExpression'
(args.type === 'FunctionExpression' && !args.geneator)

const sourceCode = context.getSourceCode()

// Handle callback to await conversion
if (callExpression.arguments.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (callExpression.arguments.length > 0) {
if (callExpression.arguments.length === 1) {

)
return fixer.replaceText(
callExpression.parent,
`await ${nextTickCaller}();${callbackBody};`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need make sure the nextTick call is an expression statement, otherwise this will cause sytax errors.

if (foo) nextTick(...)
(() => nextTick(...))();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker How would you suggest the fix method handle these cases?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore or turn into a block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ignore" means no fix

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've added a check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it related to this one: #2508 (comment) ?
I removed the .slice() method which keeps the block

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry didn't see that change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. Is everything good to go now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a maintainer..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

function isAwaitedFunction(callExpression) {
return (
callExpression.parent.type === 'AwaitExpression' &&
callExpression.parent.parent.type !== 'MemberExpression'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this .parent.parent check needed.

@codiini codiini requested a review from fisker July 22, 2024 13:46
Comment on lines +100 to +102
if (callback.body.type === 'BlockStatement') {
return sourceCode.getText(callback.body).trim()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this branch.

args.type === 'ArrowFunctionExpression' ||
(args.type === 'FunctionExpression' && !args.generator)
? extractCallbackBody(args, sourceCode)
: `${sourceCode.getText(args)}()`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will also broken if the callback has .id or used arguments inside.

const foo = async () => { 
	nextTick(function foo() {
    use(foo)
	  use(arguments)
	})
}

Copy link
Member

@FloEdelmann FloEdelmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codiini Could you please address @fisker's comments?

@FloEdelmann FloEdelmann linked an issue Aug 29, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New option for next-tick-style
3 participants