Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing from parent behaving unexpectedly #14

Open
alexlafroscia opened this issue Jun 24, 2019 · 5 comments
Open

Importing from parent behaving unexpectedly #14

alexlafroscia opened this issue Jun 24, 2019 · 5 comments

Comments

@alexlafroscia
Copy link

alexlafroscia commented Jun 24, 2019

I have import-helpers/sort-order configured like so:

    {
        groups: [
          'absolute',
          'module',
          '/^\\$src/',
          'parent',
          'sibling',
          'index'
        ],
        newlinesBetween: 'always',
        alphabetize: { order: 'asc' }
      }

I am seeing that either of these are considered "allowable" by the plugin

import { useCapabilities } from '..';

import { Capabilities, PREFIX } from '../useCapabilities';
import { Capabilities, PREFIX } from '../useCapabilities';

import { useCapabilities } from '..';

But this is not, because there must be a newline between groups

import { Capabilities, PREFIX } from '../useCapabilities';
import { useCapabilities } from '..';

This doesn't seem right to me, since it seems like they are in separate groups, but the groups can be ordered either way. Any idea what might be going on?

@willhoney7
Copy link
Owner

Hi there!

Sorry for the late response. I've done some investigating and have determined the issue is with the .. import statement. The rule doesn't know how to classify it (which is a bug). I'm working on it and hope to have a fix out soon.

@willhoney7
Copy link
Owner

Hmm, question for you @alexlafroscia:

What would you expect .. to be classified as?
What about ../.. or ../../index.ts?

I think by strict definition they should be in the index group... but I don't think it's super valuable to classify these parent directory imports as index. To me, if I'm importing from an index file a few directories up, I want it to be sorted with the other files from that directory, not in a separate group of index files.

I see a few possible solutions:

  1. If the import path ends in a directory (../.. , . , ../) or relative directories and then an index (../../index.ts , ./index), it is classified as index.
  2. If the path is one of: ., ./, ./index, ./index.js (or any line ending), classify as an index. Anything with .. will be classed as relative to parent
  3. Remove the index group and have them all be classified as relative to parent or relative to sibling.

Which solution do you like best? or do you have a different one? I'm leaning towards 3 and releasing this change as v2.

@alexlafroscia
Copy link
Author

alexlafroscia commented Jul 4, 2019 via email

@willhoney7
Copy link
Owner

@ai, as a vocal user of this rule, do you have any other thoughts on this?

@ai
Copy link
Contributor

ai commented Jul 5, 2019

I think ../index.js should be classified as parent since it is not different between x/ and x/index.js.

According to the docs, I thought that only ./ and ./index is part of index group.

// 5. "index" of the current directory
import main from './';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants