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

Local imports using webpack aliases are being incorrectly identified as separate groups #1034

Closed
cheapsteak opened this issue Feb 28, 2018 · 9 comments

Comments

@cheapsteak
Copy link

cheapsteak commented Feb 28, 2018

Using a webpack config using aliases (and eslint-import-resolver-webpack), each import for an internal modules is being treated as a separate "group". With 'newlines-between': 'always-and-inside-groups', they each have to be separated by a new line

resolve: {
  alias: {
    '~': path.resolve(__dirname, 'src'),
  },
},
import React from 'react';

import ComponentA from '~/components/ComponentA'; // [eslint] There should be at least one empty line between import groups (import/order)
import ComponentB from '~/components/ComponentB'; // [eslint] There should be at least one empty line between import groups (import/order)
import ComponentC from '~/components/ComponentC'; // no error

import ComponentD from '~/components/ComponentD'; // no error

Reproduced here: https://github.com/cheapsteak/eslint-import-order-bug-reproduction
Pull locally and check the lint messages for this file - https://github.com/cheapsteak/eslint-import-order-bug-reproduction/blob/master/src/App.js

@fsmaia
Copy link
Contributor

fsmaia commented Mar 1, 2018

I'm having a different issue, but I think they are related.

In my case, I have some aliases set in webpack, and resolver is properly configured:

resolve: {
  alias: {
    actions: path.join(__dirname, './src/actions'),
    components: path.join(__dirname, './src/components'),
    constants: path.join(__dirname, './src/constants'),
    //...
  },
},

And import/order is configured with:

{
  "groups": ["builtin", "external", "internal", "parent", "sibling", "index"],
  "newlines-between": "always"
}

I'm trying to import a external followed by internal module:

import { createSelector } from 'reselect';

import { MY_CONSTANT_EXAMPLE } from 'constants/my-constant';

However, I'm getting this error:

[eslint] constants/my-constant import should occur before import of reselect (import/order)

The example above works fine if I replace constants with src/constants.

@laugri
Copy link

laugri commented Jul 27, 2018

Hi @cheapsteak , did you find a workaround since you ran into this issue ?

@cheapsteak
Copy link
Author

Unfortunately not

@doits
Copy link

doits commented Feb 13, 2019

+1 named imports should be all in the internal group

@echenley
Copy link
Contributor

echenley commented Mar 1, 2019

@fsmaia I am having the exact same issue with a constants folder in particular. All of my other internal aliases seem to work.

Edit: looks like import something from 'constants/something' is being identified as 'builtin', rather than 'internal'.

@echenley
Copy link
Contributor

echenley commented Mar 1, 2019

@ljharb It seems like isBuiltIn needs to be updated to check for a resolved path:

https://github.com/benmosher/eslint-plugin-import/blob/master/src/core/importType.js#L24-L28

// import 'fs' - path is null
// import 'constants' - path is '/absolute/path/src/constants' with webpack resolver
export function isBuiltIn(name, settings, path) {
  const base = baseModule(name)
  const extras = (settings && settings['import/core-modules']) || []
  return !path && coreModules[base] || extras.indexOf(base) > -1
}

@ljharb
Copy link
Member

ljharb commented Mar 3, 2019

@echenley a PR with a test case would be appreciated :-)

@ljharb
Copy link
Member

ljharb commented Apr 11, 2019

Now that #1297 is merged, is there anything left to do here?

@echenley
Copy link
Contributor

I believe this can be closed.

@ljharb ljharb closed this as completed Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants