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

Rule proposal: no-dynamic-require #2477

Closed
axetroy opened this issue Oct 12, 2024 · 1 comment
Closed

Rule proposal: no-dynamic-require #2477

axetroy opened this issue Oct 12, 2024 · 1 comment
Labels

Comments

@axetroy
Copy link
Contributor

axetroy commented Oct 12, 2024

Description

The input to require should be a static string literal.

This is beneficial to various Bundles and Analyzer. And less difficult to migrate to es module.

The dynamic require should be prohibited.

Fail

require(pathToTargetModule)
require('./icons/' + iconType + '.svg')

Pass

require('./path/to/module')
require('./icons/pass.svg')

Proposed rule name

no-dynamic-require

Additional Info

Of course, in our project, there will indeed be such a situation.

require(pathToTargetModule)

This is a typical scenario that needs to be ignored.

exports[`no-dynamic-require 1`] = `
## invalid(1): require(foo)

  > Input
  
        1 | require(foo)

  > Error 1/1: Unexpected dynamic require.
  
      > 1 | require(foo)
          | ^^^^^^^^^^^^

## invalid(2): require("foo" + "bar")

  > Input
  
        1 | require("foo" + "bar")

  > Error 1/1: Unexpected dynamic require.
  
      > 1 | require("foo" + "bar")
          | ^^^^^^^^^^^^^^^^^^^^^^

## invalid(3): require(\`\${foo}/bar\`)

  > Input
  
        1 | require(\`\${foo}/bar\`)

  > Error 1/1: Unexpected dynamic require.
  
      > 1 | require(\`\${foo}/bar\`)
          | ^^^^^^^^^^^^^^^^^^^^^
`;

@sindresorhus
Copy link
Owner

We are not adding any new CommonJS-related rules here.

And it seems more appropriate for https://github.com/un-ts/eslint-plugin-import-x anyway

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants