From 14eafdda4f9a15a7262fef806e73a7ed2266ab1c Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 16 Feb 2024 11:29:44 +1300 Subject: [PATCH] refactor: use types from `@typescript-eslint/utils` for snapshot processor (#1504) --- .../__tests__/snapshot-processor.test.ts | 36 +++++++++++++++---- src/processors/snapshot-processor.ts | 7 ++-- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/processors/__tests__/snapshot-processor.test.ts b/src/processors/__tests__/snapshot-processor.test.ts index b4a807af5..4ea625822 100644 --- a/src/processors/__tests__/snapshot-processor.test.ts +++ b/src/processors/__tests__/snapshot-processor.test.ts @@ -12,7 +12,7 @@ describe('snapshot-processor', () => { it('should pass on untouched source code to source array', () => { const { preprocess } = snapshotProcessor; const sourceCode = "const name = 'johnny bravo';"; - const result = preprocess(sourceCode); + const result = preprocess(sourceCode, 'my-file.snap'); expect(result).toEqual([sourceCode]); }); @@ -21,13 +21,35 @@ describe('snapshot-processor', () => { 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 }), - ), - ]); - expect(result).toEqual([{ ruleId: 'jest/no-large-snapshots' }]); + 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 index 883074cb8..f455e3cb1 100644 --- a/src/processors/snapshot-processor.ts +++ b/src/processors/snapshot-processor.ts @@ -1,14 +1,15 @@ // 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 PostprocessMessage = { ruleId: string }; +type SnapshotProcessor = Required; export const meta = { name: packageName, version: packageVersion }; -export const preprocess = (source: string): string[] => [source]; -export const postprocess = (messages: PostprocessMessage[][]) => +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');