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

Commit

Permalink
feat(projects): support toml on demand, replace eslint-plugin-jsonc w…
Browse files Browse the repository at this point in the history
…ith prettier-plugin-pkg
  • Loading branch information
honghuangdc committed Dec 8, 2023
1 parent 046858b commit 4b48bd0
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 284 deletions.
1 change: 1 addition & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default defineBuildConfig({
entries: ['src/index'],
clean: true,
declaration: true,
externals: ['eslint-plugin-vue', 'vue-eslint-parser', 'prettier-plugin-toml'],
rollup: {
emitCJS: true,
inlineDependencies: true,
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from '@soybeanjs/eslint-config';

export default defineConfig(
{ vue: true, formatter: { markdown: true, yaml: true } },
{ vue: true, formatter: { markdown: true, yaml: true, toml: true } },
{
rules: {
'vue/multi-word-component-names': [
Expand Down
6 changes: 6 additions & 0 deletions example/demo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This is a TOML comment

# This is a multiline
# TOML comment

str1 = "I'm a string."
60 changes: 32 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
{
"name": "@soybeanjs/eslint-config",
"type": "module",
"version": "1.0.7",
"packageManager": "pnpm@8.11.0",
"author": {
"name": "Soybean",
"email": "soybeanjs@outlook.com",
"url": "https://github.com/soybeanjs"
},
"license": "MIT",
"homepage": "https://github.com/soybeanjs/eslint-flat-config",
"type": "module",
"repository": {
"url": "https://github.com/soybeanjs/eslint-flat-config.git"
},
"homepage": "https://github.com/soybeanjs/eslint-flat-config",
"bugs": {
"url": "https://github.com/soybeanjs/eslint-flat-config/issues"
},
"author": {
"name": "Soybean",
"email": "soybeanjs@outlook.com",
"url": "https://github.com/soybeanjs"
},
"license": "MIT",
"packageManager": "pnpm@8.11.0",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": ["dist"],
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"lint": "eslint . --fix",
"commit": "soy git-commit",
"cleanup": "soy cleanup",
"update-pkg": "soy ncu",
"typecheck": "tsc --noEmit --skipLibCheck",
"commit": "soy git-commit",
"lint": "eslint . --fix",
"prepare": "pnpm build && simple-git-hooks",
"publish-pkg": "pnpm -r publish --access public",
"release": "soy release && pnpm publish-pkg",
"prepare": "pnpm build && simple-git-hooks"
"typecheck": "tsc --noEmit --skipLibCheck",
"update-pkg": "soy ncu"
},
"peerDependencies": {
"eslint": ">=8.40.0",
"eslint-plugin-vue": ">=9.0.0",
"typescript": ">=5.0.0",
"vue-eslint-parser": ">=9.0.0"
"typescript": ">=5.0.0"
},
"peerDependenciesMeta": {
"eslint-plugin-vue": {
"optional": true
},
"prettier-plugin-toml": {
"optional": true
},
"vue-eslint-parser": {
"optional": true
}
Expand All @@ -59,21 +59,21 @@
"@antfu/install-pkg": "0.3.1",
"@eslint/eslintrc": "2.1.4",
"@eslint/js": "8.55.0",
"@toml-tools/parser": "1.0.0",
"@typescript-eslint/eslint-plugin": "6.13.2",
"@typescript-eslint/parser": "6.13.2",
"eslint-config-prettier": "9.1.0",
"eslint-define-config": "2.0.0",
"eslint-parser-plain": "0.1.0",
"eslint-plugin-i": "2.29.0",
"eslint-plugin-jsonc": "2.10.0",
"eslint-plugin-n": "16.3.1",
"eslint-plugin-prettier": "5.0.1",
"eslint-plugin-unicorn": "49.0.0",
"globals": "13.23.0",
"jsonc-eslint-parser": "2.4.0",
"local-pkg": "0.5.0",
"prettier": "4.0.0-alpha.3",
"prettier-plugin-jsdoc": "1.1.1",
"prettier-plugin-pkg": "0.18.0",
"prompts": "2.4.2"
},
"devDependencies": {
Expand All @@ -91,17 +91,21 @@
"eslint": "8.55.0",
"eslint-plugin-vue": "9.19.2",
"lint-staged": "15.2.0",
"prettier-plugin-toml": "2.0.1",
"simple-git-hooks": "2.9.0",
"typescript": "5.3.2",
"unbuild": "2.0.0",
"vue": "3.3.10",
"vue-eslint-parser": "9.3.2"
},
"simple-git-hooks": {
"commit-msg": "pnpm soy git-commit-verify",
"pre-commit": "pnpm typecheck && pnpm lint-staged"
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"lint-staged": {
"*": "eslint --fix"
},
"simple-git-hooks": {
"commit-msg": "pnpm soy git-commit-verify",
"pre-commit": "pnpm typecheck && pnpm lint-staged"
}
}
28 changes: 19 additions & 9 deletions src/configs/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as plainParser from 'eslint-parser-plain';
import jsonParser from 'jsonc-eslint-parser';
import prettierPlugin from 'eslint-plugin-prettier';
import type { FlatESLintConfig } from 'eslint-define-config';
import {
Expand All @@ -12,16 +10,19 @@ import {
GLOB_MARKDOWN,
GLOB_POSTCSS,
GLOB_SCSS,
GLOB_TOML,
GLOB_YAML
} from '../constants/glob';

import { ensurePackages, interopDefault } from '../shared';
import type { Option, PrettierLanguageRules, PrettierParser } from '../types';

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

const plainParser = await interopDefault(import('eslint-parser-plain'));

function createPrettierFormatter(files: string[], parser: PrettierParser, plugins?: string[]) {
const rules: Partial<PrettierLanguageRules> = {
Expand All @@ -30,15 +31,16 @@ export function createFormatterConfig(
};

if (plugins?.length) {
rules.plugins = plugins;
rules.plugins = [...(rules.plugins || []), ...plugins];
}

const isJson = parser === 'json' || parser === 'json5' || parser === 'json-stringify';
// const isJson = parser === 'json' || parser === 'json5' || parser === 'json-stringify';

const config: FlatESLintConfig = {
files,
languageOptions: {
parser: isJson ? jsonParser : plainParser
// parser: isJson ? jsonParser : plainParser
parser: plainParser
},
plugins: {
prettier: prettierPlugin
Expand Down Expand Up @@ -67,7 +69,7 @@ export function createFormatterConfig(
}

if (json) {
const jsonConfig = createPrettierFormatter([GLOB_JSON, GLOB_JSONC], 'json');
const jsonConfig = createPrettierFormatter([GLOB_JSON, GLOB_JSONC], 'json-stringify', ['prettier-plugin-pkg']);
const json5Config = createPrettierFormatter([GLOB_JSON5], 'json5');
configs.push(jsonConfig, json5Config);
}
Expand All @@ -82,5 +84,13 @@ export function createFormatterConfig(
configs.push(yamlConfig);
}

if (toml) {
await ensurePackages(['prettier-plugin-toml']);

const tomlConfig = createPrettierFormatter([GLOB_TOML], 'toml', ['prettier-plugin-toml']);

configs.push(tomlConfig);
}

return configs;
}
1 change: 0 additions & 1 deletion src/configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './ignore';
export * from './javascript';
export * from './json';
export * from './node';
export * from './import';
export * from './unicorn';
Expand Down
Loading

0 comments on commit 4b48bd0

Please sign in to comment.