@@ -9,7 +9,10 @@ import * as path from 'path';
99import type { PluginItem } from '@babel/core' ;
1010import type { Expression , File , Program } from '@babel/types' ;
1111import * as fs from 'graceful-fs' ;
12- import type { BuiltInParserName as PrettierParserName } from 'prettier' ;
12+ import type {
13+ BuiltInParserName as PrettierParserName ,
14+ ParserOptions as PrettierParserOptions ,
15+ } from 'prettier' ;
1316import semver = require( 'semver' ) ;
1417import type { Config } from '@jest/types' ;
1518import type { Frame } from 'jest-message-util' ;
@@ -32,6 +35,13 @@ const {parseSync} = requireOutside(
3235 '@babel/core' ,
3336) as typeof import ( '@babel/core' ) ;
3437
38+ type Prettier = typeof import ( 'prettier' ) ;
39+ type PrettierFunctionParser = (
40+ text : string ,
41+ parsers : Record < string , PrettierFunctionParser > ,
42+ options : PrettierParserOptions ,
43+ ) => any ;
44+
3545export type InlineSnapshot = {
3646 snapshot : string ;
3747 frame : Frame ;
@@ -44,24 +54,24 @@ export function saveInlineSnapshots(
4454) : void {
4555 const prettier = prettierPath
4656 ? // @ts -expect-error requireOutside Babel transform
47- ( requireOutside ( prettierPath ) as typeof import ( 'prettier' ) )
48- : null ;
57+ ( requireOutside ( prettierPath ) as Prettier )
58+ : undefined ;
4959
5060 const snapshotsByFile = groupSnapshotsByFile ( snapshots ) ;
5161
5262 for ( const sourceFilePath of Object . keys ( snapshotsByFile ) ) {
5363 saveSnapshotsForFile (
5464 snapshotsByFile [ sourceFilePath ] ,
5565 sourceFilePath ,
56- prettier && semver . gte ( prettier . version , '1.5.0' ) ? prettier : null ,
66+ prettier && semver . gte ( prettier . version , '1.5.0' ) ? prettier : undefined ,
5767 ) ;
5868 }
5969}
6070
6171const saveSnapshotsForFile = (
6272 snapshots : Array < InlineSnapshot > ,
6373 sourceFilePath : Config . Path ,
64- prettier : any ,
74+ prettier ?: Prettier ,
6575) => {
6676 const sourceFile = fs . readFileSync ( sourceFilePath , 'utf8' ) ;
6777
@@ -245,7 +255,7 @@ const traverseAst = (
245255} ;
246256
247257const runPrettier = (
248- prettier : any ,
258+ prettier : Prettier ,
249259 sourceFilePath : string ,
250260 sourceFileWithSnapshots : string ,
251261 snapshotMatcherNames : Array < string > ,
@@ -262,7 +272,14 @@ const runPrettier = (
262272 // For older versions of Prettier, fallback to a simple parser detection.
263273 const inferredParser = prettier . getFileInfo
264274 ? prettier . getFileInfo . sync ( sourceFilePath ) . inferredParser
265- : ( config && config . parser ) || simpleDetectParser ( sourceFilePath ) ;
275+ : ( config && typeof config . parser === 'string' && config . parser ) ||
276+ simpleDetectParser ( sourceFilePath ) ;
277+
278+ if ( ! inferredParser ) {
279+ throw new Error (
280+ `Could not infer Prettier parser for file ${ sourceFilePath } ` ,
281+ ) ;
282+ }
266283
267284 // Snapshots have now been inserted. Run prettier to make sure that the code is
268285 // formatted, except snapshot indentation. Snapshots cannot be formatted until
@@ -289,15 +306,11 @@ const runPrettier = (
289306const createFormattingParser = (
290307 snapshotMatcherNames : Array < string > ,
291308 inferredParser : string ,
292- ) => (
293- text : string ,
294- parsers : Record < string , ( text : string ) => any > ,
295- options : any ,
296- ) => {
309+ ) : PrettierFunctionParser => ( text , parsers , options ) => {
297310 // Workaround for https://github.com/prettier/prettier/issues/3150
298311 options . parser = inferredParser ;
299312
300- const ast = resolveAst ( parsers [ inferredParser ] ( text ) ) ;
313+ const ast = resolveAst ( parsers [ inferredParser ] ( text , parsers , options ) ) ;
301314 babelTraverse ( ast , {
302315 CallExpression ( { node : { arguments : args , callee} } ) {
303316 if (
0 commit comments