Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Rule manage absolute imports of a custom path as a vendor import #2

Closed
heavybeard opened this issue Mar 29, 2018 · 6 comments
Closed

Comments

@heavybeard
Copy link

First of all, thanks for your TSLint plugin, very useful.

I tried to order a custom path import after a relative import and the error appears, but it says "Import of node_modules must be higher than custom import".

Is possibile to manage custom path with a different rule? Or maybe, simply change the error message?
For example "Absolute import must be higher than relative import"

@heavybeard
Copy link
Author

Custom map from tsconfig.json

screen shot 2018-03-29 at 11 31 47

Tested imports

screen shot 2018-03-29 at 11 29 08

@marcuzy
Copy link
Owner

marcuzy commented Jun 8, 2018

It's a curious point. I've never used this feature. Seems, it's impossible to find out whether it's node_module or something from path section. Anyway, I'll take a look.

@jens-duttke
Copy link

@marcuzy: I've created a fork of your code, which implements support for aliases, like I need it:
https://github.com/jens-duttke/tslint-origin-ordered-imports-rule

The rule is: First absolute paths, then aliases (starting with "@"), then relative paths (starting with ".").
So this is correct:

import * as _ from 'lodash';

import MyAction from '@my-action';

import MyClass from './my-class';

But this is wrong:

import MyAction from '@my-action';
import * as _ from 'lodash';

import MyClass from './my-class';

It doesn't check if the alias links to a node_modules package, nor does it check if there is a node module which starts with an "@". But since both things are very unlikely, it works fine for me.

It would be nice if you could implement this, or a similar solution into your package.
Or even better: if it would be possible to configure that totally free in the tslint.json, e.g.

"origin-ordered-imports": [true, [
	"^@thisImportCanAlwaysBeOnTop$",	// (Reg ex) An import of a module matching this name, can always be the first one
	1,					// one-blank-line
	"^[A-Z]",				// (Reg ex) Modules starting with uppercase letters
	0,					// optional, to explicitly indicate no-blank-lines
	"^[a-z]",				// (Reg ex) Modules starting with lowercase letters
	1,					// one-blank-line
	"^@",					// (Reg ex) Modules starting with @
	1,					// one-blank-line
	"^\."					// (Reg ex) Modules starting with .
]],

@marcuzy
Copy link
Owner

marcuzy commented Nov 12, 2018

@jens-duttke I'll consider your idea, thanx. I found out that tslint provides access to the path section of tsconfig.json so it's possible to implement a support for aliases without any configuration. I see the only issue here, if the rule considers the path section then it'll need to read the project directory to be sure that files are existent. I hope, this solution can be optimized (ex. scan the project directory once and cache it or get access to tslint's tree of files if it's possible).

@mrmckeb
Copy link

mrmckeb commented Dec 2, 2018

I'd love this too, obviously the simpler the better.

One option is to use a no-implicit-dependencies-style whitelist for modules that would force them to be treated as local modules.

@marcuzy
Copy link
Owner

marcuzy commented Mar 18, 2019

🎉 Just published a beta version which supports custom rules 🎉.

You could try this:

npm i -D tslint-origin-ordered-imports-rule@beta

With tsconfig.json like this:

"rules": {
    "origin-ordered-imports": [ 
        true, 
        "one-blank-line",
        [
            "lib",
            "^@docebo/.+",
            "user"
        ] 
    ]
}

This config defines three import groups, their order and requires "one-blank-line" between them.
"lib" and "user" are built-in types which work like in previous versions. See updated docs.

Please, test the beta and write here any issues you will find. 🖖

@marcuzy marcuzy closed this as completed Mar 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants