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

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

谢谢

这对你有用吗?

{
  "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"匹配器移动到列表末尾,因此仅当没有其他匹配器时才始终匹配
  • 在所有匹配器的末尾添加了.*。 🤷 ♂️
 "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 }
    ]

最新更新