Skip to content

Commit 19c231a

Browse files
committed
Support flow parser
1 parent acf59cf commit 19c231a

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/jest-snapshot/src/utils.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import path from 'path';
1818
import prettyFormat from 'pretty-format';
1919
import prettier from 'prettier';
2020
import traverse from 'babel-traverse';
21-
import {templateElement, templateLiteral} from 'babel-types';
21+
import {templateElement, templateLiteral, file} from 'babel-types';
2222

2323
export type InlineSnapshot = {|
2424
snapshot: string,
@@ -195,11 +195,12 @@ export const saveInlineSnapshots = (
195195
const sourceFile = fs.readFileSync(sourceFilePath, 'utf8');
196196

197197
const config = prettier.resolveConfig.sync(sourceFilePath);
198+
const {inferredParser} = prettier.getFileInfo.sync(sourceFilePath);
198199
const newSourceFile = prettier.format(
199200
sourceFile,
200201
Object.assign({}, config, {
201202
filepath: sourceFilePath,
202-
parser: createParser(snapshots),
203+
parser: createParser(snapshots, inferredParser),
203204
}),
204205
);
205206

@@ -217,9 +218,17 @@ const groupSnapshotsByFrame = (snapshots: InlineSnapshot[]) => {
217218
}, {});
218219
};
219220

220-
const createParser = (snapshots: InlineSnapshot[]) => (text, parsers) => {
221+
const createParser = (snapshots: InlineSnapshot[], inferredParser: string) => (
222+
text: string,
223+
parsers: {[key: string]: (string) => any},
224+
) => {
221225
const groupedSnapshots = groupSnapshotsByFrame(snapshots);
222-
const ast = parsers.babylon(text);
226+
let ast = parsers[inferredParser](text);
227+
// flow uses a 'Program' parent node, babel expects a 'File'.
228+
if (ast.type !== 'File') {
229+
ast = file(ast, ast.comments, ast.tokens);
230+
delete ast.program.comments;
231+
}
223232

224233
traverse(ast, {
225234
CallExpression({node: {arguments: args, callee}}) {

0 commit comments

Comments
 (0)