[import/order] Force sort order of certain packages within "external" group
See original GitHub issueWe’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:
- Created 4 years ago
- Reactions:6
- Comments:9 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
this worked for me
@ljharb it is. I think I should open PR and issue to add smth like that into documentation?