如何使用eslint-plugin-import始终将import类型放在最后

huangapple go评论82阅读模式
英文:

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"],
  },
],

huangapple
  • 本文由 发表于 2023年2月18日 16:05:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定