aziznal/dart-import-sorter

Option for relative files to be the last ones

Closed this issue ยท 9 comments

If I want to sort a relative path import and have my own package also imported, the default configuration will put import '../../mocks/common.dart'; with an empty line before since it's a different group, which I don't want.

If I unify the two last groups (my packages and relative paths), now import '../../mocks/common.dart'; appears first before my packages.

Is there a way to configure it as desired?

Example

If I have:

import 'dart:convert';
import '../../mocks/common.dart';

import 'package:mockito/mockito.dart';

import 'package/<my-package>.dart';

The desired outcome would be:

import 'dart:convert';

import 'package:mockito/mockito.dart';

import 'package/<my-package>.dart';
import '../../mocks/common.dart';

Thanks!

Currently imports within the same group are simply sorted alphabetically. This is why the relative import appears before the package import.

I understand your use case. Basically, you want everything related to your package to be grouped together, and also keep the relative imports at the bottom.

I'll think of a solution and reply back here soon. There would basically be an extra config for sorting within groups I think.

Awesome! ๐Ÿ˜„ Thanks ๐Ÿ™Œ

Also, for context, this is something that I would use since it is for a Flutter project where import_sorter is being used.

I'm thinking of making it possible to add the same config of regex-based import sorting to groups as well.

Basically, this is how the config would look:

{
    "dartimportsorter.matchingRules": [
        {
            "label": "Flutter",
            "regex": "^package:flutter.*$",
            "order": 1,
            "regexFlags": ["m"],
        },
        {
            "label": "Dart",
            "regex": "^dart:.*$",
            "order": 2,
            "regexFlags": ["m"]
        },
		{
	        "label": "Package imports that ARE your app",
	        "regex": "^package:<app_name>.*$",
	        "order": 101,
	        "regexFlags": ["m"],
			"groupSortingRules": [
				{
					"label": "package imports",
					"regex": "^package.*$",
					"suborder": 1
				},
				{
					"label": "relative imports",
					"regex": "^\\..*$",
					"suborder": 10
				}
			]
	    },
        {
            "label": "Everything else",
            "regex": ".*",
            "order": 3,
            "regexFlags": ["m"]
        }
    ]
}

That way, you could (optionally) sort imports within the same group exactly how you want.

Hey. I just made a release. Check it out here. Let me know how it works out for you!

Thank you! ๐Ÿ˜„ I might be doing something wrong, because I cannot get it to work ๐Ÿค”

Using the updated configuration from the extension README:

[
    {
        "label": "Dart",
        "regex": "^dart:.*$",
        "regexFlags": ["m"],
        "order": 1,
    },
    {
        "label": "Flutter",
        "regex": "^package:flutter/.*$",
        "regexFlags": ["m"],
        "order": 10,
    },
    {
        "label": "Package imports that are NOT your app",
        "regex": "^package:(?!<app_name>).*$",
        "regexFlags": ["m"],
        "order": 100,
    },
    {
        "label": "Package imports that ARE your app as well as relative imports",
        "regex": "^package:<app_name>.*$|^\\..*$",
        "regexFlags": ["m"],
        "order": 101,
        "subgroupSortingRules": [
            {
                "label": "Package imports that ARE your app",
                "regex": "^package:<app_name>.*$",
                "regexFlags": ["m"],
                "order": 1,
            },
            {
                "label": "Relative",
                "regex": "^\\..*$",
                "regexFlags": ["m"],
                "order": 2,
            }
    }
]

And following my example, if I have:

import 'dart:convert';
import '../../mocks/common.dart';

import 'package:mockito/mockito.dart';

import 'package/<my-package>.dart';

It still gets sorted as:

import 'dart:convert';

import 'package:mockito/mockito.dart';

import '../../mocks/common.dart';
import 'package/<my-package>.dart';

I have v0.3.2 installed, let me know if you need any more information :)

And again, thank you!

(deleted the comment before after double-checking the README)

I did a little digging in. I found an issue in the code which is preventing user rules from being parsed correctly. Pushing a fix soon. (also there are missing parentheses in the readme. sorry ๐Ÿ™ˆ also fixing those in a giffy)

Just made a release and tested it out. All works fine on my end now.

You should try with this config:

    "dartimportsorter.matchingRules": [
        {
            "label": "Dart",
            "regex": "^dart:.*$",
            "regexFlags": [
                "m"
            ],
            "order": 1,
        },
        {
            "label": "Flutter",
            "regex": "^package:flutter/.*$",
            "regexFlags": [
                "m"
            ],
            "order": 10,
        },
        {
            "label": "Package imports that are NOT your app",
            "regex": "^package:(?!<app_name>).*$",
            "regexFlags": [
                "m"
            ],
            "order": 100,
        },
        {
            "label": "Package imports that ARE your app as well as relative imports",
            "regex": "^(package:<app_name>|\\.).*$",
            "regexFlags": [
                "m"
            ],
            "order": 101,
            "subgroupSortingRules": [
                {
                    "label": "Package imports",
                    "regex": "^package:<app_name>.*$",
                    "order": 1,
                },
                {
                    "label": "Relative",
                    "regex": "^\\..*$",
                    "order": 2,
                },
            ]
        }
    ]

Specifically, make sure your app name is set correctly in the import itself.

I'll keep this issue open till I get your feedback

It does work now indeed! ๐ŸŽ‰ Thanks a lot for your help and improvements :) will be using the extension even more now ๐Ÿ˜„