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

n/no-sync does not work with named import #102

Closed
blephy opened this issue Jun 29, 2023 · 2 comments
Closed

n/no-sync does not work with named import #102

blephy opened this issue Jun 29, 2023 · 2 comments

Comments

@blephy
Copy link

blephy commented Jun 29, 2023

When 'n/no-sync': ['error', { allowAtRootLevel: false }],

import { readFileSync } from 'node:fs';

const TOTO: Buffer = readFileSync('/toto');

works

but

import fs from 'node:fs';

const TOTO: Buffer = fs.readFileSync('/toto');

is reported as espected

@scagood
Copy link

scagood commented Sep 28, 2023

mmm, there are a couple of ways to go about this 🤔

The main issue is that we only check for a MemberExpression (something.else) as long as one of the properties ends with Sync. (regardless of if they're called as a function)

eg:

// These match
fs.magicSync('path')
fs.readFileSync.call(null, 'path')

// These do not match
readFileSync('path')
readFileSync.call(null, 'path')

Option 1

We can just change the selector a little bit (lib/rules/no-sync.js:36)
eg:

[
  // fs.readFileSync()
  "MemberExpression[property.name=/.*Sync$/]",
  // readFileSync.call(null, 'path')
  "MemberExpression[object.name=/.*Sync$/]",
  // readFileSync()
  ":not(MemberExpression) > Identifier[name=/Sync$/]",
]

Option 2

We can try to detect the "Sync" imports from the built modules as they're imported.

Also, if I remember correctly createReadStream creates the filehandle synchronously, but that does not end with Sync so we may also need to have other functions marked as 'sync' (don't hold me to that though 😂)

This would be done using the import/require declarations, then we'd traverse to the variable usage to see whether a Sync method was used. I created a quick gist here:
https://gist.github.com/scagood/bb6513bfe84a35f682c604f27a59e775

@aladdin-add
Copy link

fixed by #127 and released in 16.2.0.

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

No branches or pull requests

3 participants