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

Add import/order rule #180

Merged
merged 1 commit into from
Oct 31, 2023
Merged

Add import/order rule #180

merged 1 commit into from
Oct 31, 2023

Conversation

chriszarate
Copy link
Member

Add the import/order rule to enforce the import grouping and order of imports / requires. Below is therefore the enforced structure:

/**
 * External dependencies
 */
import { redis } from '@automattic/vip-go';
import addOne from 'addOne';
import { Buffer } from 'buffer';
import leftPad from 'leftPad';
import http from 'node:http';

/**
 * Internal dependencies
 */
import eventsLib from '@lib/modules/event-log/event-log';
import { AnomaliesService } from '@src/anomalies/anomalies.service';
import { EventsService } from '@src/events/events.service';

/**
 * Types
 */
import type { BlogDetail } from '@automattic/vip-go-node-internal';
import type { Repository } from '@nest/common';
import type { Anomaly } from '@src/anomalies/entities/anomaly.entity';
  • Ordering is based on the import / require path, not the imported tokens.
  • This rule does not enforce the use of comments, but it does enforce an extra newline between groups.
  • Because type imports are a treated as a different kind of import and because we are using the grouping feature of this rule, we have to assign type imports to a group. Rather than put them with external or internal, I've chosen to create a third group dedicated to type imports. Most files do not use type imports, so they will just not have a third import group.

Copy link
Contributor

@abdullah-kasim abdullah-kasim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're going to need to add path groups to cater for babel/tsconfig aliases. I couldn't find any docs as to whether import/order is reading tsconfig settings or not.

We could probably identify any paths that start with src or start with @ to be aliases EDIT: Maybe not @, node_modules has dibs on that for namespacing, and we're not alone on this issue.

P.S: Is it too late to switch over to ~ (unix has dibs on that)? Or $ ? 😂

@chriszarate
Copy link
Member Author

@abdullah-kasim Try it out in GOOP, the rule works well and picks up on tsconfig aliases. You can just copy and paste this into the .eslintrc, since the import plugin is already included in this package, nothing else is needed:

		// Enforce external / internal import groups and alphabetical ordering.
		'import/order': [
			'error',
			{
				'newlines-between': 'always',
				alphabetize: {
					order: 'asc',
				},
				groups: [
					[ 'builtin', 'external' ],
					[ 'index', 'internal', 'object', 'parent', 'sibling' ],
					[ 'type' ],
				],
			},
		],

@abdullah-kasim
Copy link
Contributor

Wow, that's interesting! Sorry about that, should've tested it first.

vip-go-api on  trunk [📝] took 10s 
[ 11:10:35 ] eslint /Users/uzer/Documents/Development/vip-go-api/src/inactive-users/inactive-users.e2e.spec.ts --fix

/Users/uzer/Documents/Development/vip-go-api/src/inactive-users/inactive-users.e2e.spec.ts
   59:3   warning  Disabled test                                                                      jest/no-disabled-tests
  158:21  warning  Identifier name 'inactiveUserWithIgnoreInactiveCheckInThePast' is too long (> 40)  id-length

✖ 2 problems (0 errors, 2 warnings)

before

import alerts from '@lib/utils/alerts';
import {
	createTestApp,
	createTestingModule,
	TestApp,
} from '@src/shared/test-utils/modules.test-utils';
import { InactiveUsersRepository } from '@src/inactive-users/inactive-users.repository';
import { expectErrorResponseBody } from '@src/shared/test-utils/expect.test-utils';
import { AppModule } from '@src/app.module';
import { createApiUser, createApiUserToken } from '@fixtures/api-user/api-user';
import { createClient } from '@fixtures/client/client';
import moment from 'moment/moment';
import testUtils from '@fixtures/global-setup/test-utils';
import { UsersRepository } from '@src/users/users.repository';
import { EventsService } from '@src/events/events.service';

after

import moment from 'moment/moment';

import { createApiUser, createApiUserToken } from '@fixtures/api-user/api-user';
import { createClient } from '@fixtures/client/client';
import testUtils from '@fixtures/global-setup/test-utils';
import alerts from '@lib/utils/alerts';
import { AppModule } from '@src/app.module';
import { EventsService } from '@src/events/events.service';
import { InactiveUsersRepository } from '@src/inactive-users/inactive-users.repository';
import { expectErrorResponseBody } from '@src/shared/test-utils/expect.test-utils';
import {
	createTestApp,
	createTestingModule,
	TestApp,
} from '@src/shared/test-utils/modules.test-utils';
import { UsersRepository } from '@src/users/users.repository';

Copy link

@maxschmeling maxschmeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. I propose that we either find a way to automatically insert and enforce the comment or comments ( hopefully not 3 lines each ) or we don't use them at all. I'm personally 100% ok with no comments, especially with the line break and automatic grouping.

@chriszarate chriszarate merged commit 329a5fe into trunk Oct 31, 2023
4 checks passed
@chriszarate chriszarate deleted the add/import-order branch October 31, 2023 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants