eslint-plugin-import icon indicating copy to clipboard operation
eslint-plugin-import copied to clipboard

Is it possible to sort imports based on variable names within group?

Open techsin opened this issue 5 years ago • 4 comments

"Sort the order within each group in alphabetical manner based on import path"

That's the current way... what If I wanted something like this..


import React from 'react';
import CompA from './zecomp.js
import CompB from './abigcomp.com

but sorting based on path gives me


import React from 'react';
import CompB from './abigcomp.com
import CompA from './zecomp.js

techsin avatar Dec 19 '19 16:12 techsin

It seems doable to allow a config option alphebeticalBy that defaults to "specifier" but can be set to "binding".

However, this only really makes sense with a default import, or a namespace import - what about import { z, a } from 'path'?

ljharb avatar Dec 20 '19 00:12 ljharb

sort to {a, z} then use first word, a?

techsin avatar Dec 20 '19 01:12 techsin

Would the rule sort the named imports, as well? That almost feels like a bit of an overreach of this rule.

ljharb avatar Dec 20 '19 05:12 ljharb

yes, but if i had a rule expecting it to autofix (sort) my import based on var names, i'd want it to take the liberty of making opinionated choices as long as it doesn't modify function of my code.

how many imports are there?

const a = require(..) import * from '...'; import abc from 'abc'; import * as abc from 'abc'; import {props} from 'abc';

  • could be at the most top. if there is as then that name if {..} then first prop name, after sorting...

very opinionated for sure. But complexity is inherent.

techsin avatar Dec 20 '19 05:12 techsin