Skip to content

feat(eslint-config): add support for type imports #171

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@next/eslint-plugin-next": "15.0.3",
"@typescript-eslint/utils": "8.15.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "2.31.0",
"eslint-plugin-jsx-a11y": "6.10.2",
"eslint-plugin-prettier": "5.2.1",
"eslint-plugin-react": "7.37.2",
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-config/src/configs/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import globals from 'globals';

import { type FlatConfigArray } from '@/types';

import { importSortPluginConfig } from '@/plugins';
import { importPluginConfig, importSortPluginConfig } from '@/plugins';

export default [
eslint.configs.recommended,
importPluginConfig,
importSortPluginConfig,
{
name: 'commencis/base',
Expand Down
16 changes: 16 additions & 0 deletions packages/eslint-config/src/plugins/importPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-expect-error - eslint-plugin-import is not typed
import importPlugin from 'eslint-plugin-import';

import { FlatConfig } from '@/types';

import { importRules } from '@/rules';

export const importPluginConfig: FlatConfig = {
name: 'commencis/plugin:import',
plugins: {
import: importPlugin,
},
rules: {
...importRules,
},
};
1 change: 1 addition & 0 deletions packages/eslint-config/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './importPlugin';
export * from './importSortPlugin';
export * from './jsxA11yPlugin';
export * from './nextPlugin';
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-config/src/rules/importRules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Linter } from '@typescript-eslint/utils/ts-eslint';

export const importRules: Linter.RulesRecord = {
'import/no-duplicates': ['error', { 'prefer-inline': true }],
};
25 changes: 17 additions & 8 deletions packages/eslint-config/src/rules/importSortRules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Linter } from '@typescript-eslint/utils/ts-eslint';

const sliceDollarSign = (str: string): string =>
str.endsWith('$') ? str.slice(0, -1) : str;
const groupWithTypes = (regexes: string[]): string[] =>
regexes.map((regex) => [regex, `${sliceDollarSign(regex)}.*\\u0000$`]).flat();

export const importSortRules: Linter.RulesRecord = {
'simple-import-sort/imports': [
'error',
Expand All @@ -9,33 +14,37 @@ export const importSortRules: Linter.RulesRecord = {
['^\\u0000'],

// Main frameworks & libraries
[
groupWithTypes([
'^(react(-native|-dom)?(/.*)?)$',
'^next',
'^vue',
'^nuxt',
'^@angular(/.*|$)',
'^expo',
'^node',
],
]),

// External packages
['^@commencis', '^@?\\w'],
groupWithTypes(['^@commencis', '^@?\\w']),

// Internal common directories
['^@?/?(config|types|interfaces|constants|helpers|utils|lib)(/.*|$)'],
groupWithTypes([
'^@?/?(config|types|interfaces|constants|helpers|utils|lib)(/.*|$)',
]),

// Internal directories
['^@/'],
groupWithTypes(['^@/']),

// Components
['((.*)/)?(providers|layouts|pages|modules|features|components)/?'],
groupWithTypes([
'((.*)/)?(providers|layouts|pages|modules|features|components)/?',
]),

// Relative parent imports: '../' comes last
['^\\.\\.(?!/?$)', '^\\.\\./?$'],
groupWithTypes(['^\\.\\.(?!/?$)', '^\\.\\./?$']),

// Relative imports: './' comes last
['^\\./(?=.*/)(?!/?$)', '^\\.(?!/?$)', '^\\./?$'],
groupWithTypes(['^\\./(?=.*/)(?!/?$)', '^\\.(?!/?$)', '^\\./?$']),

// Styles
['^.+\\.(s?css|(style(s)?)\\..+)$'],
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/src/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './importRules';
export * from './importSortRules';
export * from './nextPluginRules';
export * from './reactHooksRules';
Expand Down
8 changes: 8 additions & 0 deletions packages/eslint-config/src/rules/typescriptRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { Linter } from '@typescript-eslint/utils/ts-eslint';

export const typescriptRules: Linter.RulesRecord = {
'@typescript-eslint/consistent-type-definitions': 'off',
'@typescript-eslint/consistent-type-imports': [
'error',
{
prefer: 'type-imports',
fixStyle: 'separate-type-imports',
},
],
'@typescript-eslint/no-import-type-side-effects': 'error',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/array-type': 'off',

Expand Down
Loading