question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improper order of imports with Hyphens and Periods in the paths

See original GitHub issue

Hi @lydell,

Kudos

We are trying to convert an Angular 10 application from the deprecated tsLint dependency to using eslint. There were strong recommendations to use this eslint plugin. And I agree with the recommendations - your extensive research is well documented with every reasoning and finding there is to know about sorting import statements within Javascript / TypeScript files. Good work!

Issue

However, there is one thing bothering me while using this plugin. In my opinion, your plugin seems to incorrectly sort import statements which have the same set of characters from the beginning of the “from path string” followed by:

  1. a hyphen character (- a.k.a. the hyphen-minus unicode character U+002D)
  2. versus a period character (. a.k.a the full-stop unicode character U+002E).

Example from path strings

  1. 'my-package' vs 'my.package' (Common part = 'my)
  2. './widget/abc-def.tsx' vs './widget/abc.defgh.tsx' (Common part = './widget/abc)
  3. '../notification-list-routing.module.ts' vs '../notification-list.component.ts' (Common part = '../notification-list)

According to normal string sorting, hyphens come before periods. However, the default configuration of this plugin, seems to sort the from path strings containing periods first followed by the hyphen character.

Example

The following import sequence seems to be correct in my opinion (as well as TSLint’s ordered-import rule).

import { fileNameWithHyphens } from './file-with-hyphens.js';
import { fileNameWithPeriods } from './file.with.periods.js';

is auto-fixed into:

import { fileNameWithPeriods } from './file.with.periods.js';
import { fileNameWithHyphens } from './file-with-hyphens.js';

Sample Repository

To demonstrate this issue clearly, I have created a small sample plain-vanilla JavaScript-based NodeJS 14.x compatible application here: https://github.com/akaustav/import-order-test. The index.js file disables your plugin rule on line 1 - otherwise the simple-import-sort/imports rule keeps complaining about the order of the import lines on line 2 and 3. It even uses your documented comparator function - see this file.

Question

Is this expected behavior?

Ameet

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
akaustavcommented, Oct 2, 2021

@lydell - I think I understand your requirement a bit better now after looking at the sorting expectations outlined between lines 880 and 992 - why you wanted to sort . and / before other special characters. And I am starting to see the problem too - more to follow.

https://github.com/lydell/eslint-plugin-simple-import-sort/blob/7aa4e22b94adde01a66079d13cf48b3aaca9904d/test/imports.test.js#L880-L928

1reaction
lydellcommented, Oct 1, 2021

I will try to analyze each of the regular expressions and determine which code line might be causing this switch.

It’s the one with the switch:

https://github.com/lydell/eslint-plugin-simple-import-sort/blob/7aa4e22b94adde01a66079d13cf48b3aaca9904d/src/shared.js#L812-L826

version history

Have you looked into using a .git-blame-ignore-revs file?

For me personally, I don’t care about git history for imports. To me, imports are basically “useless” code that I collapse in my editor. I automatically import what’s needed. There’s no logic in them, so it’s not interesting to git blame.

It was flagged by one of our senior developers and the PR wouldn’t be approved until I fix this problem.

Note that tslint compatibility is not a goal of this plugin. I created this plugin before I even used TypeScript.

I think your next steps are:

  1. Research what can be done about this.
  2. Post your results here.
  3. Make a PR or fork this plugin. I might be able to fix this instead, depending on the results of your research. Note that if the code becomes much more complicated I might not be interested in merging.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it ok to use dashes in Python files when trying to import them?
It can be very helpful to use hyphens in Python directory and file names when you explicitly want to prevent imports. For example...
Read more >
Errors using special characters in Access databases - Office
Lists the special characters to avoid using when you work with the database object names or the field names in all versions of...
Read more >
eslint-plugin-import | Yarn - Package Manager
This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, and prevent issues with misspelling of file paths and import names.
Read more >
5.3: Proofreading for Punctuation – Communication at Work
1. Identify and correct punctuation errors involving commas, apostrophes, colons and semicolons, parentheses and brackets, quotation marks, hyphens and dashes, ...
Read more >
Fix error messages with Uploaded ads - Google Ads Help
The ad you uploaded includes a file path or file name with characters other than letters, numbers, periods ( . ), hyphens (...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found