Skip to content

Commit

Permalink
feat: introduce type options, enable `ts/explicit-function-return-t…
Browse files Browse the repository at this point in the history
…ype` for `lib`
  • Loading branch information
antfu committed Jul 17, 2024
1 parent c118b46 commit 3dd7b57
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 14 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ And that's it! Or you can configure each integration individually, for example:
import antfu from '@antfu/eslint-config'

export default antfu({
// Type of the project. 'lib' for libraries, the default is 'app'
type: 'lib',

// Enable stylistic formatting rules
// stylistic: true,

Expand All @@ -181,7 +184,7 @@ export default antfu({
quotes: 'single', // or 'double'
},

// TypeScript and Vue are auto-detected, you can also explicitly enable them:
// TypeScript and Vue are autoetected, you can also explicitly enable them:
typescript: true,
vue: true,

Expand Down
1 change: 1 addition & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default antfu(
astro: true,
typescript: true,
formatters: true,
type: 'lib',
},
{
ignores: [
Expand Down
2 changes: 1 addition & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as p from '@clack/prompts'
import { run } from './run'
import { pkgJson } from './constants'

function header() {
function header(): void {
// eslint-disable-next-line no-console
console.log('\n')
p.intro(`${c.green(`@antfu/eslint-config `)}${c.dim(`v${pkgJson.version}`)}`)
Expand Down
2 changes: 1 addition & 1 deletion src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface CliRunOptions {
extra?: string[]
}

export async function run(options: CliRunOptions = {}) {
export async function run(options: CliRunOptions = {}): Promise<void> {
const argSkipPrompt = !!process.env.SKIP_PROMPT || options.yes
const argTemplate = <FrameworkOption[]>options.frameworks?.map(m => m.trim())
const argExtra = <ExtraLibrariesOption[]>options.extra?.map(m => m.trim())
Expand Down
2 changes: 1 addition & 1 deletion src/cli/stages/update-eslint-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import parse from 'parse-gitignore'
import { getEslintConfigContent } from '../utils'
import type { PromptResult } from '../types'

export async function updateEslintFiles(result: PromptResult) {
export async function updateEslintFiles(result: PromptResult): Promise<void> {
const cwd = process.cwd()
const pathESLintIgnore = path.join(cwd, '.eslintignore')
const pathPackageJSON = path.join(cwd, 'package.json')
Expand Down
2 changes: 1 addition & 1 deletion src/cli/stages/update-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as p from '@clack/prompts'
import { dependenciesMap, pkgJson } from '../constants'
import type { ExtraLibrariesOption, PromptResult } from '../types'

export async function updatePackageJson(result: PromptResult) {
export async function updatePackageJson(result: PromptResult): Promise<void> {
const cwd = process.cwd()

const pathPackageJSON = path.join(cwd, 'package.json')
Expand Down
4 changes: 2 additions & 2 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execSync } from 'node:child_process'

export function isGitClean() {
export function isGitClean(): boolean {
try {
execSync('git diff-index --quiet HEAD --')
return true
Expand All @@ -13,7 +13,7 @@ export function isGitClean() {
export function getEslintConfigContent(
mainConfig: string,
additionalConfigs?: string[],
) {
): string {
return `
import antfu from '@antfu/eslint-config'
Expand Down
2 changes: 2 additions & 0 deletions src/configs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export async function test(
'test/prefer-hooks-in-order': 'error',
'test/prefer-lowercase-title': 'error',

'ts/explicit-function-return-type': 'off',

...overrides,
},
},
Expand Down
22 changes: 19 additions & 3 deletions src/configs/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import process from 'node:process'
import { GLOB_ASTRO_TS, GLOB_MARKDOWN, GLOB_TS, GLOB_TSX } from '../globs'
import type { OptionsComponentExts, OptionsFiles, OptionsOverrides, OptionsTypeScriptParserOptions, OptionsTypeScriptWithTypes, TypedFlatConfigItem } from '../types'
import type { OptionsComponentExts, OptionsFiles, OptionsOverrides, OptionsProjectType, OptionsTypeScriptParserOptions, OptionsTypeScriptWithTypes, TypedFlatConfigItem } from '../types'
import { pluginAntfu } from '../plugins'
import { interopDefault, renameRules } from '../utils'

export async function typescript(
options: OptionsFiles & OptionsComponentExts & OptionsOverrides & OptionsTypeScriptWithTypes & OptionsTypeScriptParserOptions = {},
options: OptionsFiles & OptionsComponentExts & OptionsOverrides & OptionsTypeScriptWithTypes & OptionsTypeScriptParserOptions & OptionsProjectType = {},
): Promise<TypedFlatConfigItem[]> {
const {
componentExts = [],
overrides = {},
parserOptions = {},
type = 'app',
} = options

const files = options.files ?? [
Expand Down Expand Up @@ -123,7 +124,11 @@ export async function typescript(
'no-useless-constructor': 'off',
'ts/ban-ts-comment': ['error', { 'ts-expect-error': 'allow-with-description' }],
'ts/consistent-type-definitions': ['error', 'interface'],
'ts/consistent-type-imports': ['error', { disallowTypeAnnotations: false, prefer: 'type-imports' }],
'ts/consistent-type-imports': ['error', {
disallowTypeAnnotations: false,
prefer: 'type-imports',
}],

'ts/method-signature-style': ['error', 'property'], // https://www.totaltypescript.com/method-shorthand-syntax-considered-harmful
'ts/no-dupe-class-members': 'error',
'ts/no-dynamic-delete': 'off',
Expand All @@ -143,6 +148,17 @@ export async function typescript(
'ts/prefer-ts-expect-error': 'error',
'ts/triple-slash-reference': 'off',
'ts/unified-signatures': 'off',

...(type === 'lib'
? {
'ts/explicit-function-return-type': ['error', {
allowExpressions: true,
allowHigherOrderFunctions: true,
allowIIFEs: true,
}],
}
: {}
),
...overrides,
},
},
Expand Down
4 changes: 3 additions & 1 deletion src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { interopDefault } from './utils'
import { formatters } from './configs/formatters'
import { regexp } from './configs/regexp'
import type { RuleOptions } from './typegen'

const flatConfigProps: (keyof TypedFlatConfigItem)[] = [
'name',
Expand Down Expand Up @@ -155,6 +156,7 @@ export function antfu(
...typescriptOptions,
componentExts,
overrides: getOverrides(options, 'typescript'),
type: options.type,
}))
}

Expand Down Expand Up @@ -308,7 +310,7 @@ export function resolveSubOptions<K extends keyof OptionsConfig>(
export function getOverrides<K extends keyof OptionsConfig>(
options: OptionsConfig,
key: K,
) {
): Partial<Linter.RulesRecord & RuleOptions> {
const sub = resolveSubOptions(options, key)
return {
...(options.overrides as any)?.[key],
Expand Down
11 changes: 10 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ export interface OptionsOverrides {
overrides?: TypedFlatConfigItem['rules']
}

export interface OptionsProjectType {
/**
* Type of the project. `lib` will enable more strict rules for libraries.
*
* @default 'app'
*/
type?: 'app' | 'lib'
}

export interface OptionsRegExp {
/**
* Override rulelevels
Expand All @@ -194,7 +203,7 @@ export interface OptionsUnoCSS extends OptionsOverrides {
strict?: boolean
}

export interface OptionsConfig extends OptionsComponentExts {
export interface OptionsConfig extends OptionsComponentExts, OptionsProjectType {
/**
* Enable gitignore support.
*
Expand Down
7 changes: 5 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export async function combine(...configs: Awaitable<TypedFlatConfigItem | TypedF
* }]
* ```
*/
export function renameRules(rules: Record<string, any>, map: Record<string, string>) {
export function renameRules(
rules: Record<string, any>,
map: Record<string, string>,
): Record<string, any> {
return Object.fromEntries(
Object.entries(rules)
.map(([key, value]) => {
Expand Down Expand Up @@ -104,7 +107,7 @@ export async function interopDefault<T>(m: Awaitable<T>): Promise<T extends { de
return (resolved as any).default || resolved
}

export async function ensurePackages(packages: (string | undefined)[]) {
export async function ensurePackages(packages: (string | undefined)[]): Promise<void> {
if (process.env.CI || process.stdout.isTTY === false)
return

Expand Down

0 comments on commit 3dd7b57

Please sign in to comment.