-
Notifications
You must be signed in to change notification settings - Fork 56
Description
What rule do you want to change?
classes, enums, imports, modules, sort-objects (in destructuring), variable-declarations
Describe the problem
What do we think about adding an option to these rules to sort by usage (in the same file)?
Taking variable-declarations as an example, the variable declarations would be sorted so that the first-used variable is declared first, and so on.
We would probably consider usage only within the current scope, and not usage when something is exported and used in another file.
This should actually allow us to bypass dependency tracking in most cases, because this sort method naturally takes care of non-circular dependencies.
This was previously brought up in #434 , but that issue was quite different.
- In Feature: sort-modules in order of use #434, this was discussed only for
modules, whereas this issue would add this functionality to several different rules. - The actual functionality of sorting by usage was only briefly discussed in Feature: sort-modules in order of use #434, before that issue went off-topic, and began discussing other bugs and issues in
modules. - Feature: sort-modules in order of use #434 was closed after some minor bugs in
moduleswere fixed, but the actual functionality of having an option to sort by usage wasn't implemented.
Code example
Correct sort order:
const b = 2, a = 1, c = b + a;
console.log(c);
console.log(a, b);
b and a are first used on line 1, while c is first used on line 3.
b is used before a, so it is sorted first.
Note that we didn't even need to determine that c depends on a and b — applying this sort order automatically and naturally takes care of dependencies!
Additional comments
No response
Validations
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.