Skip to content

Commit

Permalink
feat: upgrade to @typescript-eslint v8 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Aug 12, 2024
1 parent 3211617 commit a4db4d1
Show file tree
Hide file tree
Showing 17 changed files with 663 additions and 770 deletions.
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "eslint-vitest-rule-tester",
"type": "module",
"version": "0.4.0",
"packageManager": "pnpm@9.5.0",
"packageManager": "pnpm@9.7.0",
"description": "ESLint rule tester with Vitest",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down Expand Up @@ -52,24 +52,24 @@
},
"dependencies": {
"@antfu/utils": "^0.7.10",
"@types/eslint": "^8.56.10",
"@typescript-eslint/utils": "^7.16.0"
"@types/eslint": "^9.6.0",
"@typescript-eslint/utils": "^8.0.1"
},
"devDependencies": {
"@antfu/eslint-config": "^2.22.2",
"@antfu/ni": "^0.22.0",
"@types/node": "^20.14.10",
"bumpp": "^9.4.1",
"eslint": "^9.7.0",
"@antfu/eslint-config": "^2.25.1",
"@antfu/ni": "^0.22.1",
"@types/node": "^22.2.0",
"bumpp": "^9.5.1",
"eslint": "^9.9.0",
"esno": "^4.7.0",
"lint-staged": "^15.2.7",
"pnpm": "^9.5.0",
"lint-staged": "^15.2.8",
"pnpm": "^9.7.0",
"rimraf": "^6.0.1",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.5.3",
"typescript": "^5.5.4",
"unbuild": "^2.0.0",
"vite": "^5.3.3",
"vitest": "^2.0.2"
"vite": "^5.4.0",
"vitest": "^2.0.5"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
Expand Down
1,211 changes: 548 additions & 663 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Linter } from 'eslint'
import { objectPick } from '@antfu/utils'
import type { CompatConfigOptions } from './types'

export function pickFlatConfigFromOptions(options: CompatConfigOptions): Linter.FlatConfig | undefined {
export function pickFlatConfigFromOptions(options: CompatConfigOptions): Linter.Config | undefined {
const picked = objectPick(
options,
[
Expand Down
2 changes: 1 addition & 1 deletion src/rule-tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function createRuleTester(options: RuleTesterInitOptions): RuleTester {
...toArray(options.configs)
.map(c => c.languageOptions)
.filter(<T>(c: T | undefined): c is T => c !== undefined),
) as Linter.FlatConfig['languageOptions']
) as Linter.Config['languageOptions']

const linter = new Linter({
configType: 'flat',
Expand Down
14 changes: 7 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ export interface TestExecutionResult extends Linter.FixReport {

export interface CompatConfigOptions {
parserOptions?: Linter.ParserOptions
parser?: Linter.ParserModule
languageOptions?: Linter.FlatConfig['languageOptions']
linterOptions?: Linter.FlatConfig['linterOptions']
settings?: Linter.FlatConfig['settings']
processor?: Linter.FlatConfig['processor']
files?: Linter.FlatConfig['files']
parser?: Linter.Parser
languageOptions?: Linter.Config['languageOptions']
linterOptions?: Linter.Config['linterOptions']
settings?: Linter.Config['settings']
processor?: Linter.Config['processor']
files?: Linter.Config['files']
}

export type RuleModule = any // to allow any rule module
Expand Down Expand Up @@ -122,7 +122,7 @@ export interface RuleTesterInitOptions extends CompatConfigOptions, RuleTesterBe
/**
* Additional flat configs to be merged with the rule config
*/
configs?: Linter.FlatConfig | Linter.FlatConfig[]
configs?: Linter.Config | Linter.Config[]
/**
* The default filenames to use for type-aware tests.
* @default { js: 'file.js', jsx: 'file.jsx', ts: 'file.ts', tsx: 'file.tsx' }
Expand Down
10 changes: 5 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { unindent, unindent as $ }

export function normalizeTestCase(
c: TestCase,
languageOptions: Linter.FlatConfig['languageOptions'],
languageOptions: Linter.Config['languageOptions'],
defaultFilenames: DefaultFilenames,
type?: 'valid' | 'invalid',
): NormalizedTestCase {
Expand Down Expand Up @@ -59,7 +59,7 @@ export function normalizeCaseError(error: TestCaseError | string, rule?: RuleMod
}

function getDefaultJavaScriptFilename(
languageOptions: Linter.FlatConfig['languageOptions'],
languageOptions: Linter.Config['languageOptions'],
defaultFilenames: DefaultFilenames,
) {
return languageOptions?.parserOptions?.ecmaFeatures?.jsx
Expand All @@ -68,7 +68,7 @@ function getDefaultJavaScriptFilename(
}

function getDefaultTypeScriptFilename(
languageOptions: Linter.FlatConfig['languageOptions'],
languageOptions: Linter.Config['languageOptions'],
defaultFilenames: DefaultFilenames,
) {
const rootPath = (isUsingTypeScriptTypings(languageOptions)
Expand All @@ -89,11 +89,11 @@ function getDefaultTypeScriptFilename(
return path.join(rootPath, filename)
}

export function isUsingTypeScriptParser(languageOptions: Linter.FlatConfig['languageOptions']) {
export function isUsingTypeScriptParser(languageOptions: Linter.Config['languageOptions']): boolean {
return languageOptions?.parser?.meta?.name === 'typescript-eslint/parser'
}

export function isUsingTypeScriptTypings(languageOptions: Linter.FlatConfig['languageOptions']) {
export function isUsingTypeScriptTypings(languageOptions: Linter.Config['languageOptions']): boolean {
return languageOptions?.parserOptions?.program
|| languageOptions?.parserOptions?.project
|| languageOptions?.parserOptions?.projectService
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`no-array-push > invalid > const foo = [1, 2, 3]; foo.push(4) 1`] = `"const foo = [1, 2, 3]; foo.push(4)"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`no-array-push > invalid > const foo = [1, 2, 3]; foo.push(4) 1`] = `"const foo = [1, 2, 3]; foo.push(4)"`;
3 changes: 3 additions & 0 deletions test/rules/__snapshots__/no-array-push.project.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`no-array-push > invalid > const foo = [1, 2, 3]; foo.push(4) 1`] = `"const foo = [1, 2, 3]; foo.push(4)"`;
9 changes: 0 additions & 9 deletions test/rules/__snapshots__/no-array-push.test.ts.snap

This file was deleted.

28 changes: 28 additions & 0 deletions test/rules/no-array-push.cases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from 'vitest'
import type { RuleTesterInitOptions, TestCasesOptions } from '../../src'
import rule, { RULE_NAME } from './no-array-push'

const valids = [
'const foo = [1, 2, 3]; foo.pop()',
'const foo = { push: () => {} }; foo.push()',
]

const invalids = [
'const foo = [1, 2, 3]; foo.push(4)',
]

export const common = {
name: RULE_NAME,
rule: rule as any,

valid: valids,
invalid: invalids.map(i => ({
code: i,
errors: [{ messageId: 'noArrayPush' }],
})),

onResult(_case, result) {
if (_case.type === 'invalid')
expect(result.output).toMatchSnapshot()
},
} satisfies TestCasesOptions & RuleTesterInitOptions
19 changes: 19 additions & 0 deletions test/rules/no-array-push.project-service-custom.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import path from 'node:path'
import tsParser from '@typescript-eslint/parser'
import { run } from '../../src'
import { common } from './no-array-push.cases'

// Using `projectService` - custom fixture.
run({
...common,
languageOptions: {
parser: tsParser,
parserOptions: {
tsconfigRootDir: path.join(import.meta.dirname, './ts-fixture'),
projectService: {
// Ensure we're not using the default project
maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING: 0,
},
},
},
})
16 changes: 16 additions & 0 deletions test/rules/no-array-push.project-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import tsParser from '@typescript-eslint/parser'
import { run } from '../../src'
import { common } from './no-array-push.cases'

// Using `projectService` - default project.
run({
...common,
languageOptions: {
parser: tsParser,
parserOptions: {
projectService: {
allowDefaultProject: ['*.ts'],
},
},
},
})
16 changes: 16 additions & 0 deletions test/rules/no-array-push.project.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from 'node:path'
import tsParser from '@typescript-eslint/parser'
import { run } from '../../src'
import { common } from './no-array-push.cases'

// Using `project` - custom fixture.
run({
...common,
languageOptions: {
parser: tsParser,
parserOptions: {
tsconfigRootDir: path.join(import.meta.dirname, './ts-fixture'),
project: true,
},
},
})
69 changes: 0 additions & 69 deletions test/rules/no-array-push.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/rules/no-array-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default createEslintRule<Options, MessageIds>({
type: 'problem',
docs: {
description: 'Prevent calling push on an array',
recommended: 'recommended',
},
schema: [],
messages: {
Expand Down
1 change: 0 additions & 1 deletion test/rules/top-level-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default createEslintRule<Options, MessageIds>({
type: 'problem',
docs: {
description: 'Enforce top-level functions to be declared with function keyword',
recommended: 'stylistic',
},
fixable: 'code',
schema: [],
Expand Down

0 comments on commit a4db4d1

Please sign in to comment.