Skip to content

Commit

Permalink
feat: support run eslint after oxlint
Browse files Browse the repository at this point in the history
  • Loading branch information
tmg0 committed Mar 10, 2024
1 parent 4957be7 commit 12f1c60
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 13 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
node_modules
dist

.oxlint
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"@antfu/eslint-config": "^2.8.0",
"@types/node": "^20.11.25",
"esbuild": "^0.20.1",
"eslint": "^8.57.0",
"jiti": "^1.21.0",
"oxlint": "^0.2.13",
"tsup": "^8.0.2",
Expand Down
7 changes: 7 additions & 0 deletions playground/tsup/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export const re = /(?:'|\"|`)/gm







3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import process from 'node:process'
import { detectPackageManager } from 'nypm'
import { version } from '../../package.json'
import type { OxlintContext, OxlintOptions, PackageManagerName } from './types'
import { runOxlintCommand } from './oxlint'
import { runLinkCommand } from './oxlint'

export function createOxlint(options: OxlintOptions) {
const ctx = createInternalContext(options)

async function init() {
await ctx.runOxlintCommand([], ctx)
await ctx.runLinkCommand([], ctx)
}

return {
Expand All @@ -18,7 +18,7 @@ export function createOxlint(options: OxlintOptions) {
}

function createInternalContext(options: OxlintOptions): OxlintContext {
let packageManagerName: PackageManagerName
let packageManagerName: PackageManagerName | undefined = options.packageManager

async function getPackageManager() {
if (!packageManagerName) {
Expand All @@ -32,6 +32,6 @@ function createInternalContext(options: OxlintOptions): OxlintContext {
version,
options,
getPackageManager,
runOxlintCommand,
runLinkCommand,
}
}
37 changes: 36 additions & 1 deletion src/core/oxlint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,39 @@ export async function runOxlintCommand(ids: string | string[], ctx: OxlintContex
], ctx)
}

export async function runEslintCommand() {}
export async function doesDependencyExist(name: string) {
const packageJSON = await import(join(process.cwd(), 'package.json'))
if (Object.keys(packageJSON.dependencies).includes(name))
return true
if (Object.keys(packageJSON.devDependencies).includes(name))
return true
return false
}

export async function runEslintCommand(ids: string | string[], ctx: OxlintContext) {
const options = ctx.options

const paths = (() => {
if (Array.isArray(ids) ? !!ids.length : !!ids)
return [ids]
if (options.path)
return [options.path]
return ['.']
})().flat().map(path => join(process.cwd(), path))

try {
await runNpxCommand('eslint', [
options.fix ? '--fix' : '',
...paths,
], ctx)
}
catch {
// TODO: Throw error
}
}

export async function runLinkCommand(ids: string | string[], ctx: OxlintContext) {
await runOxlintCommand(ids, ctx)
if (await doesDependencyExist('eslint'))
await runEslintCommand(ids, ctx)
}
3 changes: 2 additions & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface OxlintOptions {
noIgnore: boolean
quiet: boolean
denyWarnings: boolean
packageManager?: PackageManagerName | undefined
}

export type NpxCommand = 'oxlint' | 'eslint'
Expand All @@ -25,5 +26,5 @@ export interface OxlintContext {
version: string
options: OxlintOptions
getPackageManager: () => Promise<PackageManagerName>
runOxlintCommand: (ids: string | string[], ctx: OxlintContext) => Promise<void>
runLinkCommand: (ids: string | string[], ctx: OxlintContext) => Promise<void>
}
8 changes: 3 additions & 5 deletions tests/esbuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ it('esbuild', async () => {
plugins: [
Oxlint({
path: 'playground/tsup',
packageManager: 'npm',
}),
],
})

setTimeout(async () => {
const raw = (await import(`${ENTRY_POINT}?raw`)).default
expect(raw.includes('|"|')).toBe(true)
}, 500)
})
expect(true).toBe(true)
}, 60 * 1000)

0 comments on commit 12f1c60

Please sign in to comment.