-
Notifications
You must be signed in to change notification settings - Fork 238
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: support new config system #1245
Closed
jjangga0214
wants to merge
5
commits into
jest-community:main
from
jjangga0214:feat/new-config-system
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ebad597
feat: support new config system
jjangga0214 b33eb54
feat: add `files` to the new config
jjangga0214 a2a13d9
docs: add guide to the new config system
jjangga0214 e663933
fix: `./all` entrypoint
jjangga0214 b5edd54
docs: fix EOL of legacy config
jjangga0214 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import globals from '../globals.json'; | ||
import jest from '../index'; | ||
import legacy from '../legacy'; | ||
|
||
export default [ | ||
{ | ||
files: ['**/*.snap'], | ||
plugins: { | ||
jest, | ||
}, | ||
processor: 'jest/.snap', | ||
}, | ||
{ | ||
files: [ | ||
'**/*.{test,spec}.{js,cjs,mjs,jsx,ts,tsx}', | ||
'**/__tests__/*.{js,cjs,mjs,jsx,ts,tsx}', | ||
], | ||
languageOptions: { | ||
globals, | ||
}, | ||
plugins: { | ||
jest, | ||
}, | ||
rules: legacy.configs.all.rules, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import legacy from '../legacy'; | ||
import all from './all'; | ||
|
||
const [snap, test] = all; | ||
|
||
export default [ | ||
snap, | ||
{ | ||
...test, | ||
rules: legacy.configs.recommended.rules, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import legacy from '../legacy'; | ||
import all from './all'; | ||
|
||
const [snap, test] = all; | ||
|
||
export default [ | ||
snap, | ||
{ | ||
...test, | ||
rules: legacy.configs.style.rules, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,6 @@ | ||
import { readdirSync } from 'fs'; | ||
import { join, parse } from 'path'; | ||
import type { TSESLint } from '@typescript-eslint/utils'; | ||
import globals from './globals.json'; | ||
import * as snapshotProcessor from './processors/snapshot-processor'; | ||
import legacy from './legacy'; | ||
|
||
type RuleModule = TSESLint.RuleModule<string, unknown[]> & { | ||
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>; | ||
}; | ||
|
||
// v5 of `@typescript-eslint/experimental-utils` removed this | ||
declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' { | ||
export interface RuleMetaDataDocs { | ||
category: 'Best Practices' | 'Possible Errors'; | ||
} | ||
} | ||
|
||
// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606 | ||
/* istanbul ignore next */ | ||
const interopRequireDefault = (obj: any): { default: any } => | ||
obj && obj.__esModule ? obj : { default: obj }; | ||
|
||
const importDefault = (moduleName: string) => | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports | ||
interopRequireDefault(require(moduleName)).default; | ||
|
||
const rulesDir = join(__dirname, 'rules'); | ||
const excludedFiles = ['__tests__', 'detectJestVersion', 'utils']; | ||
|
||
const rules = readdirSync(rulesDir) | ||
.map(rule => parse(rule).name) | ||
.filter(rule => !excludedFiles.includes(rule)) | ||
.reduce<Record<string, RuleModule>>( | ||
(acc, curr) => ({ | ||
...acc, | ||
[curr]: importDefault(join(rulesDir, curr)) as RuleModule, | ||
}), | ||
{}, | ||
); | ||
|
||
const recommendedRules = Object.entries(rules) | ||
.filter(([, rule]) => rule.meta.docs.recommended) | ||
.reduce( | ||
(acc, [name, rule]) => ({ | ||
...acc, | ||
[`jest/${name}`]: rule.meta.docs.recommended, | ||
}), | ||
{}, | ||
); | ||
|
||
const allRules = Object.entries(rules) | ||
.filter(([, rule]) => !rule.meta.deprecated) | ||
.reduce( | ||
(acc, [name]) => ({ | ||
...acc, | ||
[`jest/${name}`]: 'error', | ||
}), | ||
{}, | ||
); | ||
|
||
const createConfig = (rules: Record<string, TSESLint.Linter.RuleLevel>) => ({ | ||
plugins: ['jest'], | ||
env: { 'jest/globals': true }, | ||
rules, | ||
}); | ||
|
||
export = { | ||
configs: { | ||
all: createConfig(allRules), | ||
recommended: createConfig(recommendedRules), | ||
style: createConfig({ | ||
'jest/no-alias-methods': 'warn', | ||
'jest/prefer-to-be': 'error', | ||
'jest/prefer-to-contain': 'error', | ||
'jest/prefer-to-have-length': 'error', | ||
}), | ||
}, | ||
environments: { | ||
globals: { | ||
globals, | ||
}, | ||
}, | ||
processors: { | ||
'.snap': snapshotProcessor, | ||
}, | ||
rules, | ||
export default { | ||
rules: legacy.rules, | ||
processors: legacy.processors, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { readdirSync } from 'fs'; | ||
import { join, parse } from 'path'; | ||
import type { TSESLint } from '@typescript-eslint/utils'; | ||
import globals from './globals.json'; | ||
import * as snapshotProcessor from './processors/snapshot-processor'; | ||
|
||
type RuleModule = TSESLint.RuleModule<string, unknown[]> & { | ||
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>; | ||
}; | ||
|
||
// v5 of `@typescript-eslint/experimental-utils` removed this | ||
declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' { | ||
export interface RuleMetaDataDocs { | ||
category: 'Best Practices' | 'Possible Errors'; | ||
} | ||
} | ||
|
||
// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606 | ||
/* istanbul ignore next */ | ||
const interopRequireDefault = (obj: any): { default: any } => | ||
obj && obj.__esModule ? obj : { default: obj }; | ||
|
||
const importDefault = (moduleName: string) => | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports | ||
interopRequireDefault(require(moduleName)).default; | ||
|
||
const rulesDir = join(__dirname, 'rules'); | ||
const excludedFiles = ['__tests__', 'detectJestVersion', 'utils']; | ||
|
||
const rules = readdirSync(rulesDir) | ||
.map(rule => parse(rule).name) | ||
.filter(rule => !excludedFiles.includes(rule)) | ||
.reduce<Record<string, RuleModule>>( | ||
(acc, curr) => ({ | ||
...acc, | ||
[curr]: importDefault(join(rulesDir, curr)) as RuleModule, | ||
}), | ||
{}, | ||
); | ||
|
||
const recommendedRules = Object.entries(rules) | ||
.filter(([, rule]) => rule.meta.docs.recommended) | ||
.reduce( | ||
(acc, [name, rule]) => ({ | ||
...acc, | ||
[`jest/${name}`]: rule.meta.docs.recommended, | ||
}), | ||
{}, | ||
); | ||
|
||
const allRules = Object.entries(rules) | ||
.filter(([, rule]) => !rule.meta.deprecated) | ||
.reduce( | ||
(acc, [name]) => ({ | ||
...acc, | ||
[`jest/${name}`]: 'error', | ||
}), | ||
{}, | ||
); | ||
|
||
const createConfig = (rules: Record<string, TSESLint.Linter.RuleLevel>) => ({ | ||
plugins: ['jest'], | ||
env: { 'jest/globals': true }, | ||
rules, | ||
}); | ||
|
||
export = { | ||
configs: { | ||
all: createConfig(allRules), | ||
recommended: createConfig(recommendedRules), | ||
style: createConfig({ | ||
'jest/no-alias-methods': 'warn', | ||
'jest/prefer-to-be': 'error', | ||
'jest/prefer-to-contain': 'error', | ||
'jest/prefer-to-have-length': 'error', | ||
}), | ||
}, | ||
environments: { | ||
globals: { | ||
globals, | ||
}, | ||
}, | ||
processors: { | ||
'.snap': snapshotProcessor, | ||
}, | ||
rules, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
hmm, I don't think we produce ESM?
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.
exports
can benefit CJS projects, like eslint-plugin-jest.There's a difference in module resolution between the legacy and new config system.
In the legacy config system, as we know,
require
orimport
) in eslintrc.js.plugin:jest/recommended
orplugin: [ 'jest' ]
), and eslint internallyrequire()
(notimport()
) them.In the new config system,
require
d orimport
ed, as eslint support both CJS and ESM for eslint.config.js.Thus,
Therefore, by this PR,
require('eslint-plugin-jest')
. This is not breaking change.import jest from 'eslint-plugin-jest'
. This feels natural. This can be possible by the conditional export.require( 'eslint-plugin-jest/new')
.