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

Add extensions order support for import/order #1239

Closed
atassis opened this issue Nov 23, 2018 · 18 comments
Closed

Add extensions order support for import/order #1239

atassis opened this issue Nov 23, 2018 · 18 comments

Comments

@atassis
Copy link

atassis commented Nov 23, 2018

It would be nice to order imports based on their extension. For our case, we want to leave all css modules imported at the end of the imports list

import styles from '../styles.scss';
import CustomModule from './custom-module';```
Here it is more preferable to warn us move `styles` after `CustomModule`
@fuko-fabio
Copy link

Would be perfect if we could add this feature. We have the same case with css files.

@rjhilgefort
Copy link

Would love to see this as well.

@Glinkis
Copy link

Glinkis commented Mar 12, 2020

I thought this could be accomplished with the pathGroups option, like this:

"pathGroups": [
  {
    "pattern": "**/*.scss",
    "group": "index",
    "position": "after"
  }
]

But I can't seem to get this to work.

@Glinkis
Copy link

Glinkis commented Mar 12, 2020

After some experimenting, this seems to do the trick:

"pathGroups": [
  {
    "pattern": "./*.scss",
    "group": "sibling",
    "position": "after"
  }
]

@ljharb ljharb closed this as completed Apr 23, 2020
@zero-t4
Copy link

zero-t4 commented Jun 9, 2020

I thought this could be accomplished with the pathGroups option, like this:

"pathGroups": [
  {
    "pattern": "**/*.scss",
    "group": "index",
    "position": "after"
  }
]

But I can't seem to get this to work.

Im also stuck with it.
@ljharb Why this case does not work?

@constgen
Copy link

"**/*.scss" pattern will not match paths started with ./ and ../. It only matches paths like @/styles/index.css and styles/index.css. To make it work you have to use "{.,..}/**/*.css" pattern. For example my config looks like

"pathGroups": [
  {
    "pattern": "**/*.+(css|sass|less|scss|pcss|styl)",
    "patternOptions": {"dot": true, "nocomment": true},
    "group": "unknown",
    "position": "after"
  },
  {
    "pattern": "{.,..}/**/*.+(css|sass|less|scss|pcss|styl)",
    "patternOptions": {"dot": true, "nocomment": true},
    "group": "unknown",
    "position": "after"
  }
],

Used 2 patterns to cover all cases

@itayganor
Copy link

"**/*.scss" pattern will not match paths started with ./ and ../. It only matches paths like @/styles/index.css and styles/index.css. To make it work you have to use "{.,..}/**/*.css" pattern. For example my config looks like

"pathGroups": [
  {
    "pattern": "**/*.+(css|sass|less|scss|pcss|styl)",
    "patternOptions": {"dot": true, "nocomment": true},
    "group": "unknown",
    "position": "after"
  },
  {
    "pattern": "{.,..}/**/*.+(css|sass|less|scss|pcss|styl)",
    "patternOptions": {"dot": true, "nocomment": true},
    "group": "unknown",
    "position": "after"
  }
],

Used 2 patterns to cover all cases

Nice, I've been using the following pattern lately:

{.,..}/**/*.{less,css,scss}

But I have some imports that start with ../../ and this glob won't match :(
Do you have any idea how to match everything with a file extension?

This is frustrating

@ljharb
Copy link
Member

ljharb commented Jul 29, 2020

**/*.*?

@constgen
Copy link

constgen commented Aug 3, 2020

Unfortunately this is a known problem of Minimatch. And the solution is ugly - list all possible "up" paths (1,2,3,4,5... levels up)
https://github.com/atomspace/atomspace-eslint/blob/master/src/configs/import.config.json#L104
And keep finger crossed that the project structure will be not very deep

@ThisIsOstad
Copy link

To solve this, you can set matchBase property in patternOptions.

{
  pathGroups: [
  {
    pattern: '*.scss',
    group: 'index',
    patternOptions: { matchBase: true },
    position: 'after'
  ]
}

@itayganor
Copy link

@MoradiDavijani This is amazing. You made my day ❤️

@yuri-sakharov
Copy link

yuri-sakharov commented Feb 4, 2021

Doesn't work for me (typescript project)

"import/order": ["error", {
            "groups": ["external", ["internal", "parent", "sibling", "index"], "unknown"],
            "pathGroups": [
                {
                    "pattern": "*.scss",
                    "patternOptions": { "matchBase": true },
                    "group": "unknown",
                    "position": "after"
                }
            ],
            "pathGroupsExcludedImportTypes": [],
            "newlines-between": "always"
        }],

expected .scss in bottom but result is still same.

import './AvatarsGroup.scss';
import { Avatar } from 'components/Avatar';
import { PlusIndicator } from 'components/PlusIndicator';

Any ideas?

@ljharb
Copy link
Member

ljharb commented Feb 4, 2021

@yuri-sakharov you may also need to set up the import/extensions settings as described in the readme.

@constgen
Copy link

constgen commented Feb 5, 2021

@yuri-sakharov this is expected to work so. You faced a completely different issue. There are already several opened tickets for this https://github.com/benmosher/eslint-plugin-import/issues?q=is%3Aissue+is%3Aopen+side+effect+label%3A%22import%2Fexport+ordering%22+

@nickvirdenlegalshield
Copy link

@yuri-sakharov I was having trouble just like you with other solutions, such as with solutions by @constgen and @MoradiDavijani, which were helpful in solving the issue.

After some toying around with it, I figured out that I had to set warnOnUnassignedImports to true.

Once I did that, it started picking up on the unassigned imports (e.g. import './foo.scss';, but it would put them after sibling instead at the very end of the import order like I wanted - after object.

Here's my configuration:

Eslint version: 7.31.0
eslint-plugin-import version: 2.24.0

Import order

import './foo.scss';
import barModule from '../../barModule';
import bazModule from './bazModule';

ESLint configuration

'import/order': [
    // other config here
    'pathGroups': [
      {
        pattern: '{.,..}/*\.scss', // same directory only
        // pattern: '{.,..}/**/*\.scss' // same & outside directories (e.g. import '../foo/foo.scss')
        group: 'object',
        position: 'after'
      }
    ],
    'warnOnUnassignedImports': true
  }
]

@debjyoti-pandit
Copy link

@nickvirdenlegalshield getting the same issue.
Did you guys have the solution to why it is importing before the nearest sibling?

@andrmoel
Copy link

andrmoel commented Aug 9, 2023

@nickvirdenlegalshield unfortunately warnOnUnassignedImports does not work with autofix.

@20manas
Copy link

20manas commented Jan 11, 2024

In summary, to match one or more extensions, including unassigned imports:

'pathGroups': [
  {
    pattern: '*.{css,scss}',
    patternOptions: {matchBase: true},
    group: 'unknown',
    position: 'after',
  },
],
// no auto-fix
'warnOnUnassignedImports': true,

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

No branches or pull requests