Typescript TSLint:有序导入配置

Typescript TSLint:有序导入配置,typescript,tslint,Typescript,Tslint,我正在尝试配置我的TSLint规则有序导入,以获得如下所示的导入订单: // React import React from 'react'; import { View } from 'react-native'; // Libs import * as _ from 'lodash'; import * as moment from 'moment'; // Internal libs import Bar from '@app/bar'; import Foo from '@app/f

我正在尝试配置我的TSLint规则
有序导入
,以获得如下所示的导入订单:

// React
import React from 'react';
import { View } from 'react-native';

// Libs
import * as _ from 'lodash';
import * as moment from 'moment';

// Internal libs
import Bar from '@app/bar';
import Foo from '@app/foo';

// Relative paths
import { Element } from './my-element';
import MyFunction from './my-function';

这是我试图创建的规则,但我仍然无法达到上述效果

除了
react
之外,我似乎无法匹配绝对导入,我尝试使用
null
以及像
^这样的非react匹配!作出反应
,但它不起作用

这是我的规则

{
  "ordered-imports": [
      true,
      {
        "module-source-path": "full",
        "grouped-imports": true,
        "groups": [
          {
            "name": "react",
            "match": "^react",
            "order": 1
          },
          {
            "name": "node_modules",
            "match": null,
            "order": 2
          },
          {
            "name": "internal modules",
            "match": "^@app",
            "order": 3
          },
          {
            "name": "relative dir",
            "match": "^[.]",
            "order": 4
          }
        ]
      }
    ]
}

有人能帮我找出我做错了什么吗


谢谢这对你有用吗

{
  "rules": {
    "ordered-imports": [
      true,
      {
        "module-source-path": "full",
        "grouped-imports": true,
        "groups": [
          {
            "name": "react",
            "match": "^react.*",
            "order": 1
          },
          {
            "name": "internal modules",
            "match": "^@app.*",
            "order": 3
          },
          {
            "name": "relative dir",
            "match": "^[.].*",
            "order": 4
          },
          {
            "name": "node_modules",
            "match": ".*",
            "order": 2
          }
        ]
      }
    ]
  }
}
两个变化:

  • 将“null”匹配器移动到列表的末尾,因此只有在没有其他匹配器的情况下,它才会始终匹配

  • 在所有匹配器的末尾添加了
    *
    。伟大的我花了一些时间才意识到,您不必按照需要导入的顺序定义组。这对于我的角度应用程序来说非常适合。从
    ^react.*
    到“^@angular”有一点变化*
     "groups": [
          { "name": "react", "match": "^react.*", "order": 1 },
          { "name": "pacakges", "match": "^app.*", "order": 20 },
          { "name": "components", "match": "^@.*", "order": 30 },
          { "name": "parent_dir", "match": "^\\.\\.", "order": 40 },
          { "name": "current_dir", "match": "^\\.", "order": 50 },
          { "name": "extra", "match": ".*", "order": 5 }
        ]