Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
indent awaited snapshots with spaces
  • Loading branch information
robinpokorny committed Jul 4, 2022
commit b454eaaf05006e82861837d21d33d808e23781b0
19 changes: 15 additions & 4 deletions packages/jest-snapshot/src/InlineSnapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

import * as path from 'path';
import type {PluginItem} from '@babel/core';
import type {Expression, File, Program} from '@babel/types';
import {
type Expression,
type File,
type Program,
isAwaitExpression,
} from '@babel/types';
import * as fs from 'graceful-fs';
import type {
CustomParser as PrettierCustomParser,
Expand Down Expand Up @@ -312,7 +317,7 @@ const createFormattingParser =

const ast = resolveAst(parsers[inferredParser](text, options));
babelTraverse(ast, {
CallExpression({node: {arguments: args, callee}}) {
CallExpression({node: {arguments: args, callee}, parent}) {
if (
callee.type !== 'MemberExpression' ||
callee.property.type !== 'Identifier' ||
Expand All @@ -336,13 +341,19 @@ const createFormattingParser =
return;
}

const startColumn =
isAwaitExpression(parent) && parent.loc
? parent.loc.start.column
: callee.loc.start.column;

const useSpaces = !options.useTabs;
snapshot = indent(
snapshot,
Math.ceil(
useSpaces
? callee.loc.start.column / (options.tabWidth ?? 1)
: callee.loc.start.column / 2, // Each tab is 2 characters.
? startColumn / (options.tabWidth ?? 1)
: // Each tab is 2 characters.
startColumn / 2,
),
useSpaces ? ' '.repeat(options.tabWidth ?? 1) : '\t',
);
Expand Down