Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary function declarations and call in pretty-format #3962

Merged
merged 3 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions packages/pretty-format/src/__tests__/pretty_format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,25 @@ describe('prettyFormat()', () => {
expect(prettyFormat(val, options)).toEqual('');
});

it('throws if plugin does not return a string', () => {
const val = 123;
const options = {
plugins: [
{
print(val) {
return val;
},
test() {
return true;
},
},
],
};
expect(() => {
prettyFormat(val, options);
}).toThrow();
});

it('supports plugins with deeply nested arrays (#24)', () => {
const val = [[1, 2], [3, 4]];
expect(
Expand Down
124 changes: 61 additions & 63 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
Colors,
Refs,
StringOrNull,
Plugin,
Plugins,
Options,
} from 'types/PrettyFormat';
Expand Down Expand Up @@ -671,6 +672,7 @@ function printComplexValue(
}

function printPlugin(
plugin: Plugin,
val,
indent: string,
prevIndent: string,
Expand All @@ -685,20 +687,7 @@ function printPlugin(
printFunctionName: boolean,
escapeRegex: boolean,
colors: Colors,
): StringOrNull {
let plugin;

for (let p = 0; p < plugins.length; p++) {
if (plugins[p].test(val)) {
plugin = plugins[p];
break;
}
}

if (!plugin) {
return null;
}

): string {
function boundPrint(val) {
return print(
val,
Expand Down Expand Up @@ -728,7 +717,24 @@ function printPlugin(
min,
spacing,
};
return plugin.print(val, boundPrint, boundIndent, opts, colors);

const printed = plugin.print(val, boundPrint, boundIndent, opts, colors);
if (typeof printed !== 'string') {
throw new Error(
`pretty-format: Plugin must return type "string" but instead returned "${typeof printed}".`,
);
}
return printed;
}

function findPlugin(plugins: Plugins, val: any) {
for (let p = 0; p < plugins.length; p++) {
if (plugins[p].test(val)) {
return plugins[p];
}
}

return null;
}

function print(
Expand All @@ -747,24 +753,25 @@ function print(
escapeRegex: boolean,
colors: Colors,
): string {
const pluginsResult = printPlugin(
val,
indent,
prevIndent,
spacing,
edgeSpacing,
refs,
maxDepth,
currentDepth,
plugins,
min,
callToJSON,
printFunctionName,
escapeRegex,
colors,
);
if (typeof pluginsResult === 'string') {
return pluginsResult;
const plugin = findPlugin(plugins, val);
if (plugin !== null) {
return printPlugin(
plugin,
val,
indent,
prevIndent,
spacing,
edgeSpacing,
refs,
maxDepth,
currentDepth,
plugins,
min,
callToJSON,
printFunctionName,
escapeRegex,
colors,
);
}

const basicResult = printBasicValue(val, printFunctionName, escapeRegex);
Expand Down Expand Up @@ -897,34 +904,31 @@ function prettyFormat(val: any, initialOptions?: InitialOptions): string {
}
});

let indent;
let refs;
const prevIndent = '';
const currentDepth = 0;
const spacing = opts.min ? ' ' : '\n';
const edgeSpacing = opts.min ? '' : '\n';

if (opts && opts.plugins.length) {
indent = createIndent(opts.indent);
refs = [];
const pluginsResult = printPlugin(
val,
indent,
prevIndent,
spacing,
edgeSpacing,
refs,
opts.maxDepth,
currentDepth,
opts.plugins,
opts.min,
opts.callToJSON,
opts.printFunctionName,
opts.escapeRegex,
colors,
);
if (typeof pluginsResult === 'string') {
return pluginsResult;
const plugin = findPlugin(opts.plugins, val);
if (plugin !== null) {
return printPlugin(
plugin,
val,
createIndent(opts.indent),
prevIndent,
spacing,
edgeSpacing,
[],
opts.maxDepth,
currentDepth,
opts.plugins,
opts.min,
opts.callToJSON,
opts.printFunctionName,
opts.escapeRegex,
colors,
);
}
}

Expand All @@ -937,19 +941,13 @@ function prettyFormat(val: any, initialOptions?: InitialOptions): string {
return basicResult;
}

if (!indent) {
indent = createIndent(opts.indent);
}
if (!refs) {
refs = [];
}
return printComplexValue(
val,
indent,
createIndent(opts.indent),
prevIndent,
spacing,
edgeSpacing,
refs,
[],
opts.maxDepth,
currentDepth,
opts.plugins,
Expand Down