-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Think no-new-require
should be a recommendation
#104
Comments
import x from 'x'
const { xyz } = x An That said, I'm neither pro or con on this. |
Yes, but it ofc depends on how the module that you are trying to import how it's exporting things. The statement When the module is exported in a way similar to combining the grouping and export last rules, a code refactor becomes as simple as a one-line diff: - module.exports = {
+ export = {
x,
y,
z
} With this export approach, refactoring and also reviewing the code then becomes very straightforward and easy, and However, certain cases, like By following this more or less best practice then they won't feel as reluctant to wanna make that transition from cjs to esm. |
First, I think I agree that Secondly, I think you're talking about a possible new rule, something like Here is a quick astexplorer example: 👨💻 Here is the long texty example:The rule could be something like this: export const meta = {
type: "problem",
hasSuggestions: true,
fixable: false
};
export function create(context) {
console.info(context);
return {
':not(VariableDeclarator, ExpressionStatement) > CallExpression[callee.name="require"]'(node) {
context.report({
node: node,
message: `Unexpected nested require`
});
}
};
} Given: require('dotenv');
const app1 = require('express')();
const express = require('express');
const app2 = express();
const stat = require('node:fs').stat; You get: // Unexpected nested require (at 3:14)
const app1 = require('express')();
// -------------^
// Unexpected nested require (at 8:14)
const stat = require('node:fs').stat;
// -------------^ Edit [2022-10-26 12:13]With fixer -- https://astexplorer.net/#/gist/1e0ae8545cf92ad0b8e6f45822bed6ae/e6365520bd2d4de5008c23c92d043d72f0bb6b1b |
...why? refactoring cjs to esm gets harder.
it's a footgun for ppl writing this kind of syntax where they do
new require('app-header')
or stuff likeapp = require('express')()
chaining stuff on the
require()
is bad.it's better / easier if it looks more like ESM syntax and follows this format
const { xyz } = require(x)
it's not recommended that you do things such as
require(x).foo().bar
ppl complain that refactoring a project to esm is hard and takes long time. but having
no-new-require
as a recommendation would make it easier for ppl by spending less time and also making PR/reviews easier.The text was updated successfully, but these errors were encountered: