Skip to content

Feature: sort in order of use #631

@abrahamguo

Description

@abrahamguo

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.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions