-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 #567
Comments
👍 to allowing empty template literals iff it's easy to distinguish. I've never looked at the AST representation of them. I'd expect 99.999% of them to have expressions, though, so if summarily rejecting template literals is a lot cleaner, I'd go with that until someone presents a compelling argument for them. |
Yeah, it's pretty easy: https://astexplorer.net/#/ne6VWpNvM5
Same here, unless for the people who use template literals as their default string (why not). Anyway, let's include template literals as it's easy to check. |
We're using a config object in in order to use a different module in case of testing. const config = require('../../config');
const Location = require(config.get('modules:app/models/location')); how would you handle that case? |
I wouldn't do that to mock requires; that's changing production code for testing purposes. I'd use a static require and use one of the many available module mocking solutions in tests. |
thanks. It's a legacy project and I guess for now I'll add a todo/exception |
I have just found
require
calls of the following form in a codebaseIt can be useful when doing real dynamic package imports (with a plugin system for instance), but in most cases, it's not a good practice, for all the reasons that pushed the ES6 import/export system to be static.
I'm proposing a rule to forbid dynamic paths in
require()
, i.e. using something else than a string literal as a require argument. For the name, I thinkno-dynamic-require
fits well.I'm wondering whether template literals without expressions should be allowed.
Invalid
Valid
The text was updated successfully, but these errors were encountered: