Skip to content

Commit

Permalink
feat: enable perfectionist/sort-imports et al, close #456
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 6, 2024
1 parent 2427b1b commit a6efa09
Show file tree
Hide file tree
Showing 42 changed files with 146 additions and 67 deletions.
1 change: 1 addition & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-expect-error missing types
import styleMigrate from '@stylistic/eslint-plugin-migrate'

import { antfu } from './src'

export default antfu(
Expand Down
4 changes: 3 additions & 1 deletion scripts/typegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fs from 'node:fs/promises'
import { flatConfigsToRulesDTS } from 'eslint-typegen/core'

import { builtinRules } from 'eslint/use-at-your-own-risk'
import { flatConfigsToRulesDTS } from 'eslint-typegen/core'

import { astro, combine, comments, formatters, imports, javascript, jsdoc, jsonc, jsx, markdown, node, perfectionist, react, regexp, solid, sortPackageJson, stylistic, svelte, test, toml, typescript, unicorn, unocss, vue, yaml } from '../src'

const configs = await combine(
Expand Down
2 changes: 2 additions & 0 deletions src/cli/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import c from 'picocolors'

import pkgJson from '../../package.json'

import type { ExtraLibrariesOption, FrameworkOption, PromItem } from './types'

export { pkgJson }
Expand Down
8 changes: 5 additions & 3 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import process from 'node:process'

import * as p from '@clack/prompts'
import c from 'picocolors'
import { hideBin } from 'yargs/helpers'
import yargs from 'yargs'
import * as p from '@clack/prompts'
import { run } from './run'
import { hideBin } from 'yargs/helpers'

import { pkgJson } from './constants'
import { run } from './run'

function header(): void {
// eslint-disable-next-line no-console
Expand Down
10 changes: 6 additions & 4 deletions src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import c from 'picocolors'

import * as p from '@clack/prompts'
import c from 'picocolors'

import { extra, extraOptions, frameworkOptions, frameworks } from './constants'
import { isGitClean } from './utils'
import type { ExtraLibrariesOption, FrameworkOption, PromItem, PromptResult } from './types'
import { updatePackageJson } from './stages/update-package-json'
import { updateEslintFiles } from './stages/update-eslint-files'
import { updatePackageJson } from './stages/update-package-json'
import { updateVscodeSettings } from './stages/update-vscode-settings'
import { isGitClean } from './utils'

import type { ExtraLibrariesOption, FrameworkOption, PromItem, PromptResult } from './types'

export interface CliRunOptions {
/**
Expand Down
8 changes: 5 additions & 3 deletions src/cli/stages/update-eslint-files.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import fs from 'node:fs'
import fsp from 'node:fs/promises'
import process from 'node:process'
import path from 'node:path'
import c from 'picocolors'
import * as p from '@clack/prompts'
import process from 'node:process'

import * as p from '@clack/prompts'
// @ts-expect-error missing types
import parse from 'parse-gitignore'
import c from 'picocolors'

import { getEslintConfigContent } from '../utils'

import type { PromptResult } from '../types'

export async function updateEslintFiles(result: PromptResult): Promise<void> {
Expand Down
6 changes: 4 additions & 2 deletions src/cli/stages/update-package-json.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import path from 'node:path'
import fsp from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import c from 'picocolors'

import * as p from '@clack/prompts'
import c from 'picocolors'

import { dependenciesMap, pkgJson } from '../constants'

import type { ExtraLibrariesOption, PromptResult } from '../types'

export async function updatePackageJson(result: PromptResult): Promise<void> {
Expand Down
8 changes: 5 additions & 3 deletions src/cli/stages/update-vscode-settings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import path from 'node:path'
import fsp from 'node:fs/promises'
import fs from 'node:fs'
import fsp from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import c from 'picocolors'

import * as p from '@clack/prompts'
import c from 'picocolors'

import { vscodeSettingsString } from '../constants'

import type { PromptResult } from '../types'

export async function updateVscodeSettings(result: PromptResult): Promise<void> {
Expand Down
3 changes: 2 additions & 1 deletion src/configs/astro.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { OptionsFiles, OptionsOverrides, OptionsStylistic, TypedFlatConfigItem } from '../types'
import { GLOB_ASTRO } from '../globs'
import { interopDefault } from '../utils'

import type { OptionsFiles, OptionsOverrides, OptionsStylistic, TypedFlatConfigItem } from '../types'

export async function astro(
options: OptionsOverrides & OptionsStylistic & OptionsFiles = {},
): Promise<TypedFlatConfigItem[]> {
Expand Down
1 change: 1 addition & 0 deletions src/configs/command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import createCommand from 'eslint-plugin-command/config'

import type { TypedFlatConfigItem } from '../types'

export async function command(): Promise<TypedFlatConfigItem[]> {
Expand Down
3 changes: 2 additions & 1 deletion src/configs/comments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TypedFlatConfigItem } from '../types'
import { pluginComments } from '../plugins'

import type { TypedFlatConfigItem } from '../types'

export async function comments(): Promise<TypedFlatConfigItem[]> {
return [
{
Expand Down
1 change: 1 addition & 0 deletions src/configs/disables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GLOB_SRC, GLOB_SRC_EXT } from '../globs'

import type { TypedFlatConfigItem } from '../types'

export async function disables(): Promise<TypedFlatConfigItem[]> {
Expand Down
6 changes: 4 additions & 2 deletions src/configs/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { isPackageExists } from 'local-pkg'

import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_SVG, GLOB_XML } from '../globs'
import type { VendoredPrettierOptions } from '../vender/prettier-types'
import { ensurePackages, interopDefault, isPackageInScope, parserPlain } from '../utils'
import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem } from '../types'
import { StylisticConfigDefaults } from './stylistic'

import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem } from '../types'
import type { VendoredPrettierOptions } from '../vender/prettier-types'

export async function formatters(
options: OptionsFormatters | true = {},
stylistic: StylisticConfig = {},
Expand Down
3 changes: 2 additions & 1 deletion src/configs/ignores.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TypedFlatConfigItem } from '../types'
import { GLOB_EXCLUDE } from '../globs'

import type { TypedFlatConfigItem } from '../types'

export async function ignores(userIgnores: string[] = []): Promise<TypedFlatConfigItem[]> {
return [
{
Expand Down
4 changes: 2 additions & 2 deletions src/configs/imports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { OptionsStylistic, TypedFlatConfigItem } from '../types'
import { pluginAntfu, pluginImport } from '../plugins'

import type { OptionsStylistic, TypedFlatConfigItem } from '../types'

export async function imports(options: OptionsStylistic = {}): Promise<TypedFlatConfigItem[]> {
const {
stylistic = true,
Expand All @@ -24,7 +25,6 @@ export async function imports(options: OptionsStylistic = {}): Promise<TypedFlat
'import/no-named-default': 'error',
'import/no-self-import': 'error',
'import/no-webpack-loader-syntax': 'error',
'import/order': 'error',

...stylistic
? {
Expand Down
6 changes: 3 additions & 3 deletions src/configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
export * from './astro'
export * from './command'
export * from './comments'
export * from './disables'
export * from './formatters'
export * from './ignores'
export * from './imports'
export * from './javascript'
export * from './jsdoc'
export * from './jsx'
export * from './jsonc'
export * from './jsx'
export * from './markdown'
export * from './node'
export * from './perfectionist'
export * from './react'
export * from './regexp'
export * from './solid'
export * from './sort'
export * from './stylistic'
Expand All @@ -23,5 +25,3 @@ export * from './unicorn'
export * from './unocss'
export * from './vue'
export * from './yaml'
export * from './regexp'
export * from './disables'
15 changes: 3 additions & 12 deletions src/configs/javascript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import globals from 'globals'
import type { OptionsIsInEditor, OptionsOverrides, TypedFlatConfigItem } from '../types'

import { pluginAntfu, pluginUnusedImports } from '../plugins'

import type { OptionsIsInEditor, OptionsOverrides, TypedFlatConfigItem } from '../types'

export async function javascript(
options: OptionsIsInEditor & OptionsOverrides = {},
): Promise<TypedFlatConfigItem[]> {
Expand Down Expand Up @@ -186,17 +188,6 @@ export async function javascript(
'prefer-rest-params': 'error',
'prefer-spread': 'error',
'prefer-template': 'error',
'sort-imports': [
'error',
{
allowSeparatedGroups: false,
ignoreCase: false,
ignoreDeclarationSort: true,
ignoreMemberSort: false,
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
},
],

'symbol-description': 'error',
'unicode-bom': ['error', 'never'],
'unused-imports/no-unused-imports': isInEditor ? 'off' : 'error',
Expand Down
1 change: 1 addition & 0 deletions src/configs/jsdoc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { interopDefault } from '../utils'

import type { OptionsStylistic, TypedFlatConfigItem } from '../types'

export async function jsdoc(options: OptionsStylistic = {}): Promise<TypedFlatConfigItem[]> {
Expand Down
3 changes: 2 additions & 1 deletion src/configs/jsonc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { OptionsFiles, OptionsOverrides, OptionsStylistic, TypedFlatConfigItem } from '../types'
import { GLOB_JSON, GLOB_JSON5, GLOB_JSONC } from '../globs'
import { interopDefault } from '../utils'

import type { OptionsFiles, OptionsOverrides, OptionsStylistic, TypedFlatConfigItem } from '../types'

export async function jsonc(
options: OptionsFiles & OptionsStylistic & OptionsOverrides = {},
): Promise<TypedFlatConfigItem[]> {
Expand Down
3 changes: 2 additions & 1 deletion src/configs/jsx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TypedFlatConfigItem } from '../types'
import { GLOB_JSX, GLOB_TSX } from '../globs'

import type { TypedFlatConfigItem } from '../types'

export async function jsx(): Promise<TypedFlatConfigItem[]> {
return [
{
Expand Down
4 changes: 3 additions & 1 deletion src/configs/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { mergeProcessors, processorPassThrough } from 'eslint-merge-processors'
import type { OptionsComponentExts, OptionsFiles, OptionsOverrides, TypedFlatConfigItem } from '../types'

import { GLOB_MARKDOWN, GLOB_MARKDOWN_CODE, GLOB_MARKDOWN_IN_MARKDOWN } from '../globs'
import { interopDefault, parserPlain } from '../utils'

import type { OptionsComponentExts, OptionsFiles, OptionsOverrides, TypedFlatConfigItem } from '../types'

export async function markdown(
options: OptionsFiles & OptionsComponentExts & OptionsOverrides = {},
): Promise<TypedFlatConfigItem[]> {
Expand Down
3 changes: 2 additions & 1 deletion src/configs/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TypedFlatConfigItem } from '../types'
import { pluginNode } from '../plugins'

import type { TypedFlatConfigItem } from '../types'

export async function node(): Promise<TypedFlatConfigItem[]> {
return [
{
Expand Down
24 changes: 22 additions & 2 deletions src/configs/perfectionist.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { TypedFlatConfigItem } from '../types'
import { pluginPerfectionist } from '../plugins'

import type { TypedFlatConfigItem } from '../types'

/**
* Optional perfectionist plugin for props and items sorting.
* Perfectionist plugin for props and items sorting.
*
* @see https://github.com/azat-io/eslint-plugin-perfectionist
*/
Expand All @@ -13,6 +14,25 @@ export async function perfectionist(): Promise<TypedFlatConfigItem[]> {
plugins: {
perfectionist: pluginPerfectionist,
},
rules: {
'perfectionist/sort-exports': ['error', { order: 'asc', type: 'natural' }],
'perfectionist/sort-imports': ['error', {
groups: [
'builtin',
'external',
'type',
['internal', 'internal-type'],
['parent', 'sibling', 'index'],
['parent-type', 'sibling-type', 'index-type'],
'object',
'unknown',
],
order: 'asc',
type: 'natural',
}],
'perfectionist/sort-named-exports': ['error', { order: 'asc', type: 'natural' }],
'perfectionist/sort-named-imports': ['error', { order: 'asc', type: 'natural' }],
},
},
]
}
4 changes: 3 additions & 1 deletion src/configs/react.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { isPackageExists } from 'local-pkg'

import { GLOB_SRC } from '../globs'
import { ensurePackages, interopDefault, toArray } from '../utils'

import type { OptionsFiles, OptionsOverrides, OptionsTypeScriptWithTypes, TypedFlatConfigItem } from '../types'
import { GLOB_SRC } from '../globs'

// react refresh
const ReactRefreshAllowConstantExportPackages = [
Expand Down
1 change: 1 addition & 0 deletions src/configs/regexp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { configs } from 'eslint-plugin-regexp'

import type { OptionsOverrides, OptionsRegExp, TypedFlatConfigItem } from '../types'

export async function regexp(
Expand Down
3 changes: 2 additions & 1 deletion src/configs/solid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GLOB_JSX, GLOB_TSX } from '../globs'
import { ensurePackages, interopDefault, toArray } from '../utils'

import type { OptionsFiles, OptionsHasTypeScript, OptionsOverrides, OptionsTypeScriptWithTypes, TypedFlatConfigItem } from '../types'
import { GLOB_JSX, GLOB_TSX } from '../globs'

export async function solid(
options: OptionsHasTypeScript & OptionsOverrides & OptionsFiles & OptionsTypeScriptWithTypes = {},
Expand Down
3 changes: 2 additions & 1 deletion src/configs/stylistic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { pluginAntfu } from '../plugins'
import { interopDefault } from '../utils'

import type { OptionsOverrides, StylisticConfig, TypedFlatConfigItem } from '../types'
import { pluginAntfu } from '../plugins'

export const StylisticConfigDefaults: StylisticConfig = {
indent: 2,
Expand Down
3 changes: 2 additions & 1 deletion src/configs/svelte.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GLOB_SVELTE } from '../globs'
import { ensurePackages, interopDefault } from '../utils'

import type { OptionsFiles, OptionsHasTypeScript, OptionsOverrides, OptionsStylistic, TypedFlatConfigItem } from '../types'
import { GLOB_SVELTE } from '../globs'

export async function svelte(
options: OptionsHasTypeScript & OptionsOverrides & OptionsStylistic & OptionsFiles = {},
Expand Down
3 changes: 2 additions & 1 deletion src/configs/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GLOB_TESTS } from '../globs'
import { interopDefault } from '../utils'

import type { OptionsFiles, OptionsIsInEditor, OptionsOverrides, TypedFlatConfigItem } from '../types'
import { GLOB_TESTS } from '../globs'

// Hold the reference so we don't redeclare the plugin on each call
let _pluginTest: any
Expand Down
3 changes: 2 additions & 1 deletion src/configs/toml.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { OptionsFiles, OptionsOverrides, OptionsStylistic, TypedFlatConfigItem } from '../types'
import { GLOB_TOML } from '../globs'
import { interopDefault } from '../utils'

import type { OptionsFiles, OptionsOverrides, OptionsStylistic, TypedFlatConfigItem } from '../types'

export async function toml(
options: OptionsOverrides & OptionsStylistic & OptionsFiles = {},
): Promise<TypedFlatConfigItem[]> {
Expand Down
Loading

0 comments on commit a6efa09

Please sign in to comment.