Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
refactor(projects): refactor complete: options and types
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Dec 13, 2023
1 parent 3a3bce0 commit e561263
Show file tree
Hide file tree
Showing 26 changed files with 374 additions and 407 deletions.
4 changes: 3 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { defineConfig as defineConfig2 } from './dist/index.js';

const require = createRequire(import.meta.url);

/** @type {{ defineConfig: import('./src/index.ts').defineConfig }} */
const { defineConfig: defineConfig1 } = require('./src/index.ts');

const useBuild = true;

/** @type {import('./src/index.ts').defineConfig} */
const defineConfig = useBuild ? defineConfig2 : defineConfig1;

export default defineConfig(
Expand All @@ -20,6 +20,8 @@ export default defineConfig(
astro: true,
formatter: {
html: true,
css: true,
json: true,
markdown: true,
yaml: true,
toml: true
Expand Down
5 changes: 5 additions & 0 deletions example/demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Demo Markdown

| Name | Description |
| --------------- | --------------- |
| [demo](demo.md) | This is a demo. |
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
"@toml-tools/parser": "1.0.0",
"@types/eslint": "8.44.8",
"@types/eslint-config-prettier": "6.11.3",
"@types/eslint__eslintrc": "2.1.1",
"@types/eslint__js": "8.42.3",
"@types/node": "20.10.4",
"@types/prompts": "2.4.9",
Expand Down
107 changes: 49 additions & 58 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 15 additions & 11 deletions src/configs/astro.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { FlatESLintConfig, Rules } from 'eslint-define-config';
import { ensurePackages, interopDefault } from '../shared';
import { GLOB_ASTRO } from '../constants/glob';
import type { PrettierLanguageRules } from '../types';
import type { FlatConfigItem, PartialPrettierExtendedOptions, RequiredRuleBaseOptions } from '../types';
import { createTsRules } from './typescript';

export async function createAstroConfig(enable?: boolean, prettierRules: Partial<PrettierLanguageRules> = {}) {
if (!enable) return [];
export async function createAstroConfig(
options?: RequiredRuleBaseOptions,
prettierRules: PartialPrettierExtendedOptions = {}
) {
if (!options) return [];

const { files, overrides } = options;

await ensurePackages(['eslint-plugin-astro', 'astro-eslint-parser', 'prettier-plugin-astro']);

Expand All @@ -20,14 +23,14 @@ export async function createAstroConfig(enable?: boolean, prettierRules: Partial

const { plugins = [] } = prettierRules;

const pRules: Partial<PrettierLanguageRules> = {
const pRules: PartialPrettierExtendedOptions = {
...prettierRules,
plugins: plugins.concat('prettier-plugin-astro')
};

const configs: FlatESLintConfig[] = [
const configs: FlatConfigItem[] = [
{
files: [GLOB_ASTRO],
files,
languageOptions: {
parser: parserAstro,
parserOptions: {
Expand All @@ -37,14 +40,15 @@ export async function createAstroConfig(enable?: boolean, prettierRules: Partial
}
},
plugins: {
'@typescript-eslint': pluginTs as any,
astro: pluginAstro as any,
'@typescript-eslint': pluginTs,
astro: pluginAstro,
prettier: pluginPrettier
},
processor: pluginAstro.processors.astro,
rules: {
...tsRules,
...(pluginAstro.configs.recommended.rules as Partial<Rules>),
...(pluginAstro.configs.recommended.rules as FlatConfigItem['rules']),
...overrides,
'prettier/prettier': [
'warn',
{
Expand Down
13 changes: 6 additions & 7 deletions src/configs/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { FlatESLintConfig } from 'eslint-define-config';
import {
GLOB_CSS,
GLOB_HTML,
Expand All @@ -13,11 +12,11 @@ import {
GLOB_YAML
} from '../constants/glob';
import { ensurePackages, interopDefault } from '../shared';
import type { Option, PrettierLanguageRules, PrettierParser } from '../types';
import type { FlatConfigItem, Options, PartialPrettierExtendedOptions, PrettierParser } from '../types';

export async function createFormatterConfig(
options?: Option['formatter'],
prettierRules: Partial<PrettierLanguageRules> = {}
options?: Options['formatter'],
prettierRules: PartialPrettierExtendedOptions = {}
) {
const { html = true, css = true, json = true, markdown, yaml, toml } = options || {};

Expand All @@ -27,7 +26,7 @@ export async function createFormatterConfig(
]);

function createPrettierFormatter(files: string[], parser: PrettierParser, plugins?: string[]) {
const rules: Partial<PrettierLanguageRules> = {
const rules: PartialPrettierExtendedOptions = {
...prettierRules,
parser
};
Expand All @@ -36,7 +35,7 @@ export async function createFormatterConfig(
rules.plugins = [...(rules.plugins || []), ...plugins];
}

const config: FlatESLintConfig = {
const config: FlatConfigItem = {
files,
languageOptions: {
parser: parserPlain
Expand All @@ -52,7 +51,7 @@ export async function createFormatterConfig(
return config;
}

const configs: FlatESLintConfig[] = [];
const configs: FlatConfigItem[] = [];

if (css) {
const cssConfig = createPrettierFormatter([GLOB_CSS, GLOB_POSTCSS], 'css');
Expand Down
Loading

0 comments on commit e561263

Please sign in to comment.