Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/test extend #621

Merged
merged 5 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ jobs:
- name: build
run: pnpm build

- name: typecheck
run: pnpm tsc
# - name: typecheck
# run: pnpm tsc
2 changes: 1 addition & 1 deletion docs/rules/valid-title.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ To enable typechecking for vitest make sure settings key is added in your config
```js
import vitest from "eslint-plugin-vitest";

export defualt [
export default [
{
files: ["tests/**"],
plugins: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
"@types/eslint": "^9.6.1",
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.2",
"@typescript-eslint/eslint-plugin": "8.0.0",
"@typescript-eslint/eslint-plugin": "8.18.2",
"@typescript-eslint/parser": "8.18.2",
"@typescript-eslint/rule-tester": "8.18.1",
"@vitest/eslint-plugin": "^1.1.20",
"bumpp": "^9.9.2",
"concurrently": "^8.2.2",
"concurrently": "^9.1.1",
"eslint": "^9.17.0",
"eslint-doc-generator": "^2.0.2",
"eslint-plugin-eslint-plugin": "^6.4.0",
Expand Down
215 changes: 44 additions & 171 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const createConfig = <R extends Linter.RulesRecord>(rules: R) => (
[`vitest/${ruleName}`]: rules[ruleName]
}
}, {})) as {
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
}
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
}

const createConfigLegacy = (rules: Record<string, string>) => ({
plugins: ['@vitest'],
Expand Down Expand Up @@ -165,7 +165,7 @@ interface VitestPLugin extends Linter.Plugin {
version: string
}
rules: Record<string, RuleModule<any, any>>
//TODO: use classic type for config
// TODO: use classic type for config
configs?: Record<string, any>
environments?: Record<string, any>
}
Expand Down Expand Up @@ -268,13 +268,13 @@ plugin.configs = {
'legacy-all': createConfigLegacy(allRules),
'recommended': {
plugins: {
["vitest"]: plugin
['vitest']: plugin
},
rules: createConfig(recommended)
},
'all': {
plugins: {
["vitest"]: plugin
['vitest']: plugin
},
rules: createConfig(allRules)
},
Expand Down Expand Up @@ -302,5 +302,4 @@ plugin.configs = {
}
}


export default plugin
17 changes: 10 additions & 7 deletions src/rules/valid-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ type Options = {
allowArguments?: boolean
disallowedWords?: string[]
mustNotMatch?:
| Partial<Record<MatcherGroups, string | MatcherAndMessage>>
| MatcherAndMessage
| string
| Partial<Record<MatcherGroups, string | MatcherAndMessage>>
| MatcherAndMessage
| string
mustMatch?:
| Partial<Record<MatcherGroups, string | MatcherAndMessage>>
| MatcherAndMessage
| string
| Partial<Record<MatcherGroups, string | MatcherAndMessage>>
| MatcherAndMessage
| string
}[]

type CompiledMatcherAndMessage = [matcher: RegExp, message?: string]
Expand Down Expand Up @@ -91,7 +91,7 @@ const compileMatcherPatterns = (matchers:
| Partial<Record<MatcherGroups, string | MatcherAndMessage>>
| MatcherAndMessage
| string): Record<MatcherGroups, CompiledMatcherAndMessage | null> &
Record<string, CompiledMatcherAndMessage | null> => {
Record<string, CompiledMatcherAndMessage | null> => {
if (typeof matchers === 'string' || Array.isArray(matchers)) {
const compiledMatcher = compileMatcherPattern(matchers)

Expand Down Expand Up @@ -197,6 +197,9 @@ export default createEslintRule<Options, MESSAGE_IDS>({

if (vitestFnCall?.type !== 'describe' && vitestFnCall?.type !== 'test' && vitestFnCall?.type !== 'it') return

// check if extend keyword have been used
if (vitestFnCall.members.some(m => m.type == AST_NODE_TYPES.Identifier && m.name == 'extend')) return

const [argument] = node.arguments

if (settings.typecheck) {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Mostly adopted from https://github.com/jest-community/eslint-plugin-jest/blob/main/src/rules/utils/accessors.ts
// Initial license: https://github.com/jest-community/eslint-plugin-jest/blob/main/LICENSE
// mostly adopted from https://github.com/jest-community/eslint-plugin-jest/blob/main/src/rules/utils/accessors.ts
// initial license: https://github.com/jest-community/eslint-plugin-jest/blob/main/LICENSE
import {
TSESLint,
AST_NODE_TYPES,
Expand All @@ -10,18 +10,18 @@ import {
KnownMemberExpression,
ParsedExpectVitestFnCall
} from './parse-vitest-fn-call'
import { RuleListener, RuleModule } from '@typescript-eslint/utils/ts-eslint'

export interface PluginDocs {
recommended?: boolean
requiresTypeChecking?: boolean
}

export function createEslintRule<TOptions extends readonly unknown[], TMessageIds extends string>(rule: Readonly<ESLintUtils.RuleWithMetaAndName<TOptions, TMessageIds, PluginDocs>>) {
export function createEslintRule<TOptions extends readonly unknown[], TMessageIds extends string>(rule: Readonly<ESLintUtils.RuleWithMetaAndName<TOptions, TMessageIds, PluginDocs>>): RuleModule<TMessageIds, TOptions, PluginDocs, RuleListener> {
const createRule = ESLintUtils.RuleCreator<PluginDocs>(
(ruleName: string) =>
(ruleName) =>
`https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/${ruleName}.md`
)

return createRule(rule)
}

Expand Down
1 change: 0 additions & 1 deletion tests/expect-expect.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils'

import rule, { RULE_NAME } from '../src/rules/expect-expect'
import { ruleTester } from './ruleTester'

Expand Down
5 changes: 4 additions & 1 deletion tests/valid-title.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,10 @@ ruleTester.run(RULE_NAME, rule, {
'test.concurrent("foo", function () {})',
'xtest("foo", function () {})',
'xtest(`foo`, function () {})',
'someFn("foo", function () {})'
'someFn("foo", function () {})',
`export const myTest = test.extend({
archive: []
})`
],
invalid: [
{
Expand Down
Loading