Skip to content

✨ Feat: Update import order during commit#269

Merged
tyler-dane merged 3 commits intomainfrom
feat/import-formatting
Feb 22, 2025
Merged

✨ Feat: Update import order during commit#269
tyler-dane merged 3 commits intomainfrom
feat/import-formatting

Conversation

@tyler-dane
Copy link
Contributor

@tyler-dane tyler-dane commented Feb 22, 2025

This pull request introduces changes to the project's code formatting and linting configuration. The primary focus is the integration of a new Prettier plugin for sorting imports and the reorganization of ESLint configuration to accommodate this change.

closes #271
related to #149 and #257

Usage

When you commit your changes, imports will be automatically organized for the files you staged.
To have imports organized on save, you'll have to configure you editor manually.
Here is my VSCode config, which automatically organizes imports on save:

//settings.json
"editor.codeActionsOnSave": ["source.organizeImports", "source.fixAll.eslint", "source.formatDocument"],

Notice that I'm organizing imports before formatting.

Example

Given that this is the original file that you've staged for a commit:

//YourFile.tsx
import { Categories_Event, Schema_Event } from "@core/types/event.types";
import { ID_SOMEDAY_DRAFT } from "@web/common/constants/web.constants";

import { DraggableSomedayEvent } from "./Wrappers/DraggableSomedayEvent";
import { SidebarProps } from "@web/views/Calendar/hooks/draft/sidebar/useSidebar";
import { Schema_GridEvent } from "@web/common/types/web.event.types";
import { Droppable } from "@hello-pangea/dnd";
import React, { FC } from "react";

import { DraggableSomedayEvents } from "./Wrappers/DraggableSomedayEvents";

When you run git commit, the lint-staged script from package.json will run. This will trigger prettier, which will use the import rules from .prettierrc.json. The result will be:

//YourFile.tsx
// no spaces
import React, { FC } from "react";                               //third-party, no alias
import { Droppable } from "@hello-pangea/dnd";     //third-party, alias
import { Categories_Event, Schema_Event } from "@core/types/event.types";         //core first
import { ID_SOMEDAY_DRAFT } from "@web/common/constants/web.constants"; //web second
import { Schema_GridEvent } from "@web/common/types/web.event.types";
import { SidebarProps } from "@web/views/Calendar/hooks/draft/sidebar/useSidebar";
import { DraggableSomedayEvent } from "./Wrappers/DraggableSomedayEvent"; //local imports
import { DraggableSomedayEvents } from "./Wrappers/DraggableSomedayEvents";

Code formatting improvements:

  • .prettierrc: Added the @trivago/prettier-plugin-sort-imports plugin and defined import order rules to improve the consistency of import statements.

ESLint configuration updates:

  • eslint.config.mjs: Reorganized import statements and added the @trivago/prettier-plugin-sort-imports plugin to the ESLint configuration. This ensures that import statements are sorted according to the new rules defined in .prettierrc. [1] [2]

Dependency updates:

  • package.json: Added the @trivago/prettier-plugin-sort-imports package to the project dependencies.
  • package.json: Removed the eslint-plugin-import package, which is no longer needed with the new import sorting plugin.

@tyler-dane tyler-dane linked an issue Feb 22, 2025 that may be closed by this pull request
@tyler-dane tyler-dane marked this pull request as ready for review February 22, 2025 14:45
@tyler-dane tyler-dane merged commit 0882a8e into main Feb 22, 2025
3 checks passed
@tyler-dane tyler-dane deleted the feat/import-formatting branch February 22, 2025 14:46
@tyler-dane tyler-dane changed the title ✨ Update import order during commit ✨ Feat: Update import order during commit Feb 25, 2025
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.

✨ Sort Imports Automatically for better DX

1 participant