From 98087f9bb27082f9fbda59a56c65536fb9d8a0dc Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 28 Mar 2024 07:02:20 +1300 Subject: [PATCH] feat: remove `snapshot` processor and `flat/snapshot` config (#1532) BREAKING CHANGE: removed unneeded `snapshot` processor and `flat/snapshot` config --- README.md | 29 ---------- .../__snapshots__/rules.test.ts.snap | 14 ----- src/__tests__/rules.test.ts | 2 - src/index.ts | 14 +---- .../__tests__/snapshot-processor.test.ts | 55 ------------------- src/processors/snapshot-processor.ts | 15 ----- 6 files changed, 1 insertion(+), 128 deletions(-) delete mode 100644 src/processors/__tests__/snapshot-processor.test.ts delete mode 100644 src/processors/snapshot-processor.ts diff --git a/README.md b/README.md index afd0e7da6..825ed26a8 100644 --- a/README.md +++ b/README.md @@ -275,35 +275,6 @@ While the `recommended` and `style` configurations only change in major versions the `all` configuration may change in any release and is thus unsuited for installations requiring long-term consistency. -## Snapshot processing - -> [!NOTE] -> -> This is only relevant for `eslint.config.js` - -This plugin provides a -[custom processor](https://eslint.org/docs/latest/extend/custom-processors) to -allow rules to "lint" snapshot files. - -For `.eslintrc` based configs, this is automatically enabled out of the box but -must be opted into for `eslint.config.js` using the `flat/snapshots` config: - -```js -const jest = require('eslint-plugin-jest'); - -module.exports = [ - { - ...jest.configs['flat/snapshots'], - rules: { - 'jest/no-large-snapshots': ['error', { maxSize: 1 }], - }, - }, -]; -``` - -Unlike other configs, this includes a `files` array that matches `.snap` files -meaning you can use it directly - ## Rules diff --git a/src/__tests__/__snapshots__/rules.test.ts.snap b/src/__tests__/__snapshots__/rules.test.ts.snap index 3044c709a..b0d635013 100644 --- a/src/__tests__/__snapshots__/rules.test.ts.snap +++ b/src/__tests__/__snapshots__/rules.test.ts.snap @@ -193,20 +193,6 @@ exports[`rules should export configs that refer to actual rules 1`] = ` "jest/valid-title": "error", }, }, - "flat/snapshots": { - "files": [ - "**/*.snap", - ], - "plugins": { - "jest": ObjectContaining { - "meta": { - "name": "eslint-plugin-jest", - "version": Any, - }, - }, - }, - "processor": "jest/snapshots", - }, "flat/style": { "languageOptions": { "globals": { diff --git a/src/__tests__/rules.test.ts b/src/__tests__/rules.test.ts index 8236727bd..3e06d7bbd 100644 --- a/src/__tests__/rules.test.ts +++ b/src/__tests__/rules.test.ts @@ -61,7 +61,6 @@ describe('rules', () => { 'flat/recommended': { plugins: { jest: expectJestPlugin } }, 'flat/style': { plugins: { jest: expectJestPlugin } }, 'flat/all': { plugins: { jest: expectJestPlugin } }, - 'flat/snapshots': { plugins: { jest: expectJestPlugin } }, }); expect(Object.keys(recommendedConfigs)).toEqual([ 'all', @@ -70,7 +69,6 @@ describe('rules', () => { 'flat/all', 'flat/recommended', 'flat/style', - 'flat/snapshots', ]); expect(Object.keys(recommendedConfigs.all.rules)).toHaveLength( ruleNames.length - deprecatedRules.length, diff --git a/src/index.ts b/src/index.ts index 569ae64b8..d6322697a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,6 @@ import { version as packageVersion, } from '../package.json'; import globals from './globals.json'; -import * as snapshotProcessor from './processors/snapshot-processor'; type RuleModule = TSESLint.RuleModule & { meta: Required, 'docs'>>; @@ -76,8 +75,7 @@ const plugin = { | 'style' | 'flat/all' | 'flat/recommended' - | 'flat/style' - | 'flat/snapshots', + | 'flat/style', Pick, 'rules'> >, environments: { @@ -85,10 +83,6 @@ const plugin = { globals, }, }, - processors: { - snapshots: snapshotProcessor, - '.snap': snapshotProcessor, - }, rules, }; @@ -113,12 +107,6 @@ plugin.configs = { 'flat/all': createFlatConfig(allRules), 'flat/recommended': createFlatConfig(recommendedRules), 'flat/style': createFlatConfig(styleRules), - 'flat/snapshots': { - // @ts-expect-error this is introduced in flat config - files: ['**/*.snap'], - plugins: { jest: plugin }, - processor: 'jest/snapshots', - }, }; export = plugin; diff --git a/src/processors/__tests__/snapshot-processor.test.ts b/src/processors/__tests__/snapshot-processor.test.ts deleted file mode 100644 index 4ea625822..000000000 --- a/src/processors/__tests__/snapshot-processor.test.ts +++ /dev/null @@ -1,55 +0,0 @@ -import * as snapshotProcessor from '../snapshot-processor'; - -describe('snapshot-processor', () => { - it('exports an object with preprocess and postprocess functions', () => { - expect(snapshotProcessor).toMatchObject({ - preprocess: expect.any(Function), - postprocess: expect.any(Function), - }); - }); - - describe('preprocess function', () => { - it('should pass on untouched source code to source array', () => { - const { preprocess } = snapshotProcessor; - const sourceCode = "const name = 'johnny bravo';"; - const result = preprocess(sourceCode, 'my-file.snap'); - - expect(result).toEqual([sourceCode]); - }); - }); - - describe('postprocess function', () => { - it('should only return messages about snapshot specific rules', () => { - const { postprocess } = snapshotProcessor; - - const result = postprocess( - [ - ['no-console', 'global-require', 'jest/no-large-snapshots'].map( - ruleId => ({ - ruleId, - column: 1, - line: 1, - source: null, - nodeType: 'Program', - message: 'something is not right about this...', - severity: 1, - }), - ), - ], - 'my-file.snap', - ); - - expect(result).toEqual([ - { - ruleId: 'jest/no-large-snapshots', - column: 1, - line: 1, - source: null, - nodeType: 'Program', - message: 'something is not right about this...', - severity: 1, - }, - ]); - }); - }); -}); diff --git a/src/processors/snapshot-processor.ts b/src/processors/snapshot-processor.ts deleted file mode 100644 index f455e3cb1..000000000 --- a/src/processors/snapshot-processor.ts +++ /dev/null @@ -1,15 +0,0 @@ -// https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins -// https://github.com/typescript-eslint/typescript-eslint/issues/808 -import type { TSESLint } from '@typescript-eslint/utils'; -import { - name as packageName, - version as packageVersion, -} from '../../package.json'; - -type SnapshotProcessor = Required; - -export const meta = { name: packageName, version: packageVersion }; -export const preprocess: SnapshotProcessor['preprocess'] = source => [source]; -export const postprocess: SnapshotProcessor['postprocess'] = messages => - // snapshot files should only be linted with snapshot specific rules - messages[0].filter(message => message.ruleId === 'jest/no-large-snapshots');