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

Bug: n/no-sync incorrectly applies to method definitions #355

Open
1 task
benjie opened this issue Oct 14, 2024 · 5 comments · May be fixed by #364
Open
1 task

Bug: n/no-sync incorrectly applies to method definitions #355

benjie opened this issue Oct 14, 2024 · 5 comments · May be fixed by #364
Labels

Comments

@benjie
Copy link

benjie commented Oct 14, 2024

Environment

Node version: 20.12.1
npm version: pnpm 9
ESLint version: 8.57.0
eslint-plugin-n version: 17.11.1
Operating System: Ubuntu

What rule do you want to report?

n/no-sync

Link to Minimal Reproducible Example

https://eslint-online-playground.netlify.app/#eNp9kTFvwyAQhf/KiamNHHt3lU5dO3UsVYXss0uLDwTnyFHk/14wdtJKURY40ON7946zCL6pcFKDM1h+B1GLxlJgUHCAsyQAj8GaI/pQ5zPA68iKtaXLBYAKJ2qAvzT14S2WD58FKN+HAiKNceLHqxag2kFZlrCrtqu52KrAY9clQr0yb5AOz/dhuVi2uMyiEBiMJi4jodN9TqkHZz0DQeftAHKV7J0Ze017kuJJkiScFlWLnRoNw3tirt5ZmaYSIVsAPxq8Dgoilyqy+5REijrZeG+9FP9b/IhesUunmh/Vp1+wFDtcIFLwyWF+O9g24qVYvKRo8fiCDqlFajSGpFl9tzT5mVGMIdZrizei/lVdpjb/AupTp8w=

What did you expect to happen?

The following code does not do any synchronous calls, but it does define two methods that end in Sync:

const a = {
  resolvers: {
    Mutation: {
      async thingsSync(_, args, context) {
        /* ... */
      },
      stuffSync: async (_, args, context) => {
        /* ... */
      }
    }
  }
}

The validation rule should only fire for function calls / method execution, not definition.

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

@benjie benjie added the bug label Oct 14, 2024
@scagood
Copy link

scagood commented Oct 14, 2024

This sounds simiular to #354 🤔

@benjie
Copy link
Author

benjie commented Oct 14, 2024

I almost filed a comment there, but then determined that it was sufficiently different to justify its own issue. This issue is about a general issue that no-sync should only apply to actual calls where *Sync(...) is executed; whereas #354 is effectively asking for an allow-list of *Sync functions to allow. I'd love to see both features implemented 👍

@scagood
Copy link

scagood commented Oct 15, 2024

This is because the only test is a selector:

const allowedAtRootLevelSelector = [
// fs.readFileSync()
":function MemberExpression > Identifier[name=/Sync$/]",
// readFileSync.call(null, 'path')
":function MemberExpression > Identifier[name=/Sync$/]",
// readFileSync()
":function :not(MemberExpression) > Identifier[name=/Sync$/]",
].join(",")
const disallowedAtRootLevelSelector = [
// fs.readFileSync()
"MemberExpression > Identifier[name=/Sync$/]",
// readFileSync.call(null, 'path')
"MemberExpression > Identifier[name=/Sync$/]",
// readFileSync()
":not(MemberExpression) > Identifier[name=/Sync$/]",
].join(",")

We could restrict this to CallExpression nodes and check for somethingSync.apply, somethingSync.bind, and somethingSync.call 🤔

@benjie
Copy link
Author

benjie commented Oct 15, 2024

Are there common cases where .apply, .call or .bind are used with sync calls? I don't think you'd generally want to override this and splat syntax can be used for passing multiple args (callback(...args)) which covers most of the use cases for .apply and .call at least.

@scagood scagood linked a pull request Oct 18, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants