|
|
@@ -1,14 +1,15 @@
|
|
|
import { spawnSync } from 'node:child_process'
|
|
|
import { randomUUID } from 'node:crypto'
|
|
|
+import { existsSync } from 'node:fs'
|
|
|
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'
|
|
|
import { join, relative } from 'node:path'
|
|
|
-import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
|
+import { fileURLToPath } from 'node:url'
|
|
|
import { flattenDiagnosticMessageText, parseConfigFileTextToJson } from 'typescript'
|
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
|
|
const repositoryRoot = fileURLToPath(new URL('..', import.meta.url))
|
|
|
-const eslintCli = fileURLToPath(new URL('../node_modules/eslint/bin/eslint.js', import.meta.url))
|
|
|
const oxlintCli = fileURLToPath(new URL('../node_modules/oxlint/bin/oxlint', import.meta.url))
|
|
|
+const tsxCli = fileURLToPath(new URL('../node_modules/tsx/dist/cli.mjs', import.meta.url))
|
|
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
|
@@ -18,11 +19,11 @@ function isUnknownArray(value: unknown): value is unknown[] {
|
|
|
return Array.isArray(value)
|
|
|
}
|
|
|
|
|
|
-function runStagedFormatter(paths: readonly string[]) {
|
|
|
- return spawnSync(process.execPath, [eslintCli, '--config', 'eslint.format.config.mjs', '--fix', '--no-warn-ignored', ...paths], {
|
|
|
+function runRepositoryOxlint(args: readonly string[], env: NodeJS.ProcessEnv = {}) {
|
|
|
+ return spawnSync(process.execPath, [tsxCli, 'scripts/run-oxlint.ts', ...args], {
|
|
|
cwd: repositoryRoot,
|
|
|
encoding: 'utf8',
|
|
|
- env: { ...process.env, NO_COLOR: '1' },
|
|
|
+ env: { ...process.env, NO_COLOR: '1', ...env },
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -150,7 +151,7 @@ export const longProbe = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
|
|
|
}
|
|
|
}, 20_000)
|
|
|
|
|
|
- it('keeps formatter rules aligned with Oxlint validation', async () => {
|
|
|
+ it('keeps the complete stylistic contract in Oxlint', async () => {
|
|
|
const oxlintPath = join(repositoryRoot, '.oxlintrc.json')
|
|
|
const result = parseConfigFileTextToJson(oxlintPath, await readFile(oxlintPath, 'utf8'))
|
|
|
if (result.error !== undefined) {
|
|
|
@@ -160,27 +161,67 @@ export const longProbe = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
|
|
|
if (!isRecord(parsed) || !isUnknownArray(parsed.overrides)) {
|
|
|
throw new Error('.oxlintrc.json must contain an overrides array')
|
|
|
}
|
|
|
+ expect(parsed.ignorePatterns).toEqual(expect.arrayContaining([
|
|
|
+ 'packages/typert/generator/tests/fixtures/type-model/**',
|
|
|
+ ]))
|
|
|
const stylisticOverride = parsed.overrides.find((value: unknown) =>
|
|
|
isRecord(value) && isRecord(value.rules) && '@stylistic/max-len' in value.rules)
|
|
|
if (!isRecord(stylisticOverride) || !isRecord(stylisticOverride.rules)) {
|
|
|
throw new Error('.oxlintrc.json must contain the @stylistic validator override')
|
|
|
}
|
|
|
- const validatorRules = { ...stylisticOverride.rules }
|
|
|
- const maxLen = validatorRules['@stylistic/max-len']
|
|
|
- delete validatorRules['@stylistic/max-len']
|
|
|
-
|
|
|
- const formatterUrl = pathToFileURL(join(repositoryRoot, 'eslint.format.config.mjs')).href
|
|
|
- const formatterModule = await import(formatterUrl) as unknown
|
|
|
- if (!isRecord(formatterModule) || !isUnknownArray(formatterModule.default)) {
|
|
|
- throw new Error('eslint.format.config.mjs must default-export a config array')
|
|
|
- }
|
|
|
- const formatterOverride = formatterModule.default.find((value: unknown) => isRecord(value) && isRecord(value.rules))
|
|
|
- if (!isRecord(formatterOverride) || !isRecord(formatterOverride.rules)) {
|
|
|
- throw new Error('eslint.format.config.mjs must contain a rules object')
|
|
|
+ expect(stylisticOverride.rules).toMatchObject({
|
|
|
+ '@stylistic/indent': ['error', 2],
|
|
|
+ '@stylistic/semi': ['error', 'never'],
|
|
|
+ '@stylistic/quotes': ['error', 'single', { avoidEscape: true }],
|
|
|
+ '@stylistic/comma-dangle': ['error', 'always-multiline'],
|
|
|
+ '@stylistic/eol-last': ['error', 'always'],
|
|
|
+ '@stylistic/no-trailing-spaces': 'error',
|
|
|
+ '@stylistic/object-curly-spacing': ['error', 'always'],
|
|
|
+ '@stylistic/arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }],
|
|
|
+ '@stylistic/member-delimiter-style': ['error', {
|
|
|
+ multiline: { delimiter: 'none' },
|
|
|
+ singleline: { delimiter: 'semi', requireLast: false },
|
|
|
+ }],
|
|
|
+ '@stylistic/max-len': ['error', { code: 140, ignoreUrls: true, ignoreStrings: true, ignoreTemplateLiterals: true }],
|
|
|
+ })
|
|
|
+ const typeGraphOverride = parsed.overrides.find((value: unknown) =>
|
|
|
+ isRecord(value)
|
|
|
+ && isUnknownArray(value.files)
|
|
|
+ && value.files.includes('packages/typert/generator/tests/fixtures/type-model/packages/host/src/models.ts'))
|
|
|
+ expect(typeGraphOverride).toMatchObject({
|
|
|
+ rules: { '@stylistic/quotes': 'off' },
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('checks preserved TypeGraph syntax without type-aware analysis', () => {
|
|
|
+ const result = runOxlint([
|
|
|
+ '--config',
|
|
|
+ '.oxlintrc.staged.json',
|
|
|
+ 'packages/typert/generator/tests/fixtures/type-model',
|
|
|
+ ])
|
|
|
+
|
|
|
+ expect(result.error).toBeUndefined()
|
|
|
+ expect(result.status, normalizedOutput(result)).toBe(0)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('keeps repository lint workflows Oxlint-only', async () => {
|
|
|
+ const packageJson = JSON.parse(await readFile(join(repositoryRoot, 'package.json'), 'utf8')) as unknown
|
|
|
+ if (!isRecord(packageJson) || !isRecord(packageJson.scripts) || !isRecord(packageJson.devDependencies)) {
|
|
|
+ throw new Error('package.json must contain scripts and devDependencies objects')
|
|
|
}
|
|
|
|
|
|
- expect(validatorRules).toStrictEqual(formatterOverride.rules)
|
|
|
- expect(maxLen).toStrictEqual(['error', { code: 140, ignoreUrls: true, ignoreStrings: true, ignoreTemplateLiterals: true }])
|
|
|
+ expect(packageJson.scripts['lint:contracts-ready']).toBe('tsx scripts/run-oxlint.ts .')
|
|
|
+ expect(packageJson.scripts['lint:fix:contracts-ready']).toBe(
|
|
|
+ 'tsx scripts/run-oxlint.ts --config .oxlintrc.staged.json packages/typert/generator/tests/fixtures/type-model --fix && tsx scripts/run-oxlint.ts . --fix',
|
|
|
+ )
|
|
|
+ expect(packageJson.devDependencies).not.toHaveProperty('eslint')
|
|
|
+ expect(packageJson.devDependencies).not.toHaveProperty('@typescript-eslint/parser')
|
|
|
+ expect(existsSync(join(repositoryRoot, 'eslint.format.config.mjs'))).toBe(false)
|
|
|
+
|
|
|
+ const lefthook = await readFile(join(repositoryRoot, 'lefthook.yml'), 'utf8')
|
|
|
+ expect(lefthook).toContain('scripts/run-oxlint.ts --config .oxlintrc.staged.json --fix')
|
|
|
+ expect(lefthook).not.toContain('node_modules/.bin/eslint')
|
|
|
+ expect(lefthook).not.toContain('eslint.format.config.mjs')
|
|
|
})
|
|
|
|
|
|
it('reports an unused suppression', async () => {
|
|
|
@@ -227,10 +268,13 @@ export const longProbe = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
|
|
|
if (result.error !== undefined) {
|
|
|
throw new Error(flattenDiagnosticMessageText(result.error.messageText, '\n'))
|
|
|
}
|
|
|
- expect(result.config).toMatchObject({
|
|
|
+ const stagedConfig = result.config as unknown
|
|
|
+ if (!isRecord(stagedConfig)) throw new Error('.oxlintrc.staged.json must contain a config object')
|
|
|
+ expect(stagedConfig).toMatchObject({
|
|
|
extends: ['./.oxlintrc.json'],
|
|
|
options: { typeAware: false },
|
|
|
})
|
|
|
+ expect(stagedConfig.ignorePatterns).not.toContain('packages/typert/generator/tests/fixtures/type-model/**')
|
|
|
|
|
|
const suffix = randomUUID()
|
|
|
const path = join(repositoryRoot, 'scripts', `staged-lint-probe-${suffix}.ts`)
|
|
|
@@ -254,30 +298,76 @@ export const longProbe = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- it('applies staged stylistic fixes before Oxlint validation', async () => {
|
|
|
+ it('preserves successful fix output channels', async () => {
|
|
|
const suffix = randomUUID()
|
|
|
- const configPath = await writeContractConfig(suffix)
|
|
|
- const directory = join(repositoryRoot, 'scripts', `.oxlint-contract-${suffix}`)
|
|
|
- const path = join(directory, 'fix.ts')
|
|
|
+ const path = join(repositoryRoot, 'scripts', `staged-lint-probe-${suffix}.ts`)
|
|
|
|
|
|
try {
|
|
|
- await mkdir(directory, { recursive: true })
|
|
|
- await writeFile(path, 'const value={answer:1}; \nconsole.log(value)\n')
|
|
|
-
|
|
|
- const relativePath = relative(repositoryRoot, path)
|
|
|
- const formatResult = runStagedFormatter([relativePath])
|
|
|
- const lintResult = runOxlint(['--config', relative(repositoryRoot, configPath), '--fix', relativePath])
|
|
|
-
|
|
|
- expect(formatResult.error).toBeUndefined()
|
|
|
- expect(formatResult.status, normalizedOutput(formatResult)).toBe(0)
|
|
|
- expect(lintResult.error).toBeUndefined()
|
|
|
- expect(lintResult.status, normalizedOutput(lintResult)).toBe(0)
|
|
|
- await expect(readFile(path, 'utf8')).resolves.toBe('const value={ answer:1 }\nconsole.log(value)\n')
|
|
|
+ await writeFile(path, '// oxlint-disable-next-line no-console\nexport const value = 1\n')
|
|
|
+ const result = runRepositoryOxlint([
|
|
|
+ '--config',
|
|
|
+ '.oxlintrc.staged.json',
|
|
|
+ '--format',
|
|
|
+ 'unix',
|
|
|
+ '--fix',
|
|
|
+ relative(repositoryRoot, path),
|
|
|
+ ])
|
|
|
+
|
|
|
+ expect(result.error).toBeUndefined()
|
|
|
+ expect(result.status, normalizedOutput(result)).toBe(0)
|
|
|
+ expect(result.stdout).toContain('Unused oxlint-disable directive')
|
|
|
+ expect(result.stderr).toBe('')
|
|
|
} finally {
|
|
|
- await Promise.all([
|
|
|
- rm(directory, { recursive: true, force: true }),
|
|
|
- rm(configPath, { force: true }),
|
|
|
+ await rm(path, { force: true })
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ it('prints only the final diagnostics when a fix retry still fails', async () => {
|
|
|
+ const suffix = randomUUID()
|
|
|
+ const path = join(repositoryRoot, 'scripts', `staged-lint-probe-${suffix}.ts`)
|
|
|
+
|
|
|
+ try {
|
|
|
+ await writeFile(path, `export const longProbe = ${'1 + '.repeat(80)}1\n`)
|
|
|
+ const result = runRepositoryOxlint([
|
|
|
+ '--config',
|
|
|
+ '.oxlintrc.staged.json',
|
|
|
+ '--format',
|
|
|
+ 'unix',
|
|
|
+ '--fix',
|
|
|
+ relative(repositoryRoot, path),
|
|
|
])
|
|
|
+ const output = normalizedOutput(result)
|
|
|
+
|
|
|
+ expect(result.error).toBeUndefined()
|
|
|
+ expect(result.status, output).toBe(1)
|
|
|
+ expect(output.match(/@stylistic\(max-len\)/g)).toHaveLength(1)
|
|
|
+ } finally {
|
|
|
+ await rm(path, { force: true })
|
|
|
}
|
|
|
- }, 20_000)
|
|
|
+ })
|
|
|
+
|
|
|
+ it.each(['--fix', '--fix-suggestions', '--fix-dangerously'])(
|
|
|
+ 'converges overlapping staged stylistic fixes through Oxlint under %s',
|
|
|
+ async (fixFlag) => {
|
|
|
+ const suffix = randomUUID()
|
|
|
+ const directory = join(repositoryRoot, 'scripts', `.oxlint-contract-${suffix}`)
|
|
|
+ const path = join(directory, 'fix.ts')
|
|
|
+
|
|
|
+ try {
|
|
|
+ await mkdir(directory, { recursive: true })
|
|
|
+ await writeFile(path, 'const value={answer:1}; \nconsole.log(value)\n')
|
|
|
+
|
|
|
+ const relativePath = relative(repositoryRoot, path)
|
|
|
+ const lintResult = runRepositoryOxlint(['--config', '.oxlintrc.staged.json', fixFlag, relativePath])
|
|
|
+
|
|
|
+ expect(lintResult.error).toBeUndefined()
|
|
|
+ expect(lintResult.status, normalizedOutput(lintResult)).toBe(0)
|
|
|
+ expect(normalizedOutput(lintResult)).not.toContain('@stylistic')
|
|
|
+ await expect(readFile(path, 'utf8')).resolves.toBe('const value={ answer:1 }\nconsole.log(value)\n')
|
|
|
+ } finally {
|
|
|
+ await rm(directory, { recursive: true, force: true })
|
|
|
+ }
|
|
|
+ },
|
|
|
+ 20_000,
|
|
|
+ )
|
|
|
})
|