Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] order: allow controlling intragroup spacing of type-only imports via newlines-between-types #3127

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Xunnamius
Copy link
Contributor

Depends on #3104

This PR implements in import/order: a new option analogous to newlines-between but specifically for controlling newlines between type-only imports.

A demo package containing this feature is temporarily available for easy testing:

npm install --save-dev eslint-plugin-import@npm:@-xun/eslint-plugin-import-experimental

Allow controlling intragroup spacing of type-only imports

This is implemented via newlines-between-types. The proposed documentation corresponding to this new feature can be previewed here.

Example

Given this code:

import type A from 'fs';
import type B from 'path';
import type C from '../foo.js';
import type D from './bar.js';
import type E from './';

import a from 'fs';
import b from 'path';

import c from '../foo.js';

import d from './bar.js';

import e from './';

And the following settings, the rule check will fail:

{
  "groups": ["type", "builtin", "parent", "sibling", "index"],
  "sortTypesAmongThemselves": true,
  "newlines-between": "always"
}

With --fix yielding:

import type A from 'fs';
import type B from 'path';

import type C from '../foo.js';

import type D from './bar.js';

import type E from './';

import a from 'fs';
import b from 'path';

import c from '../foo.js';

import d from './bar.js';

import e from './';

However, with the following settings, the rule check will succeed instead:

{
  "groups": ["type", "builtin", "parent", "sibling", "index"],
  "sortTypesAmongThemselves": true,
  "newlines-between": "always",
+ "newlines-between-types": "ignore"
}

sortTypesAmongThemselves allows sorting type-only and normal imports separately. By default, newlines-between will govern all newlines between import statements like normal.

I generally want my type-only imports to be sorted for ease of reference but never have newlines between them (save space) while I want my normal imports (which I tend to visually peruse more often) to be aesthetically pleasing, grouped, and sorted. newlines-between is too coarse-grained for this, so this PR introduces newlines-between-types, a setting identical to newlines-between except it only applies to type-only imports, and only when sortTypesAmongThemselves is enabled (i.e. it is backward-compatible).

When newlines-between and newlines-between-types conflict, newlines-between-types takes precedence for type-only imports. For normal imports, newlines-between-types is ignored entirely.

One issue that might warrant further discussion is which setting governs the newline separating type-only imports from normal imports. Right now, I have it so newlines-between-types controls this space, but perhaps it should be its own setting.

Copy link

codecov bot commented Dec 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.30%. Comparing base (8b2d570) to head (eba9746).

❗ There is a different number of reports uploaded between BASE (8b2d570) and HEAD (eba9746). Click for more details.

HEAD has 4 uploads less than BASE
Flag BASE (8b2d570) HEAD (eba9746)
6 2
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3127      +/-   ##
==========================================
- Coverage   95.04%   85.30%   -9.74%     
==========================================
  Files          84       84              
  Lines        3634     3662      +28     
  Branches     1279     1302      +23     
==========================================
- Hits         3454     3124     -330     
- Misses        180      538     +358     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ljharb ljharb marked this pull request as draft December 23, 2024 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant