From c30ce589b60040e0234a7ab8115bf427db1fdac6 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 13 Sep 2020 10:50:55 +0200 Subject: [PATCH 1/2] chore(jest-snapshot): type prettier usage properly --- packages/jest-snapshot/src/State.ts | 2 +- .../jest-snapshot/src/inline_snapshots.ts | 55 ++++++++----------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 1d7ac9586b5a..497f75d56a2a 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -63,7 +63,7 @@ export default class SnapshotState { private _inlineSnapshots: Array; private _uncheckedKeys: Set; private _getBabelTraverse: () => BabelTraverse; - private _getPrettier: () => null | typeof import('prettier'); + private _getPrettier: () => null | Prettier; added: number; expand: boolean; diff --git a/packages/jest-snapshot/src/inline_snapshots.ts b/packages/jest-snapshot/src/inline_snapshots.ts index 108cd4850b06..260ad1eb54b3 100644 --- a/packages/jest-snapshot/src/inline_snapshots.ts +++ b/packages/jest-snapshot/src/inline_snapshots.ts @@ -8,17 +8,19 @@ import * as path from 'path'; import * as fs from 'graceful-fs'; import semver = require('semver'); -import { - CallExpression, - file, - templateElement, - templateLiteral, -} from '@babel/types'; +import {file, templateElement, templateLiteral} from '@babel/types'; import type {Frame} from 'jest-message-util'; +import type { + CustomParser as PrettierCustomParser, + BuiltInParser as PrettierParser, + BuiltInParserName as PrettierParserName, +} from 'prettier'; import type {Config} from '@jest/types'; import type {BabelTraverse, Prettier} from './types'; import {escapeBacktickString} from './utils'; +type PrettierParserMap = Record; + export type InlineSnapshot = { snapshot: string; frame: Frame; @@ -59,7 +61,7 @@ export function saveInlineSnapshots( const saveSnapshotsForFile = ( snapshots: Array, sourceFilePath: Config.Path, - prettier: any, + prettier: Prettier, babelTraverse: BabelTraverse, ) => { const sourceFile = fs.readFileSync(sourceFilePath, 'utf8'); @@ -67,14 +69,13 @@ const saveSnapshotsForFile = ( // Resolve project configuration. // For older versions of Prettier, do not load configuration. const config = prettier.resolveConfig - ? prettier.resolveConfig.sync(sourceFilePath, { - editorconfig: true, - }) + ? prettier.resolveConfig.sync(sourceFilePath, {editorconfig: true}) : null; // Detect the parser for the test file. // For older versions of Prettier, fallback to a simple parser detection. - const inferredParser = prettier.getFileInfo + // @ts-expect-error + const inferredParser: PrettierParserName = prettier.getFileInfo ? prettier.getFileInfo.sync(sourceFilePath).inferredParser : (config && config.parser) || simpleDetectParser(sourceFilePath); @@ -163,8 +164,8 @@ const indent = (snapshot: string, numIndents: number, indentation: string) => { }; const getAst = ( - parsers: Record any>, - inferredParser: string, + parsers: PrettierParserMap, + inferredParser: PrettierParserName, text: string, ) => { // Flow uses a 'Program' parent node, babel expects a 'File'. @@ -180,13 +181,9 @@ const getAst = ( const createInsertionParser = ( snapshots: Array, snapshotMatcherNames: Array, - inferredParser: string, + inferredParser: PrettierParserName, babelTraverse: BabelTraverse, -) => ( - text: string, - parsers: Record any>, - options: any, -) => { +): PrettierCustomParser => (text, parsers, options) => { // Workaround for https://github.com/prettier/prettier/issues/3150 options.parser = inferredParser; @@ -195,7 +192,7 @@ const createInsertionParser = ( const ast = getAst(parsers, inferredParser, text); babelTraverse(ast, { - CallExpression({node: {arguments: args, callee}}: {node: CallExpression}) { + CallExpression({node: {arguments: args, callee}}) { if ( callee.type !== 'MemberExpression' || callee.property.type !== 'Identifier' || @@ -247,19 +244,15 @@ const createInsertionParser = ( // This parser formats snapshots to the correct indentation. const createFormattingParser = ( snapshotMatcherNames: Array, - inferredParser: string, + inferredParser: PrettierParserName, babelTraverse: BabelTraverse, -) => ( - text: string, - parsers: Record any>, - options: any, -) => { +): PrettierCustomParser => (text, parsers, options) => { // Workaround for https://github.com/prettier/prettier/issues/3150 options.parser = inferredParser; const ast = getAst(parsers, inferredParser, text); babelTraverse(ast, { - CallExpression({node: {arguments: args, callee}}: {node: CallExpression}) { + CallExpression({node: {arguments: args, callee}}) { if ( callee.type !== 'MemberExpression' || callee.property.type !== 'Identifier' || @@ -288,10 +281,10 @@ const createFormattingParser = ( snapshot, Math.ceil( useSpaces - ? callee.loc.start.column / options.tabWidth + ? callee.loc.start.column / (options.tabWidth ?? 1) : callee.loc.start.column / 2, // Each tab is 2 characters. ), - useSpaces ? ' '.repeat(options.tabWidth) : '\t', + useSpaces ? ' '.repeat(options.tabWidth ?? 1) : '\t', ); const replacementNode = templateLiteral( @@ -309,10 +302,10 @@ const createFormattingParser = ( return ast; }; -const simpleDetectParser = (filePath: Config.Path) => { +const simpleDetectParser = (filePath: Config.Path): PrettierParserName => { const extname = path.extname(filePath); if (/tsx?$/.test(extname)) { return 'typescript'; } - return 'babylon'; + return 'babel'; }; From 98a673699a0bb49aa16339149c82070fb7accdca Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 13 Sep 2020 11:03:00 +0200 Subject: [PATCH 2/2] avoid duplicating types --- packages/jest-snapshot/src/State.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-snapshot/src/State.ts b/packages/jest-snapshot/src/State.ts index 497f75d56a2a..67d26015c582 100644 --- a/packages/jest-snapshot/src/State.ts +++ b/packages/jest-snapshot/src/State.ts @@ -62,8 +62,8 @@ export default class SnapshotState { private _snapshotPath: Config.Path; private _inlineSnapshots: Array; private _uncheckedKeys: Set; - private _getBabelTraverse: () => BabelTraverse; - private _getPrettier: () => null | Prettier; + private _getBabelTraverse: SnapshotStateOptions['getBabelTraverse']; + private _getPrettier: SnapshotStateOptions['getPrettier']; added: number; expand: boolean;