question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[import/order] Force sort order of certain packages within "external" group

See original GitHub issue

We’re using the import/order rule to enforce a convention in module imports. Here’s how the import/order rule is currently configured in our project.

// in .eslintrc.js

module.exports = {
  rules: {
      'import/order': ['error', {
          groups: ['builtin', 'external', 'internal'],
          'newlines-between': 'always',
          alphabetize: {
              order: 'asc',
              caseInsensitive: true,
          },
      }],
}

This will result in the following import conventions:

// in a module with a React component 

// "external" modules are imported first
import { Button } from 'antd'
import PropTypes from 'prop-types'
import React from 'react'

// "internal" modules are imported second
import { BrandedIcon1 } from 'components/BrandedIcon1'
import { BrandedIcon2 } from 'components/BrandedIcon2'

All is well and good, except that our team’s convention is to import React first when a module has React components. Therefore, the example above would become:

import React from 'react' // force React to be imported first in the "external" group
import { Button } from 'antd'
import PropTypes from 'prop-types'

import { BrandedIcon1 } from 'components/BrandedIcon1'
import { BrandedIcon2 } from 'components/BrandedIcon2'

Is it possible to achieve this desired sort order using the import/order rule? I was a bit confused by the documentation of the pathGroups and pathGroupsExcludedImportTypes and I wasn’t sure if these options could be used to achieve this result. Thanks in advance for the help and keep up the great work on this plugin!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
egemoncommented, Feb 26, 2020

this worked for me

      "newlines-between": "always",
      "groups": ["builtin", "external", "internal", "parent", "sibling", "index"],
      "pathGroups": [
        {
          "pattern": "weplay-singleton/**",
          "group": "internal",
          "position": "before"
        },
        {
          "pattern": "weplay-core/**",
          "group": "internal",
          "position": "before"
        },
        {
          "pattern": "weplay-components/**",
          "group": "internal",
          "position": "before"
        },
        {
          "pattern": "weplay-competitive/**",
          "group": "internal",
          "position": "before"
        },
        {
          "pattern": "weplay-events/**",
          "group": "internal",
          "position": "before"
        },
        {
          "pattern": "weplay-media/**",
          "group": "internal",
          "position": "before"
        },
        {
          "pattern": "weplay-platform/**",
          "group": "internal",
          "position": "before"
        },
      ],
      pathGroupsExcludedImportTypes: [
        'weplay-singleton',
        'weplay-core',
        'weplay-components',
        'weplay-competitive',
        'weplay-events',
        'weplay-media',
        'weplay-platform',
      ]
    }],```
3reactions
egemoncommented, Mar 3, 2020

@ljharb it is. I think I should open PR and issue to add smth like that into documentation?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[import/order] Force sort order of certain packages within " ...
in a module with a React component // "external" modules are imported first import { Button } from 'antd' import PropTypes from 'prop-types' ......
Read more >
eslint import/order breaks down when using typescript ...
As you can see, I'm using the pathGroups to tell eslint that the proper position of any import that starts with ~/ is...
Read more >
Sorting your imports correctly in React
First at all makes it easier to see what you imported from specific packages, also if you group them, you can easily distinguish...
Read more >
Imports
Checks that the groups of import declarations appear in the order ... To configure the check to force some packages in special import...
Read more >
sort-imports - ESLint - Pluggable JavaScript Linter
The import statement is used to import members (functions, objects or primitives) that have been exported from an external module. Using a specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found