-
Notifications
You must be signed in to change notification settings - Fork 154
fix: generate configs and rules statically #1102
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
Changes from all commits
bd90e46
5737fa8
3c2c86b
a551ae2
9e82e66
c2ddd3a
55ce519
1404fd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,20 @@ | ||
| import { join } from 'path'; | ||
|
|
||
| import { importDefault, SUPPORTED_TESTING_FRAMEWORKS } from '../utils'; | ||
| import angular from './angular'; | ||
| import dom from './dom'; | ||
| import marko from './marko'; | ||
| import react from './react'; | ||
| import svelte from './svelte'; | ||
| import vue from './vue'; | ||
|
|
||
| import type { SupportedTestingFramework } from '../utils'; | ||
| import type { TSESLint } from '@typescript-eslint/utils'; | ||
|
|
||
| const configsDir = __dirname; | ||
| import type { Linter } from 'eslint'; | ||
|
|
||
| const getConfigForFramework = (framework: SupportedTestingFramework) => | ||
| importDefault<TSESLint.SharedConfig.RulesRecord>(join(configsDir, framework)); | ||
| const legacyConfigs: Record<SupportedTestingFramework, Linter.LegacyConfig> = { | ||
| dom, | ||
| angular, | ||
| react, | ||
| vue, | ||
| svelte, | ||
| marko, | ||
| }; | ||
|
|
||
| export default SUPPORTED_TESTING_FRAMEWORKS.reduce( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove dynamic generation in favor of static generation. |
||
| (allConfigs, framework) => ({ | ||
| ...allConfigs, | ||
| [framework]: getConfigForFramework(framework), | ||
| }), | ||
| {} | ||
| ) as Record<SupportedTestingFramework, TSESLint.SharedConfig.RulesRecord>; | ||
| export { legacyConfigs }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,56 @@ | ||
| import configs from './configs'; | ||
| import { legacyConfigs } from './configs'; | ||
| import rules from './rules'; | ||
|
|
||
| import type { SupportedTestingFramework } from './utils'; | ||
| import type { TSESLint } from '@typescript-eslint/utils'; | ||
|
|
||
| // we can't natively import package.json as tsc will copy it into dist/ | ||
| const { | ||
| name: packageName, | ||
| version: packageVersion, | ||
| // we can't natively import package.json as tsc will copy it into dist/ | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| } = require('../package.json') as { name: string; version: string }; | ||
|
|
||
| type FinalConfigs = Record< | ||
| SupportedTestingFramework | `flat/${SupportedTestingFramework}`, | ||
| TSESLint.ClassicConfig.Config | TSESLint.FlatConfig.Config | ||
| >; | ||
|
|
||
| const plugin = { | ||
| meta: { | ||
| name: packageName, | ||
| version: packageVersion, | ||
| }, | ||
| // ugly cast for now to keep TypeScript happy since | ||
| // we don't have types for flat config yet | ||
| configs: {} as Record< | ||
| SupportedTestingFramework | `flat/${SupportedTestingFramework}`, | ||
| TSESLint.SharedConfig.RulesRecord | ||
| >, | ||
| configs: {} as FinalConfigs, | ||
| rules, | ||
| }; | ||
|
|
||
| plugin.configs = { | ||
| ...configs, | ||
| ...(Object.fromEntries( | ||
| Object.entries(configs).map(([framework, config]) => [ | ||
| `flat/${framework}`, | ||
| { | ||
| plugins: { 'testing-library': plugin }, | ||
| rules: config.rules, | ||
| }, | ||
| ]) | ||
| ) as unknown as Record< | ||
| `flat/${SupportedTestingFramework}`, | ||
| TSESLint.SharedConfig.RulesRecord & { plugins: unknown } | ||
| >), | ||
| }; | ||
| ...legacyConfigs, | ||
| 'flat/dom': { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove dynamic generation in favor of static generation. |
||
| plugins: { 'testing-library': plugin }, | ||
| rules: legacyConfigs.dom.rules, | ||
| }, | ||
| 'flat/angular': { | ||
| plugins: { 'testing-library': plugin }, | ||
| rules: legacyConfigs.angular.rules, | ||
| }, | ||
| 'flat/react': { | ||
| plugins: { 'testing-library': plugin }, | ||
| rules: legacyConfigs.react.rules, | ||
| }, | ||
| 'flat/vue': { | ||
| plugins: { 'testing-library': plugin }, | ||
| rules: legacyConfigs.vue.rules, | ||
| }, | ||
| 'flat/svelte': { | ||
| plugins: { 'testing-library': plugin }, | ||
| rules: legacyConfigs.svelte.rules, | ||
| }, | ||
| 'flat/marko': { | ||
| plugins: { 'testing-library': plugin }, | ||
| rules: legacyConfigs.marko.rules, | ||
| }, | ||
| } satisfies FinalConfigs; | ||
|
|
||
| export = plugin; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,59 @@ | ||
| import { readdirSync } from 'fs'; | ||
| import { join, parse } from 'path'; | ||
| import awaitAsyncEvents from './await-async-events'; | ||
| import awaitAsyncQueries from './await-async-queries'; | ||
| import awaitAsyncUtils from './await-async-utils'; | ||
| import consistentDataTestid from './consistent-data-testid'; | ||
| import noAwaitSyncEvents from './no-await-sync-events'; | ||
| import noAwaitSyncQueries from './no-await-sync-queries'; | ||
| import noContainer from './no-container'; | ||
| import noDebuggingUtils from './no-debugging-utils'; | ||
| import noDomImport from './no-dom-import'; | ||
| import noGlobalRegexpFlagInQuery from './no-global-regexp-flag-in-query'; | ||
| import noManualCleanup from './no-manual-cleanup'; | ||
| import noNodeAccess from './no-node-access'; | ||
| import noPromiseInFireEvent from './no-promise-in-fire-event'; | ||
| import noRenderInLifecycle from './no-render-in-lifecycle'; | ||
| import noTestIdQueries from './no-test-id-queries'; | ||
| import noUnnecessaryAct from './no-unnecessary-act'; | ||
| import noWaitForMultipleAssertions from './no-wait-for-multiple-assertions'; | ||
| import noWaitForSideEffects from './no-wait-for-side-effects'; | ||
| import noWaitForSnapshot from './no-wait-for-snapshot'; | ||
| import preferExplicitAssert from './prefer-explicit-assert'; | ||
| import preferFindBy from './prefer-find-by'; | ||
| import preferImplicitAssert from './prefer-implicit-assert'; | ||
| import preferPresenceQueries from './prefer-presence-queries'; | ||
| import preferQueryByDisappearance from './prefer-query-by-disappearance'; | ||
| import preferQueryMatchers from './prefer-query-matchers'; | ||
| import preferScreenQueries from './prefer-screen-queries'; | ||
| import preferUserEvent from './prefer-user-event'; | ||
| import renderResultNamingConvention from './render-result-naming-convention'; | ||
|
|
||
| import { importDefault } from '../utils'; | ||
|
|
||
| import type { TestingLibraryPluginRuleModule } from '../utils'; | ||
|
|
||
| const rulesDir = __dirname; | ||
| const excludedFiles = ['index']; | ||
|
|
||
| export default readdirSync(rulesDir) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove dynamic generation in favor of static generation. |
||
| .map((rule) => parse(rule).name) | ||
| .filter((ruleName) => !excludedFiles.includes(ruleName)) | ||
| .reduce<Record<string, TestingLibraryPluginRuleModule<string, unknown[]>>>( | ||
| (allRules, ruleName) => ({ | ||
| ...allRules, | ||
| [ruleName]: importDefault< | ||
| TestingLibraryPluginRuleModule<string, unknown[]> | ||
| >(join(rulesDir, ruleName)), | ||
| }), | ||
| {} | ||
| ); | ||
| export default { | ||
| 'await-async-events': awaitAsyncEvents, | ||
| 'await-async-queries': awaitAsyncQueries, | ||
| 'await-async-utils': awaitAsyncUtils, | ||
| 'consistent-data-testid': consistentDataTestid, | ||
| 'no-await-sync-events': noAwaitSyncEvents, | ||
| 'no-await-sync-queries': noAwaitSyncQueries, | ||
| 'no-container': noContainer, | ||
| 'no-debugging-utils': noDebuggingUtils, | ||
| 'no-dom-import': noDomImport, | ||
| 'no-global-regexp-flag-in-query': noGlobalRegexpFlagInQuery, | ||
| 'no-manual-cleanup': noManualCleanup, | ||
| 'no-node-access': noNodeAccess, | ||
| 'no-promise-in-fire-event': noPromiseInFireEvent, | ||
| 'no-render-in-lifecycle': noRenderInLifecycle, | ||
| 'no-test-id-queries': noTestIdQueries, | ||
| 'no-unnecessary-act': noUnnecessaryAct, | ||
| 'no-wait-for-multiple-assertions': noWaitForMultipleAssertions, | ||
| 'no-wait-for-side-effects': noWaitForSideEffects, | ||
| 'no-wait-for-snapshot': noWaitForSnapshot, | ||
| 'prefer-explicit-assert': preferExplicitAssert, | ||
| 'prefer-find-by': preferFindBy, | ||
| 'prefer-implicit-assert': preferImplicitAssert, | ||
| 'prefer-presence-queries': preferPresenceQueries, | ||
| 'prefer-query-by-disappearance': preferQueryByDisappearance, | ||
| 'prefer-query-matchers': preferQueryMatchers, | ||
| 'prefer-screen-queries': preferScreenQueries, | ||
| 'prefer-user-event': preferUserEvent, | ||
| 'render-result-naming-convention': renderResultNamingConvention, | ||
| }; | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,9 +47,9 @@ | |
| "prettier-base": "prettier . --ignore-unknown --cache --log-level warn", | ||
| "rule-doc-generator": "eslint-doc-generator", | ||
| "semantic-release": "semantic-release", | ||
| "test": "jest", | ||
| "test:ci": "pnpm run test --ci --coverage", | ||
| "test:watch": "pnpm run test --watch", | ||
| "test": "vitest run", | ||
| "test:ci": "vitest run --coverage", | ||
| "test:watch": "vitest", | ||
| "type-check": "tsc --noEmit" | ||
| }, | ||
| "dependencies": { | ||
|
|
@@ -62,32 +62,30 @@ | |
| "@eslint/compat": "^1.3.2", | ||
| "@eslint/eslintrc": "^3.3.1", | ||
| "@eslint/js": "^9.35.0", | ||
| "@swc/core": "^1.9.3", | ||
| "@swc/jest": "^0.2.37", | ||
| "@types/jest": "^29.5.14", | ||
| "@types/node": "^22.9.3", | ||
| "@typescript-eslint/rule-tester": "^8.15.0", | ||
| "@vitest/coverage-v8": "^3.2.4", | ||
| "@vitest/eslint-plugin": "^1.5.2", | ||
| "del-cli": "^6.0.0", | ||
| "eslint": "^9.35.0", | ||
| "eslint-config-prettier": "^10.1.8", | ||
| "eslint-doc-generator": "^2.2.2", | ||
| "eslint-import-resolver-typescript": "^4.4.4", | ||
| "eslint-plugin-import-x": "^4.16.1", | ||
| "eslint-plugin-jest": "^29.0.1", | ||
| "eslint-plugin-node": "^11.1.0", | ||
| "eslint-plugin-promise": "^7.1.0", | ||
| "eslint-remote-tester": "^3.0.1", | ||
| "eslint-remote-tester-repositories": "^1.0.1", | ||
| "globals": "^16.3.0", | ||
| "husky": "^9.1.7", | ||
| "jest": "^29.7.0", | ||
| "lint-staged": "^15.2.10", | ||
| "prettier": "3.6.2", | ||
| "semantic-release": "^25.0.2", | ||
| "semver": "^7.6.3", | ||
| "ts-node": "^10.9.2", | ||
| "typescript": "^5.7.2", | ||
| "typescript-eslint": "^8.15.0" | ||
| "typescript-eslint": "^8.15.0", | ||
| "vitest": "^3.2.4" | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't install vitest v4 since is not compatible with node v18. |
||
| }, | ||
| "peerDependencies": { | ||
| "eslint": "^8.57.0 || ^9.0.0" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this for type validation purposes.