Compare commits
25 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
60002c33fd | ||
|
502f6b0c37 | ||
|
d1cd722857 | ||
|
d2150eec3b | ||
|
d3c3e1b84d | ||
|
3963633712 | ||
|
e9193343e8 | ||
|
e8d73f737e | ||
|
3a74c8e351 | ||
|
3fa861730d | ||
|
ebbc1a7f78 | ||
|
417b60c18b | ||
|
9768ccd35e | ||
|
87d26fb63a | ||
|
e17d2f8f52 | ||
|
4ef978a092 | ||
|
ab850b0ae4 | ||
|
7632765715 | ||
|
d3c1119dbe | ||
|
421d406e4f | ||
|
447d326997 | ||
|
6c0f76e229 | ||
|
564db7d3c4 | ||
|
4167a78419 | ||
|
37a4d762c3 |
50
README.md
50
README.md
@@ -3,7 +3,7 @@
|
|||||||
[](https://npmjs.com/package/@antfu/eslint-config)
|
[](https://npmjs.com/package/@antfu/eslint-config)
|
||||||
|
|
||||||
- Single quotes, no semi
|
- Single quotes, no semi
|
||||||
- Auto fix for formatting (aimed to be used standalone without Prettier)
|
- Auto fix for formatting (aimed to be used standalone **without** Prettier)
|
||||||
- Designed to work with TypeScript, Vue out-of-box
|
- Designed to work with TypeScript, Vue out-of-box
|
||||||
- Lint also for json, yaml, markdown
|
- Lint also for json, yaml, markdown
|
||||||
- Sorted imports, dangling commas
|
- Sorted imports, dangling commas
|
||||||
@@ -68,9 +68,53 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Extended Reading
|
### Lint Staged
|
||||||
|
|
||||||
Learn more about the context - [Why I don't use Prettier](https://antfu.me/posts/why-not-prettier).
|
If you want to apply lint and auto-fix before every commit, you can add the following to your `package.json`:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"simple-git-hooks": {
|
||||||
|
"pre-commit": "pnpm lint-staged"
|
||||||
|
},
|
||||||
|
"lint-staged": {
|
||||||
|
"*": "eslint --fix"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
and then
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm i -D lint-staged simple-git-hooks
|
||||||
|
```
|
||||||
|
|
||||||
|
## FAQ
|
||||||
|
|
||||||
|
### Prettier?
|
||||||
|
|
||||||
|
[Why I don't use Prettier](https://antfu.me/posts/why-not-prettier)
|
||||||
|
|
||||||
|
### How to lint CSS?
|
||||||
|
|
||||||
|
This config does NOT lint CSS. I personally use [UnoCSS](https://github.com/unocss/unocss) so I don't write CSS. If you still prefer CSS, you can use [stylelint](https://stylelint.io/) for CSS linting.
|
||||||
|
|
||||||
|
### I prefer XXX...
|
||||||
|
|
||||||
|
Sure, you can override the rules in your `.eslintrc` file.
|
||||||
|
|
||||||
|
<!-- eslint-skip -->
|
||||||
|
|
||||||
|
```jsonc
|
||||||
|
{
|
||||||
|
"extends": "@antfu",
|
||||||
|
"rules": {
|
||||||
|
// your rules...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Or you can always fork this repo and make your own.
|
||||||
|
|
||||||
## Check Also
|
## Check Also
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"private": true,
|
"private": true,
|
||||||
"packageManager": "pnpm@7.8.0",
|
"packageManager": "pnpm@8.0.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "vite-ssg build",
|
"build": "vite-ssg build",
|
||||||
"dev": "vite --port 3333 --open",
|
"dev": "vite --port 3333 --open",
|
||||||
@@ -15,48 +15,48 @@
|
|||||||
"up": "taze major -I"
|
"up": "taze major -I"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@unocss/reset": "^0.49.4",
|
"@unocss/reset": "^0.50.6",
|
||||||
"@vueuse/core": "^9.12.0",
|
"@vueuse/core": "^9.13.0",
|
||||||
"@vueuse/head": "^1.0.25",
|
"@vueuse/head": "^1.1.23",
|
||||||
"nprogress": "^0.2.0",
|
"nprogress": "^0.2.0",
|
||||||
"pinia": "^2.0.30",
|
"pinia": "^2.0.33",
|
||||||
"vue": "^3.2.47",
|
"vue": "^3.2.47",
|
||||||
"vue-demi": "^0.13.11",
|
"vue-demi": "^0.13.11",
|
||||||
"vue-i18n": "^9.2.2",
|
"vue-i18n": "^9.2.2",
|
||||||
"vue-router": "^4.1.6"
|
"vue-router": "^4.1.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@antfu/eslint-config": "^0.35.1",
|
"@antfu/eslint-config": "^0.37.0",
|
||||||
"@iconify-json/carbon": "^1.1.14",
|
"@iconify-json/carbon": "^1.1.16",
|
||||||
"@intlify/vite-plugin-vue-i18n": "^7.0.0",
|
"@intlify/vite-plugin-vue-i18n": "^7.0.0",
|
||||||
"@types/markdown-it-link-attributes": "^3.0.1",
|
"@types/markdown-it-link-attributes": "^3.0.1",
|
||||||
"@types/nprogress": "^0.2.0",
|
"@types/nprogress": "^0.2.0",
|
||||||
"@vitejs/plugin-vue": "^4.0.0",
|
"@vitejs/plugin-vue": "^4.1.0",
|
||||||
"@vue/test-utils": "^2.2.10",
|
"@vue/test-utils": "^2.3.2",
|
||||||
"critters": "^0.0.16",
|
"critters": "^0.0.16",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"cypress": "^12.5.1",
|
"cypress": "^12.9.0",
|
||||||
"eslint": "^8.33.0",
|
"eslint": "^8.37.0",
|
||||||
"eslint-plugin-cypress": "^2.12.1",
|
"eslint-plugin-cypress": "^2.13.2",
|
||||||
"https-localhost": "^4.7.1",
|
"https-localhost": "^4.7.1",
|
||||||
"markdown-it-link-attributes": "^4.0.1",
|
"markdown-it-link-attributes": "^4.0.1",
|
||||||
"markdown-it-shiki": "^0.7.2",
|
"markdown-it-shiki": "^0.8.0",
|
||||||
"pnpm": "^7.26.3",
|
"pnpm": "^8.0.0",
|
||||||
"shiki": "^0.14.0",
|
"shiki": "^0.14.1",
|
||||||
"taze": "^0.8.5",
|
"taze": "^0.9.1",
|
||||||
"typescript": "^4.9.5",
|
"typescript": "^5.0.2",
|
||||||
"unocss": "^0.49.4",
|
"unocss": "^0.50.6",
|
||||||
"unplugin-auto-import": "^0.13.0",
|
"unplugin-auto-import": "^0.15.2",
|
||||||
"unplugin-vue-components": "^0.23.0",
|
"unplugin-vue-components": "^0.24.1",
|
||||||
"vite": "^4.1.1",
|
"vite": "^4.2.1",
|
||||||
"vite-plugin-inspect": "^0.7.15",
|
"vite-plugin-inspect": "^0.7.18",
|
||||||
"vite-plugin-pages": "^0.28.0",
|
"vite-plugin-pages": "^0.29.0",
|
||||||
"vite-plugin-pwa": "^0.14.1",
|
"vite-plugin-pwa": "^0.14.7",
|
||||||
"vite-plugin-vue-layouts": "^0.7.0",
|
"vite-plugin-vue-layouts": "^0.8.0",
|
||||||
"vite-plugin-vue-markdown": "^0.22.4",
|
"vite-plugin-vue-markdown": "^0.22.4",
|
||||||
"vite-ssg": "^0.22.1",
|
"vite-ssg": "^0.22.1",
|
||||||
"vite-ssg-sitemap": "^0.4.3",
|
"vite-ssg-sitemap": "^0.4.3",
|
||||||
"vitest": "^0.28.4",
|
"vitest": "^0.29.8",
|
||||||
"vue-tsc": "^1.0.24"
|
"vue-tsc": "^1.2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { t, availableLocales, locale } = useI18n()
|
const { t, availableLocales, locale } = useI18n()
|
||||||
|
|
||||||
const toggleLocales = () => {
|
function toggleLocales() {
|
||||||
// change to some real logic
|
// change to some real logic
|
||||||
const locales = availableLocales
|
const locales = availableLocales
|
||||||
locale.value = locales[(locales.indexOf(locale.value) + 1) % locales.length]
|
locale.value = locales[(locales.indexOf(locale.value) + 1) % locales.length]
|
||||||
|
@@ -3,7 +3,7 @@ const user = useUserStore()
|
|||||||
const name = $ref(user.savedName)
|
const name = $ref(user.savedName)
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const go = () => {
|
function go() {
|
||||||
if (name)
|
if (name)
|
||||||
router.push(`/hi/${encodeURIComponent(name)}`)
|
router.push(`/hi/${encodeURIComponent(name)}`)
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import path from 'path'
|
import path from 'node:path'
|
||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
import Vue from '@vitejs/plugin-vue'
|
import Vue from '@vitejs/plugin-vue'
|
||||||
import Pages from 'vite-plugin-pages'
|
import Pages from 'vite-plugin-pages'
|
||||||
|
16
package.json
16
package.json
@@ -1,22 +1,22 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-monorepo",
|
"name": "@antfu/eslint-config-monorepo",
|
||||||
"version": "0.35.2",
|
"version": "0.38.3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"packageManager": "pnpm@7.1.0",
|
"packageManager": "pnpm@8.0.0",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"test": "pnpm -r run test",
|
"test": "pnpm -r run test",
|
||||||
"prepare": "pnpm -r run stub",
|
"prepare": "pnpm -r run stub",
|
||||||
"release": "bumpp package.json packages/*/package.json --commit --push --tag && pnpm -r publish --access public"
|
"release": "bumpp -r && pnpm -r publish"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@antfu/eslint-config": "*",
|
"@antfu/eslint-config": "workspace:*",
|
||||||
"bumpp": "^8.2.1",
|
"bumpp": "^9.0.0",
|
||||||
"eslint": "^8.33.0",
|
"eslint": "^8.37.0",
|
||||||
"eslint-plugin-antfu": "workspace:*",
|
"eslint-plugin-antfu": "workspace:*",
|
||||||
"rimraf": "^4.1.2",
|
"rimraf": "^4.4.1",
|
||||||
"typescript": "^4.9.5"
|
"typescript": "^5.0.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -28,6 +28,17 @@ module.exports = {
|
|||||||
'pnpm-lock.yaml',
|
'pnpm-lock.yaml',
|
||||||
'yarn.lock',
|
'yarn.lock',
|
||||||
'__snapshots__',
|
'__snapshots__',
|
||||||
|
// ignore for in lint-staged
|
||||||
|
'*.css',
|
||||||
|
'*.png',
|
||||||
|
'*.ico',
|
||||||
|
'*.toml',
|
||||||
|
'*.patch',
|
||||||
|
'*.txt',
|
||||||
|
'*.crt',
|
||||||
|
'*.key',
|
||||||
|
'Dockerfile',
|
||||||
|
// force include
|
||||||
'!.github',
|
'!.github',
|
||||||
'!.vitepress',
|
'!.vitepress',
|
||||||
'!.vscode',
|
'!.vscode',
|
||||||
@@ -38,6 +49,7 @@ module.exports = {
|
|||||||
'antfu',
|
'antfu',
|
||||||
'no-only-tests',
|
'no-only-tests',
|
||||||
'unused-imports',
|
'unused-imports',
|
||||||
|
'node',
|
||||||
],
|
],
|
||||||
settings: {
|
settings: {
|
||||||
'import/resolver': {
|
'import/resolver': {
|
||||||
@@ -196,6 +208,7 @@ module.exports = {
|
|||||||
'import/no-mutable-exports': 'error',
|
'import/no-mutable-exports': 'error',
|
||||||
'import/no-unresolved': 'off',
|
'import/no-unresolved': 'off',
|
||||||
'import/no-absolute-path': 'off',
|
'import/no-absolute-path': 'off',
|
||||||
|
'import/newline-after-import': ['error', { 'count': 1 }],
|
||||||
|
|
||||||
// Common
|
// Common
|
||||||
'semi': ['error', 'never'],
|
'semi': ['error', 'never'],
|
||||||
@@ -303,6 +316,10 @@ module.exports = {
|
|||||||
'operator-linebreak': ['error', 'before'],
|
'operator-linebreak': ['error', 'before'],
|
||||||
'max-statements-per-line': ['error', { max: 1 }],
|
'max-statements-per-line': ['error', { max: 1 }],
|
||||||
|
|
||||||
|
// node
|
||||||
|
// 'node/prefer-global/process': ['error', 'never'], // Not sure if we need it as we are using `process.env.NODE_ENV` a lot in front-end.
|
||||||
|
'node/prefer-global/buffer': ['error', 'never'],
|
||||||
|
|
||||||
// unicorns
|
// unicorns
|
||||||
// Pass error message when throwing errors
|
// Pass error message when throwing errors
|
||||||
'unicorn/error-message': 'error',
|
'unicorn/error-message': 'error',
|
||||||
@@ -326,6 +343,8 @@ module.exports = {
|
|||||||
'unicorn/prefer-type-error': 'error',
|
'unicorn/prefer-type-error': 'error',
|
||||||
// Use new when throwing error
|
// Use new when throwing error
|
||||||
'unicorn/throw-new-error': 'error',
|
'unicorn/throw-new-error': 'error',
|
||||||
|
// Prefer using the node: protocol
|
||||||
|
'unicorn/prefer-node-protocol': 'error',
|
||||||
|
|
||||||
'no-use-before-define': ['error', { functions: false, classes: false, variables: true }],
|
'no-use-before-define': ['error', { functions: false, classes: false, variables: true }],
|
||||||
'eslint-comments/disable-enable-pair': 'off',
|
'eslint-comments/disable-enable-pair': 'off',
|
||||||
@@ -352,6 +371,7 @@ module.exports = {
|
|||||||
// antfu
|
// antfu
|
||||||
'antfu/if-newline': 'error',
|
'antfu/if-newline': 'error',
|
||||||
'antfu/import-dedupe': 'error',
|
'antfu/import-dedupe': 'error',
|
||||||
|
'antfu/top-level-function': 'error',
|
||||||
// 'antfu/prefer-inline-type-import': 'error',
|
// 'antfu/prefer-inline-type-import': 'error',
|
||||||
},
|
},
|
||||||
}
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-basic",
|
"name": "@antfu/eslint-config-basic",
|
||||||
"version": "0.35.2",
|
"version": "0.38.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -23,18 +23,19 @@
|
|||||||
"eslint-plugin-eslint-comments": "^3.2.0",
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
||||||
"eslint-plugin-html": "^7.1.0",
|
"eslint-plugin-html": "^7.1.0",
|
||||||
"eslint-plugin-import": "^2.27.5",
|
"eslint-plugin-import": "^2.27.5",
|
||||||
"eslint-plugin-jsonc": "^2.6.0",
|
"eslint-plugin-jsonc": "^2.7.0",
|
||||||
"eslint-plugin-markdown": "^3.0.0",
|
"eslint-plugin-markdown": "^3.0.0",
|
||||||
"eslint-plugin-n": "^15.6.1",
|
"eslint-plugin-n": "^15.7.0",
|
||||||
"eslint-plugin-no-only-tests": "^3.1.0",
|
"eslint-plugin-no-only-tests": "^3.1.0",
|
||||||
|
"eslint-plugin-node": "^11.1.0",
|
||||||
"eslint-plugin-promise": "^6.1.1",
|
"eslint-plugin-promise": "^6.1.1",
|
||||||
"eslint-plugin-unicorn": "^45.0.2",
|
"eslint-plugin-unicorn": "^46.0.0",
|
||||||
"eslint-plugin-unused-imports": "^2.0.0",
|
"eslint-plugin-unused-imports": "^2.0.0",
|
||||||
"eslint-plugin-yml": "^1.4.0",
|
"eslint-plugin-yml": "^1.5.0",
|
||||||
"jsonc-eslint-parser": "^2.1.0",
|
"jsonc-eslint-parser": "^2.2.0",
|
||||||
"yaml-eslint-parser": "^1.1.0"
|
"yaml-eslint-parser": "^1.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^8.33.0"
|
"eslint": "^8.37.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-react",
|
"name": "@antfu/eslint-config-react",
|
||||||
"version": "0.35.2",
|
"version": "0.38.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -24,8 +24,8 @@
|
|||||||
"eslint-plugin-react-hooks": "^4.6.0"
|
"eslint-plugin-react-hooks": "^4.6.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^8.33.0",
|
"eslint": "^8.37.0",
|
||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
"typescript": "^4.9.5"
|
"typescript": "^5.0.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
const fs = require('fs')
|
const fs = require('node:fs')
|
||||||
const { join } = require('path')
|
const { join } = require('node:path')
|
||||||
const basic = require('@antfu/eslint-config-basic')
|
const basic = require('@antfu/eslint-config-basic')
|
||||||
|
|
||||||
const tsconfig = process.env.ESLINT_TSCONFIG || 'tsconfig.eslint.json'
|
const tsconfig = process.env.ESLINT_TSCONFIG || 'tsconfig.eslint.json'
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-ts",
|
"name": "@antfu/eslint-config-ts",
|
||||||
"version": "0.35.2",
|
"version": "0.38.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -18,11 +18,11 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@antfu/eslint-config-basic": "workspace:*",
|
"@antfu/eslint-config-basic": "workspace:*",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
"@typescript-eslint/eslint-plugin": "^5.57.0",
|
||||||
"@typescript-eslint/parser": "^5.51.0",
|
"@typescript-eslint/parser": "^5.57.0",
|
||||||
"eslint-plugin-jest": "^27.2.1"
|
"eslint-plugin-jest": "^27.2.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^8.33.0"
|
"eslint": "^8.37.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -58,6 +58,7 @@ module.exports = {
|
|||||||
}],
|
}],
|
||||||
'vue/no-restricted-v-bind': ['error', '/^v-/'],
|
'vue/no-restricted-v-bind': ['error', '/^v-/'],
|
||||||
'vue/no-useless-v-bind': 'error',
|
'vue/no-useless-v-bind': 'error',
|
||||||
|
'vue/no-unused-refs': 'error',
|
||||||
'vue/padding-line-between-blocks': ['error', 'always'],
|
'vue/padding-line-between-blocks': ['error', 'always'],
|
||||||
'vue/prefer-separate-static-class': 'error',
|
'vue/prefer-separate-static-class': 'error',
|
||||||
|
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-vue",
|
"name": "@antfu/eslint-config-vue",
|
||||||
"version": "0.35.2",
|
"version": "0.38.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -17,10 +17,10 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@antfu/eslint-config-basic": "workspace:*",
|
"@antfu/eslint-config-basic": "workspace:*",
|
||||||
"@antfu/eslint-config-ts": "workspace:*",
|
"@antfu/eslint-config-ts": "workspace:*",
|
||||||
"eslint-plugin-vue": "^9.9.0",
|
"eslint-plugin-vue": "^9.10.0",
|
||||||
"local-pkg": "^0.4.3"
|
"local-pkg": "^0.4.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^8.33.0"
|
"eslint": "^8.37.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config",
|
"name": "@antfu/eslint-config",
|
||||||
"version": "0.35.2",
|
"version": "0.38.3",
|
||||||
"description": "Anthony's ESLint config",
|
"description": "Anthony's ESLint config",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -17,21 +17,21 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@antfu/eslint-config-vue": "workspace:*",
|
"@antfu/eslint-config-vue": "workspace:*",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
"@typescript-eslint/eslint-plugin": "^5.57.0",
|
||||||
"@typescript-eslint/parser": "^5.51.0",
|
"@typescript-eslint/parser": "^5.57.0",
|
||||||
"eslint-plugin-eslint-comments": "^3.2.0",
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
||||||
"eslint-plugin-html": "^7.1.0",
|
"eslint-plugin-html": "^7.1.0",
|
||||||
"eslint-plugin-import": "^2.27.5",
|
"eslint-plugin-import": "^2.27.5",
|
||||||
"eslint-plugin-jsonc": "^2.6.0",
|
"eslint-plugin-jsonc": "^2.7.0",
|
||||||
"eslint-plugin-n": "^15.6.1",
|
"eslint-plugin-n": "^15.7.0",
|
||||||
"eslint-plugin-promise": "^6.1.1",
|
"eslint-plugin-promise": "^6.1.1",
|
||||||
"eslint-plugin-unicorn": "^45.0.2",
|
"eslint-plugin-unicorn": "^46.0.0",
|
||||||
"eslint-plugin-vue": "^9.9.0",
|
"eslint-plugin-vue": "^9.10.0",
|
||||||
"eslint-plugin-yml": "^1.4.0",
|
"eslint-plugin-yml": "^1.5.0",
|
||||||
"jsonc-eslint-parser": "^2.1.0",
|
"jsonc-eslint-parser": "^2.2.0",
|
||||||
"yaml-eslint-parser": "^1.1.0"
|
"yaml-eslint-parser": "^1.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^8.33.0"
|
"eslint": "^8.37.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "eslint-plugin-antfu",
|
"name": "eslint-plugin-antfu",
|
||||||
"version": "0.35.2",
|
"version": "0.38.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"homepage": "https://github.com/antfu/eslint-config",
|
"homepage": "https://github.com/antfu/eslint-config",
|
||||||
"main": "./dist/index.cjs",
|
"main": "./dist/index.cjs",
|
||||||
@@ -16,11 +16,11 @@
|
|||||||
"prepublishOnly": "nr build"
|
"prepublishOnly": "nr build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@typescript-eslint/utils": "^5.51.0"
|
"@typescript-eslint/utils": "^5.57.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.13.0",
|
"@types/node": "^18.15.11",
|
||||||
"unbuild": "^1.1.1",
|
"unbuild": "^1.1.2",
|
||||||
"vitest": "^0.28.4"
|
"vitest": "^0.29.8"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@ import genericSpacing from './rules/generic-spacing'
|
|||||||
import ifNewline from './rules/if-newline'
|
import ifNewline from './rules/if-newline'
|
||||||
import importDedupe from './rules/import-dedupe'
|
import importDedupe from './rules/import-dedupe'
|
||||||
import preferInlineTypeImport from './rules/prefer-inline-type-import'
|
import preferInlineTypeImport from './rules/prefer-inline-type-import'
|
||||||
|
import topLevelFunction from './rules/top-level-function'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
rules: {
|
rules: {
|
||||||
@@ -9,5 +10,6 @@ export default {
|
|||||||
'import-dedupe': importDedupe,
|
'import-dedupe': importDedupe,
|
||||||
'prefer-inline-type-import': preferInlineTypeImport,
|
'prefer-inline-type-import': preferInlineTypeImport,
|
||||||
'generic-spacing': genericSpacing,
|
'generic-spacing': genericSpacing,
|
||||||
|
'top-level-function': topLevelFunction,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -13,7 +13,7 @@ export default createEslintRule<Options, MessageIds>({
|
|||||||
meta: {
|
meta: {
|
||||||
type: 'suggestion',
|
type: 'suggestion',
|
||||||
docs: {
|
docs: {
|
||||||
description: 'Newline after if',
|
description: 'Inline type import',
|
||||||
recommended: 'error',
|
recommended: 'error',
|
||||||
},
|
},
|
||||||
fixable: 'code',
|
fixable: 'code',
|
||||||
|
@@ -0,0 +1,58 @@
|
|||||||
|
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint'
|
||||||
|
import { it } from 'vitest'
|
||||||
|
import rule, { RULE_NAME } from './top-level-function'
|
||||||
|
|
||||||
|
const valids = [
|
||||||
|
'function foo() {}',
|
||||||
|
// allow arrow function inside function
|
||||||
|
'function foo() { const bar = () => {} }',
|
||||||
|
// allow arrow function when type is specified
|
||||||
|
'const Foo: Bar = () => {}',
|
||||||
|
// allow let/var
|
||||||
|
'let foo = () => {}',
|
||||||
|
// allow arrow function in as
|
||||||
|
'const foo = (() => {}) as any',
|
||||||
|
// allow iife
|
||||||
|
';(() => {})()',
|
||||||
|
// allow export default
|
||||||
|
'export default () => {}',
|
||||||
|
'export default defineConfig(() => {})',
|
||||||
|
]
|
||||||
|
|
||||||
|
const invalids = [
|
||||||
|
[
|
||||||
|
'const foo = (as: string, bar: number) => { return as + bar }',
|
||||||
|
'function foo (as: string, bar: number) { return as + bar }',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'const foo = <K, T extends Boolean>(as: string, bar: number): Omit<T, K> => as + bar',
|
||||||
|
'function foo <K, T extends Boolean>(as: string, bar: number): Omit<T, K> {\n return as + bar\n}',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'export const foo = () => {}',
|
||||||
|
'export function foo () {}',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'export const foo = () => ({})',
|
||||||
|
'export function foo () {\n return {}\n}',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'export const foo = async () => ({})',
|
||||||
|
'export async function foo () {\n return {}\n}',
|
||||||
|
],
|
||||||
|
]
|
||||||
|
|
||||||
|
it('runs', () => {
|
||||||
|
const ruleTester: RuleTester = new RuleTester({
|
||||||
|
parser: require.resolve('@typescript-eslint/parser'),
|
||||||
|
})
|
||||||
|
|
||||||
|
ruleTester.run(RULE_NAME, rule, {
|
||||||
|
valid: valids,
|
||||||
|
invalid: invalids.map(i => ({
|
||||||
|
code: i[0],
|
||||||
|
output: i[1],
|
||||||
|
errors: [{ messageId: 'topLevelFunctionDeclaration' }],
|
||||||
|
})),
|
||||||
|
})
|
||||||
|
})
|
83
packages/eslint-plugin-antfu/src/rules/top-level-function.ts
Normal file
83
packages/eslint-plugin-antfu/src/rules/top-level-function.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import { createEslintRule } from '../utils'
|
||||||
|
|
||||||
|
export const RULE_NAME = 'top-level-function'
|
||||||
|
export type MessageIds = 'topLevelFunctionDeclaration'
|
||||||
|
export type Options = []
|
||||||
|
|
||||||
|
export default createEslintRule<Options, MessageIds>({
|
||||||
|
name: RULE_NAME,
|
||||||
|
meta: {
|
||||||
|
type: 'problem',
|
||||||
|
docs: {
|
||||||
|
description: 'Enforce top-level functions to be declared with function keyword',
|
||||||
|
recommended: 'error',
|
||||||
|
},
|
||||||
|
fixable: 'code',
|
||||||
|
schema: [],
|
||||||
|
messages: {
|
||||||
|
topLevelFunctionDeclaration: 'Top-level functions should be declared with function keyword',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultOptions: [],
|
||||||
|
create: (context) => {
|
||||||
|
return {
|
||||||
|
VariableDeclaration(node) {
|
||||||
|
if (node.parent.type !== 'Program' && node.parent.type !== 'ExportNamedDeclaration')
|
||||||
|
return
|
||||||
|
|
||||||
|
if (node.declarations.length !== 1)
|
||||||
|
return
|
||||||
|
if (node.kind !== 'const')
|
||||||
|
return
|
||||||
|
if (node.declare)
|
||||||
|
return
|
||||||
|
|
||||||
|
const declaration = node.declarations[0]
|
||||||
|
|
||||||
|
if (declaration.init?.type !== 'ArrowFunctionExpression')
|
||||||
|
return
|
||||||
|
if (declaration.id?.type !== 'Identifier')
|
||||||
|
return
|
||||||
|
if (declaration.id.typeAnnotation)
|
||||||
|
return
|
||||||
|
|
||||||
|
const arrowFn = declaration.init
|
||||||
|
const body = declaration.init.body
|
||||||
|
const id = declaration.id
|
||||||
|
|
||||||
|
context.report({
|
||||||
|
node,
|
||||||
|
loc: {
|
||||||
|
start: id.loc.start,
|
||||||
|
end: body.loc.start,
|
||||||
|
},
|
||||||
|
messageId: 'topLevelFunctionDeclaration',
|
||||||
|
fix(fixer) {
|
||||||
|
const code = context.getSourceCode().text
|
||||||
|
const textName = code.slice(id.range[0], id.range[1])
|
||||||
|
const textArgs = arrowFn.params.length
|
||||||
|
? code.slice(arrowFn.params[0].range[0], arrowFn.params[arrowFn.params.length - 1].range[1])
|
||||||
|
: ''
|
||||||
|
const textBody = body.type === 'BlockStatement'
|
||||||
|
? code.slice(body.range[0], body.range[1])
|
||||||
|
: `{\n return ${code.slice(body.range[0], body.range[1])}\n}`
|
||||||
|
const textGeneric = arrowFn.typeParameters
|
||||||
|
? code.slice(arrowFn.typeParameters.range[0], arrowFn.typeParameters.range[1])
|
||||||
|
: ''
|
||||||
|
const textTypeReturn = arrowFn.returnType
|
||||||
|
? code.slice(arrowFn.returnType.range[0], arrowFn.returnType.range[1])
|
||||||
|
: ''
|
||||||
|
const textAsync = arrowFn.async ? 'async ' : ''
|
||||||
|
|
||||||
|
const final = `${textAsync}function ${textName} ${textGeneric}(${textArgs})${textTypeReturn} ${textBody}`
|
||||||
|
// console.log({
|
||||||
|
// input: code.slice(node.range[0], node.range[1]),
|
||||||
|
// output: final,
|
||||||
|
// })
|
||||||
|
return fixer.replaceTextRange([node.range[0], node.range[1]], final)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
2734
pnpm-lock.yaml
generated
2734
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user