TSLint:有序导入配置

时间:2019-06-04 10:17:45

标签: typescript tslint

我正在尝试配置我的TSLint规则ordered-imports,以使导入顺序如下所示:

// 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
          }
        ]
      }
    ]
}

有人可以帮我弄清楚我在做什么错吗?

谢谢

2 个答案:

答案 0 :(得分:0)

这对您有用吗?

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

两项更改:

  • 将“空”匹配器移动到列表的末尾,因此只有在没有其他匹配器的情况下,它始终会匹配
  • 在所有匹配项的末尾添加了.*。 ?‍♂️

答案 1 :(得分:0)

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