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: Detect sync functions from built in node modules #128

Closed
wants to merge 2 commits into from

Conversation

scagood
Copy link

@scagood scagood commented Oct 6, 2023

This is a different strategy to detect "Sync" functions by looking for the builtin node imports. Then we look for the usage of the function from there.

This means we can detect aliases like this too:

import { readFileSync as readFile } from 'node:fs';

readFile();

The tradeoff is that npm packages can now export Sync functions, eg:

import * as magic from 'npm:magic-module';

magic.performMagicSync();

This is the counterpoint to #127 and Option 2 for #102 (the complex option).


/** @type {import('eslint').Rule.Node} */
const memberExpression = reference.identifier.parent
if (
Copy link

Choose a reason for hiding this comment

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

nits: the name memberExpression seems a little confusing to me - it's also likely to be a non-memberexpression node?

Copy link
Author

Choose a reason for hiding this comment

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

Mm, I know what you mean.
I didn't really know what to call this, I called it memberExpression because I check to see if it's a memberExpression immediately after I create it 👀

Do you have any better name suggestions 😅

Choose a reason for hiding this comment

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

something like parentNode or identifierParentNode?

Choose a reason for hiding this comment

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

ok, given it's checked I think it's fine to be as-is. (naming is not an easy thing :). 😄

@scagood
Copy link
Author

scagood commented Oct 11, 2023

Mm, if #127 is merged shall I close this one?

data: {
importName: node.imported.name,
aliasName: variables.at(0)?.name,
},

Choose a reason for hiding this comment

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

out of curiosity: is there a benefit to use .at() over [0]?
not sure .at() was supported since node.js v16.

Copy link
Author

Choose a reason for hiding this comment

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

This is just personal preference, I like the way .at behaves for negative numbers and for non numbers

I really didn't think about it that much, from a performance standpoint you probably want to use [0]. But I dont know 🤔

Copy link
Author

Choose a reason for hiding this comment

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

Just checked, it was 16.6 - when I am at my pc I will change it to an array index

Choose a reason for hiding this comment

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

seems its support was added in node.js v16.8.0, while the package requires node.js >=16.0.0. let's just use [0]? 😅

@aladdin-add
Copy link

or you can fetch and rebase the main branch, and then do a force push:

$ git remote add up https://github.com/eslint-community/eslint-plugin-n
$ git fetch up
$ git rebase up/master
$ git push -f

@scagood
Copy link
Author

scagood commented Oct 11, 2023

I kinda meant this to be either #127 or #128. As they both solve roughly the same problem, but in different ways. I can rebase if you'd like

@aladdin-add
Copy link

👌 it's fine to close this one, thanks for doing this! ❤️

@scagood scagood deleted the no-sync-builtin branch October 11, 2023 08:48
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.

None yet

2 participants