简体   繁体   English

TSLint:ordered-imports配置

[英]TSLint: ordered-imports configuration

I'm trying to configure my TSLint rule ordered-imports to get the import order looking like this: 我正在尝试配置我的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';

This is the rule I've tried to create but I still can't get to a point where the above works. 这是我试图创建的规则,但我仍然无法达到上述工作的程度。

I don't seem to be able to match with absolute imports other than react ones, I've tried to use null as well as non-react match like ^!react but it doesn't work. 我似乎无法匹配除react之外的绝对导入,我试图使用null以及像^!react非反应匹配,但它不起作用。

This is my rules 这是我的规则

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

Can someone help me figuring out what I'm doing wrong? 有人能帮助我弄清楚我做错了什么吗?

Thanks 谢谢

Does this work for you? 这对你有用吗?

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

Two changes: 两个变化:

  • Moved the 'null' matcher to the end of the list, so it's always matched only if no others are 将'null'匹配器移动到列表的末尾,因此只有在没有其他匹配时才会匹配
  • Added .* to the end of all the matchers. 添加.*到所有匹配器的末尾。 🤷‍♂️ 🤷♂️
 "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 }
    ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM