From 3afa1735a91e26034f0f1bcc9cf647d8df38d4be Mon Sep 17 00:00:00 2001 From: Xy2002 <57112423+Xy2002@users.noreply.github.com> Date: Thu, 1 Aug 2024 19:58:44 +0800 Subject: [PATCH] feat: Add eslint and prettier --- .prettierignore | 3 +++ .prettierrc | 7 +++++++ .vscode/settings.json | 33 +++++++-------------------------- eslint.config.js | 40 +++++++++++++++++++++++++++++----------- package.json | 15 ++++++++++----- 5 files changed, 56 insertions(+), 42 deletions(-) create mode 100644 .prettierignore create mode 100644 .prettierrc diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..ca0dc72 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +pnpm-lock.yaml +LICENSE +LICENSE.md \ No newline at end of file diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..2c6c351 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "singleQuote": true, + "semi": true, + "printWidth": 100, + "trailingComma": "none", + "tabWidth": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index a619137..d27d8bb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,28 +1,9 @@ { - // Disable the default formatter, use eslint instead - "prettier.enable": false, - "editor.formatOnSave": false, - - // Auto fix - "editor.codeActionsOnSave": { - "source.fixAll.eslint": "explicit", - "source.organizeImports": "never" - }, - - // Silent the stylistic rules in you IDE, but still auto fix them - "eslint.rules.customizations": [ - { "rule": "style/*", "severity": "off", "fixable": true }, - { "rule": "format/*", "severity": "off", "fixable": true }, - { "rule": "*-indent", "severity": "off", "fixable": true }, - { "rule": "*-spacing", "severity": "off", "fixable": true }, - { "rule": "*-spaces", "severity": "off", "fixable": true }, - { "rule": "*-order", "severity": "off", "fixable": true }, - { "rule": "*-dangle", "severity": "off", "fixable": true }, - { "rule": "*-newline", "severity": "off", "fixable": true }, - { "rule": "*quotes", "severity": "off", "fixable": true }, - { "rule": "*semi", "severity": "off", "fixable": true } - ], - - // Enable eslint for all supported languages - "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact", "vue", "html", "markdown", "json", "jsonc", "yaml", "toml", "xml", "gql", "graphql", "astro", "svelte", "css", "less", "scss", "pcss", "postcss"] + "editor.formatOnSave": true, + "editor.defaultFormatter": "esbenp.prettier-vscode", + "files.autoSave": "onFocusChange", + "prettier.requireConfig": true, + "[javascript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + } } diff --git a/eslint.config.js b/eslint.config.js index ccdd37c..8ae64f5 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,13 +1,31 @@ -import antfu from '@antfu/eslint-config' +import globals from 'globals'; +import pluginJs from '@eslint/js'; +import { FlatCompat } from '@eslint/eslintrc'; +import path from 'path'; +import { fileURLToPath } from 'url'; -export default antfu({ - type: 'lib', - stylistic: { - indent: 4, - }, -}, { - rules: { - 'no-console': ['off'], - 'semi': ['error', 'always'], +// mimic CommonJS variables -- not needed if using CommonJS +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const compat = new FlatCompat({ + baseDirectory: __dirname, + resolvePluginsRelativeTo: __dirname, + recommendedConfig: pluginJs.configs.recommended +}); + +const jsRules = { + 'no-prototype-builtins': 'off' +}; + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + { + files: ['**/*.{mjs,cjs,js}'], + languageOptions: { globals: { ...globals.browser, ...globals.node } }, + rules: { + ...jsRules + } }, -}) + ...compat.extends('plugin:prettier/recommended') +]; diff --git a/package.json b/package.json index a8516ef..c78f8ed 100644 --- a/package.json +++ b/package.json @@ -38,23 +38,28 @@ "test": "vitest", "test-ui": "vitest --ui", "lint": "eslint .", - "lint:fix": "eslint . --fix" + "lint:fix": "eslint . --fix", + "format": "prettier --write ." }, "dependencies": { "date-fns": "^3.6.0" }, "devDependencies": { - "@antfu/eslint-config": "^2.24.1", + "@eslint/eslintrc": "^3.1.0", "@eslint/js": "^9.8.0", "@vitest/ui": "^2.0.5", "colorette": "^2.0.20", "del-cli": "^5.1.0", "docdash-extended-chensuiyi": "^1.1.1", - "eslint": "9.x", + "eslint": "^9.8.0", + "eslint-config-prettier": "^9.1.0", + "eslint-plugin-prettier": "^5.2.1", "fs-extra": "^11.2.0", + "globals": "^15.9.0", "jsdoc": "^4.0.3", "lint-staged": "^15.2.7", "log-symbols": "^6.0.0", + "prettier": "^3.3.3", "simple-git-hooks": "^2.11.1", "vitest": "^2.0.5" }, @@ -62,6 +67,6 @@ "pre-commit": "pnpm lint-staged" }, "lint-staged": { - "*": "eslint --fix" + "*": "prettier --write ." } -} +} \ No newline at end of file