Skip to content

Improper order of imports with Hyphens and Periods in the paths #87

@akaustav

Description

@akaustav

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions