Closed neodon closed 3 years ago
@neodon
Thanks for your feedback!
I think this one is duplicated to https://github.com/daidodo/tsimportsorter/issues/18 (Closed). But I'm happy to take any arguments since many people seem to ask the same question.
Thank you for the response. I read through the discussions. I'll echo the sentiment that it would be great to have an option to disable this behavior. It seems that it's only necessary to support an anti-pattern of having conflicting package imports (e.g. foo.js
+ foo/index.js
both exist). I've never seen this in the wild (though I'm sure a few people do it) and have only encountered it once myself when I made an obvious mistake of converting a module file into a directory with an index.js and forgot to delete the old module file.
I'd rather the extension at least let me set an option so it does not "correct" my code by making it worse in order to support a (likely rare) anti-pattern / abuse of module paths that I would never use anyway.
Edit: Also, I can't see why specifically avoiding changing dot alias paths like .
to ./
would be an issue. I could be missing something, am I?
Path normalisation is necessary for merging imports and exports, so we can format:
import { A } from "./a";
import { B } from "././a";
import { C } from ".";
import { D } from "./.";
to
import { A, B } from "./a";
import { C, D } from "./";
The only question is whether to keep the final /
for .
, ..
, etc.
I agree that we can add an option for .
, ..
, etc, that:
At a first glance I think the core logic would be in normalizePath, along with other code for a new configuration.
I'd be appreciative if you could pick it up and help improve the extension. Thanks!
Thank you! I appreciate the openness and guidance. I will see what I can do.
Hi @neodon,
Have you started working on this issue?
This issue is the next priority of my current project eslint-plugin-import Compatibility. So I'm happy to work on it if you haven't started.
Thanks!
I've only updated some tests, so feel free to take it in another direction. Here is the branch: https://github.com/neodon/tsimportsorter/tree/issue-28-minimize-paths
@neodon, thanks for sharing!
This is a community project so please feel no pressure.
And if you like you can pick up any items from eslint-plugin-import Compatibility or TODO list. The CONTRIBUTING.md could be a good start point in case you haven't noticed.
And it's also ok if you just want to use the extension and have fun!
Cheers!
Hi @neodon, this issue should be resolved by the latest release v5.0.0.
Now you can set removeLastSlashInPath
and removeLastIndexInPath
in your config if you'd like useless /
and index
to be removed.
Furthermore, you can just set up ESLint to enable import/no-useless-path-segments which has the same effect.
See also:
Release notes
Wiki about how no-useless-path-segments
is handled
I'll close the issue. But feel free to reopen it if you have further questions. Thanks!
First, thank you for taking over this extension and improving it. I was sorry to see TypeScript Import Sorter become unmaintained. I'm glad to have an up-to-date alternative because it saves my team a lot of hassle and code conflicts with ordering of imports.
Describe the bug When I need to import the default module in a folder from another module in the same folder, I typically do it with:
This extension automatically replaces that import with:
Importing from
'./'
(and even'./index'
) triggers linting errors due to it being redundant ('.'
is sufficient):Note: This happens with both JavaScript and TypeScript.
To Reproduce Steps to reproduce the behavior:
src
namedindex.js
. Inindex.js
, put this code:src
namedcli.js
. Incli.js
, put this code:foo.js
or otherwise trigger the import sorting extension.Expected behavior I expect the import path to be the shortest/simplest path necessary to successfully import the module. In this case, it should stay as
'.'
.Actual behavior The import path is always changed to
'./'
, which triggers linting errors for redundancy in the path.Screenshots If applicable, add screenshots to help explain your problem.
OS (please complete the following information):
VS Code (please complete the following information):
repo with reproduction of issue https://github.com/neodon/tsimportsorter-issue
Installed VS Code extensions I disabled all extensions except JS/TS Import/Export Sorter v3.0.4
I appreciate any discussion, and if there is agreement that this is a bug, I would be interested in submitting a pull request. Thank you.
Edit: After looking at the tests for path normalization and giving it some more thought, I think the fix for this bug would be limited to paths like
.
,..
,../..
, etc and not something like./foo/
. The reason is that you might have bothfoo.js
andfoo/index.js
, so the trailing slash in those cases is needed to differentiate.