jscs-dev / node-jscs

:arrow_heading_up: JavaScript Code Style checker (unmaintained)
https://jscs-dev.github.io
MIT License
4.96k stars 510 forks source link

New Rule: Alphabetize Imports #2049

Closed danny-andrews closed 8 years ago

danny-andrews commented 8 years ago

It might be nice to have a rule that requires imports to be alphabetized. Even more useful would be a rule that forces you to separate local imports from external imports with a newline.

hzoo commented 8 years ago

For sure! Your other suggestion can be a separate rule. Also would you like to give it a shot?

rheh commented 8 years ago

To be clear, we are referring to ES6 imports here? If so, esnext provides the following ways of importing:

    // Default exports and named exports
    import theDefault, { named1, named2 } from 'src/mylib';
    import theDefault from 'src/mylib';
    import { named1, named2 } from 'src/mylib';

    // Renaming: import named1 as myNamed1
    import { named1 as myNamed1, named2 } from 'src/mylib';

    // Importing the module as an object
    // (with one property per named export)
    import * as mylib from 'src/mylib';

    // Only load the module, don’t import anything
    import 'src/mylib';
hzoo commented 8 years ago

This could be for es6 imports or commonjs requires?

kepta commented 8 years ago

Can someone be kind enough to explain what alphabetise imports are? Google failed to give me an answer :/

danny-andrews commented 8 years ago

It means sorting your imports in alphabetical order. e.g. This rule would complain about this:

import a from 'a';
import z from 'z';
import c from 'c';

It should be:

import a from 'a';
import c from 'c';
import z from 'z';
rheh commented 8 years ago

I will take a look a implementing this.