Skip to content

Commit

Permalink
feat: deny supports array
Browse files Browse the repository at this point in the history
* chore: inspector is unusual

* fix: typo

* chore: wip

* fix: typo `=` to `===`

* chore: update
  • Loading branch information
lvjiaxuan committed Apr 11, 2024
1 parent 66e0dcc commit 34b26db
Show file tree
Hide file tree
Showing 12 changed files with 1,027 additions and 368 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,27 @@ export default lv({
Options type, respects its original [options](https://oxc-project.github.io/docs/guide/usage/linter.html#useful-options):
<!-- eslint-skip -->
```ts
type OptionsOXLint = {
deny?: Categories | 'all'
/**
* References https://oxc-project.github.io/docs/guide/usage/linter.html#useful-options-and-examples
*/
export type OXLintOptions = {

/**
* Deny the rule or category.
*
* @default 'correctness'
*/
deny?: 'all' | Categories | Categories[]

/**
* Allow the rule or category.
*/
allow?: (keyof OXLintRules)[]
// plugins: TODO
} | boolean
```
> [!Tip]
> 1. [Categories](https://github.com/oxc-project/oxc/blob/2beacd3f4d2707ab64ff98bf05462673e9993b71/crates/oxc_linter/src/rule.rs#L37) of OXLint.
> 1. [Categories](https://github.com/oxc-project/oxc/blob/main/crates/oxc_linter/src/rule.rs#L35) of OXLint.
> 2. [Rules](https://github.com/oxc-project/oxc/tree/main/crates/oxc_linter/src/rules) supported by OXLint.
Modify lint scritp:
Expand All @@ -63,7 +75,7 @@ Modify lint scritp:
"scripts": {
- "lint": "eslint ."
+ "lint": "npx oxlint . && eslint .",
+ "lint:fix": "npx oxlint . --fix && eslint . --fix"
+ "lintf": "npx oxlint . --fix && eslint . --fix"
}
}
```
18 changes: 7 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,33 @@
"private": true,
"scripts": {
"lint": "oxlint . && eslint .",
"lint:fix": "oxlint . --fix && eslint . --fix"
"lintf": "oxlint . --fix && eslint . --fix",
"view": "pnpm dlx @eslint/config-inspector"
},
"devDependencies": {
"@antfu/eslint-config": "^2.12.2",
"@antfu/eslint-config": "^2.13.3",
"@lvjiaxuan/eslint-config": "workspace:*",
"@lvjiaxuan/eslint-plugin-oxlint": "workspace:^",
"@types/eslint": "^8.56.7",
"@types/node": "^20.12.5",
"@types/node": "^20.12.7",
"eslint": "npm:eslint-ts-patch@8.56.0-0",
"eslint-ts-patch": "8.56.0-0",
"oxlint": "^0.2.15",
"oxlint": "^0.2.16",
"simple-git-hooks": "^2.11.1",
"tsup": "^8.0.2",
"typescript": "^5.4.4",
"typescript": "^5.4.5",
"vitest": "^1.4.0"
},
"lvr": {
"all": true,
"tag": 10
},
"pnpm": {
"patchedDependencies": {
"@antfu/eslint-config@2.12.2": "patches/@antfu__eslint-config@2.12.2.patch"
}
},
"simple-git-hooks": {
"pre-commit": [
"npx lint-staged"
]
},
"lint-staged": {
"*": "pnpm run lint:fix"
"*": "pnpm run lintf"
}
}
4 changes: 2 additions & 2 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.ms",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
Expand All @@ -46,7 +46,7 @@
}
},
"dependencies": {
"@antfu/eslint-config": "^2.12.2",
"@antfu/eslint-config": "^2.13.3",
"@lvjiaxuan/eslint-plugin": "workspace:*",
"@lvjiaxuan/eslint-plugin-oxlint": "workspace:*",
"fs-extra": "^11.2.0"
Expand Down
8 changes: 6 additions & 2 deletions packages/eslint-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ const lv: (...args: AntfuParams) => AntfuReturnType = (...args) => {

let isUseDetected = false
if (typeof tsOptions === 'object') {
if (tsOptions.notDetectTsconfig === true) {
if ('notDetectTsconfig' in tsOptions && tsOptions.notDetectTsconfig === true) {
// Do nothing.
}
else if (!Object.hasOwn(tsOptions, 'tsconfigPath')) {
// Add detected `tsconfigPath` if no-set.
const paths = await detectTsconfigPaths()

if (paths.length) {
tsOptions.tsconfigPath = paths
isUseDetected = true
Expand All @@ -47,6 +48,9 @@ const lv: (...args: AntfuParams) => AntfuReturnType = (...args) => {

tsOptions.parserOptions ??= {}
tsOptions.parserOptions = {
/**
* @see https://github.com/typescript-eslint/typescript-eslint/issues/2094#issuecomment-1820936720
*/
warnOnUnsupportedTypeScriptVersion: true,
EXPERIMENTAL_useProjectService: true,
...tsOptions.parserOptions,
Expand Down Expand Up @@ -79,7 +83,7 @@ const lv: (...args: AntfuParams) => AntfuReturnType = (...args) => {
flatConfigItemsWithTsConfig.find(item => item.name === 'antfu/typescript/parser')!,
)

const rulesItemIdx = configs.findIndex(item => item.name = 'antfu/typescript/rules')
const rulesItemIdx = configs.findIndex(item => item.name === 'antfu/typescript/rules')
configs.splice(
rulesItemIdx + 1,
0,
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-oxlint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@antfu/eslint-config": "^2.12.2",
"@antfu/eslint-config": "^2.13.3",
"@antfu/eslint-define-config": "1.23.0-2",
"@type-challenges/utils": "^0.1.1"
}
Expand Down
37 changes: 17 additions & 20 deletions packages/eslint-plugin-oxlint/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
import type { RuleLevel } from '@antfu/eslint-define-config'
import type { TypedFlatConfigItem } from '@antfu/eslint-config'
import type { MergeInsertions, UnionToIntersection } from '@type-challenges/utils'
import type { Categories, CategoryRules, OXLintOptions, OXLintRules } from './types'
import categoryRules from './../category-rules.json'

type CategoryRules<Rules extends typeof categoryRules = typeof categoryRules> = MergeInsertions<{
[K in keyof Rules]: {
[KK in keyof Rules[K]]: RuleLevel
}
}>

type Categories = keyof typeof categoryRules

const rules = categoryRules as CategoryRules

export type OXLintRules = MergeInsertions<UnionToIntersection<CategoryRules[Categories]>>

export type OXLintOptions = {
deny?: Categories | 'all'
allow?: (keyof OXLintRules)[]
// TODO plugins
} | boolean
export * from './types'

export function oxlint(options: OXLintOptions = true): TypedFlatConfigItem[] {
if (options === true)
Expand All @@ -29,16 +14,29 @@ export function oxlint(options: OXLintOptions = true): TypedFlatConfigItem[] {

let denyRules: Partial<OXLintRules> = {}

// deny option
if (options.deny === 'all') {
for (const c in rules)
denyRules = { ...denyRules, ...rules[c as Categories] }
}
else if (options.deny) {
else if (typeof options.deny === 'string') {
denyRules = rules[options.deny]
}
else if (Array.isArray(options.deny)) {
for (const c of options.deny) {
if (options.deny.includes(c))
denyRules = { ...denyRules, ...rules[c] }
}
}

// allow option
for (const r of options?.allow ?? []) {
if (denyRules[r])
delete denyRules[r]
}

return [{
name: 'lvjixuan/plugin/oxlint',
name: 'lvjiaxuan/plugin/oxlint',
rules: denyRules,
}]
}
Expand All @@ -51,6 +49,5 @@ export default {
acc[c as Categories] = { rules: r as CategoryRules[Categories] }
return acc
}, {} as Record<Categories, { rules: CategoryRules[Categories] }>),
// plugins classification
},
}
30 changes: 30 additions & 0 deletions packages/eslint-plugin-oxlint/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { MergeInsertions, UnionToIntersection } from '@type-challenges/utils'
import type categoryRules from './../category-rules.json'

export type CategoryRules<Rules extends typeof categoryRules = typeof categoryRules> = MergeInsertions<{
[K in keyof Rules]: {
[KK in keyof Rules[K]]: 'off'
}
}>

export type Categories = keyof typeof categoryRules

export type OXLintRules = MergeInsertions<UnionToIntersection<CategoryRules[Categories]>>

/**
* References https://oxc-project.github.io/docs/guide/usage/linter.html#useful-options-and-examples
*/
export type OXLintOptions = {

/**
* Deny the rule or category.
*
* @default 'correctness'
*/
deny?: 'all' | Categories | Categories[]

/**
* Allow the rule or category.
*/
allow?: (keyof OXLintRules)[]
} | boolean
6 changes: 3 additions & 3 deletions packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
"eslint": ">=8.21.0"
},
"dependencies": {
"@antfu/eslint-config": "^2.12.2",
"@typescript-eslint/utils": "^7.5.0"
"@antfu/eslint-config": "^2.13.3",
"@typescript-eslint/utils": "^7.6.0"
},
"devDependencies": {
"@typescript-eslint/rule-tester": "^7.5.0"
"@typescript-eslint/rule-tester": "^7.6.0"
},
"publishConfig": {
"access": "public",
Expand Down
60 changes: 0 additions & 60 deletions patches/@antfu__eslint-config@2.12.2.patch

This file was deleted.

0 comments on commit 34b26db

Please sign in to comment.