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

问题描述 投票:0回答:2

我有一个反应本机项目,其中我使用以以下开头的绝对路径

components/ features/ or infrastructure/

我希望它们与节点模块导入分开,但我想

import type {xxx} from 'features|components|infrastructure|';

始终在组中排在最后,或者更好的是所有导入类型始终在整个导入部分排在最后,并且最好按字母顺序排序。

到目前为止我想出了这样的配置

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 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';

谢谢。

javascript reactjs typescript react-native eslint
2个回答
2
投票

使用组并将

type
放在最后

"import/order": [
  "error",
  {
    groups: ["builtin", "external", "internal", "parent", "sibling", "index", "object", "type"],
  },
],

0
投票

我可以通过在

type
选项中包含
pathGroupsExcludedImportTypes
来实现它。这告诉插件从路径组中排除类型导入。

pathGroupsExcludedImportTypes: [
  'builtin',
  'object',
  'type',
],
© www.soinside.com 2019 - 2024. All rights reserved.