Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
4b51ef2eba | ||
|
588494cc01 | ||
|
3c1207657f | ||
|
da59484756 | ||
|
501ff74200 | ||
|
adaf70b651 | ||
|
3ed0230512 | ||
|
ac3f63086d | ||
|
f7a5571259 |
10
README.md
10
README.md
@@ -4,7 +4,7 @@
|
||||
|
||||
- Single quotes, no semi
|
||||
- Auto fix for formatting (aimed to be used standalone without Prettier)
|
||||
- TypeScript, Vue, React out-of-box
|
||||
- Designed to work with TypeScript, Vue out-of-box
|
||||
- Lint also for json, yaml, markdown
|
||||
- Sorted imports, dangling commas for cleaner commit diff
|
||||
- Reasonable defaults, best practices, only one-line of config
|
||||
@@ -42,22 +42,26 @@ For example:
|
||||
|
||||
### Config VS Code auto fix
|
||||
|
||||
Create `.vscode/settings.json`
|
||||
Install [VS Code ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) and create `.vscode/settings.json`
|
||||
|
||||
```json
|
||||
{
|
||||
"prettier.enable": false,
|
||||
"editor.formatOnSave": false,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Extended Reading
|
||||
|
||||
Learn more about the context - [Why I don't use Prettier](https://antfu.me/posts/why-not-prettier).
|
||||
|
||||
## Check Also
|
||||
|
||||
- [antfu/dotfiles](https://github.com/antfu/dotfiles) - My dotfiles
|
||||
- [antfu/vscode-settings](https://github.com/antfu/vscode-settings) - My VS Code settings
|
||||
- [antfu/eslint-config](https://github.com/antfu/eslint-config) - My ESLint config
|
||||
- [antfu/ts-starter](https://github.com/antfu/ts-starter) - My starter template for TypeScript library
|
||||
- [antfu/vitesse](https://github.com/antfu/vitesse) - My starter template for Vue & Vite app
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@antfu/eslint-config-monorepo",
|
||||
"version": "0.27.0",
|
||||
"version": "0.29.0",
|
||||
"private": true,
|
||||
"packageManager": "pnpm@7.1.0",
|
||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||
@@ -16,6 +16,7 @@
|
||||
"bumpp": "^8.2.1",
|
||||
"eslint": "^8.23.0",
|
||||
"eslint-plugin-antfu": "workspace:*",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^4.8.2"
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@antfu/eslint-config",
|
||||
"version": "0.27.0",
|
||||
"version": "0.29.0",
|
||||
"description": "Anthony's ESLint config",
|
||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||
"license": "MIT",
|
||||
|
@@ -35,6 +35,7 @@ module.exports = {
|
||||
'html',
|
||||
'unicorn',
|
||||
'antfu',
|
||||
'no-only-tests',
|
||||
],
|
||||
settings: {
|
||||
'import/resolver': {
|
||||
@@ -155,6 +156,7 @@ module.exports = {
|
||||
files: ['*.test.ts', '*.test.js', '*.spec.ts', '*.spec.js'],
|
||||
rules: {
|
||||
'no-unused-expressions': 'off',
|
||||
'no-only-tests/no-only-tests': 'error',
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@antfu/eslint-config-basic",
|
||||
"version": "0.27.0",
|
||||
"version": "0.29.0",
|
||||
"description": "",
|
||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||
"license": "MIT",
|
||||
@@ -26,6 +26,7 @@
|
||||
"eslint-plugin-jsonc": "^2.4.0",
|
||||
"eslint-plugin-markdown": "^3.0.0",
|
||||
"eslint-plugin-n": "^15.2.5",
|
||||
"eslint-plugin-no-only-tests": "^3.1.0",
|
||||
"eslint-plugin-promise": "^6.0.1",
|
||||
"eslint-plugin-unicorn": "^43.0.2",
|
||||
"eslint-plugin-yml": "^1.1.0",
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "eslint-plugin-antfu",
|
||||
"version": "0.27.0",
|
||||
"version": "0.29.0",
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/antfu/eslint-config",
|
||||
"main": "./dist/index.cjs",
|
||||
@@ -19,6 +19,7 @@
|
||||
"@typescript-eslint/utils": "^5.36.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.11.8",
|
||||
"unbuild": "^0.8.10",
|
||||
"vitest": "^0.22.1"
|
||||
}
|
||||
|
@@ -1,3 +1,4 @@
|
||||
import genericSpacing from './rules/generic-spacing'
|
||||
import ifNewline from './rules/if-newline'
|
||||
import importDedupe from './rules/import-dedupe'
|
||||
import preferInlineTypeImport from './rules/prefer-inline-type-import'
|
||||
@@ -7,5 +8,6 @@ export default {
|
||||
'if-newline': ifNewline,
|
||||
'import-dedupe': importDedupe,
|
||||
'prefer-inline-type-import': preferInlineTypeImport,
|
||||
'generic-spacing': genericSpacing,
|
||||
},
|
||||
}
|
||||
|
@@ -0,0 +1,32 @@
|
||||
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint'
|
||||
import { it } from 'vitest'
|
||||
import rule, { RULE_NAME } from './generic-spacing'
|
||||
|
||||
const valids = [
|
||||
'type Foo<T = true> = T',
|
||||
`
|
||||
type Foo<
|
||||
T = true,
|
||||
K = false
|
||||
> = T`,
|
||||
]
|
||||
const invalids = [
|
||||
['type Foo<T=true> = T', 'type Foo<T = true> = T'],
|
||||
['type Foo<T,K> = T', 'type Foo<T, K> = T'],
|
||||
['type Foo<T=false,K=1|2> = T', 'type Foo<T = false, K = 1|2> = T', 3],
|
||||
] as const
|
||||
|
||||
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].trim(),
|
||||
errors: Array.from({ length: i[2] || 1 }, () => ({ messageId: 'genericSpacingMismatch' })),
|
||||
})),
|
||||
})
|
||||
})
|
69
packages/eslint-plugin-antfu/src/rules/generic-spacing.ts
Normal file
69
packages/eslint-plugin-antfu/src/rules/generic-spacing.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { createEslintRule } from '../utils'
|
||||
|
||||
export const RULE_NAME = 'generic-spacing'
|
||||
export type MessageIds = 'genericSpacingMismatch'
|
||||
export type Options = []
|
||||
|
||||
export default createEslintRule<Options, MessageIds>({
|
||||
name: RULE_NAME,
|
||||
meta: {
|
||||
type: 'suggestion',
|
||||
docs: {
|
||||
description: 'Spaces around generic type parameters',
|
||||
recommended: 'error',
|
||||
},
|
||||
fixable: 'code',
|
||||
schema: [],
|
||||
messages: {
|
||||
genericSpacingMismatch: 'Generic spaces mismatch',
|
||||
},
|
||||
},
|
||||
defaultOptions: [],
|
||||
create: (context) => {
|
||||
const sourceCode = context.getSourceCode()
|
||||
return {
|
||||
TSTypeParameterDeclaration: (node) => {
|
||||
const params = node.params
|
||||
for (let i = 1; i < params.length; i++) {
|
||||
const prev = params[i - 1]
|
||||
const current = params[i]
|
||||
const from = prev.range[1]
|
||||
const to = current.range[0]
|
||||
const span = sourceCode.text.slice(from, to)
|
||||
if (span !== ', ' && !span.match(/,\n/)) {
|
||||
context.report({
|
||||
*fix(fixer) {
|
||||
yield fixer.replaceTextRange([from, to], ', ')
|
||||
},
|
||||
loc: {
|
||||
start: prev.loc.end,
|
||||
end: current.loc.start,
|
||||
},
|
||||
messageId: 'genericSpacingMismatch',
|
||||
node,
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
TSTypeParameter: (node) => {
|
||||
if (!node.default)
|
||||
return
|
||||
const from = node.name.range[1]
|
||||
const to = node.default.range[0]
|
||||
if (sourceCode.text.slice(from, to) !== ' = ') {
|
||||
context.report({
|
||||
*fix(fixer) {
|
||||
yield fixer.replaceTextRange([from, to], ' = ')
|
||||
},
|
||||
loc: {
|
||||
start: node.name.loc.end,
|
||||
end: node.default.loc.start,
|
||||
},
|
||||
messageId: 'genericSpacingMismatch',
|
||||
node,
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@antfu/eslint-config-react",
|
||||
"version": "0.27.0",
|
||||
"version": "0.29.0",
|
||||
"description": "",
|
||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||
"license": "MIT",
|
||||
|
@@ -106,6 +106,9 @@ module.exports = {
|
||||
'lines-between-class-members': 'off',
|
||||
'@typescript-eslint/lines-between-class-members': ['error', 'always', { exceptAfterSingleLine: true }],
|
||||
|
||||
// antfu
|
||||
'antfu/generic-spacing': 'error',
|
||||
|
||||
// The following rule overrides require a parser service, aka. require a `typescript.json` path.
|
||||
// This needs to be done individually for each project, and it slows down linting significantly.
|
||||
// 'no-throw-literal': 'off',
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@antfu/eslint-config-ts",
|
||||
"version": "0.27.0",
|
||||
"version": "0.29.0",
|
||||
"description": "",
|
||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||
"license": "MIT",
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@antfu/eslint-config-vue",
|
||||
"version": "0.27.0",
|
||||
"version": "0.29.0",
|
||||
"description": "",
|
||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||
"license": "MIT",
|
||||
|
19
pnpm-lock.yaml
generated
19
pnpm-lock.yaml
generated
@@ -8,12 +8,14 @@ importers:
|
||||
bumpp: ^8.2.1
|
||||
eslint: ^8.23.0
|
||||
eslint-plugin-antfu: workspace:*
|
||||
rimraf: ^3.0.2
|
||||
typescript: ^4.8.2
|
||||
devDependencies:
|
||||
'@antfu/eslint-config': link:packages/all
|
||||
bumpp: 8.2.1
|
||||
eslint: 8.23.0
|
||||
eslint-plugin-antfu: link:packages/eslint-plugin-antfu
|
||||
rimraf: 3.0.2
|
||||
typescript: 4.8.2
|
||||
|
||||
packages/all:
|
||||
@@ -61,6 +63,7 @@ importers:
|
||||
eslint-plugin-jsonc: ^2.4.0
|
||||
eslint-plugin-markdown: ^3.0.0
|
||||
eslint-plugin-n: ^15.2.5
|
||||
eslint-plugin-no-only-tests: ^3.1.0
|
||||
eslint-plugin-promise: ^6.0.1
|
||||
eslint-plugin-unicorn: ^43.0.2
|
||||
eslint-plugin-yml: ^1.1.0
|
||||
@@ -74,6 +77,7 @@ importers:
|
||||
eslint-plugin-jsonc: 2.4.0_eslint@8.23.0
|
||||
eslint-plugin-markdown: 3.0.0_eslint@8.23.0
|
||||
eslint-plugin-n: 15.2.5_eslint@8.23.0
|
||||
eslint-plugin-no-only-tests: 3.1.0
|
||||
eslint-plugin-promise: 6.0.1_eslint@8.23.0
|
||||
eslint-plugin-unicorn: 43.0.2_eslint@8.23.0
|
||||
eslint-plugin-yml: 1.1.0_eslint@8.23.0
|
||||
@@ -84,12 +88,14 @@ importers:
|
||||
|
||||
packages/eslint-plugin-antfu:
|
||||
specifiers:
|
||||
'@types/node': ^18.11.8
|
||||
'@typescript-eslint/utils': ^5.36.1
|
||||
unbuild: ^0.8.10
|
||||
vitest: ^0.22.1
|
||||
dependencies:
|
||||
'@typescript-eslint/utils': 5.36.1
|
||||
devDependencies:
|
||||
'@types/node': 18.11.8
|
||||
unbuild: 0.8.10
|
||||
vitest: 0.22.1
|
||||
|
||||
@@ -580,8 +586,8 @@ packages:
|
||||
'@types/unist': 2.0.6
|
||||
dev: false
|
||||
|
||||
/@types/node/17.0.23:
|
||||
resolution: {integrity: sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==}
|
||||
/@types/node/18.11.8:
|
||||
resolution: {integrity: sha512-uGwPWlE0Hj972KkHtCDVwZ8O39GmyjfMane1Z3GUBGGnkZ2USDq7SxLpVIiIHpweY9DS0QTDH0Nw7RNBsAAZ5A==}
|
||||
dev: true
|
||||
|
||||
/@types/normalize-package-data/2.4.0:
|
||||
@@ -591,7 +597,7 @@ packages:
|
||||
/@types/resolve/1.17.1:
|
||||
resolution: {integrity: sha512-yy7HuzQhj0dhGpD8RLXSZWEkLsV9ibvxvi6EiJ3bkqLAO1RGo0WbkWQiwpRlSFymTJRz0d3k5LM3kkx8ArDbLw==}
|
||||
dependencies:
|
||||
'@types/node': 17.0.23
|
||||
'@types/node': 18.11.8
|
||||
dev: true
|
||||
|
||||
/@types/unist/2.0.6:
|
||||
@@ -1849,6 +1855,11 @@ packages:
|
||||
semver: 7.3.7
|
||||
dev: false
|
||||
|
||||
/eslint-plugin-no-only-tests/3.1.0:
|
||||
resolution: {integrity: sha512-Lf4YW/bL6Un1R6A76pRZyE1dl1vr31G/ev8UzIc/geCgFWyrKil8hVjYqWVKGB/UIGmb6Slzs9T0wNezdSVegw==}
|
||||
engines: {node: '>=5.0.0'}
|
||||
dev: false
|
||||
|
||||
/eslint-plugin-promise/6.0.1_eslint@8.23.0:
|
||||
resolution: {integrity: sha512-uM4Tgo5u3UWQiroOyDEsYcVMOo7re3zmno0IZmB5auxoaQNIceAbXEkSt8RNrKtaYehARHG06pYK6K1JhtP0Zw==}
|
||||
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
|
||||
@@ -3645,7 +3656,7 @@ packages:
|
||||
dependencies:
|
||||
'@types/chai': 4.3.3
|
||||
'@types/chai-subset': 1.3.3
|
||||
'@types/node': 17.0.23
|
||||
'@types/node': 18.11.8
|
||||
chai: 4.3.6
|
||||
debug: 4.3.4
|
||||
local-pkg: 0.4.2
|
||||
|
@@ -1,6 +1,9 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": "."
|
||||
"baseUrl": ".",
|
||||
"target": "es2020",
|
||||
"module": "es2020",
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"include": [
|
||||
"./packages/**/*.ts"
|
||||
|
Reference in New Issue
Block a user