英文:
How to put import type always as last one with eslint-plugin-import
问题
我有一个React Native项目,在其中我使用以以下方式开头的绝对路径
``` components/ features/ or infrastructure/ ```
我希望它们与节点模块导入分开,但我希望
```import type {xxx} from 'features|components|infrastructure|';```
始终在组内的最后,甚至最好是在整个导入部分中所有的import type都在最后,并且最好按字母顺序排序。
到目前为止,我想到了这样一个配置
module.exports = {
root: true,
extends: ['@react-native-community'],
plugins: ['import'],
rules: {
'import/order': [
'error',
{
groups: [
['builtin', 'external'],
'internal',
'parent',
['sibling', 'index'],
'object',
'type',
],
pathGroups: [
{
pattern: '@(react|react-native)',
group: 'external',
position: 'before',
},
{
pattern: 'components/',
group: 'internal',
},
{
pattern: 'features/',
group: 'internal',
},
{
pattern: 'infrastructure/**',
group: 'internal',
},
],
pathGroupsExcludedImportTypes: ['react'],
'newlines-between': 'always',
alphabetize: {
order: 'asc',
caseInsensitive: true,
},
},
],
},
};
但这里的问题是它没有区分import和import type,并且它将导入放置如下
import React from 'react';
import { View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import RegistrationScreen from 'features/account/screens/Registration';
import type { Test } from 'features/account/types';
import { colors } from 'infrastructure/theme';
谢谢。
英文:
I have a react native project in which I use absolute paths starting with
components/ features/ or infrastructure/
I wanted them to be separated from the node modules imports but I want to
import type {xxx} from 'features|components|infrastructure|';
to always go last within a group, or even better to all the import type to always go last in the entire imports section and preferably sorted alphabetically.
So far I came up with such a config
module.exports = {
root: true,
extends: ['@react-native-community'],
plugins: ['import'],
rules: {
'import/order': [
'error',
{
groups: [
['builtin', 'external'],
'internal',
'parent',
['sibling', 'index'],
'object',
'type',
],
pathGroups: [
{
pattern: '@(react|react-native)',
group: 'external',
position: 'before',
},
{
pattern: 'components/**',
group: 'internal',
},
{
pattern: 'features/**',
group: 'internal',
},
{
pattern: 'infrastructure/**',
group: 'internal',
},
],
pathGroupsExcludedImportTypes: ['react'],
'newlines-between': 'always',
alphabetize: {
order: 'asc',
caseInsensitive: true,
},
},
],
},
};
but the problem here is that it does not separate import and import type and it puts imports like so
import React from 'react';
import { View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import RegistrationScreen from 'features/account/screens/Registration';
import type { Test } from 'features/account/types';
import { colors } from 'infrastructure/theme';
Thanks.
答案1
得分: 2
"import/order": [
"error",
{
groups: ["内置模块", "外部模块", "内部模块", "父级模块", "同级模块", "索引模块", "对象模块", "类型模块"],
},
],
英文:
Use groups and put type
in the end
"import/order": [
"error",
{
groups: ["builtin", "external", "internal", "parent", "sibling", "index", "object", "type"],
},
],
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论