node icon indicating copy to clipboard operation
node copied to clipboard

Add ESLint rule to enforce `{a, b, c} = require()` is in alphabetical order

Open cola119 opened this issue 1 year ago • 0 comments

What is the problem this feature will solve?

Currently ESlint doesn't have any rule to detect require statement with out of order variables (i.e. {b, a} = require("path"), and which sometimes can lead in a big burden in code review for both reviewers and authors. The order should be enforced by linter if we regard it as important and want code base to be always consistent.

What is the feature you are proposing to solve the problem?

https://github.com/nodejs/node/pull/44116 is an experimental implementation for this rule, which is named sort-requires. If this rule is enabled, following require statement will be prohibited.

const { validateBoolean, validateArray} = require('internal/validators')
                         ^^^^^^^^^^^^^
validateArray should occur before validateBoolean

This rule will reduce the amount of stylistic review related to ordering and will help to maintain consistency throughout the entire codebase.

What alternatives have you considered?

We can document the requirements for the ordering and leave that responsibility to the code owner's awareness and code review. If the order is not important for the codebase, contributors don't have to take care of it.

cola119 avatar Aug 06 '22 14:08 cola119