Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSLint: ordered-imports configuration

I'm trying to configure my TSLint rule ordered-imports to get the import order looking like this:

// 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.

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

like image 987
alexmngn Avatar asked Jun 04 '19 10:06

alexmngn


2 Answers

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
  • Added .* to the end of all the matchers. 🤷‍♂️
like image 67
Josh Avatar answered Nov 10 '22 06:11

Josh


 "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 }
    ]
like image 2
Talal Siddiqui Avatar answered Nov 10 '22 07:11

Talal Siddiqui