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

Add requireResolve option #1217

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

vikr01
Copy link
Contributor

@vikr01 vikr01 commented Oct 21, 2018

Closes #585

Alternate to #1216

Adds an option requireResolve to check require.resolve statements.

Might want to support require.resolve by default in a future major version though.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.09%) to 97.349% when pulling 4773c69 on vikr01:feature/require-resolve-option into b4a2f11 on benmosher:master.

@coveralls
Copy link

coveralls commented Oct 21, 2018

Coverage Status

Coverage increased (+0.003%) to 97.36% when pulling 1d387cf on vikr01:feature/require-resolve-option into db471a8 on benmosher:master.

@ljharb
Copy link
Member

ljharb commented Oct 21, 2018

There was no need for a separate PR, for future reference.

I'll keep both PRs updated.

/*eslint import/no-unresolved: [2, { requireResolve: true }]*/
const { default: x } = require.resolve('./foo') // reported if './foo' is not found

require.resolve(0) // ignored
Copy link
Member

Choose a reason for hiding this comment

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

why is this ignored? i'd expect this to be coerced to a string and then validated the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Attempting to run require.resolve(0) gives me this stacktrace:

TypeError [ERR_INVALID_ARG_TYPE]: The "request" argument must be of type string. Received type number
    at Function.resolve (internal/modules/cjs/helpers.js:28:13)

Copy link
Member

Choose a reason for hiding this comment

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

it seems useful to me to have the linter warn on it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree -- but we should follow the same rules we have for commonjs require right?

Copy link
Member

Choose a reason for hiding this comment

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

Not necessarily - require is used in multiple ways, and so has edge cases - require.resolve, however, does not have those edge cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what you mean. I've never run into any cases where the arguments of require (commonjs) and require.resolve work differently.

From the node documentation it says that require.resolve "gets the exact filename that will be loaded when require()".

utils/moduleVisitor.js Show resolved Hide resolved
@vikr01 vikr01 force-pushed the feature/require-resolve-option branch from 1615ae1 to 1d387cf Compare October 30, 2018 07:14
@vikr01
Copy link
Contributor Author

vikr01 commented Feb 3, 2019

@benmosher Could you review this when you have a chance?

@lgandecki
Copy link

Hello, any reason why this is not merged? I'd also love to have this functionality. I am also happy to change - can deal with the conflicts and test. Just wouldn't want to do the extra work if there is a separate reason for this not being considered anymore.
Thanks!

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

This needs a rebase, and would need test cases as well (and doc updates).

@ljharb ljharb marked this pull request as draft January 31, 2021 20:35
@lgandecki
Copy link

@ljharb I'm happy to rebase and do the necessary work.
It seems to me that this PR does have test cases, are there any specifics that you would like to be added?

@ljharb
Copy link
Member

ljharb commented May 14, 2021

@lgandecki that'd be great!

current tests look OK, but the more the better :-) in general, for every new test case, i'd prefer it to be tested in each parser, and both with and without the new option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Support for checking require.resolve()
4 participants