Compare commits

..

7 Commits

Author SHA1 Message Date
Anthony Fu
59308e8e36 chore: release v0.40.0 2023-08-01 17:00:05 +02:00
曾明健
cf1240c847 feat: add rule no-import-node-modules-by-path (#219) 2023-08-01 16:59:19 +02:00
Ted Xu
0ff5e45fdd feat: format for named tuple (#229) 2023-08-01 16:58:56 +02:00
Anthony Fu
effc708692 feat: add default in exports 2023-08-01 16:58:10 +02:00
Anthony Fu
ea307c91e5 fix: put import before require in exports 2023-08-01 16:56:25 +02:00
曾明健
b7ce1614d8 docs: make config correct (#224) 2023-07-20 14:44:19 +02:00
Cloyd Lau
bf390b0693 docs: add code style badge (#220)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
2023-07-17 16:53:38 +02:00
15 changed files with 274 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
# @antfu/eslint-config
[![npm](https://img.shields.io/npm/v/@antfu/eslint-config?color=a1b858&label=)](https://npmjs.com/package/@antfu/eslint-config)
[![npm](https://img.shields.io/npm/v/@antfu/eslint-config?color=444&label=)](https://npmjs.com/package/@antfu/eslint-config) [![code style](https://antfu.me/badge-code-style.svg)](https://github.com/antfu/eslint-config)
- Single quotes, no semi
- Auto fix for formatting (aimed to be used standalone **without** Prettier)
@@ -81,6 +81,7 @@ Type aware rules are enabled when a `tsconfig.eslint.json` is found in the proje
```js
// .eslintrc.js
const process = require('node:process')
process.env.ESLINT_TSCONFIG = 'tsconfig.json'
module.exports = {
@@ -109,6 +110,16 @@ and then
npm i -D lint-staged simple-git-hooks
```
## Badge
If you enjoy this code style, and would like to mention it in your project, here is the badge you can use:
```md
[![code style](https://antfu.me/badge-code-style.svg)](https://github.com/antfu/eslint-config)
```
[![code style](https://antfu.me/badge-code-style.svg)](https://github.com/antfu/eslint-config)
## FAQ
### Prettier?

View File

@@ -1,6 +1,6 @@
{
"name": "@antfu/eslint-config-monorepo",
"version": "0.39.8",
"version": "0.40.0",
"private": true,
"packageManager": "pnpm@8.6.7",
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",

View File

@@ -142,8 +142,9 @@ module.exports = {
pathPattern: '^exports.*$',
order: [
'types',
'require',
'import',
'require',
'default',
],
},
],
@@ -395,6 +396,7 @@ module.exports = {
'yml/no-empty-document': 'off',
// antfu
'antfu/no-import-node-modules-by-path': 'error',
'antfu/if-newline': 'error',
'antfu/import-dedupe': 'error',
'antfu/top-level-function': 'error',

View File

@@ -1,6 +1,6 @@
{
"name": "@antfu/eslint-config-basic",
"version": "0.39.8",
"version": "0.40.0",
"description": "",
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
"license": "MIT",

View File

@@ -1,6 +1,6 @@
{
"name": "@antfu/eslint-config-react",
"version": "0.39.8",
"version": "0.40.0",
"description": "",
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
"license": "MIT",

View File

@@ -161,6 +161,7 @@ module.exports = {
'antfu/no-cjs-exports': 'error',
'antfu/no-ts-export-equal': 'error',
'antfu/no-const-enum': 'error',
'antfu/named-tuple-spacing': 'error',
// off
'@typescript-eslint/consistent-indexed-object-style': 'off',

View File

@@ -1,6 +1,6 @@
{
"name": "@antfu/eslint-config-ts",
"version": "0.39.8",
"version": "0.40.0",
"description": "",
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
"license": "MIT",

View File

@@ -1,6 +1,6 @@
{
"name": "@antfu/eslint-config-vue",
"version": "0.39.8",
"version": "0.40.0",
"description": "",
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
"license": "MIT",

View File

@@ -1,6 +1,6 @@
{
"name": "@antfu/eslint-config",
"version": "0.39.8",
"version": "0.40.0",
"description": "Anthony's ESLint config",
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
"license": "MIT",

View File

@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-antfu",
"version": "0.39.8",
"version": "0.40.0",
"license": "MIT",
"homepage": "https://github.com/antfu/eslint-config",
"main": "./dist/index.cjs",

View File

@@ -3,9 +3,11 @@ import ifNewline from './rules/if-newline'
import importDedupe from './rules/import-dedupe'
import preferInlineTypeImport from './rules/prefer-inline-type-import'
import topLevelFunction from './rules/top-level-function'
import noImportNodeModulesByPath from './rules/no-import-node-modules-by-path'
import noTsExportEqual from './rules/no-ts-export-equal'
import noCjsExports from './rules/no-cjs-exports'
import noConstEnum from './rules/no-const-enum'
import namedTupleSpacing from './rules/named-tuple-spacing'
export default {
rules: {
@@ -14,8 +16,10 @@ export default {
'prefer-inline-type-import': preferInlineTypeImport,
'generic-spacing': genericSpacing,
'top-level-function': topLevelFunction,
'no-import-node-modules-by-path': noImportNodeModulesByPath,
'no-cjs-exports': noCjsExports,
'no-ts-export-equal': noTsExportEqual,
'no-const-enum': noConstEnum,
'named-tuple-spacing': namedTupleSpacing,
},
}

View File

@@ -0,0 +1,95 @@
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint'
import { it } from 'vitest'
import rule, { RULE_NAME } from './named-tuple-spacing'
const valids = [
'type T = [i: number]',
'type T = [i?: number]',
'type T = [i: number, j: number]',
`const emit = defineEmits<{
change: [id: number]
update: [value: string]
}>()`,
]
it('runs', () => {
const ruleTester: RuleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
})
ruleTester.run(RULE_NAME, rule, {
valid: valids,
invalid: [
{
code: 'type T = [i:number]',
output: 'type T = [i: number]',
errors: [{ messageId: 'expectedSpaceAfter' }],
},
{
code: 'type T = [i: number]',
output: 'type T = [i: number]',
errors: [{ messageId: 'expectedSpaceAfter' }],
},
{
code: 'type T = [i?:number]',
output: 'type T = [i?: number]',
errors: [{ messageId: 'expectedSpaceAfter' }],
},
{
code: 'type T = [i? :number]',
output: 'type T = [i?: number]',
errors: [{ messageId: 'unexpectedSpaceBetween' }, { messageId: 'expectedSpaceAfter' }],
},
{
code: 'type T = [i : number]',
output: 'type T = [i: number]',
errors: [{ messageId: 'unexpectedSpaceBefore' }],
},
{
code: 'type T = [i : number]',
output: 'type T = [i: number]',
errors: [{ messageId: 'unexpectedSpaceBefore' }],
},
{
code: 'type T = [i ? : number]',
output: 'type T = [i?: number]',
errors: [{ messageId: 'unexpectedSpaceBetween' }, { messageId: 'unexpectedSpaceBefore' }],
},
{
code: 'type T = [i:number, j:number]',
output: 'type T = [i: number, j: number]',
errors: [{ messageId: 'expectedSpaceAfter' }, { messageId: 'expectedSpaceAfter' }],
},
{
code: `
const emit = defineEmits<{
change: [id:number]
update: [value:string]
}>()
`,
output: `
const emit = defineEmits<{
change: [id: number]
update: [value: string]
}>()
`,
errors: [{ messageId: 'expectedSpaceAfter' }, { messageId: 'expectedSpaceAfter' }],
},
{
code: `
const emit = defineEmits<{
change: [id? :number]
update: [value:string]
}>()
`,
output: `
const emit = defineEmits<{
change: [id?: number]
update: [value: string]
}>()
`,
errors: [{ messageId: 'unexpectedSpaceBetween' }, { messageId: 'expectedSpaceAfter' }, { messageId: 'expectedSpaceAfter' }],
},
],
})
})

View File

@@ -0,0 +1,79 @@
import { createEslintRule } from '../utils'
export const RULE_NAME = 'named-tuple-spacing'
export type MessageIds = 'expectedSpaceAfter' | 'unexpectedSpaceBetween' | 'unexpectedSpaceBefore'
export type Options = []
export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Expect space before type declaration in named tuple',
recommended: 'error',
},
fixable: 'code',
schema: [],
messages: {
expectedSpaceAfter: 'Expected a space after the \':\'.',
unexpectedSpaceBetween: 'Unexpected space between \'?\' and the \':\'.',
unexpectedSpaceBefore: 'Unexpected space before the \':\'.',
},
},
defaultOptions: [],
create: (context) => {
const sourceCode = context.getSourceCode()
return {
TSNamedTupleMember: (node) => {
const code = sourceCode.text.slice(node.range[0], node.range[1])
const reg = /(\w+)(\s*)(\?\s*)?:(\s*)(\w+)/
const labelName = node.label.name
const spaceBeforeColon = code.match(reg)?.[2]
const optionalMark = code.match(reg)?.[3]
const spacesAfterColon = code.match(reg)?.[4]
const elementType = code.match(reg)?.[5]
function getReplaceValue() {
let ret = labelName
if (node.optional)
ret += '?'
ret += ': '
ret += elementType
return ret
}
if (optionalMark?.length > 1) {
context.report({
node,
messageId: 'unexpectedSpaceBetween',
*fix(fixer) {
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
},
})
}
if (spaceBeforeColon?.length) {
context.report({
node,
messageId: 'unexpectedSpaceBefore',
*fix(fixer) {
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
},
})
}
if (spacesAfterColon.length !== 1) {
context.report({
node,
messageId: 'expectedSpaceAfter',
*fix(fixer) {
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
},
})
}
},
}
},
})

View File

@@ -0,0 +1,31 @@
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint'
import { it } from 'vitest'
import rule, { RULE_NAME } from './no-import-node-modules-by-path'
const valids = [
'import xxx from "a"',
'import "b"',
'const c = require("c")',
'require("d")',
]
const invalids = [
'import a from "../node_modules/a"',
'import "../node_modules/b"',
'const c = require("../node_modules/c")',
'require("../node_modules/d")',
]
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,
errors: [{ messageId: 'noImportNodeModulesByPath' }],
})),
})
})

View File

@@ -0,0 +1,42 @@
import { createEslintRule } from '../utils'
export const RULE_NAME = 'no-import-node-modules-by-path'
export type MessageIds = 'noImportNodeModulesByPath'
export type Options = []
export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Prevent importing modules in `node_modules` folder by relative or absolute path',
recommended: 'error',
},
schema: [],
messages: {
noImportNodeModulesByPath: 'Do not import modules in `node_modules` folder by path',
},
},
defaultOptions: [],
create: (context) => {
return {
'ImportDeclaration': (node) => {
if (node.source.value.includes('/node_modules/')) {
context.report({
node,
messageId: 'noImportNodeModulesByPath',
})
}
},
'CallExpression[callee.name="require"]': (node: any) => {
const value = node.arguments[0]?.value
if (typeof value === 'string' && value.includes('/node_modules/')) {
context.report({
node,
messageId: 'noImportNodeModulesByPath',
})
}
},
}
},
})